DrawerActivity.java 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * Copyright (C) 2016 Andy Scherzinger
  6. * Copyright (C) 2016 Nextcloud
  7. * Copyright (C) 2016 ownCloud Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android.ui.activity;
  23. import android.accounts.Account;
  24. import android.accounts.AccountManager;
  25. import android.accounts.AccountManagerFuture;
  26. import android.content.Intent;
  27. import android.content.res.Configuration;
  28. import android.graphics.drawable.Drawable;
  29. import android.os.Build;
  30. import android.os.Bundle;
  31. import android.os.Handler;
  32. import android.support.design.widget.NavigationView;
  33. import android.support.v4.view.GravityCompat;
  34. import android.support.v4.widget.DrawerLayout;
  35. import android.support.v7.app.ActionBarDrawerToggle;
  36. import android.view.Menu;
  37. import android.view.MenuItem;
  38. import android.view.View;
  39. import android.widget.ImageView;
  40. import android.widget.LinearLayout;
  41. import android.widget.ProgressBar;
  42. import android.widget.TextView;
  43. import com.owncloud.android.MainApp;
  44. import com.owncloud.android.R;
  45. import com.owncloud.android.authentication.AccountUtils;
  46. import com.owncloud.android.datamodel.OCFile;
  47. import com.owncloud.android.lib.common.OwnCloudAccount;
  48. import com.owncloud.android.lib.common.Quota;
  49. import com.owncloud.android.lib.common.UserInfo;
  50. import com.owncloud.android.lib.common.operations.RemoteOperation;
  51. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  52. import com.owncloud.android.lib.common.utils.Log_OC;
  53. import com.owncloud.android.lib.resources.files.SearchOperation;
  54. import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
  55. import com.owncloud.android.ui.TextDrawable;
  56. import com.owncloud.android.ui.events.ChangeMenuEvent;
  57. import com.owncloud.android.ui.events.DummyDrawerEvent;
  58. import com.owncloud.android.ui.events.MenuItemClickEvent;
  59. import com.owncloud.android.ui.events.SearchEvent;
  60. import com.owncloud.android.ui.fragment.OCFileListFragment;
  61. import com.owncloud.android.utils.DisplayUtils;
  62. import org.greenrobot.eventbus.EventBus;
  63. import org.greenrobot.eventbus.Subscribe;
  64. import org.greenrobot.eventbus.ThreadMode;
  65. import org.parceler.Parcels;
  66. /**
  67. * Base class to handle setup of the drawer implementation including user switching and avatar fetching and fallback
  68. * generation.
  69. */
  70. public abstract class DrawerActivity extends ToolbarActivity implements DisplayUtils.AvatarGenerationListener {
  71. private static final String TAG = DrawerActivity.class.getSimpleName();
  72. private static final String KEY_IS_ACCOUNT_CHOOSER_ACTIVE = "IS_ACCOUNT_CHOOSER_ACTIVE";
  73. private static final String KEY_CHECKED_MENU_ITEM = "CHECKED_MENU_ITEM";
  74. private static final int ACTION_MANAGE_ACCOUNTS = 101;
  75. private static final int MENU_ORDER_ACCOUNT = 1;
  76. private static final int MENU_ORDER_ACCOUNT_FUNCTION = 2;
  77. /**
  78. * menu account avatar radius.
  79. */
  80. private float mMenuAccountAvatarRadiusDimension;
  81. /**
  82. * current account avatar radius.
  83. */
  84. private float mCurrentAccountAvatarRadiusDimension;
  85. /**
  86. * other accounts avatar radius.
  87. */
  88. private float mOtherAccountAvatarRadiusDimension;
  89. /**
  90. * Reference to the drawer layout.
  91. */
  92. private DrawerLayout mDrawerLayout;
  93. /**
  94. * Reference to the drawer toggle.
  95. */
  96. protected ActionBarDrawerToggle mDrawerToggle;
  97. /**
  98. * Reference to the navigation view.
  99. */
  100. private NavigationView mNavigationView;
  101. /**
  102. * Reference to the account chooser toggle.
  103. */
  104. private ImageView mAccountChooserToggle;
  105. /**
  106. * Reference to the middle account avatar.
  107. */
  108. private ImageView mAccountMiddleAccountAvatar;
  109. /**
  110. * Reference to the end account avatar.
  111. */
  112. private ImageView mAccountEndAccountAvatar;
  113. /**
  114. * Flag to signal if the account chooser is active.
  115. */
  116. private boolean mIsAccountChooserActive;
  117. /**
  118. * Id of the checked menu item.
  119. */
  120. private int mCheckedMenuItem = Menu.NONE;
  121. /**
  122. * accounts for the (max) three displayed accounts in the drawer header.
  123. */
  124. private Account[] mAvatars = new Account[3];
  125. /**
  126. * container layout of the quota view.
  127. */
  128. private LinearLayout mQuotaView;
  129. /**
  130. * progress bar of the quota view.
  131. */
  132. private ProgressBar mQuotaProgressBar;
  133. /**
  134. * text view of the quota view.
  135. */
  136. private TextView mQuotaTextView;
  137. /**
  138. * runnable that will be executed after the drawer has been closed.
  139. */
  140. private Runnable pendingRunnable;
  141. /**
  142. * Initializes the drawer, its content and highlights the menu item with the given id.
  143. * This method needs to be called after the content view has been set.
  144. *
  145. * @param menuItemId the menu item to be checked/highlighted
  146. */
  147. protected void setupDrawer(int menuItemId) {
  148. setupDrawer();
  149. setDrawerMenuItemChecked(menuItemId);
  150. }
  151. /**
  152. * Initializes the drawer and its content.
  153. * This method needs to be called after the content view has been set.
  154. */
  155. protected void setupDrawer() {
  156. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  157. mNavigationView = (NavigationView) findViewById(R.id.nav_view);
  158. if (mNavigationView != null) {
  159. setupDrawerHeader();
  160. setupDrawerMenu(mNavigationView);
  161. setupQuotaElement();
  162. // show folder sync menu item only for Android 6+
  163. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M &&
  164. mNavigationView.getMenu().findItem(R.id.nav_folder_sync) != null) {
  165. mNavigationView.getMenu().removeItem(R.id.nav_folder_sync);
  166. }
  167. }
  168. setupDrawerToggle();
  169. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  170. }
  171. /**
  172. * initializes and sets up the drawer toggle.
  173. */
  174. private void setupDrawerToggle() {
  175. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
  176. /** Called when a drawer has settled in a completely closed state. */
  177. public void onDrawerClosed(View view) {
  178. super.onDrawerClosed(view);
  179. // standard behavior of drawer is to switch to the standard menu on closing
  180. if (mIsAccountChooserActive) {
  181. toggleAccountList();
  182. }
  183. invalidateOptionsMenu();
  184. mDrawerToggle.setDrawerIndicatorEnabled(isDrawerIndicatorAvailable());
  185. if (pendingRunnable != null) {
  186. new Handler().post(pendingRunnable);
  187. pendingRunnable = null;
  188. }
  189. }
  190. /** Called when a drawer has settled in a completely open state. */
  191. public void onDrawerOpened(View drawerView) {
  192. super.onDrawerOpened(drawerView);
  193. mDrawerToggle.setDrawerIndicatorEnabled(true);
  194. invalidateOptionsMenu();
  195. }
  196. };
  197. // Set the drawer toggle as the DrawerListener
  198. mDrawerLayout.addDrawerListener(mDrawerToggle);
  199. mDrawerToggle.setDrawerIndicatorEnabled(true);
  200. }
  201. /**
  202. * initializes and sets up the drawer header.
  203. */
  204. private void setupDrawerHeader() {
  205. mAccountChooserToggle = (ImageView) findNavigationViewChildById(R.id.drawer_account_chooser_toogle);
  206. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  207. mIsAccountChooserActive = false;
  208. mAccountMiddleAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_middle);
  209. mAccountEndAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_end);
  210. findNavigationViewChildById(R.id.drawer_active_user)
  211. .setOnClickListener(new View.OnClickListener() {
  212. @Override
  213. public void onClick(View v) {
  214. toggleAccountList();
  215. }
  216. });
  217. }
  218. /**
  219. * setup quota elements of the drawer.
  220. */
  221. private void setupQuotaElement() {
  222. mQuotaView = (LinearLayout) findViewById(R.id.drawer_quota);
  223. mQuotaProgressBar = (ProgressBar) findViewById(R.id.drawer_quota_ProgressBar);
  224. mQuotaTextView = (TextView) findViewById(R.id.drawer_quota_text);
  225. DisplayUtils.colorPreLollipopHorizontalProgressBar(mQuotaProgressBar);
  226. }
  227. /**
  228. * setup drawer content, basically setting the item selected listener.
  229. *
  230. * @param navigationView the drawers navigation view
  231. */
  232. protected void setupDrawerMenu(NavigationView navigationView) {
  233. // on pre lollipop the light theme adds a black tint to icons with white coloring
  234. // ruining the generic avatars, so tinting for icons is deactivated pre lollipop
  235. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  236. navigationView.setItemIconTintList(null);
  237. }
  238. // setup actions for drawer menu items
  239. navigationView.setNavigationItemSelectedListener(
  240. new NavigationView.OnNavigationItemSelectedListener() {
  241. @Override
  242. public boolean onNavigationItemSelected(final MenuItem menuItem) {
  243. mDrawerLayout.closeDrawers();
  244. // pending runnable will be executed after the drawer has been closed
  245. pendingRunnable = new Runnable() {
  246. @Override
  247. public void run() {
  248. selectNavigationItem(menuItem);
  249. }
  250. };
  251. return true;
  252. }
  253. });
  254. // handle correct state
  255. if (mIsAccountChooserActive) {
  256. navigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  257. } else {
  258. navigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  259. }
  260. Account account = AccountUtils.
  261. getCurrentOwnCloudAccount(MainApp.getAppContext());
  262. boolean searchSupported = AccountUtils.hasSearchSupport(account);
  263. if ((getResources().getBoolean(R.bool.bottom_toolbar_enabled) || (!searchSupported)) &&
  264. (account != null)){
  265. navigationView.getMenu().removeItem(R.id.nav_photos);
  266. if (getResources().getBoolean(R.bool.bottom_toolbar_enabled)) {
  267. navigationView.getMenu().removeItem(R.id.nav_all_files);
  268. navigationView.getMenu().removeItem(R.id.nav_settings);
  269. navigationView.getMenu().removeItem(R.id.nav_favorites);
  270. }
  271. if (!searchSupported) {
  272. navigationView.getMenu().removeItem(R.id.nav_videos);
  273. }
  274. }
  275. if (getResources().getBoolean(R.bool.use_home) && navigationView.getMenu().findItem(R.id.nav_all_files) !=
  276. null) {
  277. navigationView.getMenu().findItem(R.id.nav_all_files).setTitle(getResources().
  278. getString(R.string.drawer_item_home));
  279. navigationView.getMenu().findItem(R.id.nav_all_files).setIcon(R.drawable.ic_home);
  280. }
  281. if (!getResources().getBoolean(R.bool.participate_enabled)) {
  282. navigationView.getMenu().removeItem(R.id.nav_participate);
  283. }
  284. if (!getResources().getBoolean(R.bool.shared_enabled)) {
  285. navigationView.getMenu().removeItem(R.id.nav_shared);
  286. }
  287. if (!getResources().getBoolean(R.bool.contacts_backup)) {
  288. navigationView.getMenu().removeItem(R.id.nav_contacts);
  289. }
  290. if (AccountUtils.hasSearchSupport(account)) {
  291. if (!getResources().getBoolean(R.bool.recently_added_enabled)) {
  292. navigationView.getMenu().removeItem(R.id.nav_recently_added);
  293. }
  294. if (!getResources().getBoolean(R.bool.recently_modified_enabled)) {
  295. navigationView.getMenu().removeItem(R.id.nav_recently_modified);
  296. }
  297. if (!getResources().getBoolean(R.bool.videos_enabled)) {
  298. navigationView.getMenu().removeItem(R.id.nav_videos);
  299. }
  300. } else if (account != null) {
  301. navigationView.getMenu().removeItem(R.id.nav_recently_added);
  302. navigationView.getMenu().removeItem(R.id.nav_recently_modified);
  303. navigationView.getMenu().removeItem(R.id.nav_videos);
  304. }
  305. }
  306. @Subscribe(threadMode = ThreadMode.MAIN)
  307. public void onMessageEvent(MenuItemClickEvent event) {
  308. unsetAllDrawerMenuItems();
  309. switch (event.menuItem.getItemId()) {
  310. case R.id.nav_bar_files:
  311. showFiles(false);
  312. break;
  313. case R.id.nav_bar_settings:
  314. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  315. startActivity(settingsIntent);
  316. break;
  317. default:
  318. break;
  319. }
  320. }
  321. @Subscribe(threadMode = ThreadMode.MAIN)
  322. public void onMessageEvent(DummyDrawerEvent event) {
  323. unsetAllDrawerMenuItems();
  324. }
  325. private void selectNavigationItem(final MenuItem menuItem) {
  326. switch (menuItem.getItemId()) {
  327. case R.id.nav_all_files:
  328. menuItem.setChecked(true);
  329. mCheckedMenuItem = menuItem.getItemId();
  330. showFiles(false);
  331. EventBus.getDefault().post(new ChangeMenuEvent());
  332. break;
  333. case R.id.nav_favorites:
  334. menuItem.setChecked(true);
  335. mCheckedMenuItem = menuItem.getItemId();
  336. switchToSearchFragment(new SearchEvent("", SearchOperation.SearchType.FAVORITE_SEARCH,
  337. SearchEvent.UnsetType.NO_UNSET), menuItem);
  338. break;
  339. case R.id.nav_photos:
  340. menuItem.setChecked(true);
  341. mCheckedMenuItem = menuItem.getItemId();
  342. switchToSearchFragment(new SearchEvent("image/%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  343. SearchEvent.UnsetType.NO_UNSET), menuItem);
  344. break;
  345. case R.id.nav_on_device:
  346. menuItem.setChecked(true);
  347. mCheckedMenuItem = menuItem.getItemId();
  348. showFiles(true);
  349. break;
  350. case R.id.nav_uploads:
  351. Intent uploadListIntent = new Intent(getApplicationContext(),
  352. UploadListActivity.class);
  353. uploadListIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  354. startActivity(uploadListIntent);
  355. break;
  356. case R.id.nav_activity:
  357. Intent activityIntent = new Intent(getApplicationContext(), ActivitiesListActivity.class);
  358. activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  359. startActivity(activityIntent);
  360. break;
  361. case R.id.nav_folder_sync:
  362. Intent folderSyncIntent = new Intent(getApplicationContext(), FolderSyncActivity.class);
  363. startActivity(folderSyncIntent);
  364. break;
  365. case R.id.nav_contacts:
  366. Intent contactsIntent = new Intent(getApplicationContext(), ContactsPreferenceActivity.class);
  367. startActivity(contactsIntent);
  368. break;
  369. case R.id.nav_settings:
  370. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  371. startActivity(settingsIntent);
  372. break;
  373. case R.id.nav_participate:
  374. Intent participateIntent = new Intent(getApplicationContext(),
  375. ParticipateActivity.class);
  376. startActivity(participateIntent);
  377. break;
  378. case R.id.drawer_menu_account_add:
  379. createAccount(false);
  380. break;
  381. case R.id.drawer_menu_account_manage:
  382. Intent manageAccountsIntent = new Intent(getApplicationContext(),
  383. ManageAccountsActivity.class);
  384. startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
  385. break;
  386. case R.id.nav_recently_added:
  387. menuItem.setChecked(true);
  388. mCheckedMenuItem = menuItem.getItemId();
  389. switchToSearchFragment(new SearchEvent("%",SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  390. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  391. break;
  392. case R.id.nav_recently_modified:
  393. menuItem.setChecked(true);
  394. mCheckedMenuItem = menuItem.getItemId();
  395. switchToSearchFragment(new SearchEvent("", SearchOperation.SearchType.RECENTLY_MODIFIED_SEARCH,
  396. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  397. break;
  398. case R.id.nav_shared:
  399. menuItem.setChecked(true);
  400. mCheckedMenuItem = menuItem.getItemId();
  401. switchToSearchFragment(new SearchEvent("", SearchOperation.SearchType.SHARED_SEARCH,
  402. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  403. break;
  404. case R.id.nav_videos:
  405. menuItem.setChecked(true);
  406. mCheckedMenuItem = menuItem.getItemId();
  407. switchToSearchFragment(new SearchEvent("video/%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  408. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  409. break;
  410. case Menu.NONE:
  411. // account clicked
  412. accountClicked(menuItem.getTitle().toString());
  413. default:
  414. Log_OC.i(TAG, "Unknown drawer menu item clicked: " + menuItem.getTitle());
  415. }
  416. }
  417. private void switchToSearchFragment(SearchEvent event, MenuItem menuItem) {
  418. Intent recentlyAddedIntent = new Intent(getBaseContext(), FileDisplayActivity.class);
  419. recentlyAddedIntent.putExtra(OCFileListFragment.SEARCH_EVENT, Parcels.wrap(event));
  420. recentlyAddedIntent.putExtra(FileDisplayActivity.DRAWER_MENU_ID, menuItem.getItemId());
  421. recentlyAddedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  422. startActivity(recentlyAddedIntent);
  423. }
  424. /**
  425. * show the file list to the user.
  426. *
  427. * @param onDeviceOnly flag to decide if all files or only the ones on the device should be shown
  428. */
  429. public abstract void showFiles(boolean onDeviceOnly);
  430. /**
  431. * sets the new/current account and restarts. In case the given account equals the actual/current account the
  432. * call will be ignored.
  433. *
  434. * @param accountName The account name to be set
  435. */
  436. private void accountClicked(String accountName) {
  437. if (!AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()).name.equals(accountName)) {
  438. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), accountName);
  439. restart();
  440. }
  441. }
  442. /**
  443. * click method for mini avatars in drawer header.
  444. *
  445. * @param view the clicked ImageView
  446. */
  447. public void onAccountDrawerClick(View view) {
  448. accountClicked(view.getContentDescription().toString());
  449. }
  450. /**
  451. * checks if the drawer exists and is opened.
  452. *
  453. * @return <code>true</code> if the drawer is open, else <code>false</code>
  454. */
  455. public boolean isDrawerOpen() {
  456. return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.START);
  457. }
  458. /**
  459. * closes the drawer.
  460. */
  461. public void closeDrawer() {
  462. if (mDrawerLayout != null) {
  463. mDrawerLayout.closeDrawer(GravityCompat.START);
  464. }
  465. }
  466. /**
  467. * opens the drawer.
  468. */
  469. public void openDrawer() {
  470. if (mDrawerLayout != null) {
  471. mDrawerLayout.openDrawer(GravityCompat.START);
  472. }
  473. }
  474. /**
  475. * Enable or disable interaction with all drawers.
  476. *
  477. * @param lockMode The new lock mode for the given drawer. One of {@link DrawerLayout#LOCK_MODE_UNLOCKED},
  478. * {@link DrawerLayout#LOCK_MODE_LOCKED_CLOSED} or {@link DrawerLayout#LOCK_MODE_LOCKED_OPEN}.
  479. */
  480. public void setDrawerLockMode(int lockMode) {
  481. if (mDrawerLayout != null) {
  482. mDrawerLayout.setDrawerLockMode(lockMode);
  483. }
  484. }
  485. /**
  486. * Enable or disable the drawer indicator.
  487. *
  488. * @param enable <code>true</code> to enable, <code>false</code> to disable
  489. */
  490. public void setDrawerIndicatorEnabled(boolean enable) {
  491. if (mDrawerToggle != null) {
  492. mDrawerToggle.setDrawerIndicatorEnabled(enable);
  493. }
  494. }
  495. /**
  496. * updates the account list in the drawer.
  497. */
  498. public void updateAccountList() {
  499. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  500. if (mNavigationView != null && mDrawerLayout != null) {
  501. if (accounts.length > 0) {
  502. repopulateAccountList(accounts);
  503. setAccountInDrawer(AccountUtils.getCurrentOwnCloudAccount(this));
  504. populateDrawerOwnCloudAccounts();
  505. // activate second/end account avatar
  506. if (mAvatars[1] != null) {
  507. DisplayUtils.setAvatar(mAvatars[1], this,
  508. mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  509. findNavigationViewChildById(R.id.drawer_account_end));
  510. mAccountEndAccountAvatar.setVisibility(View.VISIBLE);
  511. } else {
  512. mAccountEndAccountAvatar.setVisibility(View.GONE);
  513. }
  514. // activate third/middle account avatar
  515. if (mAvatars[2] != null) {
  516. DisplayUtils.setAvatar(mAvatars[2], this,
  517. mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  518. findNavigationViewChildById(R.id.drawer_account_middle));
  519. mAccountMiddleAccountAvatar.setVisibility(View.VISIBLE);
  520. } else {
  521. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  522. }
  523. } else {
  524. mAccountEndAccountAvatar.setVisibility(View.GONE);
  525. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  526. }
  527. }
  528. }
  529. /**
  530. * re-populates the account list.
  531. *
  532. * @param accounts list of accounts
  533. */
  534. private void repopulateAccountList(Account[] accounts) {
  535. // remove all accounts from list
  536. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_accounts);
  537. // add all accounts to list
  538. for (int i = 0; i < accounts.length; i++) {
  539. try {
  540. // show all accounts except the currently active one
  541. if (!getAccount().name.equals(accounts[i].name)) {
  542. MenuItem accountMenuItem = mNavigationView.getMenu().add(
  543. R.id.drawer_menu_accounts,
  544. Menu.NONE,
  545. MENU_ORDER_ACCOUNT,
  546. accounts[i].name)
  547. .setIcon(TextDrawable.createAvatar(
  548. accounts[i].name,
  549. mMenuAccountAvatarRadiusDimension)
  550. );
  551. DisplayUtils.setAvatar(accounts[i], this, mMenuAccountAvatarRadiusDimension, getResources(), getStorageManager(), accountMenuItem);
  552. }
  553. } catch (Exception e) {
  554. Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
  555. mNavigationView.getMenu().add(
  556. R.id.drawer_menu_accounts,
  557. Menu.NONE,
  558. MENU_ORDER_ACCOUNT,
  559. accounts[i].name)
  560. .setIcon(R.drawable.ic_user);
  561. }
  562. }
  563. // re-add add-account and manage-accounts
  564. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_add,
  565. MENU_ORDER_ACCOUNT_FUNCTION,
  566. getResources().getString(R.string.prefs_add_account)).setIcon(R.drawable.ic_account_plus);
  567. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_manage,
  568. MENU_ORDER_ACCOUNT_FUNCTION,
  569. getResources().getString(R.string.drawer_manage_accounts)).setIcon(R.drawable.ic_settings);
  570. // adding sets menu group back to visible, so safety check and setting invisible
  571. showMenu();
  572. }
  573. /**
  574. * Updates title bar and home buttons (state and icon).
  575. * <p/>
  576. * Assumes that navigation drawer is NOT visible.
  577. */
  578. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  579. super.updateActionBarTitleAndHomeButton(chosenFile);
  580. /// set home button properties
  581. if (mDrawerToggle != null && chosenFile != null) {
  582. mDrawerToggle.setDrawerIndicatorEnabled(isRoot(chosenFile));
  583. } else if (mDrawerToggle != null){
  584. mDrawerToggle.setDrawerIndicatorEnabled(false);
  585. }
  586. }
  587. /**
  588. * sets the given account name in the drawer in case the drawer is available. The account name is shortened
  589. * beginning from the @-sign in the username.
  590. *
  591. * @param account the account to be set in the drawer
  592. */
  593. protected void setAccountInDrawer(Account account) {
  594. if (mDrawerLayout != null && account != null) {
  595. TextView username = (TextView) findNavigationViewChildById(R.id.drawer_username);
  596. TextView usernameFull = (TextView) findNavigationViewChildById(R.id.drawer_username_full);
  597. usernameFull.setText(account.name);
  598. try {
  599. OwnCloudAccount oca = new OwnCloudAccount(account, this);
  600. username.setText(oca.getDisplayName());
  601. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  602. Log_OC.w(TAG, "Couldn't read display name of account fallback to account name");
  603. username.setText(AccountUtils.getAccountUsername(account.name));
  604. }
  605. DisplayUtils.setAvatar(account, this,
  606. mCurrentAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  607. findNavigationViewChildById(R.id.drawer_current_account));
  608. // check and show quota info if available
  609. getAndDisplayUserQuota();
  610. }
  611. }
  612. /**
  613. * Toggle between standard menu and account list including saving the state.
  614. */
  615. private void toggleAccountList() {
  616. mIsAccountChooserActive = !mIsAccountChooserActive;
  617. showMenu();
  618. }
  619. /**
  620. * depending on the #mIsAccountChooserActive flag shows the account chooser or the standard menu.
  621. */
  622. private void showMenu() {
  623. if (mNavigationView != null) {
  624. if (mIsAccountChooserActive) {
  625. mAccountChooserToggle.setImageResource(R.drawable.ic_up);
  626. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  627. if (!getResources().getBoolean(R.bool.multiaccount_support) &&
  628. mNavigationView.getMenu().findItem(R.id.drawer_menu_account_add) != null) {
  629. mNavigationView.getMenu().removeItem(R.id.drawer_menu_account_add);
  630. }
  631. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, false);
  632. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_bottom, false);
  633. } else {
  634. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  635. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  636. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, true);
  637. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_bottom, true);
  638. }
  639. }
  640. }
  641. /**
  642. * shows or hides the quota UI elements.
  643. *
  644. * @param showQuota show/hide quota information
  645. */
  646. private void showQuota(boolean showQuota) {
  647. if (showQuota) {
  648. mQuotaView.setVisibility(View.VISIBLE);
  649. } else {
  650. mQuotaView.setVisibility(View.GONE);
  651. }
  652. }
  653. /**
  654. * configured the quota to be displayed.
  655. *
  656. * @param usedSpace the used space
  657. * @param totalSpace the total space
  658. * @param relative the percentage of space already used
  659. */
  660. private void setQuotaInformation(long usedSpace, long totalSpace, int relative) {
  661. mQuotaProgressBar.setProgress(relative);
  662. DisplayUtils.colorHorizontalProgressBar(mQuotaProgressBar, DisplayUtils.getRelativeInfoColor(this, relative));
  663. mQuotaTextView.setText(String.format(
  664. getString(R.string.drawer_quota),
  665. DisplayUtils.bytesToHumanReadable(usedSpace),
  666. DisplayUtils.bytesToHumanReadable(totalSpace)));
  667. showQuota(true);
  668. }
  669. protected void unsetAllDrawerMenuItems() {
  670. if (mNavigationView != null && mNavigationView.getMenu() != null) {
  671. Menu menu = mNavigationView.getMenu();
  672. for (int i = 0; i < menu.size(); i++) {
  673. menu.getItem(i).setChecked(false);
  674. }
  675. }
  676. mCheckedMenuItem = Menu.NONE;
  677. }
  678. /**
  679. * checks/highlights the provided menu item if the drawer has been initialized and the menu item exists.
  680. *
  681. * @param menuItemId the menu item to be highlighted
  682. */
  683. protected void setDrawerMenuItemChecked(int menuItemId) {
  684. if (mNavigationView != null && mNavigationView.getMenu() != null && mNavigationView.getMenu().findItem
  685. (menuItemId) != null) {
  686. mNavigationView.getMenu().findItem(menuItemId).setChecked(true);
  687. mCheckedMenuItem = menuItemId;
  688. } else {
  689. Log_OC.w(TAG, "setDrawerMenuItemChecked has been called with invalid menu-item-ID");
  690. }
  691. }
  692. /**
  693. * Retrieves and shows the user quota if available
  694. */
  695. private void getAndDisplayUserQuota() {
  696. // set user space information
  697. Thread t = new Thread(new Runnable() {
  698. public void run() {
  699. RemoteOperation getQuotaInfoOperation = new GetRemoteUserInfoOperation();
  700. RemoteOperationResult result = getQuotaInfoOperation.execute(
  701. AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this), DrawerActivity.this);
  702. if (result.isSuccess() && result.getData() != null) {
  703. final UserInfo userInfo = (UserInfo) result.getData().get(0);
  704. final Quota quota = userInfo.getQuota();
  705. if (quota != null) {
  706. final long used = quota.getUsed();
  707. final long total = quota.getTotal();
  708. final int relative = (int) Math.ceil(quota.getRelative());
  709. final long quotaValue = quota.getQuota();
  710. runOnUiThread(new Runnable() {
  711. @Override
  712. public void run() {
  713. if (quotaValue > 0
  714. || quotaValue == GetRemoteUserInfoOperation.QUOTA_LIMIT_INFO_NOT_AVAILABLE) {
  715. /**
  716. * show quota in case
  717. * it is available and calculated (> 0) or
  718. * in case of legacy servers (==QUOTA_LIMIT_INFO_NOT_AVAILABLE)
  719. */
  720. setQuotaInformation(used, total, relative);
  721. } else {
  722. /**
  723. * quotaValue < 0 means special cases like
  724. * {@link RemoteGetUserQuotaOperation.SPACE_NOT_COMPUTED},
  725. * {@link RemoteGetUserQuotaOperation.SPACE_UNKNOWN} or
  726. * {@link RemoteGetUserQuotaOperation.SPACE_UNLIMITED}
  727. * thus don't display any quota information.
  728. */
  729. showQuota(false);
  730. }
  731. }
  732. });
  733. }
  734. }
  735. }
  736. });
  737. t.start();
  738. }
  739. @Override
  740. protected void onCreate(Bundle savedInstanceState) {
  741. super.onCreate(savedInstanceState);
  742. if (savedInstanceState != null) {
  743. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  744. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  745. }
  746. mCurrentAccountAvatarRadiusDimension = getResources()
  747. .getDimension(R.dimen.nav_drawer_header_avatar_radius);
  748. mOtherAccountAvatarRadiusDimension = getResources()
  749. .getDimension(R.dimen.nav_drawer_header_avatar_other_accounts_radius);
  750. mMenuAccountAvatarRadiusDimension = getResources()
  751. .getDimension(R.dimen.nav_drawer_menu_avatar_radius);
  752. }
  753. @Override
  754. protected void onSaveInstanceState(Bundle outState) {
  755. super.onSaveInstanceState(outState);
  756. outState.putBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, mIsAccountChooserActive);
  757. outState.putInt(KEY_CHECKED_MENU_ITEM, mCheckedMenuItem);
  758. }
  759. @Override
  760. public void onRestoreInstanceState(Bundle savedInstanceState) {
  761. super.onRestoreInstanceState(savedInstanceState);
  762. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  763. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  764. // (re-)setup drawer state
  765. showMenu();
  766. // check/highlight the menu item if present
  767. if (mCheckedMenuItem > Menu.NONE || mCheckedMenuItem < Menu.NONE) {
  768. setDrawerMenuItemChecked(mCheckedMenuItem);
  769. }
  770. }
  771. @Override
  772. protected void onPostCreate(Bundle savedInstanceState) {
  773. super.onPostCreate(savedInstanceState);
  774. // Sync the toggle state after onRestoreInstanceState has occurred.
  775. if (mDrawerToggle != null) {
  776. mDrawerToggle.syncState();
  777. if (isDrawerOpen()) {
  778. mDrawerToggle.setDrawerIndicatorEnabled(true);
  779. }
  780. }
  781. updateAccountList();
  782. }
  783. @Override
  784. public void onConfigurationChanged(Configuration newConfig) {
  785. super.onConfigurationChanged(newConfig);
  786. if (mDrawerToggle != null) {
  787. mDrawerToggle.onConfigurationChanged(newConfig);
  788. }
  789. }
  790. @Override
  791. public void onBackPressed() {
  792. if (isDrawerOpen()) {
  793. closeDrawer();
  794. return;
  795. }
  796. super.onBackPressed();
  797. }
  798. @Override
  799. protected void onResume() {
  800. super.onResume();
  801. setDrawerMenuItemChecked(mCheckedMenuItem);
  802. }
  803. @Override
  804. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  805. super.onActivityResult(requestCode, resultCode, data);
  806. // update Account list and active account if Manage Account activity replies with
  807. // - ACCOUNT_LIST_CHANGED = true
  808. // - RESULT_OK
  809. if (requestCode == ACTION_MANAGE_ACCOUNTS
  810. && resultCode == RESULT_OK
  811. && data.getBooleanExtra(ManageAccountsActivity.KEY_ACCOUNT_LIST_CHANGED, false)) {
  812. // current account has changed
  813. if (data.getBooleanExtra(ManageAccountsActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
  814. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  815. restart();
  816. } else {
  817. updateAccountList();
  818. }
  819. }
  820. }
  821. /**
  822. * Finds a view that was identified by the id attribute from the drawer header.
  823. *
  824. * @param id the view's id
  825. * @return The view if found or <code>null</code> otherwise.
  826. */
  827. private View findNavigationViewChildById(int id) {
  828. return ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0).findViewById(id);
  829. }
  830. /**
  831. * restart helper method which is called after a changing the current account.
  832. */
  833. protected abstract void restart();
  834. @Override
  835. protected void onAccountCreationSuccessful(AccountManagerFuture<Bundle> future) {
  836. super.onAccountCreationSuccessful(future);
  837. updateAccountList();
  838. restart();
  839. }
  840. /**
  841. * populates the avatar drawer array with the first three ownCloud {@link Account}s while the first element is
  842. * always the current account.
  843. */
  844. private void populateDrawerOwnCloudAccounts() {
  845. mAvatars = new Account[3];
  846. Account[] accountsAll = AccountManager.get(this).getAccountsByType
  847. (MainApp.getAccountType());
  848. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  849. mAvatars[0] = currentAccount;
  850. int j = 0;
  851. for (int i = 1; i <= 2 && i < accountsAll.length && j < accountsAll.length; j++) {
  852. if (!currentAccount.equals(accountsAll[j])) {
  853. mAvatars[i] = accountsAll[j];
  854. i++;
  855. }
  856. }
  857. }
  858. @Override
  859. public void avatarGenerated(Drawable avatarDrawable, Object callContext) {
  860. if (callContext instanceof MenuItem) {
  861. MenuItem mi = (MenuItem) callContext;
  862. mi.setIcon(avatarDrawable);
  863. } else if (callContext instanceof ImageView) {
  864. ImageView iv = (ImageView) callContext;
  865. iv.setImageDrawable(avatarDrawable);
  866. }
  867. }
  868. @Override
  869. public boolean shouldCallGeneratedCallback(String tag, Object callContext) {
  870. if (callContext instanceof MenuItem) {
  871. MenuItem mi = (MenuItem) callContext;
  872. return String.valueOf(mi.getTitle()).equals(tag);
  873. } else if (callContext instanceof ImageView) {
  874. ImageView iv = (ImageView) callContext;
  875. return String.valueOf(iv.getTag()).equals(tag);
  876. }
  877. return false;
  878. }
  879. /**
  880. * Adds other listeners to react on changes of the drawer layout.
  881. *
  882. * @param listener Object interested in changes of the drawer layout.
  883. */
  884. public void addDrawerListener(DrawerLayout.DrawerListener listener) {
  885. if (mDrawerLayout != null) {
  886. mDrawerLayout.addDrawerListener(listener);
  887. } else {
  888. Log_OC.e(TAG, "Drawer layout not ready to add drawer listener");
  889. }
  890. }
  891. public boolean isDrawerIndicatorAvailable() {
  892. return true;
  893. }
  894. @Override
  895. protected void onStart() {
  896. super.onStart();
  897. EventBus.getDefault().register(this);
  898. }
  899. @Override
  900. protected void onStop() {
  901. EventBus.getDefault().unregister(this);
  902. super.onStop();
  903. }
  904. }