From 139357778d4b093f0f905d23be7f527555a558dd Mon Sep 17 00:00:00 2001 From: AndyScherzinger Date: Wed, 27 Jun 2018 09:28:14 +0200 Subject: [PATCH] extract drawer menu filtering to separate util class --- .../android/ui/activity/DrawerActivity.java | 77 ++----------- .../android/utils/DrawerMenuUtil.java | 102 ++++++++++++++++++ 2 files changed, 113 insertions(+), 66 deletions(-) create mode 100644 src/main/java/com/owncloud/android/utils/DrawerMenuUtil.java diff --git a/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java b/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java index 6c9423d527..8e98fbf430 100644 --- a/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java @@ -89,6 +89,7 @@ import com.owncloud.android.ui.events.MenuItemClickEvent; import com.owncloud.android.ui.events.SearchEvent; import com.owncloud.android.ui.trashbin.TrashbinActivity; import com.owncloud.android.utils.DisplayUtils; +import com.owncloud.android.utils.DrawerMenuUtil; import com.owncloud.android.utils.FilesSyncHelper; import com.owncloud.android.utils.ThemeUtils; import com.owncloud.android.utils.svg.MenuSimpleTarget; @@ -342,75 +343,19 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU } private void filterDrawerMenu(Menu menu, Account account) { - boolean searchSupported = AccountUtils.hasSearchSupport(account); + DrawerMenuUtil.filterForBottomToolbarMenuItems(menu, getResources()); + DrawerMenuUtil.filterSearchMenuItems(menu, account, getResources()); + DrawerMenuUtil.filterTrashbinMenuItems(menu, account, getContentResolver()); - if (getResources().getBoolean(R.bool.bottom_toolbar_enabled) && account != null) { - filterMenuItems(menu, R.id.nav_all_files, R.id.nav_settings, R.id.nav_favorites, R.id.nav_photos); - } + DrawerMenuUtil.setupHomeMenuItem(menu, getResources()); - if (!searchSupported && account != null) { - filterMenuItems(menu, R.id.nav_photos, R.id.nav_favorites, R.id.nav_videos); - } + DrawerMenuUtil.removeMenuItem(menu, R.id.nav_participate, !getResources().getBoolean(R.bool.participate_enabled)); + DrawerMenuUtil.removeMenuItem(menu, R.id.nav_shared, !getResources().getBoolean(R.bool.shared_enabled)); + DrawerMenuUtil.removeMenuItem(menu, R.id.nav_contacts, !getResources().getBoolean(R.bool.contacts_backup) + || !getResources().getBoolean(R.bool.show_drawer_contacts_backup)); - if (account != null) { - FileDataStorageManager storageManager = new FileDataStorageManager(account, getContentResolver()); - OCCapability capability = storageManager.getCapability(account.name); - - if (AccountUtils.getServerVersion(getAccount()).compareTo(OwnCloudVersion.nextcloud_14) < 0 || - capability.getFilesUndelete().isFalse() || capability.getFilesUndelete().isUnknown()) { - filterMenuItems(menu, R.id.nav_trashbin); - } - } - - if (getResources().getBoolean(R.bool.use_home) && menu.findItem(R.id.nav_all_files) != null) { - menu.findItem(R.id.nav_all_files).setTitle(getResources().getString(R.string.drawer_item_home)); - menu.findItem(R.id.nav_all_files).setIcon(R.drawable.ic_home); - } - - if (!getResources().getBoolean(R.bool.participate_enabled)) { - menu.removeItem(R.id.nav_participate); - } - - if (!getResources().getBoolean(R.bool.shared_enabled)) { - menu.removeItem(R.id.nav_shared); - } - - if (!getResources().getBoolean(R.bool.contacts_backup) - || !getResources().getBoolean(R.bool.show_drawer_contacts_backup)) { - menu.removeItem(R.id.nav_contacts); - } - - if (getResources().getBoolean(R.bool.syncedFolder_light)) { - menu.removeItem(R.id.nav_synced_folders); - } - - if (!getResources().getBoolean(R.bool.show_drawer_logout)) { - menu.removeItem(R.id.nav_logout); - } - - if (AccountUtils.hasSearchSupport(account)) { - if (!getResources().getBoolean(R.bool.recently_added_enabled)) { - menu.removeItem(R.id.nav_recently_added); - } - - if (!getResources().getBoolean(R.bool.recently_modified_enabled)) { - menu.removeItem(R.id.nav_recently_modified); - } - - if (!getResources().getBoolean(R.bool.videos_enabled)) { - menu.removeItem(R.id.nav_videos); - } - } else if (account != null) { - filterMenuItems(menu, R.id.nav_recently_added, R.id.nav_recently_modified, R.id.nav_videos); - } - } - - private static void filterMenuItems(Menu menu, int... menuIds) { - if (menuIds != null) { - for (int menuId : menuIds) { - menu.removeItem(menuId); - } - } + DrawerMenuUtil.removeMenuItem(menu, R.id.nav_synced_folders, getResources().getBoolean(R.bool.syncedFolder_light)); + DrawerMenuUtil.removeMenuItem(menu, R.id.nav_logout, !getResources().getBoolean(R.bool.show_drawer_logout)); } @Subscribe(threadMode = ThreadMode.MAIN) diff --git a/src/main/java/com/owncloud/android/utils/DrawerMenuUtil.java b/src/main/java/com/owncloud/android/utils/DrawerMenuUtil.java new file mode 100644 index 0000000000..6d57268bf4 --- /dev/null +++ b/src/main/java/com/owncloud/android/utils/DrawerMenuUtil.java @@ -0,0 +1,102 @@ +/* + * 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.utils; + +import android.accounts.Account; +import android.content.ContentResolver; +import android.content.res.Resources; +import android.view.Menu; + +import com.owncloud.android.R; +import com.owncloud.android.authentication.AccountUtils; +import com.owncloud.android.datamodel.FileDataStorageManager; +import com.owncloud.android.lib.resources.status.OCCapability; +import com.owncloud.android.lib.resources.status.OwnCloudVersion; + +/** + * A helper class for drawer menu related operations. + */ +public class DrawerMenuUtil { + private DrawerMenuUtil() { + } + + public static void filterForBottomToolbarMenuItems(Menu menu, Resources resources) { + if (resources.getBoolean(R.bool.bottom_toolbar_enabled)) { + filterMenuItems(menu, R.id.nav_all_files, R.id.nav_settings, R.id.nav_favorites, R.id.nav_photos); + } + } + + public static void filterSearchMenuItems(Menu menu, Account account, Resources resources) { + boolean hasSearchSupport = AccountUtils.hasSearchSupport(account); + if (account != null && !hasSearchSupport) { + filterMenuItems(menu, R.id.nav_photos, R.id.nav_favorites, R.id.nav_videos); + } + + if (hasSearchSupport) { + if (!resources.getBoolean(R.bool.recently_added_enabled)) { + menu.removeItem(R.id.nav_recently_added); + } + + if (!resources.getBoolean(R.bool.recently_modified_enabled)) { + menu.removeItem(R.id.nav_recently_modified); + } + + if (!resources.getBoolean(R.bool.videos_enabled)) { + menu.removeItem(R.id.nav_videos); + } + } else if (account != null) { + filterMenuItems(menu, R.id.nav_recently_added, R.id.nav_recently_modified, R.id.nav_videos); + } + } + + public static void filterTrashbinMenuItems(Menu menu, Account account, ContentResolver contentResolver) { + if (account != null) { + FileDataStorageManager storageManager = new FileDataStorageManager(account, contentResolver); + OCCapability capability = storageManager.getCapability(account.name); + + if (AccountUtils.getServerVersion(account).compareTo(OwnCloudVersion.nextcloud_14) < 0 || + capability.getFilesUndelete().isFalse() || capability.getFilesUndelete().isUnknown()) { + filterMenuItems(menu, R.id.nav_trashbin); + } + } + } + + public static void removeMenuItem(Menu menu, int id, boolean remove) { + if (remove) { + menu.removeItem(id); + } + } + + public static void setupHomeMenuItem(Menu menu, Resources resources) { + if (resources.getBoolean(R.bool.use_home) && menu.findItem(R.id.nav_all_files) != null) { + menu.findItem(R.id.nav_all_files).setTitle(resources.getString(R.string.drawer_item_home)); + menu.findItem(R.id.nav_all_files).setIcon(R.drawable.ic_home); + } + } + + private static void filterMenuItems(Menu menu, int... menuIds) { + if (menuIds != null) { + for (int menuId : menuIds) { + menu.removeItem(menuId); + } + } + } +}