From 7800d33ff5e018ec8ae26b6e79c6d5481e17ecf6 Mon Sep 17 00:00:00 2001 From: AndyScherzinger Date: Sun, 20 May 2018 18:51:33 +0200 Subject: [PATCH] move stuff to helper class --- .../fragment/FileDetailSharingFragment.java | 148 ++++------------- .../util/FileDetailSharingFragmentHelper.java | 152 ++++++++++++++++++ 2 files changed, 179 insertions(+), 121 deletions(-) create mode 100644 src/main/java/com/owncloud/android/ui/fragment/util/FileDetailSharingFragmentHelper.java diff --git a/src/main/java/com/owncloud/android/ui/fragment/FileDetailSharingFragment.java b/src/main/java/com/owncloud/android/ui/fragment/FileDetailSharingFragment.java index 43a730b06b..31392f5126 100644 --- a/src/main/java/com/owncloud/android/ui/fragment/FileDetailSharingFragment.java +++ b/src/main/java/com/owncloud/android/ui/fragment/FileDetailSharingFragment.java @@ -57,6 +57,7 @@ import com.owncloud.android.ui.activity.FileActivity; import com.owncloud.android.ui.adapter.UserListAdapter; import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment; import com.owncloud.android.ui.dialog.SharePasswordDialogFragment; +import com.owncloud.android.ui.fragment.util.FileDetailSharingFragmentHelper; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -157,34 +158,11 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt private void setupView() { setShareByLinkInfo(file.isSharedViaLink()); setShareWithUserInfo(); - setupSearchView(); - } - - private void setupSearchView() { - SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE); - // assumes parent activity is the searchable activity - searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName())); - - // do not iconify the widget; expand it by default - searchView.setIconifiedByDefault(false); - - // avoid fullscreen with softkeyboard - searchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); - - searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { - @Override - public boolean onQueryTextSubmit(String query) { - Log_OC.v(TAG, "onQueryTextSubmit intercepted, query: " + query); - // return true to prevent the query is processed to be queried; - return true; - } - - @Override - public boolean onQueryTextChange(String newText) { - // leave it for the parent listener in the hierarchy / default behaviour - return false; - } - }); + FileDetailSharingFragmentHelper.setupSearchView( + (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE), + searchView, + getActivity().getComponentName() + ); } /** @@ -231,38 +209,13 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt usersList.setVisibility(View.VISIBLE); usersList.setAdapter(mUserGroupsAdapter); noList.setVisibility(View.GONE); - setListViewHeightBasedOnChildren(usersList); + FileDetailSharingFragmentHelper.setListViewHeightBasedOnChildren(usersList); } else { usersList.setVisibility(View.GONE); noList.setVisibility(View.VISIBLE); } } - /** - * Fix scroll in ListView when the parent is a ScrollView - */ - private static void setListViewHeightBasedOnChildren(ListView listView) { - ListAdapter listAdapter = listView.getAdapter(); - if (listAdapter == null) { - return; - } - int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST); - int totalHeight = 0; - View view = null; - for (int i = 0; i < listAdapter.getCount(); i++) { - view = listAdapter.getView(i, view, listView); - if (i == 0) { - view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT)); - } - view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); - totalHeight += view.getMeasuredHeight(); - } - ViewGroup.LayoutParams params = listView.getLayoutParams(); - params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); - listView.setLayoutParams(params); - listView.requestLayout(); - } - @OnClick(R.id.share_by_link) public void toggleShareByLink() { if (shareByLink.isChecked()) { @@ -306,55 +259,22 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt private void prepareOptionsMenu(Menu menu) { Resources res = getResources(); - setupHideFileListingMenuItem(menu.findItem(R.id.action_share_link_hide_file_listing)); - setupPasswordMenuItem(menu.findItem(R.id.action_share_link_password), res); - setupExpirationDateMenuItem(menu.findItem(R.id.action_share_link_expiration_date), res); - } - - private void setupPasswordMenuItem(MenuItem password, Resources res) { - if (publicShare.isPasswordProtected()) { - password.setTitle(res.getString( - R.string.share_via_link_menu_password_label, - res.getString(R.string.share_via_link_password_title) - )); - } else { - password.setTitle(res.getString( - R.string.share_via_link_menu_password_label, - res.getString(R.string.share_via_link_no_password_title) - )); - } - } - - private void setupHideFileListingMenuItem(MenuItem fileListing) { - if (!file.isFolder()) { - fileListing.setVisible(false); - } else { - if(shareByLinkAllowEditing.isChecked()) { - boolean readOnly = (publicShare.getPermissions() & OCShare.READ_PERMISSION_FLAG) != 0; - fileListing.setChecked(!readOnly); - } else { - fileListing.setVisible(false); - } - } - } - - private void setupExpirationDateMenuItem(MenuItem expirationDate, Resources res) { - long expirationDateValue = publicShare.getExpirationDate(); - if (expirationDateValue > 0) { - String formattedDate = - SimpleDateFormat.getDateInstance().format( - new Date(expirationDateValue) - ); - expirationDate.setTitle(res.getString( - R.string.share_via_link_menu_expiration_date_label, - formattedDate - )); - } else { - expirationDate.setTitle(res.getString( - R.string.share_via_link_menu_expiration_date_label, - res.getString(R.string.share_via_link_menu_expiration_date_never) - )); - } + FileDetailSharingFragmentHelper.setupHideFileListingMenuItem( + menu.findItem(R.id.action_share_link_hide_file_listing), + file.isFolder(), + shareByLinkAllowEditing.isChecked(), + publicShare.getPermissions() + ); + FileDetailSharingFragmentHelper.setupPasswordMenuItem( + menu.findItem(R.id.action_share_link_password), + publicShare.isPasswordProtected(), + res + ); + FileDetailSharingFragmentHelper.setupExpirationDateMenuItem( + menu.findItem(R.id.action_share_link_expiration_date), + publicShare.getExpirationDate(), + res + ); } private boolean optionsItemSelected(MenuItem item) { @@ -459,8 +379,8 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt * instance ready to use. If not ready, does nothing. */ public void refreshPublicShareFromDB() { - if (isPublicShareDisabled()) { - hidePublicShare(); + if (FileDetailSharingFragmentHelper.isPublicShareDisabled(capabilities)) { + shareByLinkContainer.setVisibility(View.GONE); } else if (((FileActivity) getActivity()).getStorageManager() != null) { // Get public share publicShare = ((FileActivity) getActivity()).getStorageManager().getFirstShareByPathAndType( @@ -475,8 +395,8 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt } /** - * Updates in the UI the section about public share with the information in the current public share bound to - * mFile, if any. + * Updates in the UI the section about public share with the information + * in the current public share bound to, if any. */ private void updatePublicShareSection() { if (publicShare != null && ShareType.PUBLIC_LINK.equals(publicShare.getShareType())) { @@ -489,18 +409,4 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt } } } - - /** - * @return 'True' when public share is disabled in the server. - */ - private boolean isPublicShareDisabled() { - return (capabilities != null && capabilities.getFilesSharingPublicEnabled().isFalse()); - } - - /** - * Hides all the UI elements related to public share. - */ - private void hidePublicShare() { - shareByLinkContainer.setVisibility(View.GONE); - } } diff --git a/src/main/java/com/owncloud/android/ui/fragment/util/FileDetailSharingFragmentHelper.java b/src/main/java/com/owncloud/android/ui/fragment/util/FileDetailSharingFragmentHelper.java new file mode 100644 index 0000000000..7d4e381f03 --- /dev/null +++ b/src/main/java/com/owncloud/android/ui/fragment/util/FileDetailSharingFragmentHelper.java @@ -0,0 +1,152 @@ +/* + * Nextcloud Android client application + * + * @author Andy Scherzinger + * Copyright (C) 2018 Andy Scherzinger + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this program. If not, see . + */ + +package com.owncloud.android.ui.fragment.util; + +import android.app.SearchManager; +import android.content.ComponentName; +import android.content.Context; +import android.content.res.Resources; +import android.support.v7.widget.SearchView; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.view.inputmethod.EditorInfo; +import android.widget.ListAdapter; +import android.widget.ListView; + +import com.owncloud.android.R; +import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.lib.resources.shares.OCShare; +import com.owncloud.android.lib.resources.status.OCCapability; + +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Helper calls for visibility logic of the sharing fragment. + */ +public class FileDetailSharingFragmentHelper { + + public static void setupHideFileListingMenuItem(MenuItem fileListing, + boolean isFolder, + boolean isEditingAllowed, + int publicSharePermissions) { + if (!isFolder) { + fileListing.setVisible(false); + } else { + if (isEditingAllowed) { + boolean readOnly = (publicSharePermissions & OCShare.READ_PERMISSION_FLAG) != 0; + fileListing.setChecked(!readOnly); + } else { + fileListing.setVisible(false); + } + } + } + + public static void setupPasswordMenuItem(MenuItem password, boolean isPasswordProtected, Resources res) { + if (isPasswordProtected) { + password.setTitle(res.getString( + R.string.share_via_link_menu_password_label, + res.getString(R.string.share_via_link_password_title) + )); + } else { + password.setTitle(res.getString( + R.string.share_via_link_menu_password_label, + res.getString(R.string.share_via_link_no_password_title) + )); + } + } + + public static void setupExpirationDateMenuItem(MenuItem expirationDate, long expirationDateValue, Resources res) { + if (expirationDateValue > 0) { + String formattedDate = + SimpleDateFormat.getDateInstance().format( + new Date(expirationDateValue) + ); + expirationDate.setTitle(res.getString( + R.string.share_via_link_menu_expiration_date_label, + formattedDate + )); + } else { + expirationDate.setTitle(res.getString( + R.string.share_via_link_menu_expiration_date_label, + res.getString(R.string.share_via_link_menu_expiration_date_never) + )); + } + } + + public static void setupSearchView(SearchManager searchManager, SearchView searchView, ComponentName componentName) { + // assumes parent activity is the searchable activity + searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName)); + + // do not iconify the widget; expand it by default + searchView.setIconifiedByDefault(false); + + // avoid fullscreen with softkeyboard + searchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); + + searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + // return true to prevent the query is processed to be queried; + return true; + } + + @Override + public boolean onQueryTextChange(String newText) { + // leave it for the parent listener in the hierarchy / default behaviour + return false; + } + }); + } + + /** + * @return 'True' when public share is disabled in the server. + */ + public static boolean isPublicShareDisabled(OCCapability capabilities) { + return (capabilities != null && capabilities.getFilesSharingPublicEnabled().isFalse()); + } + + /** + * Fix scroll in ListView when the parent is a ScrollView + */ + public static void setListViewHeightBasedOnChildren(ListView listView) { + ListAdapter listAdapter = listView.getAdapter(); + if (listAdapter == null) { + return; + } + int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST); + int totalHeight = 0; + View view = null; + for (int i = 0; i < listAdapter.getCount(); i++) { + view = listAdapter.getView(i, view, listView); + if (i == 0) { + view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT)); + } + view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); + totalHeight += view.getMeasuredHeight(); + } + ViewGroup.LayoutParams params = listView.getLayoutParams(); + params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); + listView.setLayoutParams(params); + listView.requestLayout(); + } +}