DrawerActivity.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.owncloud.android.ui.activity;
  20. import android.accounts.Account;
  21. import android.accounts.AccountManager;
  22. import android.accounts.AccountManagerFuture;
  23. import android.content.Intent;
  24. import android.content.res.Configuration;
  25. import android.graphics.Bitmap;
  26. import android.os.Bundle;
  27. import android.support.annotation.NonNull;
  28. import android.support.design.widget.NavigationView;
  29. import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
  30. import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
  31. import android.support.v4.view.GravityCompat;
  32. import android.support.v4.widget.DrawerLayout;
  33. import android.support.v7.app.ActionBarDrawerToggle;
  34. import android.util.TypedValue;
  35. import android.view.Menu;
  36. import android.view.MenuItem;
  37. import android.view.View;
  38. import android.widget.ImageView;
  39. import android.widget.TextView;
  40. import com.owncloud.android.MainApp;
  41. import com.owncloud.android.R;
  42. import com.owncloud.android.authentication.AccountUtils;
  43. import com.owncloud.android.datamodel.OCFile;
  44. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  45. import com.owncloud.android.lib.common.utils.Log_OC;
  46. import com.owncloud.android.ui.TextDrawable;
  47. import com.owncloud.android.utils.BitmapUtils;
  48. import java.io.UnsupportedEncodingException;
  49. import java.security.NoSuchAlgorithmException;
  50. /**
  51. * Base class to handle setup of the drawer implementation.
  52. */
  53. public abstract class DrawerActivity extends ToolbarActivity {
  54. private static final String TAG = DrawerActivity.class.getSimpleName();
  55. private static final String KEY_IS_ACCOUNT_CHOOSER_ACTIVE = "IS_ACCOUNT_CHOOSER_ACTIVE";
  56. private static final String KEY_CHECKED_MENU_ITEM = "CHECKED_MENU_ITEM";
  57. private static final int ACTION_MANAGE_ACCOUNTS = 101;
  58. private static final int MENU_ORDER_ACCOUNT = 1;
  59. private static final int MENU_ORDER_ACCOUNT_FUNCTION = 2;
  60. /**
  61. * Reference to the drawer layout.
  62. */
  63. private DrawerLayout mDrawerLayout;
  64. /**
  65. * Reference to the drawer toggle.
  66. */
  67. private ActionBarDrawerToggle mDrawerToggle;
  68. /**
  69. * Reference to the navigation view.
  70. */
  71. private NavigationView mNavigationView;
  72. /**
  73. * Reference to the account chooser toggle.
  74. */
  75. private ImageView mAccountChooserToggle;
  76. /**
  77. * Flag to signal if the account chooser is active.
  78. */
  79. private boolean mIsAccountChooserActive;
  80. /**
  81. * Id of the checked menu item.
  82. */
  83. private int mCheckedMenuItem = Menu.NONE;
  84. /**
  85. * Initializes the drawer, its content and highlights the menu item with the given id.
  86. * This method needs to be called after the content view has been set.
  87. *
  88. * @param menuItemId the menu item to be checked/highlighted
  89. */
  90. protected void setupDrawer(int menuItemId) {
  91. setupDrawer();
  92. setDrawerMenuItemChecked(menuItemId);
  93. }
  94. /**
  95. * Initializes the drawer and its content.
  96. * This method needs to be called after the content view has been set.
  97. */
  98. protected void setupDrawer() {
  99. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  100. mNavigationView = (NavigationView) findViewById(R.id.nav_view);
  101. if (mNavigationView != null) {
  102. setupDrawerContent(mNavigationView);
  103. mAccountChooserToggle = (ImageView) findNavigationViewChildById(R.id.drawer_account_chooser_toogle);
  104. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  105. mIsAccountChooserActive = false;
  106. findNavigationViewChildById(R.id.drawer_active_user)
  107. .setOnClickListener(new View.OnClickListener() {
  108. @Override
  109. public void onClick(View v) {
  110. toggleAccountList();
  111. }
  112. });
  113. }
  114. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
  115. /** Called when a drawer has settled in a completely closed state. */
  116. public void onDrawerClosed(View view) {
  117. super.onDrawerClosed(view);
  118. // standard behavior of drawer is to switch to the standard menu on closing
  119. if (mIsAccountChooserActive) {
  120. toggleAccountList();
  121. }
  122. invalidateOptionsMenu();
  123. }
  124. /** Called when a drawer has settled in a completely open state. */
  125. public void onDrawerOpened(View drawerView) {
  126. super.onDrawerOpened(drawerView);
  127. mDrawerToggle.setDrawerIndicatorEnabled(true);
  128. invalidateOptionsMenu();
  129. }
  130. };
  131. // Set the drawer toggle as the DrawerListener
  132. mDrawerLayout.setDrawerListener(mDrawerToggle);
  133. mDrawerToggle.setDrawerIndicatorEnabled(true);
  134. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  135. }
  136. /**
  137. * setup drawer content, basically setting the item selected listener.
  138. *
  139. * @param navigationView the drawers navigation view
  140. */
  141. protected void setupDrawerContent(NavigationView navigationView) {
  142. navigationView.setNavigationItemSelectedListener(
  143. new NavigationView.OnNavigationItemSelectedListener() {
  144. @Override
  145. public boolean onNavigationItemSelected(MenuItem menuItem) {
  146. mDrawerLayout.closeDrawers();
  147. switch (menuItem.getItemId()) {
  148. case R.id.nav_all_files:
  149. menuItem.setChecked(true);
  150. mCheckedMenuItem = menuItem.getItemId();
  151. allFilesOption();
  152. break;
  153. case R.id.nav_uploads:
  154. Intent uploadListIntent = new Intent(getApplicationContext(),
  155. UploadListActivity.class);
  156. uploadListIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  157. startActivity(uploadListIntent);
  158. break;
  159. case R.id.nav_settings:
  160. Intent settingsIntent = new Intent(getApplicationContext(),
  161. Preferences.class);
  162. startActivity(settingsIntent);
  163. break;
  164. case R.id.drawer_menu_account_add:
  165. createAccount();
  166. break;
  167. case R.id.drawer_menu_account_manage:
  168. Intent manageAccountsIntent = new Intent(getApplicationContext(),
  169. ManageAccountsActivity.class);
  170. startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
  171. break;
  172. case Menu.NONE:
  173. // account clicked
  174. AccountUtils.setCurrentOwnCloudAccount(
  175. getApplicationContext(), menuItem.getTitle().toString());
  176. restart();
  177. default:
  178. Log_OC.i(TAG, "Unknown drawer menu item clicked: " + menuItem.getTitle());
  179. }
  180. return true;
  181. }
  182. });
  183. // handle correct state
  184. if (mIsAccountChooserActive) {
  185. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  186. } else {
  187. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  188. }
  189. }
  190. /**
  191. * checks if the drawer exists and is opened.
  192. *
  193. * @return <code>true</code> if the drawer is open, else <code>false</code>
  194. */
  195. public boolean isDrawerOpen() {
  196. return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.START);
  197. }
  198. /**
  199. * closes the drawer.
  200. */
  201. public void closeDrawer() {
  202. if (mDrawerLayout != null) {
  203. mDrawerLayout.closeDrawer(GravityCompat.START);
  204. }
  205. }
  206. /**
  207. * opens the drawer.
  208. */
  209. public void openDrawer() {
  210. if (mDrawerLayout != null) {
  211. mDrawerLayout.openDrawer(GravityCompat.START);
  212. }
  213. }
  214. /**
  215. * Enable or disable interaction with all drawers.
  216. *
  217. * @param lockMode The new lock mode for the given drawer. One of {@link DrawerLayout#LOCK_MODE_UNLOCKED},
  218. * {@link DrawerLayout#LOCK_MODE_LOCKED_CLOSED} or {@link DrawerLayout#LOCK_MODE_LOCKED_OPEN}.
  219. */
  220. public void setDrawerLockMode(int lockMode) {
  221. if (mDrawerLayout != null) {
  222. mDrawerLayout.setDrawerLockMode(lockMode);
  223. }
  224. }
  225. /**
  226. * Enable or disable the drawer indicator.
  227. *
  228. * @param enable <code>true</code> to enable, <code>false</code> to disable
  229. */
  230. public void setDrawerIndicatorEnabled(boolean enable) {
  231. if (mDrawerToggle != null) {
  232. mDrawerToggle.setDrawerIndicatorEnabled(enable);
  233. }
  234. }
  235. /**
  236. * updates the account list in the drawer.
  237. */
  238. public void updateAccountList() {
  239. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  240. if (accounts.length > 0 && mNavigationView != null) {
  241. repopulateAccountList(accounts);
  242. setAccountInDrawer(AccountUtils.getCurrentOwnCloudAccount(this));
  243. }
  244. }
  245. /**
  246. * re-populates the account list.
  247. *
  248. * @param accounts list of accounts
  249. */
  250. private void repopulateAccountList(Account[] accounts) {
  251. // remove all accounts from list
  252. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_accounts);
  253. // add all accounts to list
  254. for (int i = 0; i < accounts.length; i++) {
  255. try {
  256. mNavigationView.getMenu().add(
  257. R.id.drawer_menu_accounts,
  258. Menu.NONE,
  259. MENU_ORDER_ACCOUNT,
  260. accounts[i].name)
  261. .setIcon(createAvatar(accounts[i].name, 16));
  262. } catch (Exception e) {
  263. Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
  264. mNavigationView.getMenu().add(
  265. R.id.drawer_menu_accounts,
  266. Menu.NONE,
  267. MENU_ORDER_ACCOUNT,
  268. accounts[i].name)
  269. .setIcon(R.drawable.ic_account_circle);
  270. }
  271. }
  272. // re-add add-account and manage-accounts
  273. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_add,
  274. MENU_ORDER_ACCOUNT_FUNCTION,
  275. getResources().getString(R.string.prefs_add_account)).setIcon(R.drawable.ic_account_plus);
  276. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_manage,
  277. MENU_ORDER_ACCOUNT_FUNCTION,
  278. getResources().getString(R.string.drawer_manage_accounts)).setIcon(R.drawable.ic_settings);
  279. // adding sets menu group back to visible, so safety check and setting invisible
  280. showMenu();
  281. }
  282. /**
  283. * Method that gets called on drawer menu click for 'All Files'.
  284. */
  285. public abstract void allFilesOption();
  286. /**
  287. * Updates title bar and home buttons (state and icon).
  288. * <p/>
  289. * Assumes that navigation drawer is NOT visible.
  290. */
  291. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  292. super.updateActionBarTitleAndHomeButton(chosenFile);
  293. /// set home button properties
  294. if(mDrawerToggle != null) {
  295. mDrawerToggle.setDrawerIndicatorEnabled(isRoot(chosenFile));
  296. }
  297. }
  298. /**
  299. * sets the given account name in the drawer in case the drawer is available. The account name is shortened
  300. * beginning from the @-sign in the username.
  301. *
  302. * @param account the account to be set in the drawer
  303. */
  304. protected void setAccountInDrawer(Account account) {
  305. if (mDrawerLayout != null && account != null) {
  306. TextView username = (TextView) findNavigationViewChildById(R.id.drawer_username);
  307. TextView usernameFull = (TextView) findNavigationViewChildById(R.id.drawer_username_full);
  308. usernameFull.setText(account.name);
  309. int lastAtPos = account.name.lastIndexOf("@");
  310. username.setText(account.name.substring(0, lastAtPos));
  311. ImageView userIcon = (ImageView) findNavigationViewChildById(R.id.drawer_usericon);
  312. setAvatarInDrawer(account);
  313. }
  314. }
  315. /**
  316. * sets the avatar of the current account in the drawer in case the drawer is available.
  317. *
  318. * @param account the account to be set in the drawer
  319. */
  320. private void setAvatarInDrawer(Account account) {
  321. if (mDrawerLayout != null && account != null) {
  322. ImageView userIcon = (ImageView) findNavigationViewChildById(R.id.drawer_usericon);
  323. int lastAtPos = account.name.lastIndexOf("@");
  324. String username = account.name.substring(0, lastAtPos);
  325. // Thumbnail in Cache?
  326. Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache("a_" + username);
  327. if (thumbnail != null) {
  328. RoundedBitmapDrawable roundedAvatar = RoundedBitmapDrawableFactory.create
  329. (MainApp.getAppContext().getResources(), thumbnail);
  330. roundedAvatar.setCircular(true);
  331. userIcon.setImageDrawable(roundedAvatar);
  332. } else {
  333. // generate new avatar
  334. if (ThumbnailsCacheManager.cancelPotentialAvatarWork(username, userIcon)) {
  335. final ThumbnailsCacheManager.AvatarGenerationTask task =
  336. new ThumbnailsCacheManager.AvatarGenerationTask(
  337. userIcon, getStorageManager(), account
  338. );
  339. if (thumbnail == null) {
  340. try {
  341. userIcon.setImageDrawable(
  342. createAvatar(
  343. account.name,
  344. getResources().getDimension(R.dimen.nav_drawer_header_avatar_radius)
  345. )
  346. );
  347. } catch (Exception e) {
  348. Log_OC.e(TAG, "Error calculating RGB value for active account icon.", e);
  349. userIcon.setImageResource(R.drawable.ic_account_circle);
  350. }
  351. } else {
  352. final ThumbnailsCacheManager.AsyncAvatarDrawable asyncDrawable =
  353. new ThumbnailsCacheManager.AsyncAvatarDrawable(
  354. getResources(),
  355. thumbnail,
  356. task
  357. );
  358. RoundedBitmapDrawable roundedAvatar = RoundedBitmapDrawableFactory.create
  359. (MainApp.getAppContext().getResources(), asyncDrawable.getBitmap());
  360. roundedAvatar.setCircular(true);
  361. userIcon.setImageDrawable(roundedAvatar);
  362. }
  363. task.execute(username);
  364. }
  365. }
  366. }
  367. }
  368. @NonNull
  369. private TextDrawable createAvatar(String accountName, float radiusInDp) throws UnsupportedEncodingException,
  370. NoSuchAlgorithmException {
  371. int[] rgb = BitmapUtils.calculateRGB(accountName);
  372. float radiusInPx = TypedValue.applyDimension(
  373. TypedValue.COMPLEX_UNIT_DIP,
  374. radiusInDp,
  375. getResources().getDisplayMetrics());
  376. return new TextDrawable(
  377. accountName.substring(0, 1).toUpperCase(), rgb[0], rgb[1], rgb[2], radiusInPx);
  378. }
  379. /**
  380. * Toggle between standard menu and account list including saving the state.
  381. */
  382. private void toggleAccountList() {
  383. mIsAccountChooserActive = !mIsAccountChooserActive;
  384. showMenu();
  385. }
  386. /**
  387. * depending on the #mIsAccountChooserActive flag shows the account chooser or the standard menu.
  388. */
  389. private void showMenu() {
  390. if(mNavigationView != null) {
  391. if (mIsAccountChooserActive) {
  392. mAccountChooserToggle.setImageResource(R.drawable.ic_up);
  393. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  394. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, false);
  395. } else {
  396. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  397. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  398. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, true);
  399. }
  400. }
  401. }
  402. /**
  403. * checks/highlights the provided menu item if the drawer has been initialized and the menu item exists.
  404. *
  405. * @param menuItemId the menu item to be highlighted
  406. */
  407. protected void setDrawerMenuItemChecked(int menuItemId) {
  408. if (mNavigationView != null && mNavigationView.getMenu() != null && mNavigationView.getMenu().findItem
  409. (menuItemId) != null) {
  410. mNavigationView.getMenu().findItem(menuItemId).setChecked(true);
  411. mCheckedMenuItem = menuItemId;
  412. } else {
  413. Log_OC.w(TAG, "setDrawerMenuItemChecked has been called with invalid menu-item-ID");
  414. }
  415. }
  416. @Override
  417. protected void onCreate(Bundle savedInstanceState) {
  418. super.onCreate(savedInstanceState);
  419. if (savedInstanceState != null) {
  420. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  421. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  422. }
  423. }
  424. @Override
  425. protected void onSaveInstanceState(Bundle outState) {
  426. super.onSaveInstanceState(outState);
  427. outState.putBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, mIsAccountChooserActive);
  428. outState.putInt(KEY_CHECKED_MENU_ITEM, mCheckedMenuItem);
  429. }
  430. @Override
  431. public void onRestoreInstanceState(Bundle savedInstanceState) {
  432. super.onRestoreInstanceState(savedInstanceState);
  433. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  434. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  435. // (re-)setup drawer state
  436. showMenu();
  437. // check/highlight the menu item if present
  438. if (mCheckedMenuItem > Menu.NONE || mCheckedMenuItem < Menu.NONE) {
  439. setDrawerMenuItemChecked(mCheckedMenuItem);
  440. }
  441. }
  442. @Override
  443. protected void onPostCreate(Bundle savedInstanceState) {
  444. super.onPostCreate(savedInstanceState);
  445. // Sync the toggle state after onRestoreInstanceState has occurred.
  446. if (mDrawerToggle != null) {
  447. mDrawerToggle.syncState();
  448. if (isDrawerOpen()) {
  449. mDrawerToggle.setDrawerIndicatorEnabled(true);
  450. }
  451. }
  452. updateAccountList();
  453. }
  454. @Override
  455. public void onConfigurationChanged(Configuration newConfig) {
  456. super.onConfigurationChanged(newConfig);
  457. if (mDrawerToggle != null) {
  458. mDrawerToggle.onConfigurationChanged(newConfig);
  459. }
  460. }
  461. @Override
  462. public void onBackPressed() {
  463. if (isDrawerOpen()) {
  464. closeDrawer();
  465. return;
  466. }
  467. super.onBackPressed();
  468. }
  469. @Override
  470. protected void onResume() {
  471. super.onResume();
  472. setDrawerMenuItemChecked(mCheckedMenuItem);
  473. }
  474. @Override
  475. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  476. super.onActivityResult(requestCode, resultCode, data);
  477. // update Account list and active account if Manage Account activity replies with
  478. // - ACCOUNT_LIST_CHANGED = true
  479. // - RESULT_OK
  480. if (requestCode == ACTION_MANAGE_ACCOUNTS
  481. && resultCode == RESULT_OK
  482. && data.getBooleanExtra(ManageAccountsActivity.KEY_ACCOUNT_LIST_CHANGED, false)) {
  483. // current account has changed
  484. if (data.getBooleanExtra(ManageAccountsActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
  485. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  486. restart();
  487. } else {
  488. updateAccountList();
  489. }
  490. }
  491. }
  492. /**
  493. * Finds a view that was identified by the id attribute from the drawer header.
  494. *
  495. * @param id the view's id
  496. * @return The view if found or <code>null</code> otherwise.
  497. */
  498. private View findNavigationViewChildById(int id) {
  499. return ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0).findViewById(id);
  500. }
  501. /**
  502. * restart helper method which is called after a changing the current account.
  503. */
  504. protected abstract void restart();
  505. @Override
  506. protected void onAccountCreationSuccessful(AccountManagerFuture<Bundle> future) {
  507. super.onAccountCreationSuccessful(future);
  508. updateAccountList();
  509. restart();
  510. }
  511. }