DrawerActivity.java 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * @author Tobias Kaminsky
  6. * Copyright (C) 2016 Andy Scherzinger
  7. * Copyright (C) 2017 Tobias Kaminsky
  8. * Copyright (C) 2016 Nextcloud
  9. * Copyright (C) 2016 ownCloud Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public
  22. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package com.owncloud.android.ui.activity;
  25. import android.accounts.Account;
  26. import android.accounts.AccountManager;
  27. import android.accounts.AccountManagerFuture;
  28. import android.content.Context;
  29. import android.content.Intent;
  30. import android.content.res.Configuration;
  31. import android.graphics.PorterDuff;
  32. import android.graphics.drawable.ColorDrawable;
  33. import android.graphics.drawable.Drawable;
  34. import android.graphics.drawable.LayerDrawable;
  35. import android.os.Build;
  36. import android.os.Bundle;
  37. import android.os.Handler;
  38. import android.support.design.widget.NavigationView;
  39. import android.support.v4.view.GravityCompat;
  40. import android.support.v4.widget.DrawerLayout;
  41. import android.support.v7.app.ActionBarDrawerToggle;
  42. import android.support.v7.graphics.drawable.DrawerArrowDrawable;
  43. import android.text.Html;
  44. import android.text.TextUtils;
  45. import android.view.Menu;
  46. import android.view.MenuItem;
  47. import android.view.View;
  48. import android.view.ViewGroup;
  49. import android.webkit.URLUtil;
  50. import android.widget.ImageView;
  51. import android.widget.LinearLayout;
  52. import android.widget.ProgressBar;
  53. import android.widget.TextView;
  54. import com.bumptech.glide.Glide;
  55. import com.bumptech.glide.request.animation.GlideAnimation;
  56. import com.bumptech.glide.request.target.SimpleTarget;
  57. import com.owncloud.android.MainApp;
  58. import com.owncloud.android.R;
  59. import com.owncloud.android.authentication.AccountUtils;
  60. import com.owncloud.android.authentication.PassCodeManager;
  61. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  62. import com.owncloud.android.datamodel.ExternalLinksProvider;
  63. import com.owncloud.android.datamodel.FileDataStorageManager;
  64. import com.owncloud.android.datamodel.OCFile;
  65. import com.owncloud.android.lib.common.ExternalLink;
  66. import com.owncloud.android.lib.common.ExternalLinkType;
  67. import com.owncloud.android.lib.common.OwnCloudAccount;
  68. import com.owncloud.android.lib.common.Quota;
  69. import com.owncloud.android.lib.common.UserInfo;
  70. import com.owncloud.android.lib.common.accounts.ExternalLinksOperation;
  71. import com.owncloud.android.lib.common.operations.RemoteOperation;
  72. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  73. import com.owncloud.android.lib.common.utils.Log_OC;
  74. import com.owncloud.android.lib.resources.files.SearchOperation;
  75. import com.owncloud.android.lib.resources.status.CapabilityBooleanType;
  76. import com.owncloud.android.lib.resources.status.OCCapability;
  77. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  78. import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
  79. import com.owncloud.android.operations.GetCapabilitiesOperarion;
  80. import com.owncloud.android.ui.TextDrawable;
  81. import com.owncloud.android.ui.activities.ActivitiesActivity;
  82. import com.owncloud.android.ui.events.AccountRemovedEvent;
  83. import com.owncloud.android.ui.events.ChangeMenuEvent;
  84. import com.owncloud.android.ui.events.DummyDrawerEvent;
  85. import com.owncloud.android.ui.events.MenuItemClickEvent;
  86. import com.owncloud.android.ui.events.SearchEvent;
  87. import com.owncloud.android.ui.trashbin.TrashbinActivity;
  88. import com.owncloud.android.utils.DisplayUtils;
  89. import com.owncloud.android.utils.FilesSyncHelper;
  90. import com.owncloud.android.utils.ThemeUtils;
  91. import com.owncloud.android.utils.svg.MenuSimpleTarget;
  92. import org.greenrobot.eventbus.EventBus;
  93. import org.greenrobot.eventbus.Subscribe;
  94. import org.greenrobot.eventbus.ThreadMode;
  95. import java.util.ArrayList;
  96. /**
  97. * Base class to handle setup of the drawer implementation including user switching and avatar fetching and fallback
  98. * generation.
  99. */
  100. public abstract class DrawerActivity extends ToolbarActivity implements DisplayUtils.AvatarGenerationListener {
  101. private static final String TAG = DrawerActivity.class.getSimpleName();
  102. private static final String KEY_IS_ACCOUNT_CHOOSER_ACTIVE = "IS_ACCOUNT_CHOOSER_ACTIVE";
  103. private static final String KEY_CHECKED_MENU_ITEM = "CHECKED_MENU_ITEM";
  104. private static final int ACTION_MANAGE_ACCOUNTS = 101;
  105. private static final int MENU_ORDER_ACCOUNT = 1;
  106. private static final int MENU_ORDER_ACCOUNT_FUNCTION = 2;
  107. private static final int MENU_ORDER_EXTERNAL_LINKS = 3;
  108. private static final int MENU_ITEM_EXTERNAL_LINK = 111;
  109. /**
  110. * menu account avatar radius.
  111. */
  112. private float mMenuAccountAvatarRadiusDimension;
  113. /**
  114. * current account avatar radius.
  115. */
  116. private float mCurrentAccountAvatarRadiusDimension;
  117. /**
  118. * other accounts avatar radius.
  119. */
  120. private float mOtherAccountAvatarRadiusDimension;
  121. /**
  122. * Reference to the drawer layout.
  123. */
  124. protected DrawerLayout mDrawerLayout;
  125. /**
  126. * Reference to the drawer toggle.
  127. */
  128. protected ActionBarDrawerToggle mDrawerToggle;
  129. /**
  130. * Reference to the navigation view.
  131. */
  132. private NavigationView mNavigationView;
  133. /**
  134. * Reference to the account chooser toggle.
  135. */
  136. private ImageView mAccountChooserToggle;
  137. /**
  138. * Reference to the middle account avatar.
  139. */
  140. private ImageView mAccountMiddleAccountAvatar;
  141. /**
  142. * Reference to the end account avatar.
  143. */
  144. private ImageView mAccountEndAccountAvatar;
  145. /**
  146. * Flag to signal if the account chooser is active.
  147. */
  148. private boolean mIsAccountChooserActive;
  149. /**
  150. * Id of the checked menu item.
  151. */
  152. private int mCheckedMenuItem = Menu.NONE;
  153. /**
  154. * accounts for the (max) three displayed accounts in the drawer header.
  155. */
  156. private Account[] mAvatars = new Account[3];
  157. /**
  158. * container layout of the quota view.
  159. */
  160. private LinearLayout mQuotaView;
  161. /**
  162. * progress bar of the quota view.
  163. */
  164. private ProgressBar mQuotaProgressBar;
  165. /**
  166. * text view of the quota view.
  167. */
  168. private TextView mQuotaTextPercentage;
  169. private TextView mQuotaTextLink;
  170. /**
  171. * runnable that will be executed after the drawer has been closed.
  172. */
  173. private Runnable pendingRunnable;
  174. private ExternalLinksProvider externalLinksProvider;
  175. private ArbitraryDataProvider arbitraryDataProvider;
  176. /**
  177. * Initializes the drawer, its content and highlights the menu item with the given id.
  178. * This method needs to be called after the content view has been set.
  179. *
  180. * @param menuItemId the menu item to be checked/highlighted
  181. */
  182. protected void setupDrawer(int menuItemId) {
  183. setupDrawer();
  184. setDrawerMenuItemChecked(menuItemId);
  185. }
  186. /**
  187. * Initializes the drawer and its content.
  188. * This method needs to be called after the content view has been set.
  189. */
  190. protected void setupDrawer() {
  191. mDrawerLayout = findViewById(R.id.drawer_layout);
  192. mNavigationView = findViewById(R.id.nav_view);
  193. if (mNavigationView != null) {
  194. setupDrawerHeader();
  195. setupDrawerMenu(mNavigationView);
  196. setupQuotaElement();
  197. }
  198. setupDrawerToggle();
  199. if(getSupportActionBar() != null) {
  200. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  201. }
  202. }
  203. /**
  204. * initializes and sets up the drawer toggle.
  205. */
  206. private void setupDrawerToggle() {
  207. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
  208. /** Called when a drawer has settled in a completely closed state. */
  209. public void onDrawerClosed(View view) {
  210. super.onDrawerClosed(view);
  211. // standard behavior of drawer is to switch to the standard menu on closing
  212. if (mIsAccountChooserActive) {
  213. toggleAccountList();
  214. }
  215. supportInvalidateOptionsMenu();
  216. mDrawerToggle.setDrawerIndicatorEnabled(isDrawerIndicatorAvailable());
  217. if (pendingRunnable != null) {
  218. new Handler().post(pendingRunnable);
  219. pendingRunnable = null;
  220. }
  221. closeDrawer();
  222. }
  223. /** Called when a drawer has settled in a completely open state. */
  224. public void onDrawerOpened(View drawerView) {
  225. super.onDrawerOpened(drawerView);
  226. mDrawerToggle.setDrawerIndicatorEnabled(true);
  227. supportInvalidateOptionsMenu();
  228. }
  229. };
  230. // Set the drawer toggle as the DrawerListener
  231. mDrawerLayout.addDrawerListener(mDrawerToggle);
  232. mDrawerToggle.setDrawerIndicatorEnabled(true);
  233. mDrawerToggle.getDrawerArrowDrawable().setColor(ThemeUtils.fontColor(this));
  234. }
  235. /**
  236. * initializes and sets up the drawer header.
  237. */
  238. private void setupDrawerHeader() {
  239. mIsAccountChooserActive = false;
  240. mAccountMiddleAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_middle);
  241. mAccountEndAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_end);
  242. mAccountChooserToggle = (ImageView) findNavigationViewChildById(R.id.drawer_account_chooser_toggle);
  243. mAccountChooserToggle.setColorFilter(ThemeUtils.fontColor(this));
  244. if (getResources().getBoolean(R.bool.allow_profile_click)) {
  245. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  246. findNavigationViewChildById(R.id.drawer_active_user)
  247. .setOnClickListener(new View.OnClickListener() {
  248. @Override
  249. public void onClick(View v) {
  250. toggleAccountList();
  251. }
  252. });
  253. } else {
  254. mAccountChooserToggle.setVisibility(View.GONE);
  255. }
  256. }
  257. /**
  258. * setup quota elements of the drawer.
  259. */
  260. private void setupQuotaElement() {
  261. mQuotaView = (LinearLayout) findQuotaViewById(R.id.drawer_quota);
  262. mQuotaProgressBar = (ProgressBar) findQuotaViewById(R.id.drawer_quota_ProgressBar);
  263. mQuotaTextPercentage = (TextView) findQuotaViewById(R.id.drawer_quota_percentage);
  264. mQuotaTextLink = (TextView) findQuotaViewById(R.id.drawer_quota_link);
  265. ThemeUtils.colorHorizontalProgressBar(mQuotaProgressBar, ThemeUtils.primaryAccentColor(this));
  266. }
  267. /**
  268. * setup drawer content, basically setting the item selected listener.
  269. *
  270. * @param navigationView the drawers navigation view
  271. */
  272. protected void setupDrawerMenu(NavigationView navigationView) {
  273. navigationView.setItemIconTintList(null);
  274. // setup actions for drawer menu items
  275. navigationView.setNavigationItemSelectedListener(
  276. new NavigationView.OnNavigationItemSelectedListener() {
  277. @Override
  278. public boolean onNavigationItemSelected(final MenuItem menuItem) {
  279. mDrawerLayout.closeDrawers();
  280. // pending runnable will be executed after the drawer has been closed
  281. pendingRunnable = new Runnable() {
  282. @Override
  283. public void run() {
  284. selectNavigationItem(menuItem);
  285. }
  286. };
  287. return true;
  288. }
  289. });
  290. // handle correct state
  291. if (mIsAccountChooserActive) {
  292. navigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  293. } else {
  294. navigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  295. }
  296. Account account = AccountUtils.getCurrentOwnCloudAccount(this);
  297. filterDrawerMenu(navigationView.getMenu(), account);
  298. }
  299. private void filterDrawerMenu(Menu menu, Account account) {
  300. boolean searchSupported = AccountUtils.hasSearchSupport(account);
  301. if (getResources().getBoolean(R.bool.bottom_toolbar_enabled) && account != null) {
  302. filterMenuItems(menu, R.id.nav_all_files, R.id.nav_settings, R.id.nav_favorites, R.id.nav_photos);
  303. }
  304. if (!searchSupported && account != null) {
  305. filterMenuItems(menu, R.id.nav_photos, R.id.nav_favorites, R.id.nav_videos);
  306. }
  307. if (account != null) {
  308. FileDataStorageManager storageManager = new FileDataStorageManager(account, getContentResolver());
  309. OCCapability capability = storageManager.getCapability(account.name);
  310. if (AccountUtils.getServerVersion(getAccount()).compareTo(OwnCloudVersion.nextcloud_14) < 0 ||
  311. capability.getFilesUndelete().isFalse() || capability.getFilesUndelete().isUnknown()) {
  312. filterMenuItems(menu, R.id.nav_trashbin);
  313. }
  314. }
  315. if (getResources().getBoolean(R.bool.use_home) && menu.findItem(R.id.nav_all_files) != null) {
  316. menu.findItem(R.id.nav_all_files).setTitle(getResources().getString(R.string.drawer_item_home));
  317. menu.findItem(R.id.nav_all_files).setIcon(R.drawable.ic_home);
  318. }
  319. if (!getResources().getBoolean(R.bool.participate_enabled)) {
  320. menu.removeItem(R.id.nav_participate);
  321. }
  322. if (!getResources().getBoolean(R.bool.shared_enabled)) {
  323. menu.removeItem(R.id.nav_shared);
  324. }
  325. if (!getResources().getBoolean(R.bool.contacts_backup)
  326. || !getResources().getBoolean(R.bool.show_drawer_contacts_backup)) {
  327. menu.removeItem(R.id.nav_contacts);
  328. }
  329. if (getResources().getBoolean(R.bool.syncedFolder_light)) {
  330. menu.removeItem(R.id.nav_synced_folders);
  331. }
  332. if (!getResources().getBoolean(R.bool.show_drawer_logout)) {
  333. menu.removeItem(R.id.nav_logout);
  334. }
  335. if (AccountUtils.hasSearchSupport(account)) {
  336. if (!getResources().getBoolean(R.bool.recently_added_enabled)) {
  337. menu.removeItem(R.id.nav_recently_added);
  338. }
  339. if (!getResources().getBoolean(R.bool.recently_modified_enabled)) {
  340. menu.removeItem(R.id.nav_recently_modified);
  341. }
  342. if (!getResources().getBoolean(R.bool.videos_enabled)) {
  343. menu.removeItem(R.id.nav_videos);
  344. }
  345. } else if (account != null) {
  346. filterMenuItems(menu, R.id.nav_recently_added, R.id.nav_recently_modified, R.id.nav_videos);
  347. }
  348. }
  349. private static void filterMenuItems(Menu menu, int... menuIds) {
  350. if (menuIds != null) {
  351. for (int menuId : menuIds) {
  352. menu.removeItem(menuId);
  353. }
  354. }
  355. }
  356. @Subscribe(threadMode = ThreadMode.MAIN)
  357. public void onMessageEvent(MenuItemClickEvent event) {
  358. unsetAllDrawerMenuItems();
  359. switch (event.menuItem.getItemId()) {
  360. case R.id.nav_bar_files:
  361. showFiles(false);
  362. break;
  363. case R.id.nav_bar_settings:
  364. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  365. startActivity(settingsIntent);
  366. break;
  367. default:
  368. break;
  369. }
  370. }
  371. @Subscribe(threadMode = ThreadMode.MAIN)
  372. public void onMessageEvent(DummyDrawerEvent event) {
  373. unsetAllDrawerMenuItems();
  374. }
  375. private void selectNavigationItem(final MenuItem menuItem) {
  376. setDrawerMenuItemChecked(menuItem.getItemId());
  377. switch (menuItem.getItemId()) {
  378. case R.id.nav_all_files:
  379. showFiles(false);
  380. EventBus.getDefault().post(new ChangeMenuEvent());
  381. break;
  382. case R.id.nav_favorites:
  383. EventBus.getDefault().post(new SearchEvent("", SearchOperation.SearchType.FAVORITE_SEARCH,
  384. SearchEvent.UnsetType.NO_UNSET));
  385. break;
  386. case R.id.nav_photos:
  387. EventBus.getDefault().post(new SearchEvent("image/%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  388. SearchEvent.UnsetType.NO_UNSET));
  389. break;
  390. case R.id.nav_on_device:
  391. EventBus.getDefault().post(new ChangeMenuEvent());
  392. showFiles(true);
  393. break;
  394. case R.id.nav_uploads:
  395. Intent uploadListIntent = new Intent(getApplicationContext(), UploadListActivity.class);
  396. uploadListIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  397. startActivity(uploadListIntent);
  398. break;
  399. case R.id.nav_trashbin:
  400. Intent trashbinIntent = new Intent(getApplicationContext(), TrashbinActivity.class);
  401. trashbinIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  402. startActivity(trashbinIntent);
  403. break;
  404. case R.id.nav_activity:
  405. Intent activityIntent = new Intent(getApplicationContext(), ActivitiesActivity.class);
  406. activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  407. startActivity(activityIntent);
  408. break;
  409. case R.id.nav_notifications:
  410. Intent notificationsIntent = new Intent(getApplicationContext(), NotificationsActivity.class);
  411. startActivity(notificationsIntent);
  412. break;
  413. case R.id.nav_synced_folders:
  414. Intent syncedFoldersIntent = new Intent(getApplicationContext(), SyncedFoldersActivity.class);
  415. startActivity(syncedFoldersIntent);
  416. break;
  417. case R.id.nav_contacts:
  418. Intent contactsIntent = new Intent(getApplicationContext(), ContactsPreferenceActivity.class);
  419. startActivity(contactsIntent);
  420. break;
  421. case R.id.nav_settings:
  422. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  423. startActivity(settingsIntent);
  424. break;
  425. case R.id.nav_participate:
  426. Intent participateIntent = new Intent(getApplicationContext(), ParticipateActivity.class);
  427. startActivity(participateIntent);
  428. break;
  429. case R.id.nav_logout:
  430. mCheckedMenuItem = -1;
  431. menuItem.setChecked(false);
  432. UserInfoActivity.openAccountRemovalConfirmationDialog(getAccount(), getFragmentManager(), true);
  433. break;
  434. case R.id.drawer_menu_account_add:
  435. createAccount(false);
  436. break;
  437. case R.id.drawer_menu_account_manage:
  438. Intent manageAccountsIntent = new Intent(getApplicationContext(), ManageAccountsActivity.class);
  439. startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
  440. break;
  441. case R.id.nav_recently_added:
  442. EventBus.getDefault().post(new SearchEvent("%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  443. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR));
  444. break;
  445. case R.id.nav_recently_modified:
  446. EventBus.getDefault().post(new SearchEvent("", SearchOperation.SearchType.RECENTLY_MODIFIED_SEARCH,
  447. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR));
  448. break;
  449. case R.id.nav_shared:
  450. EventBus.getDefault().post(new SearchEvent("", SearchOperation.SearchType.SHARED_SEARCH,
  451. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR));
  452. break;
  453. case R.id.nav_videos:
  454. EventBus.getDefault().post(new SearchEvent("video/%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  455. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR));
  456. break;
  457. case Menu.NONE:
  458. // account clicked
  459. accountClicked(menuItem.getTitle().toString());
  460. break;
  461. default:
  462. break;
  463. }
  464. if (menuItem.getItemId() >= MENU_ITEM_EXTERNAL_LINK && menuItem.getItemId() <= MENU_ITEM_EXTERNAL_LINK + 100) {
  465. // external link clicked
  466. externalLinkClicked(menuItem);
  467. } else {
  468. Log_OC.i(TAG, "Unknown drawer menu item clicked: " + menuItem.getTitle());
  469. }
  470. }
  471. /**
  472. * show the file list to the user.
  473. *
  474. * @param onDeviceOnly flag to decide if all files or only the ones on the device should be shown
  475. */
  476. public abstract void showFiles(boolean onDeviceOnly);
  477. /**
  478. * sets the new/current account and restarts. In case the given account equals the actual/current account the
  479. * call will be ignored.
  480. *
  481. * @param accountName The account name to be set
  482. */
  483. private void accountClicked(String accountName) {
  484. if (!AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()).name.equals(accountName)) {
  485. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), accountName);
  486. fetchExternalLinks(true);
  487. restart();
  488. }
  489. }
  490. private void externalLinkClicked(MenuItem menuItem){
  491. for (ExternalLink link : externalLinksProvider.getExternalLink(ExternalLinkType.LINK)) {
  492. if (menuItem.getTitle().toString().equalsIgnoreCase(link.name)) {
  493. Intent externalWebViewIntent = new Intent(getApplicationContext(),
  494. ExternalSiteWebView.class);
  495. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, link.name);
  496. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, link.url);
  497. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, true);
  498. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, menuItem.getItemId());
  499. startActivity(externalWebViewIntent);
  500. }
  501. }
  502. }
  503. /**
  504. * click method for mini avatars in drawer header.
  505. *
  506. * @param view the clicked ImageView
  507. */
  508. public void onAccountDrawerClick(View view) {
  509. accountClicked(view.getContentDescription().toString());
  510. }
  511. /**
  512. * checks if the drawer exists and is opened.
  513. *
  514. * @return <code>true</code> if the drawer is open, else <code>false</code>
  515. */
  516. public boolean isDrawerOpen() {
  517. return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.START);
  518. }
  519. /**
  520. * closes the drawer.
  521. */
  522. public void closeDrawer() {
  523. if (mDrawerLayout != null) {
  524. mDrawerLayout.closeDrawer(GravityCompat.START);
  525. }
  526. }
  527. /**
  528. * opens the drawer.
  529. */
  530. public void openDrawer() {
  531. if (mDrawerLayout != null) {
  532. mDrawerLayout.openDrawer(GravityCompat.START);
  533. updateExternalLinksInDrawer();
  534. updateQuotaLink();
  535. }
  536. }
  537. /**
  538. * Enable or disable interaction with all drawers.
  539. *
  540. * @param lockMode The new lock mode for the given drawer. One of {@link DrawerLayout#LOCK_MODE_UNLOCKED},
  541. * {@link DrawerLayout#LOCK_MODE_LOCKED_CLOSED} or {@link DrawerLayout#LOCK_MODE_LOCKED_OPEN}.
  542. */
  543. public void setDrawerLockMode(int lockMode) {
  544. if (mDrawerLayout != null) {
  545. mDrawerLayout.setDrawerLockMode(lockMode);
  546. }
  547. }
  548. /**
  549. * Enable or disable the drawer indicator.
  550. *
  551. * @param enable true to enable, false to disable
  552. */
  553. public void setDrawerIndicatorEnabled(boolean enable) {
  554. if (mDrawerToggle != null) {
  555. mDrawerToggle.setDrawerIndicatorEnabled(enable);
  556. }
  557. }
  558. /**
  559. * updates the account list in the drawer.
  560. */
  561. public void updateAccountList() {
  562. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType(this));
  563. ArrayList<Account> persistingAccounts = new ArrayList<>();
  564. for (Account acc: accounts) {
  565. boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(acc,
  566. ManageAccountsActivity.PENDING_FOR_REMOVAL);
  567. if (!pendingForRemoval) {
  568. persistingAccounts.add(acc);
  569. }
  570. }
  571. if (mNavigationView != null && mDrawerLayout != null) {
  572. if (persistingAccounts.size() > 0) {
  573. repopulateAccountList(persistingAccounts);
  574. setAccountInDrawer(AccountUtils.getCurrentOwnCloudAccount(this));
  575. populateDrawerOwnCloudAccounts();
  576. // activate second/end account avatar
  577. if (mAvatars[1] != null) {
  578. View accountEndView = findNavigationViewChildById(R.id.drawer_account_end);
  579. accountEndView.setTag(mAvatars[1].name);
  580. DisplayUtils.setAvatar(mAvatars[1], this, mOtherAccountAvatarRadiusDimension, getResources(),
  581. getStorageManager(), accountEndView, this);
  582. mAccountEndAccountAvatar.setVisibility(View.VISIBLE);
  583. } else {
  584. mAccountEndAccountAvatar.setVisibility(View.GONE);
  585. }
  586. // activate third/middle account avatar
  587. if (mAvatars[2] != null) {
  588. View accountMiddleView = findNavigationViewChildById(R.id.drawer_account_middle);
  589. accountMiddleView.setTag(mAvatars[2].name);
  590. DisplayUtils.setAvatar(mAvatars[2], this, mOtherAccountAvatarRadiusDimension, getResources(),
  591. getStorageManager(), accountMiddleView, this);
  592. mAccountMiddleAccountAvatar.setVisibility(View.VISIBLE);
  593. } else {
  594. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  595. }
  596. } else {
  597. mAccountEndAccountAvatar.setVisibility(View.GONE);
  598. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  599. }
  600. }
  601. }
  602. /**
  603. * re-populates the account list.
  604. *
  605. * @param accounts list of accounts
  606. */
  607. private void repopulateAccountList(ArrayList<Account> accounts) {
  608. // remove all accounts from list
  609. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_accounts);
  610. // add all accounts to list
  611. for (Account account: accounts) {
  612. try {
  613. // show all accounts except the currently active one and those pending for removal
  614. if (!getAccount().name.equals(account.name)) {
  615. MenuItem accountMenuItem = mNavigationView.getMenu().add(
  616. R.id.drawer_menu_accounts,
  617. Menu.NONE,
  618. MENU_ORDER_ACCOUNT,
  619. account.name)
  620. .setIcon(TextDrawable.createAvatar(account.name, mMenuAccountAvatarRadiusDimension));
  621. DisplayUtils.setAvatar(account, this, mMenuAccountAvatarRadiusDimension, getResources(),
  622. getStorageManager(), accountMenuItem, this);
  623. }
  624. } catch (Exception e) {
  625. Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
  626. mNavigationView.getMenu().add(
  627. R.id.drawer_menu_accounts,
  628. Menu.NONE,
  629. MENU_ORDER_ACCOUNT,
  630. account.name)
  631. .setIcon(R.drawable.ic_user);
  632. }
  633. }
  634. // re-add add-account and manage-accounts
  635. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_add,
  636. MENU_ORDER_ACCOUNT_FUNCTION,
  637. getResources().getString(R.string.prefs_add_account)).setIcon(R.drawable.ic_account_plus);
  638. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_manage,
  639. MENU_ORDER_ACCOUNT_FUNCTION,
  640. getResources().getString(R.string.drawer_manage_accounts)).setIcon(R.drawable.ic_settings);
  641. // adding sets menu group back to visible, so safety check and setting invisible
  642. showMenu();
  643. }
  644. /**
  645. * Updates title bar and home buttons (state and icon).
  646. * <p/>
  647. * Assumes that navigation drawer is NOT visible.
  648. */
  649. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  650. super.updateActionBarTitleAndHomeButton(chosenFile);
  651. // set home button properties
  652. if (mDrawerToggle != null && chosenFile != null) {
  653. mDrawerToggle.setDrawerIndicatorEnabled(isRoot(chosenFile));
  654. } else if (mDrawerToggle != null){
  655. mDrawerToggle.setDrawerIndicatorEnabled(false);
  656. }
  657. if (mDrawerToggle != null) {
  658. DrawerArrowDrawable icon = mDrawerToggle.getDrawerArrowDrawable();
  659. icon.setColorFilter(ThemeUtils.fontColor(this), PorterDuff.Mode.SRC_ATOP);
  660. mDrawerToggle.setDrawerArrowDrawable(icon);
  661. }
  662. }
  663. /**
  664. * sets the given account name in the drawer in case the drawer is available. The account name is shortened
  665. * beginning from the @-sign in the username.
  666. *
  667. * @param account the account to be set in the drawer
  668. */
  669. protected void setAccountInDrawer(Account account) {
  670. if (mDrawerLayout != null && account != null) {
  671. TextView username = (TextView) findNavigationViewChildById(R.id.drawer_username);
  672. TextView usernameFull = (TextView) findNavigationViewChildById(R.id.drawer_username_full);
  673. usernameFull.setText(account.name);
  674. usernameFull.setTextColor(ThemeUtils.fontColor(this));
  675. try {
  676. OwnCloudAccount oca = new OwnCloudAccount(account, this);
  677. username.setText(oca.getDisplayName());
  678. username.setTextColor(ThemeUtils.fontColor(this));
  679. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  680. Log_OC.w(TAG, "Couldn't read display name of account fallback to account name");
  681. username.setText(AccountUtils.getAccountUsername(account.name));
  682. }
  683. View currentAccountView = findNavigationViewChildById(R.id.drawer_current_account);
  684. currentAccountView.setTag(account.name);
  685. DisplayUtils.setAvatar(account, this, mCurrentAccountAvatarRadiusDimension, getResources(),
  686. getStorageManager(), currentAccountView, this);
  687. // check and show quota info if available
  688. getAndDisplayUserQuota();
  689. }
  690. }
  691. /**
  692. * Toggle between standard menu and account list including saving the state.
  693. */
  694. private void toggleAccountList() {
  695. mIsAccountChooserActive = !mIsAccountChooserActive;
  696. showMenu();
  697. }
  698. /**
  699. * depending on the #mIsAccountChooserActive flag shows the account chooser or the standard menu.
  700. */
  701. private void showMenu() {
  702. if (mNavigationView != null) {
  703. if (mIsAccountChooserActive) {
  704. if (mAccountChooserToggle != null) {
  705. mAccountChooserToggle.setImageResource(R.drawable.ic_up);
  706. }
  707. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  708. if (!getResources().getBoolean(R.bool.multiaccount_support) &&
  709. mNavigationView.getMenu().findItem(R.id.drawer_menu_account_add) != null) {
  710. mNavigationView.getMenu().removeItem(R.id.drawer_menu_account_add);
  711. }
  712. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, false);
  713. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_external_links, false);
  714. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_bottom, false);
  715. } else {
  716. if (mAccountChooserToggle != null) {
  717. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  718. }
  719. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  720. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, true);
  721. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_external_links, true);
  722. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_bottom, true);
  723. }
  724. }
  725. }
  726. /**
  727. * shows or hides the quota UI elements.
  728. *
  729. * @param showQuota show/hide quota information
  730. */
  731. private void showQuota(boolean showQuota) {
  732. if (showQuota) {
  733. mQuotaView.setVisibility(View.VISIBLE);
  734. } else {
  735. mQuotaView.setVisibility(View.GONE);
  736. }
  737. }
  738. /**
  739. * configured the quota to be displayed.
  740. *
  741. * @param usedSpace the used space
  742. * @param totalSpace the total space
  743. * @param relative the percentage of space already used
  744. */
  745. private void setQuotaInformation(long usedSpace, long totalSpace, int relative) {
  746. mQuotaProgressBar.setProgress(relative);
  747. ThemeUtils.colorHorizontalProgressBar(mQuotaProgressBar, DisplayUtils.getRelativeInfoColor(this, relative));
  748. updateQuotaLink();
  749. mQuotaTextPercentage.setText(String.format(
  750. getString(R.string.drawer_quota),
  751. DisplayUtils.bytesToHumanReadable(usedSpace),
  752. DisplayUtils.bytesToHumanReadable(totalSpace)));
  753. showQuota(true);
  754. }
  755. protected void unsetAllDrawerMenuItems() {
  756. if (mNavigationView != null && mNavigationView.getMenu() != null) {
  757. Menu menu = mNavigationView.getMenu();
  758. for (int i = 0; i < menu.size(); i++) {
  759. menu.getItem(i).setChecked(false);
  760. }
  761. }
  762. mCheckedMenuItem = Menu.NONE;
  763. }
  764. private void updateQuotaLink() {
  765. if (mQuotaTextLink != null) {
  766. if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  767. ArrayList<ExternalLink> quotas = externalLinksProvider.getExternalLink(ExternalLinkType.QUOTA);
  768. float density = getResources().getDisplayMetrics().density;
  769. final int size = Math.round(24 * density);
  770. if (quotas.size() > 0) {
  771. final ExternalLink firstQuota = quotas.get(0);
  772. mQuotaTextLink.setText(firstQuota.name);
  773. mQuotaTextLink.setClickable(true);
  774. mQuotaTextLink.setVisibility(View.VISIBLE);
  775. mQuotaTextLink.setOnClickListener(new View.OnClickListener() {
  776. @Override
  777. public void onClick(View v) {
  778. Intent externalWebViewIntent = new Intent(getApplicationContext(), ExternalSiteWebView.class);
  779. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, firstQuota.name);
  780. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, firstQuota.url);
  781. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, true);
  782. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, -1);
  783. startActivity(externalWebViewIntent);
  784. }
  785. });
  786. SimpleTarget target = new SimpleTarget<Drawable>() {
  787. @Override
  788. public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
  789. Drawable test = resource.getCurrent();
  790. test.setBounds(0, 0, size, size);
  791. mQuotaTextLink.setCompoundDrawablesWithIntrinsicBounds(test, null, null, null);
  792. }
  793. @Override
  794. public void onLoadFailed(Exception e, Drawable errorDrawable) {
  795. super.onLoadFailed(e, errorDrawable);
  796. Drawable test = errorDrawable.getCurrent();
  797. test.setBounds(0, 0, size, size);
  798. mQuotaTextLink.setCompoundDrawablesWithIntrinsicBounds(test, null, null, null);
  799. }
  800. };
  801. DisplayUtils.downloadIcon(this, firstQuota.iconUrl, target, R.drawable.ic_link_grey, size, size);
  802. } else {
  803. mQuotaTextLink.setVisibility(View.GONE);
  804. }
  805. } else {
  806. mQuotaTextLink.setVisibility(View.GONE);
  807. }
  808. }
  809. }
  810. /**
  811. * checks/highlights the provided menu item if the drawer has been initialized and the menu item exists.
  812. *
  813. * @param menuItemId the menu item to be highlighted
  814. */
  815. protected void setDrawerMenuItemChecked(int menuItemId) {
  816. if (mNavigationView != null && mNavigationView.getMenu() != null &&
  817. mNavigationView.getMenu().findItem(menuItemId) != null) {
  818. MenuItem item = mNavigationView.getMenu().findItem(menuItemId);
  819. item.setChecked(true);
  820. // reset all tinted icons
  821. for (int i = 0; i < mNavigationView.getMenu().size(); i++) {
  822. MenuItem menuItem = mNavigationView.getMenu().getItem(i);
  823. if (menuItem.getIcon() != null) {
  824. menuItem.getIcon().clearColorFilter();
  825. menuItem.setTitle(Html.fromHtml("<font color='#000000'>" + menuItem.getTitle() + "</font>"));
  826. }
  827. }
  828. int elementColor = ThemeUtils.elementColor(this);
  829. ThemeUtils.tintDrawable(item.getIcon(), elementColor);
  830. String colorHex = ThemeUtils.colorToHexString(elementColor);
  831. item.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + item.getTitle() + "</font>"));
  832. mCheckedMenuItem = menuItemId;
  833. } else {
  834. Log_OC.w(TAG, "setDrawerMenuItemChecked has been called with invalid menu-item-ID");
  835. }
  836. }
  837. /**
  838. * Retrieves and shows the user quota if available
  839. */
  840. private void getAndDisplayUserQuota() {
  841. // set user space information
  842. Thread t = new Thread(new Runnable() {
  843. public void run() {
  844. Context context = MainApp.getAppContext();
  845. AccountManager mAccountMgr = AccountManager.get(context);
  846. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  847. if (account == null) {
  848. return;
  849. }
  850. String userId = mAccountMgr.getUserData(account,
  851. com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
  852. RemoteOperation getQuotaInfoOperation;
  853. if (TextUtils.isEmpty(userId)) {
  854. getQuotaInfoOperation = new GetRemoteUserInfoOperation();
  855. } else {
  856. getQuotaInfoOperation = new GetRemoteUserInfoOperation(userId);
  857. }
  858. RemoteOperationResult result = getQuotaInfoOperation.execute(account, context);
  859. if (result.isSuccess() && result.getData() != null) {
  860. final UserInfo userInfo = (UserInfo) result.getData().get(0);
  861. final Quota quota = userInfo.getQuota();
  862. // Since we always call this method, might as well put it here
  863. if (userInfo.getId() != null) {
  864. mAccountMgr.setUserData(account,
  865. com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID,
  866. userInfo.getId());
  867. }
  868. if (quota != null) {
  869. final long used = quota.getUsed();
  870. final long total = quota.getTotal();
  871. final int relative = (int) Math.ceil(quota.getRelative());
  872. final long quotaValue = quota.getQuota();
  873. runOnUiThread(new Runnable() {
  874. @Override
  875. public void run() {
  876. if (quotaValue > 0
  877. || quotaValue == GetRemoteUserInfoOperation.QUOTA_LIMIT_INFO_NOT_AVAILABLE) {
  878. /**
  879. * show quota in case
  880. * it is available and calculated (> 0) or
  881. * in case of legacy servers (==QUOTA_LIMIT_INFO_NOT_AVAILABLE)
  882. */
  883. setQuotaInformation(used, total, relative);
  884. } else {
  885. /**
  886. * quotaValue < 0 means special cases like
  887. * {@link RemoteGetUserQuotaOperation.SPACE_NOT_COMPUTED},
  888. * {@link RemoteGetUserQuotaOperation.SPACE_UNKNOWN} or
  889. * {@link RemoteGetUserQuotaOperation.SPACE_UNLIMITED}
  890. * thus don't display any quota information.
  891. */
  892. showQuota(false);
  893. }
  894. }
  895. });
  896. }
  897. }
  898. }
  899. });
  900. t.start();
  901. }
  902. public void updateExternalLinksInDrawer() {
  903. if (mNavigationView != null && getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  904. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_external_links);
  905. float density = getResources().getDisplayMetrics().density;
  906. final int size = Math.round(24 * density);
  907. int greyColor = getResources().getColor(R.color.standard_grey);
  908. for (final ExternalLink link : externalLinksProvider.getExternalLink(ExternalLinkType.LINK)) {
  909. int id = mNavigationView.getMenu().add(R.id.drawer_menu_external_links,
  910. MENU_ITEM_EXTERNAL_LINK + link.id, MENU_ORDER_EXTERNAL_LINKS, link.name)
  911. .setCheckable(true).getItemId();
  912. MenuSimpleTarget target = new MenuSimpleTarget<Drawable>(id) {
  913. @Override
  914. public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
  915. setExternalLinkIcon(getIdMenuItem(), resource, greyColor);
  916. }
  917. @Override
  918. public void onLoadFailed(Exception e, Drawable errorDrawable) {
  919. super.onLoadFailed(e, errorDrawable);
  920. setExternalLinkIcon(getIdMenuItem(), errorDrawable, greyColor);
  921. }
  922. };
  923. DisplayUtils.downloadIcon(this, link.iconUrl, target, R.drawable.ic_link_grey, size, size);
  924. }
  925. }
  926. }
  927. private void setExternalLinkIcon(int id, Drawable drawable, int greyColor) {
  928. MenuItem menuItem = mNavigationView.getMenu().findItem(id);
  929. if (menuItem != null) {
  930. if (drawable != null) {
  931. menuItem.setIcon(ThemeUtils.tintDrawable(drawable, greyColor));
  932. } else {
  933. menuItem.setIcon(R.drawable.ic_link_grey);
  934. }
  935. }
  936. }
  937. public void updateHeaderBackground() {
  938. if (getAccount() != null &&
  939. getStorageManager().getCapability(getAccount().name).getServerBackground() != null) {
  940. final ViewGroup navigationHeader = (ViewGroup) findNavigationViewChildById(R.id.drawer_header_view);
  941. if (navigationHeader != null) {
  942. OCCapability capability = getStorageManager().getCapability(getAccount().name);
  943. String background = capability.getServerBackground();
  944. CapabilityBooleanType backgroundDefault = capability.getServerBackgroundDefault();
  945. CapabilityBooleanType backgroundPlain = capability.getServerBackgroundPlain();
  946. int primaryColor = ThemeUtils.primaryColor(getAccount(), false, this);
  947. if (backgroundDefault.isTrue() && backgroundPlain.isTrue()) {
  948. // use only solid color
  949. setNavigationHeaderBackground(new ColorDrawable(primaryColor), navigationHeader);
  950. } else if (backgroundDefault.isTrue() && backgroundPlain.isFalse()) {
  951. // use nc13 background image with themed color
  952. Drawable[] drawables = {new ColorDrawable(primaryColor),
  953. getResources().getDrawable(R.drawable.background_nc13)};
  954. LayerDrawable layerDrawable = new LayerDrawable(drawables);
  955. setNavigationHeaderBackground(layerDrawable, navigationHeader);
  956. } else {
  957. // use url
  958. if (URLUtil.isValidUrl(background) || background.isEmpty()) {
  959. // background image
  960. SimpleTarget target = new SimpleTarget<Drawable>() {
  961. @Override
  962. public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
  963. Drawable[] drawables = {new ColorDrawable(primaryColor), resource};
  964. LayerDrawable layerDrawable = new LayerDrawable(drawables);
  965. setNavigationHeaderBackground(layerDrawable, navigationHeader);
  966. }
  967. @Override
  968. public void onLoadFailed(Exception e, Drawable errorDrawable) {
  969. Drawable[] drawables = {new ColorDrawable(primaryColor), errorDrawable};
  970. LayerDrawable layerDrawable = new LayerDrawable(drawables);
  971. setNavigationHeaderBackground(layerDrawable, navigationHeader);
  972. }
  973. };
  974. int backgroundResource;
  975. OwnCloudVersion ownCloudVersion = AccountUtils.getServerVersion(getAccount());
  976. if (ownCloudVersion.compareTo(OwnCloudVersion.nextcloud_13) >= 0) {
  977. backgroundResource = R.drawable.background_nc13;
  978. } else {
  979. backgroundResource = R.drawable.background;
  980. }
  981. Glide.with(this)
  982. .load(background)
  983. .centerCrop()
  984. .placeholder(backgroundResource)
  985. .error(backgroundResource)
  986. .crossFade()
  987. .into(target);
  988. } else {
  989. // plain color
  990. setNavigationHeaderBackground(new ColorDrawable(primaryColor), navigationHeader);
  991. }
  992. }
  993. }
  994. }
  995. }
  996. private void setNavigationHeaderBackground(Drawable drawable, ViewGroup navigationHeader) {
  997. final ImageView background = navigationHeader.findViewById(R.id.drawer_header_background);
  998. background.setImageDrawable(drawable);
  999. }
  1000. @Override
  1001. protected void onCreate(Bundle savedInstanceState) {
  1002. super.onCreate(savedInstanceState);
  1003. if (savedInstanceState != null) {
  1004. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  1005. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  1006. }
  1007. mCurrentAccountAvatarRadiusDimension = getResources()
  1008. .getDimension(R.dimen.nav_drawer_header_avatar_radius);
  1009. mOtherAccountAvatarRadiusDimension = getResources()
  1010. .getDimension(R.dimen.nav_drawer_header_avatar_other_accounts_radius);
  1011. mMenuAccountAvatarRadiusDimension = getResources()
  1012. .getDimension(R.dimen.nav_drawer_menu_avatar_radius);
  1013. externalLinksProvider = new ExternalLinksProvider(getContentResolver());
  1014. arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  1015. }
  1016. @Override
  1017. protected void onSaveInstanceState(Bundle outState) {
  1018. super.onSaveInstanceState(outState);
  1019. outState.putBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, mIsAccountChooserActive);
  1020. outState.putInt(KEY_CHECKED_MENU_ITEM, mCheckedMenuItem);
  1021. }
  1022. @Override
  1023. public void onRestoreInstanceState(Bundle savedInstanceState) {
  1024. super.onRestoreInstanceState(savedInstanceState);
  1025. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  1026. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  1027. // (re-)setup drawer state
  1028. showMenu();
  1029. // check/highlight the menu item if present
  1030. if (mCheckedMenuItem > Menu.NONE || mCheckedMenuItem < Menu.NONE) {
  1031. setDrawerMenuItemChecked(mCheckedMenuItem);
  1032. }
  1033. }
  1034. @Override
  1035. protected void onPostCreate(Bundle savedInstanceState) {
  1036. super.onPostCreate(savedInstanceState);
  1037. // Sync the toggle state after onRestoreInstanceState has occurred.
  1038. if (mDrawerToggle != null) {
  1039. mDrawerToggle.syncState();
  1040. if (isDrawerOpen()) {
  1041. mDrawerToggle.setDrawerIndicatorEnabled(true);
  1042. }
  1043. }
  1044. updateAccountList();
  1045. updateExternalLinksInDrawer();
  1046. updateQuotaLink();
  1047. updateHeaderBackground();
  1048. }
  1049. @Override
  1050. public void onConfigurationChanged(Configuration newConfig) {
  1051. super.onConfigurationChanged(newConfig);
  1052. if (mDrawerToggle != null) {
  1053. mDrawerToggle.onConfigurationChanged(newConfig);
  1054. }
  1055. }
  1056. @Override
  1057. public void onBackPressed() {
  1058. if (isDrawerOpen()) {
  1059. closeDrawer();
  1060. return;
  1061. }
  1062. super.onBackPressed();
  1063. }
  1064. @Override
  1065. protected void onResume() {
  1066. super.onResume();
  1067. setDrawerMenuItemChecked(mCheckedMenuItem);
  1068. }
  1069. @Override
  1070. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  1071. super.onActivityResult(requestCode, resultCode, data);
  1072. // update Account list and active account if Manage Account activity replies with
  1073. // - ACCOUNT_LIST_CHANGED = true
  1074. // - RESULT_OK
  1075. if (requestCode == ACTION_MANAGE_ACCOUNTS && resultCode == RESULT_OK
  1076. && data.getBooleanExtra(ManageAccountsActivity.KEY_ACCOUNT_LIST_CHANGED, false)) {
  1077. // current account has changed
  1078. if (data.getBooleanExtra(ManageAccountsActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
  1079. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  1080. updateAccountList();
  1081. restart();
  1082. } else {
  1083. updateAccountList();
  1084. }
  1085. } else if (requestCode == PassCodeManager.PASSCODE_ACTIVITY &&
  1086. Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && data != null) {
  1087. int result = data.getIntExtra(RequestCredentialsActivity.KEY_CHECK_RESULT,
  1088. RequestCredentialsActivity.KEY_CHECK_RESULT_FALSE);
  1089. if (result == RequestCredentialsActivity.KEY_CHECK_RESULT_CANCEL) {
  1090. Log_OC.d(TAG, "PassCodeManager cancelled");
  1091. super.onBackPressed();
  1092. }
  1093. }
  1094. }
  1095. /**
  1096. * Finds a view that was identified by the id attribute from the drawer header.
  1097. *
  1098. * @param id the view's id
  1099. * @return The view if found or <code>null</code> otherwise.
  1100. */
  1101. private View findNavigationViewChildById(int id) {
  1102. NavigationView view = findViewById(R.id.nav_view);
  1103. if (view != null) {
  1104. return view.getHeaderView(0).findViewById(id);
  1105. } else {
  1106. return null;
  1107. }
  1108. }
  1109. /**
  1110. * Quota view can be either at navigation bottom or header
  1111. *
  1112. * @param id the view's id
  1113. * @return The view if found or <code>null</code> otherwise.
  1114. */
  1115. private View findQuotaViewById(int id) {
  1116. View v = ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0).findViewById(id);
  1117. if (v != null) {
  1118. return v;
  1119. } else {
  1120. return findViewById(id);
  1121. }
  1122. }
  1123. /**
  1124. * restart helper method which is called after a changing the current account.
  1125. */
  1126. protected abstract void restart();
  1127. @Override
  1128. protected void onAccountCreationSuccessful(AccountManagerFuture<Bundle> future) {
  1129. super.onAccountCreationSuccessful(future);
  1130. updateAccountList();
  1131. restart();
  1132. }
  1133. /**
  1134. * populates the avatar drawer array with the first three ownCloud {@link Account}s while the first element is
  1135. * always the current account.
  1136. */
  1137. private void populateDrawerOwnCloudAccounts() {
  1138. mAvatars = new Account[3];
  1139. Account[] accountsAll = AccountManager.get(this).getAccountsByType(MainApp.getAccountType(this));
  1140. ArrayList<Account> persistingAccounts = new ArrayList<>();
  1141. for (Account acc: accountsAll) {
  1142. boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(acc,
  1143. ManageAccountsActivity.PENDING_FOR_REMOVAL);
  1144. if (!pendingForRemoval) {
  1145. persistingAccounts.add(acc);
  1146. }
  1147. }
  1148. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  1149. mAvatars[0] = currentAccount;
  1150. int j = 0;
  1151. for (int i = 1; i <= 2 && i < persistingAccounts.size() && j < persistingAccounts.size(); j++) {
  1152. if (!currentAccount.equals(persistingAccounts.get(j))) {
  1153. mAvatars[i] = persistingAccounts.get(j);
  1154. i++;
  1155. }
  1156. }
  1157. }
  1158. @Override
  1159. public void avatarGenerated(Drawable avatarDrawable, Object callContext) {
  1160. if (callContext instanceof MenuItem) {
  1161. MenuItem mi = (MenuItem) callContext;
  1162. mi.setIcon(avatarDrawable);
  1163. } else if (callContext instanceof ImageView) {
  1164. ImageView iv = (ImageView) callContext;
  1165. iv.setImageDrawable(avatarDrawable);
  1166. }
  1167. }
  1168. @Override
  1169. public boolean shouldCallGeneratedCallback(String tag, Object callContext) {
  1170. if (callContext instanceof MenuItem) {
  1171. MenuItem mi = (MenuItem) callContext;
  1172. return String.valueOf(mi.getTitle()).equals(tag);
  1173. } else if (callContext instanceof ImageView) {
  1174. ImageView iv = (ImageView) callContext;
  1175. return String.valueOf(iv.getTag()).equals(tag);
  1176. }
  1177. return false;
  1178. }
  1179. /**
  1180. * Adds other listeners to react on changes of the drawer layout.
  1181. *
  1182. * @param listener Object interested in changes of the drawer layout.
  1183. */
  1184. public void addDrawerListener(DrawerLayout.DrawerListener listener) {
  1185. if (mDrawerLayout != null) {
  1186. mDrawerLayout.addDrawerListener(listener);
  1187. } else {
  1188. Log_OC.e(TAG, "Drawer layout not ready to add drawer listener");
  1189. }
  1190. }
  1191. public boolean isDrawerIndicatorAvailable() {
  1192. return true;
  1193. }
  1194. @Override
  1195. protected void onStart() {
  1196. super.onStart();
  1197. EventBus.getDefault().register(this);
  1198. }
  1199. @Override
  1200. protected void onStop() {
  1201. EventBus.getDefault().unregister(this);
  1202. super.onStop();
  1203. }
  1204. @Subscribe(threadMode = ThreadMode.MAIN)
  1205. public void onAccountRemovedEvent(AccountRemovedEvent event) {
  1206. updateAccountList();
  1207. }
  1208. /**
  1209. * Retrieves external links via api from 'external' app
  1210. */
  1211. public void fetchExternalLinks(final boolean force) {
  1212. if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  1213. Thread t = new Thread(() -> {
  1214. // fetch capabilities as early as possible
  1215. if ((getCapabilities() == null || getCapabilities().getAccountName().isEmpty())
  1216. && getStorageManager() != null) {
  1217. GetCapabilitiesOperarion getCapabilities = new GetCapabilitiesOperarion();
  1218. getCapabilities.execute(getStorageManager(), getBaseContext());
  1219. }
  1220. Account account = AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this);
  1221. if (account != null && getStorageManager() != null &&
  1222. getStorageManager().getCapability(account.name) != null &&
  1223. getStorageManager().getCapability(account.name).getExternalLinks().isTrue()) {
  1224. int count = arbitraryDataProvider.getIntegerValue(FilesSyncHelper.GLOBAL,
  1225. FileActivity.APP_OPENED_COUNT);
  1226. if (count > 10 || count == -1 || force) {
  1227. if (force) {
  1228. Log_OC.d("ExternalLinks", "force update");
  1229. }
  1230. arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL,
  1231. FileActivity.APP_OPENED_COUNT, "0");
  1232. Log_OC.d("ExternalLinks", "update via api");
  1233. RemoteOperation getExternalLinksOperation = new ExternalLinksOperation();
  1234. RemoteOperationResult result = getExternalLinksOperation.execute(account, DrawerActivity.this);
  1235. if (result.isSuccess() && result.getData() != null) {
  1236. externalLinksProvider.deleteAllExternalLinks();
  1237. ArrayList<ExternalLink> externalLinks = (ArrayList<ExternalLink>) (Object) result.getData();
  1238. for (ExternalLink link : externalLinks) {
  1239. externalLinksProvider.storeExternalLink(link);
  1240. }
  1241. }
  1242. } else {
  1243. arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL,
  1244. FileActivity.APP_OPENED_COUNT, String.valueOf(count + 1));
  1245. }
  1246. } else {
  1247. externalLinksProvider.deleteAllExternalLinks();
  1248. Log_OC.d("ExternalLinks", "links disabled");
  1249. }
  1250. runOnUiThread(() -> updateExternalLinksInDrawer());
  1251. });
  1252. t.start();
  1253. }
  1254. }
  1255. }