DrawerActivity.java 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 (AccountUtils.hasSearchSupport(account)) {
  288. if (!getResources().getBoolean(R.bool.recently_added_enabled)) {
  289. navigationView.getMenu().removeItem(R.id.nav_recently_added);
  290. }
  291. if (!getResources().getBoolean(R.bool.recently_modified_enabled)) {
  292. navigationView.getMenu().removeItem(R.id.nav_recently_modified);
  293. }
  294. if (!getResources().getBoolean(R.bool.videos_enabled)) {
  295. navigationView.getMenu().removeItem(R.id.nav_videos);
  296. }
  297. } else if (account != null) {
  298. navigationView.getMenu().removeItem(R.id.nav_recently_added);
  299. navigationView.getMenu().removeItem(R.id.nav_recently_modified);
  300. navigationView.getMenu().removeItem(R.id.nav_videos);
  301. }
  302. }
  303. @Subscribe(threadMode = ThreadMode.MAIN)
  304. public void onMessageEvent(MenuItemClickEvent event) {
  305. unsetAllDrawerMenuItems();
  306. switch (event.menuItem.getItemId()) {
  307. case R.id.nav_bar_files:
  308. showFiles(false);
  309. break;
  310. case R.id.nav_bar_settings:
  311. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  312. startActivity(settingsIntent);
  313. break;
  314. default:
  315. break;
  316. }
  317. }
  318. @Subscribe(threadMode = ThreadMode.MAIN)
  319. public void onMessageEvent(DummyDrawerEvent event) {
  320. unsetAllDrawerMenuItems();
  321. }
  322. private void selectNavigationItem(final MenuItem menuItem) {
  323. switch (menuItem.getItemId()) {
  324. case R.id.nav_all_files:
  325. menuItem.setChecked(true);
  326. mCheckedMenuItem = menuItem.getItemId();
  327. showFiles(false);
  328. EventBus.getDefault().post(new ChangeMenuEvent());
  329. break;
  330. case R.id.nav_favorites:
  331. menuItem.setChecked(true);
  332. mCheckedMenuItem = menuItem.getItemId();
  333. Intent favoriteIntent = new Intent(getBaseContext(), FileDisplayActivity.class);
  334. favoriteIntent.putExtra(OCFileListFragment.SEARCH_EVENT, Parcels.wrap(new SearchEvent("",
  335. SearchOperation.SearchType.FAVORITE_SEARCH, SearchEvent.UnsetType.NO_UNSET)));
  336. favoriteIntent.putExtra(FileDisplayActivity.DRAWER_MENU_ID, menuItem.getItemId());
  337. favoriteIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  338. startActivity(favoriteIntent);
  339. break;
  340. case R.id.nav_photos:
  341. menuItem.setChecked(true);
  342. mCheckedMenuItem = menuItem.getItemId();
  343. Intent photoIntent = new Intent(getBaseContext(), FileDisplayActivity.class);
  344. photoIntent.putExtra(OCFileListFragment.SEARCH_EVENT, Parcels.wrap(new SearchEvent("image/%",
  345. SearchOperation.SearchType.CONTENT_TYPE_SEARCH, SearchEvent.UnsetType.NO_UNSET)));
  346. photoIntent.putExtra(FileDisplayActivity.DRAWER_MENU_ID, menuItem.getItemId());
  347. photoIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  348. startActivity(photoIntent);
  349. break;
  350. case R.id.nav_on_device:
  351. menuItem.setChecked(true);
  352. mCheckedMenuItem = menuItem.getItemId();
  353. showFiles(true);
  354. break;
  355. case R.id.nav_uploads:
  356. Intent uploadListIntent = new Intent(getApplicationContext(),
  357. UploadListActivity.class);
  358. uploadListIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  359. startActivity(uploadListIntent);
  360. break;
  361. case R.id.nav_activity:
  362. Intent activityIntent = new Intent(getApplicationContext(), ActivitiesListActivity.class);
  363. activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  364. startActivity(activityIntent);
  365. break;
  366. case R.id.nav_folder_sync:
  367. Intent folderSyncIntent = new Intent(getApplicationContext(), FolderSyncActivity.class);
  368. startActivity(folderSyncIntent);
  369. break;
  370. case R.id.nav_settings:
  371. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  372. startActivity(settingsIntent);
  373. break;
  374. case R.id.nav_participate:
  375. Intent participateIntent = new Intent(getApplicationContext(),
  376. ParticipateActivity.class);
  377. startActivity(participateIntent);
  378. break;
  379. case R.id.drawer_menu_account_add:
  380. createAccount(false);
  381. break;
  382. case R.id.drawer_menu_account_manage:
  383. Intent manageAccountsIntent = new Intent(getApplicationContext(),
  384. ManageAccountsActivity.class);
  385. startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
  386. break;
  387. case R.id.nav_recently_added:
  388. menuItem.setChecked(true);
  389. mCheckedMenuItem = menuItem.getItemId();
  390. EventBus.getDefault().post(new SearchEvent("%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  391. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR));
  392. break;
  393. case R.id.nav_recently_modified:
  394. menuItem.setChecked(true);
  395. mCheckedMenuItem = menuItem.getItemId();
  396. EventBus.getDefault().post(new SearchEvent("", SearchOperation.SearchType.RECENTLY_MODIFIED_SEARCH,
  397. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR));
  398. break;
  399. case R.id.nav_shared:
  400. menuItem.setChecked(true);
  401. mCheckedMenuItem = menuItem.getItemId();
  402. EventBus.getDefault().post(new SearchEvent("", SearchOperation.SearchType.SHARED_SEARCH,
  403. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR));
  404. break;
  405. case R.id.nav_videos:
  406. menuItem.setChecked(true);
  407. mCheckedMenuItem = menuItem.getItemId();
  408. EventBus.getDefault().post(new SearchEvent("video/%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  409. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR));
  410. break;
  411. case Menu.NONE:
  412. // account clicked
  413. accountClicked(menuItem.getTitle().toString());
  414. default:
  415. Log_OC.i(TAG, "Unknown drawer menu item clicked: " + menuItem.getTitle());
  416. }
  417. }
  418. /**
  419. * show the file list to the user.
  420. *
  421. * @param onDeviceOnly flag to decide if all files or only the ones on the device should be shown
  422. */
  423. public abstract void showFiles(boolean onDeviceOnly);
  424. /**
  425. * sets the new/current account and restarts. In case the given account equals the actual/current account the
  426. * call will be ignored.
  427. *
  428. * @param accountName The account name to be set
  429. */
  430. private void accountClicked(String accountName) {
  431. if (!AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()).name.equals(accountName)) {
  432. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), accountName);
  433. restart();
  434. }
  435. }
  436. /**
  437. * click method for mini avatars in drawer header.
  438. *
  439. * @param view the clicked ImageView
  440. */
  441. public void onAccountDrawerClick(View view) {
  442. accountClicked(view.getContentDescription().toString());
  443. }
  444. /**
  445. * checks if the drawer exists and is opened.
  446. *
  447. * @return <code>true</code> if the drawer is open, else <code>false</code>
  448. */
  449. public boolean isDrawerOpen() {
  450. return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.START);
  451. }
  452. /**
  453. * closes the drawer.
  454. */
  455. public void closeDrawer() {
  456. if (mDrawerLayout != null) {
  457. mDrawerLayout.closeDrawer(GravityCompat.START);
  458. }
  459. }
  460. /**
  461. * opens the drawer.
  462. */
  463. public void openDrawer() {
  464. if (mDrawerLayout != null) {
  465. mDrawerLayout.openDrawer(GravityCompat.START);
  466. }
  467. }
  468. /**
  469. * Enable or disable interaction with all drawers.
  470. *
  471. * @param lockMode The new lock mode for the given drawer. One of {@link DrawerLayout#LOCK_MODE_UNLOCKED},
  472. * {@link DrawerLayout#LOCK_MODE_LOCKED_CLOSED} or {@link DrawerLayout#LOCK_MODE_LOCKED_OPEN}.
  473. */
  474. public void setDrawerLockMode(int lockMode) {
  475. if (mDrawerLayout != null) {
  476. mDrawerLayout.setDrawerLockMode(lockMode);
  477. }
  478. }
  479. /**
  480. * Enable or disable the drawer indicator.
  481. *
  482. * @param enable <code>true</code> to enable, <code>false</code> to disable
  483. */
  484. public void setDrawerIndicatorEnabled(boolean enable) {
  485. if (mDrawerToggle != null) {
  486. mDrawerToggle.setDrawerIndicatorEnabled(enable);
  487. }
  488. }
  489. /**
  490. * updates the account list in the drawer.
  491. */
  492. public void updateAccountList() {
  493. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  494. if (mNavigationView != null && mDrawerLayout != null) {
  495. if (accounts.length > 0) {
  496. repopulateAccountList(accounts);
  497. setAccountInDrawer(AccountUtils.getCurrentOwnCloudAccount(this));
  498. populateDrawerOwnCloudAccounts();
  499. // activate second/end account avatar
  500. if (mAvatars[1] != null) {
  501. DisplayUtils.setAvatar(mAvatars[1], this,
  502. mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  503. findNavigationViewChildById(R.id.drawer_account_end));
  504. mAccountEndAccountAvatar.setVisibility(View.VISIBLE);
  505. } else {
  506. mAccountEndAccountAvatar.setVisibility(View.GONE);
  507. }
  508. // activate third/middle account avatar
  509. if (mAvatars[2] != null) {
  510. DisplayUtils.setAvatar(mAvatars[2], this,
  511. mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  512. findNavigationViewChildById(R.id.drawer_account_middle));
  513. mAccountMiddleAccountAvatar.setVisibility(View.VISIBLE);
  514. } else {
  515. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  516. }
  517. } else {
  518. mAccountEndAccountAvatar.setVisibility(View.GONE);
  519. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  520. }
  521. }
  522. }
  523. /**
  524. * re-populates the account list.
  525. *
  526. * @param accounts list of accounts
  527. */
  528. private void repopulateAccountList(Account[] accounts) {
  529. // remove all accounts from list
  530. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_accounts);
  531. // add all accounts to list
  532. for (int i = 0; i < accounts.length; i++) {
  533. try {
  534. // show all accounts except the currently active one
  535. if (!getAccount().name.equals(accounts[i].name)) {
  536. MenuItem accountMenuItem = mNavigationView.getMenu().add(
  537. R.id.drawer_menu_accounts,
  538. Menu.NONE,
  539. MENU_ORDER_ACCOUNT,
  540. accounts[i].name)
  541. .setIcon(TextDrawable.createAvatar(
  542. accounts[i].name,
  543. mMenuAccountAvatarRadiusDimension)
  544. );
  545. DisplayUtils.setAvatar(accounts[i], this, mMenuAccountAvatarRadiusDimension, getResources(), getStorageManager(), accountMenuItem);
  546. }
  547. } catch (Exception e) {
  548. Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
  549. mNavigationView.getMenu().add(
  550. R.id.drawer_menu_accounts,
  551. Menu.NONE,
  552. MENU_ORDER_ACCOUNT,
  553. accounts[i].name)
  554. .setIcon(R.drawable.ic_user);
  555. }
  556. }
  557. // re-add add-account and manage-accounts
  558. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_add,
  559. MENU_ORDER_ACCOUNT_FUNCTION,
  560. getResources().getString(R.string.prefs_add_account)).setIcon(R.drawable.ic_account_plus);
  561. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_manage,
  562. MENU_ORDER_ACCOUNT_FUNCTION,
  563. getResources().getString(R.string.drawer_manage_accounts)).setIcon(R.drawable.ic_settings);
  564. // adding sets menu group back to visible, so safety check and setting invisible
  565. showMenu();
  566. }
  567. /**
  568. * Updates title bar and home buttons (state and icon).
  569. * <p/>
  570. * Assumes that navigation drawer is NOT visible.
  571. */
  572. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  573. super.updateActionBarTitleAndHomeButton(chosenFile);
  574. /// set home button properties
  575. if (mDrawerToggle != null && chosenFile != null) {
  576. mDrawerToggle.setDrawerIndicatorEnabled(isRoot(chosenFile));
  577. } else if (mDrawerToggle != null){
  578. mDrawerToggle.setDrawerIndicatorEnabled(false);
  579. }
  580. }
  581. /**
  582. * sets the given account name in the drawer in case the drawer is available. The account name is shortened
  583. * beginning from the @-sign in the username.
  584. *
  585. * @param account the account to be set in the drawer
  586. */
  587. protected void setAccountInDrawer(Account account) {
  588. if (mDrawerLayout != null && account != null) {
  589. TextView username = (TextView) findNavigationViewChildById(R.id.drawer_username);
  590. TextView usernameFull = (TextView) findNavigationViewChildById(R.id.drawer_username_full);
  591. usernameFull.setText(account.name);
  592. try {
  593. OwnCloudAccount oca = new OwnCloudAccount(account, this);
  594. username.setText(oca.getDisplayName());
  595. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  596. Log_OC.w(TAG, "Couldn't read display name of account fallback to account name");
  597. username.setText(AccountUtils.getAccountUsername(account.name));
  598. }
  599. DisplayUtils.setAvatar(account, this,
  600. mCurrentAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  601. findNavigationViewChildById(R.id.drawer_current_account));
  602. // check and show quota info if available
  603. getAndDisplayUserQuota();
  604. }
  605. }
  606. /**
  607. * Toggle between standard menu and account list including saving the state.
  608. */
  609. private void toggleAccountList() {
  610. mIsAccountChooserActive = !mIsAccountChooserActive;
  611. showMenu();
  612. }
  613. /**
  614. * depending on the #mIsAccountChooserActive flag shows the account chooser or the standard menu.
  615. */
  616. private void showMenu() {
  617. if (mNavigationView != null) {
  618. if (mIsAccountChooserActive) {
  619. mAccountChooserToggle.setImageResource(R.drawable.ic_up);
  620. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  621. if (!getResources().getBoolean(R.bool.multiaccount_support) &&
  622. mNavigationView.getMenu().findItem(R.id.drawer_menu_account_add) != null) {
  623. mNavigationView.getMenu().removeItem(R.id.drawer_menu_account_add);
  624. }
  625. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, false);
  626. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_bottom, false);
  627. } else {
  628. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  629. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  630. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, true);
  631. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_bottom, true);
  632. }
  633. }
  634. }
  635. /**
  636. * shows or hides the quota UI elements.
  637. *
  638. * @param showQuota show/hide quota information
  639. */
  640. private void showQuota(boolean showQuota) {
  641. if (showQuota) {
  642. mQuotaView.setVisibility(View.VISIBLE);
  643. } else {
  644. mQuotaView.setVisibility(View.GONE);
  645. }
  646. }
  647. /**
  648. * configured the quota to be displayed.
  649. *
  650. * @param usedSpace the used space
  651. * @param totalSpace the total space
  652. * @param relative the percentage of space already used
  653. */
  654. private void setQuotaInformation(long usedSpace, long totalSpace, int relative) {
  655. mQuotaProgressBar.setProgress(relative);
  656. DisplayUtils.colorHorizontalProgressBar(mQuotaProgressBar, DisplayUtils.getRelativeInfoColor(this, relative));
  657. mQuotaTextView.setText(String.format(
  658. getString(R.string.drawer_quota),
  659. DisplayUtils.bytesToHumanReadable(usedSpace),
  660. DisplayUtils.bytesToHumanReadable(totalSpace)));
  661. showQuota(true);
  662. }
  663. protected void unsetAllDrawerMenuItems() {
  664. if (mNavigationView != null && mNavigationView.getMenu() != null) {
  665. Menu menu = mNavigationView.getMenu();
  666. for (int i = 0; i < menu.size(); i++) {
  667. menu.getItem(i).setChecked(false);
  668. }
  669. }
  670. mCheckedMenuItem = Menu.NONE;
  671. }
  672. /**
  673. * checks/highlights the provided menu item if the drawer has been initialized and the menu item exists.
  674. *
  675. * @param menuItemId the menu item to be highlighted
  676. */
  677. protected void setDrawerMenuItemChecked(int menuItemId) {
  678. if (mNavigationView != null && mNavigationView.getMenu() != null && mNavigationView.getMenu().findItem
  679. (menuItemId) != null) {
  680. mNavigationView.getMenu().findItem(menuItemId).setChecked(true);
  681. mCheckedMenuItem = menuItemId;
  682. } else {
  683. Log_OC.w(TAG, "setDrawerMenuItemChecked has been called with invalid menu-item-ID");
  684. }
  685. }
  686. /**
  687. * Retrieves and shows the user quota if available
  688. */
  689. private void getAndDisplayUserQuota() {
  690. // set user space information
  691. Thread t = new Thread(new Runnable() {
  692. public void run() {
  693. RemoteOperation getQuotaInfoOperation = new GetRemoteUserInfoOperation();
  694. RemoteOperationResult result = getQuotaInfoOperation.execute(
  695. AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this), DrawerActivity.this);
  696. if (result.isSuccess() && result.getData() != null) {
  697. final UserInfo userInfo = (UserInfo) result.getData().get(0);
  698. final Quota quota = userInfo.getQuota();
  699. if (quota != null) {
  700. final long used = quota.getUsed();
  701. final long total = quota.getTotal();
  702. final int relative = (int) Math.ceil(quota.getRelative());
  703. final long quotaValue = quota.getQuota();
  704. runOnUiThread(new Runnable() {
  705. @Override
  706. public void run() {
  707. if (quotaValue > 0
  708. || quotaValue == GetRemoteUserInfoOperation.QUOTA_LIMIT_INFO_NOT_AVAILABLE) {
  709. /**
  710. * show quota in case
  711. * it is available and calculated (> 0) or
  712. * in case of legacy servers (==QUOTA_LIMIT_INFO_NOT_AVAILABLE)
  713. */
  714. setQuotaInformation(used, total, relative);
  715. } else {
  716. /**
  717. * quotaValue < 0 means special cases like
  718. * {@link RemoteGetUserQuotaOperation.SPACE_NOT_COMPUTED},
  719. * {@link RemoteGetUserQuotaOperation.SPACE_UNKNOWN} or
  720. * {@link RemoteGetUserQuotaOperation.SPACE_UNLIMITED}
  721. * thus don't display any quota information.
  722. */
  723. showQuota(false);
  724. }
  725. }
  726. });
  727. }
  728. }
  729. }
  730. });
  731. t.start();
  732. }
  733. @Override
  734. protected void onCreate(Bundle savedInstanceState) {
  735. super.onCreate(savedInstanceState);
  736. if (savedInstanceState != null) {
  737. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  738. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  739. }
  740. mCurrentAccountAvatarRadiusDimension = getResources()
  741. .getDimension(R.dimen.nav_drawer_header_avatar_radius);
  742. mOtherAccountAvatarRadiusDimension = getResources()
  743. .getDimension(R.dimen.nav_drawer_header_avatar_other_accounts_radius);
  744. mMenuAccountAvatarRadiusDimension = getResources()
  745. .getDimension(R.dimen.nav_drawer_menu_avatar_radius);
  746. }
  747. @Override
  748. protected void onSaveInstanceState(Bundle outState) {
  749. super.onSaveInstanceState(outState);
  750. outState.putBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, mIsAccountChooserActive);
  751. outState.putInt(KEY_CHECKED_MENU_ITEM, mCheckedMenuItem);
  752. }
  753. @Override
  754. public void onRestoreInstanceState(Bundle savedInstanceState) {
  755. super.onRestoreInstanceState(savedInstanceState);
  756. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  757. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  758. // (re-)setup drawer state
  759. showMenu();
  760. // check/highlight the menu item if present
  761. if (mCheckedMenuItem > Menu.NONE || mCheckedMenuItem < Menu.NONE) {
  762. setDrawerMenuItemChecked(mCheckedMenuItem);
  763. }
  764. }
  765. @Override
  766. protected void onPostCreate(Bundle savedInstanceState) {
  767. super.onPostCreate(savedInstanceState);
  768. // Sync the toggle state after onRestoreInstanceState has occurred.
  769. if (mDrawerToggle != null) {
  770. mDrawerToggle.syncState();
  771. if (isDrawerOpen()) {
  772. mDrawerToggle.setDrawerIndicatorEnabled(true);
  773. }
  774. }
  775. updateAccountList();
  776. }
  777. @Override
  778. public void onConfigurationChanged(Configuration newConfig) {
  779. super.onConfigurationChanged(newConfig);
  780. if (mDrawerToggle != null) {
  781. mDrawerToggle.onConfigurationChanged(newConfig);
  782. }
  783. }
  784. @Override
  785. public void onBackPressed() {
  786. if (isDrawerOpen()) {
  787. closeDrawer();
  788. return;
  789. }
  790. super.onBackPressed();
  791. }
  792. @Override
  793. protected void onResume() {
  794. super.onResume();
  795. setDrawerMenuItemChecked(mCheckedMenuItem);
  796. }
  797. @Override
  798. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  799. super.onActivityResult(requestCode, resultCode, data);
  800. // update Account list and active account if Manage Account activity replies with
  801. // - ACCOUNT_LIST_CHANGED = true
  802. // - RESULT_OK
  803. if (requestCode == ACTION_MANAGE_ACCOUNTS
  804. && resultCode == RESULT_OK
  805. && data.getBooleanExtra(ManageAccountsActivity.KEY_ACCOUNT_LIST_CHANGED, false)) {
  806. // current account has changed
  807. if (data.getBooleanExtra(ManageAccountsActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
  808. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  809. restart();
  810. } else {
  811. updateAccountList();
  812. }
  813. }
  814. }
  815. /**
  816. * Finds a view that was identified by the id attribute from the drawer header.
  817. *
  818. * @param id the view's id
  819. * @return The view if found or <code>null</code> otherwise.
  820. */
  821. private View findNavigationViewChildById(int id) {
  822. return ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0).findViewById(id);
  823. }
  824. /**
  825. * restart helper method which is called after a changing the current account.
  826. */
  827. protected abstract void restart();
  828. @Override
  829. protected void onAccountCreationSuccessful(AccountManagerFuture<Bundle> future) {
  830. super.onAccountCreationSuccessful(future);
  831. updateAccountList();
  832. restart();
  833. }
  834. /**
  835. * populates the avatar drawer array with the first three ownCloud {@link Account}s while the first element is
  836. * always the current account.
  837. */
  838. private void populateDrawerOwnCloudAccounts() {
  839. mAvatars = new Account[3];
  840. Account[] accountsAll = AccountManager.get(this).getAccountsByType
  841. (MainApp.getAccountType());
  842. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  843. mAvatars[0] = currentAccount;
  844. int j = 0;
  845. for (int i = 1; i <= 2 && i < accountsAll.length && j < accountsAll.length; j++) {
  846. if (!currentAccount.equals(accountsAll[j])) {
  847. mAvatars[i] = accountsAll[j];
  848. i++;
  849. }
  850. }
  851. }
  852. @Override
  853. public void avatarGenerated(Drawable avatarDrawable, Object callContext) {
  854. if (callContext instanceof MenuItem) {
  855. MenuItem mi = (MenuItem) callContext;
  856. mi.setIcon(avatarDrawable);
  857. } else if (callContext instanceof ImageView) {
  858. ImageView iv = (ImageView) callContext;
  859. iv.setImageDrawable(avatarDrawable);
  860. }
  861. }
  862. @Override
  863. public boolean shouldCallGeneratedCallback(String tag, Object callContext) {
  864. if (callContext instanceof MenuItem) {
  865. MenuItem mi = (MenuItem) callContext;
  866. return String.valueOf(mi.getTitle()).equals(tag);
  867. } else if (callContext instanceof ImageView) {
  868. ImageView iv = (ImageView) callContext;
  869. return String.valueOf(iv.getTag()).equals(tag);
  870. }
  871. return false;
  872. }
  873. /**
  874. * Adds other listeners to react on changes of the drawer layout.
  875. *
  876. * @param listener Object interested in changes of the drawer layout.
  877. */
  878. public void addDrawerListener(DrawerLayout.DrawerListener listener) {
  879. if (mDrawerLayout != null) {
  880. mDrawerLayout.addDrawerListener(listener);
  881. } else {
  882. Log_OC.e(TAG, "Drawer layout not ready to add drawer listener");
  883. }
  884. }
  885. public boolean isDrawerIndicatorAvailable() {
  886. return true;
  887. }
  888. @Override
  889. protected void onStart() {
  890. super.onStart();
  891. EventBus.getDefault().register(this);
  892. }
  893. @Override
  894. protected void onStop() {
  895. EventBus.getDefault().unregister(this);
  896. super.onStop();
  897. }
  898. }