DrawerActivity.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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.Build;
  27. import android.os.Bundle;
  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.view.Menu;
  35. import android.view.MenuItem;
  36. import android.view.View;
  37. import android.widget.ImageView;
  38. import android.widget.TextView;
  39. import com.owncloud.android.MainApp;
  40. import com.owncloud.android.R;
  41. import com.owncloud.android.authentication.AccountUtils;
  42. import com.owncloud.android.datamodel.OCFile;
  43. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  44. import com.owncloud.android.lib.common.utils.Log_OC;
  45. import com.owncloud.android.ui.TextDrawable;
  46. import com.owncloud.android.utils.BitmapUtils;
  47. import com.owncloud.android.utils.DisplayUtils;
  48. /**
  49. * Base class to handle setup of the drawer implementation including user switching and avatar fetching and fallback
  50. * generation.
  51. */
  52. public abstract class DrawerActivity extends ToolbarActivity {
  53. private static final String TAG = DrawerActivity.class.getSimpleName();
  54. private static final String KEY_IS_ACCOUNT_CHOOSER_ACTIVE = "IS_ACCOUNT_CHOOSER_ACTIVE";
  55. private static final String KEY_CHECKED_MENU_ITEM = "CHECKED_MENU_ITEM";
  56. private static final int ACTION_MANAGE_ACCOUNTS = 101;
  57. private static final int MENU_ORDER_ACCOUNT = 1;
  58. private static final int MENU_ORDER_ACCOUNT_FUNCTION = 2;
  59. /**
  60. * menu account avatar radius.
  61. */
  62. private float mMenuAccountAvatarRadiusDimension;
  63. /**
  64. * current account avatar radius.
  65. */
  66. private float mCurrentAccountAvatarRadiusDimension;
  67. /**
  68. * other accounts avatar radius.
  69. */
  70. private float mOtherAccountAvatarRadiusDimension;
  71. /**
  72. * Reference to the drawer layout.
  73. */
  74. private DrawerLayout mDrawerLayout;
  75. /**
  76. * Reference to the drawer toggle.
  77. */
  78. private ActionBarDrawerToggle mDrawerToggle;
  79. /**
  80. * Reference to the navigation view.
  81. */
  82. private NavigationView mNavigationView;
  83. /**
  84. * Reference to the account chooser toggle.
  85. */
  86. private ImageView mAccountChooserToggle;
  87. /**
  88. * Reference to the middle account avatar.
  89. */
  90. private ImageView mAccountMiddleAccountAvatar;
  91. /**
  92. * Reference to the end account avatar.
  93. */
  94. private ImageView mAccountEndAccountAvatar;
  95. /**
  96. * Flag to signal if the account chooser is active.
  97. */
  98. private boolean mIsAccountChooserActive;
  99. /**
  100. * Id of the checked menu item.
  101. */
  102. private int mCheckedMenuItem = Menu.NONE;
  103. /**
  104. * accounts for the (max) three displayed accounts in the drawer header.
  105. */
  106. private Account[] mAvatars = new Account[3];
  107. /**
  108. * Initializes the drawer, its content and highlights the menu item with the given id.
  109. * This method needs to be called after the content view has been set.
  110. *
  111. * @param menuItemId the menu item to be checked/highlighted
  112. */
  113. protected void setupDrawer(int menuItemId) {
  114. setupDrawer();
  115. setDrawerMenuItemChecked(menuItemId);
  116. }
  117. /**
  118. * Initializes the drawer and its content.
  119. * This method needs to be called after the content view has been set.
  120. */
  121. protected void setupDrawer() {
  122. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  123. mNavigationView = (NavigationView) findViewById(R.id.nav_view);
  124. if (mNavigationView != null) {
  125. mAccountChooserToggle = (ImageView) findNavigationViewChildById(R.id.drawer_account_chooser_toogle);
  126. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  127. mIsAccountChooserActive = false;
  128. mAccountMiddleAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_middle);
  129. mAccountEndAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_end);
  130. // on pre lollipop the light theme adds a black tint to icons with white coloring
  131. // ruining the generic avatars, so tinting for icons is deactivated pre lollipop
  132. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  133. mNavigationView.setItemIconTintList(null);
  134. }
  135. setupDrawerContent(mNavigationView);
  136. findNavigationViewChildById(R.id.drawer_active_user)
  137. .setOnClickListener(new View.OnClickListener() {
  138. @Override
  139. public void onClick(View v) {
  140. toggleAccountList();
  141. }
  142. });
  143. }
  144. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
  145. /** Called when a drawer has settled in a completely closed state. */
  146. public void onDrawerClosed(View view) {
  147. super.onDrawerClosed(view);
  148. // standard behavior of drawer is to switch to the standard menu on closing
  149. if (mIsAccountChooserActive) {
  150. toggleAccountList();
  151. }
  152. invalidateOptionsMenu();
  153. }
  154. /** Called when a drawer has settled in a completely open state. */
  155. public void onDrawerOpened(View drawerView) {
  156. super.onDrawerOpened(drawerView);
  157. mDrawerToggle.setDrawerIndicatorEnabled(true);
  158. invalidateOptionsMenu();
  159. }
  160. };
  161. // Set the drawer toggle as the DrawerListener
  162. mDrawerLayout.setDrawerListener(mDrawerToggle);
  163. mDrawerToggle.setDrawerIndicatorEnabled(true);
  164. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  165. }
  166. /**
  167. * setup drawer content, basically setting the item selected listener.
  168. *
  169. * @param navigationView the drawers navigation view
  170. */
  171. protected void setupDrawerContent(NavigationView navigationView) {
  172. navigationView.setNavigationItemSelectedListener(
  173. new NavigationView.OnNavigationItemSelectedListener() {
  174. @Override
  175. public boolean onNavigationItemSelected(MenuItem menuItem) {
  176. mDrawerLayout.closeDrawers();
  177. switch (menuItem.getItemId()) {
  178. case R.id.nav_all_files:
  179. menuItem.setChecked(true);
  180. mCheckedMenuItem = menuItem.getItemId();
  181. allFilesOption();
  182. // TODO activate when On Device branch is merged
  183. // MainApp.showOnlyFilesOnDevice(false);
  184. // refreshDirectory();
  185. break;
  186. // TODO activate when On Device branch is merged
  187. // case R.id.nav_on_device:
  188. // menuItem.setChecked(true);
  189. // mCheckedMenuItem = menuItem.getItemId();
  190. // MainApp.showOnlyFilesOnDevice(true);
  191. // refreshDirectory();
  192. // break;
  193. case R.id.nav_uploads:
  194. Intent uploadListIntent = new Intent(getApplicationContext(),
  195. UploadListActivity.class);
  196. uploadListIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  197. startActivity(uploadListIntent);
  198. break;
  199. case R.id.nav_settings:
  200. Intent settingsIntent = new Intent(getApplicationContext(),
  201. Preferences.class);
  202. startActivity(settingsIntent);
  203. break;
  204. case R.id.drawer_menu_account_add:
  205. createAccount();
  206. break;
  207. case R.id.drawer_menu_account_manage:
  208. Intent manageAccountsIntent = new Intent(getApplicationContext(),
  209. ManageAccountsActivity.class);
  210. startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
  211. break;
  212. case Menu.NONE:
  213. // account clicked
  214. accountClicked(menuItem.getTitle().toString());
  215. default:
  216. Log_OC.i(TAG, "Unknown drawer menu item clicked: " + menuItem.getTitle());
  217. }
  218. return true;
  219. }
  220. });
  221. // handle correct state
  222. if (mIsAccountChooserActive) {
  223. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  224. } else {
  225. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  226. }
  227. }
  228. /**
  229. * sets the new/current account and restarts. In case the given account equals the actual/current account the
  230. * call will be ignored.
  231. *
  232. * @param accountName The account name to be set
  233. */
  234. private void accountClicked(String accountName) {
  235. if (!AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()).name.equals(accountName)) {
  236. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), accountName);
  237. restart();
  238. }
  239. }
  240. /**
  241. * click method for mini avatars in drawer header.
  242. *
  243. * @param view the clicked ImageView
  244. */
  245. public void onAccountDrawerClick(View view) {
  246. accountClicked(view.getContentDescription().toString());
  247. }
  248. /**
  249. * checks if the drawer exists and is opened.
  250. *
  251. * @return <code>true</code> if the drawer is open, else <code>false</code>
  252. */
  253. public boolean isDrawerOpen() {
  254. return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.START);
  255. }
  256. /**
  257. * closes the drawer.
  258. */
  259. public void closeDrawer() {
  260. if (mDrawerLayout != null) {
  261. mDrawerLayout.closeDrawer(GravityCompat.START);
  262. }
  263. }
  264. /**
  265. * opens the drawer.
  266. */
  267. public void openDrawer() {
  268. if (mDrawerLayout != null) {
  269. mDrawerLayout.openDrawer(GravityCompat.START);
  270. }
  271. }
  272. /**
  273. * Enable or disable interaction with all drawers.
  274. *
  275. * @param lockMode The new lock mode for the given drawer. One of {@link DrawerLayout#LOCK_MODE_UNLOCKED},
  276. * {@link DrawerLayout#LOCK_MODE_LOCKED_CLOSED} or {@link DrawerLayout#LOCK_MODE_LOCKED_OPEN}.
  277. */
  278. public void setDrawerLockMode(int lockMode) {
  279. if (mDrawerLayout != null) {
  280. mDrawerLayout.setDrawerLockMode(lockMode);
  281. }
  282. }
  283. /**
  284. * Enable or disable the drawer indicator.
  285. *
  286. * @param enable <code>true</code> to enable, <code>false</code> to disable
  287. */
  288. public void setDrawerIndicatorEnabled(boolean enable) {
  289. if (mDrawerToggle != null) {
  290. mDrawerToggle.setDrawerIndicatorEnabled(enable);
  291. }
  292. }
  293. /**
  294. * updates the account list in the drawer.
  295. */
  296. public void updateAccountList() {
  297. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  298. if (mNavigationView != null && mDrawerLayout != null) {
  299. if (accounts.length > 0) {
  300. repopulateAccountList(accounts);
  301. setAccountInDrawer(AccountUtils.getCurrentOwnCloudAccount(this));
  302. populateDrawerOwnCloudAccounts();
  303. // activate second/end account avatar
  304. if (mAvatars[1] != null) {
  305. DisplayUtils.setAvatar(mAvatars[1],
  306. (ImageView) findNavigationViewChildById(R.id.drawer_account_end),
  307. mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager());
  308. mAccountEndAccountAvatar.setVisibility(View.VISIBLE);
  309. } else {
  310. mAccountEndAccountAvatar.setVisibility(View.GONE);
  311. }
  312. // activate third/middle account avatar
  313. if (mAvatars[2] != null) {
  314. DisplayUtils.setAvatar(mAvatars[2],
  315. (ImageView) findNavigationViewChildById(R.id.drawer_account_middle),
  316. mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager());
  317. mAccountMiddleAccountAvatar.setVisibility(View.VISIBLE);
  318. } else {
  319. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  320. }
  321. } else {
  322. mAccountEndAccountAvatar.setVisibility(View.GONE);
  323. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  324. }
  325. }
  326. }
  327. /**
  328. * re-populates the account list.
  329. *
  330. * @param accounts list of accounts
  331. */
  332. private void repopulateAccountList(Account[] accounts) {
  333. // remove all accounts from list
  334. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_accounts);
  335. // add all accounts to list
  336. for (int i = 0; i < accounts.length; i++) {
  337. try {
  338. MenuItem accountMenuItem = mNavigationView.getMenu().add(
  339. R.id.drawer_menu_accounts,
  340. Menu.NONE,
  341. MENU_ORDER_ACCOUNT,
  342. accounts[i].name)
  343. .setIcon(TextDrawable.createAvatar(
  344. accounts[i].name,
  345. mMenuAccountAvatarRadiusDimension)
  346. );
  347. setAvatar(accounts[i], accountMenuItem);
  348. } catch (Exception e) {
  349. Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
  350. mNavigationView.getMenu().add(
  351. R.id.drawer_menu_accounts,
  352. Menu.NONE,
  353. MENU_ORDER_ACCOUNT,
  354. accounts[i].name)
  355. .setIcon(R.drawable.ic_user);
  356. }
  357. }
  358. // re-add add-account and manage-accounts
  359. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_add,
  360. MENU_ORDER_ACCOUNT_FUNCTION,
  361. getResources().getString(R.string.prefs_add_account)).setIcon(R.drawable.ic_account_plus);
  362. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_manage,
  363. MENU_ORDER_ACCOUNT_FUNCTION,
  364. getResources().getString(R.string.drawer_manage_accounts)).setIcon(R.drawable.ic_settings);
  365. // adding sets menu group back to visible, so safety check and setting invisible
  366. showMenu();
  367. }
  368. /**
  369. * Method that gets called on drawer menu click for 'All Files'.
  370. */
  371. public abstract void allFilesOption();
  372. /**
  373. * Updates title bar and home buttons (state and icon).
  374. * <p/>
  375. * Assumes that navigation drawer is NOT visible.
  376. */
  377. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  378. super.updateActionBarTitleAndHomeButton(chosenFile);
  379. /// set home button properties
  380. if (mDrawerToggle != null) {
  381. mDrawerToggle.setDrawerIndicatorEnabled(isRoot(chosenFile));
  382. }
  383. }
  384. /**
  385. * sets the given account name in the drawer in case the drawer is available. The account name is shortened
  386. * beginning from the @-sign in the username.
  387. *
  388. * @param account the account to be set in the drawer
  389. */
  390. protected void setAccountInDrawer(Account account) {
  391. if (mDrawerLayout != null && account != null) {
  392. TextView username = (TextView) findNavigationViewChildById(R.id.drawer_username);
  393. TextView usernameFull = (TextView) findNavigationViewChildById(R.id.drawer_username_full);
  394. usernameFull.setText(account.name);
  395. username.setText(AccountUtils.getUsernameOfAccount(account.name));
  396. DisplayUtils.setAvatar(account, (ImageView) findNavigationViewChildById(R.id.drawer_current_account),
  397. mCurrentAccountAvatarRadiusDimension, getResources(), getStorageManager());
  398. }
  399. }
  400. /**
  401. * fetches and sets the avatar of the current account in the drawer in case the drawer is available.
  402. *
  403. * @param account the account to be set in the drawer
  404. * @param menuItem the menuItem to set the avatar on
  405. */
  406. private void setAvatar(Account account, MenuItem menuItem) {
  407. if (mDrawerLayout != null && account != null) {
  408. // Thumbnail in Cache?
  409. Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache("a_" + account.name);
  410. if (thumbnail != null) {
  411. menuItem.setIcon(
  412. BitmapUtils.bitmapToCircularBitmapDrawable(MainApp.getAppContext().getResources(), thumbnail)
  413. );
  414. } else {
  415. // generate new avatar
  416. if (ThumbnailsCacheManager.cancelPotentialAvatarWork(account.name, menuItem)) {
  417. final ThumbnailsCacheManager.AvatarGenerationTask task =
  418. new ThumbnailsCacheManager.AvatarGenerationTask(
  419. menuItem, getStorageManager(), account
  420. );
  421. if (thumbnail == null) {
  422. try {
  423. menuItem.setIcon(
  424. TextDrawable.createAvatar(
  425. account.name,
  426. mMenuAccountAvatarRadiusDimension
  427. )
  428. );
  429. } catch (Exception e) {
  430. Log_OC.e(TAG, "Error calculating RGB value for active account icon.", e);
  431. menuItem.setIcon(R.drawable.ic_account_circle);
  432. }
  433. } else {
  434. final ThumbnailsCacheManager.AsyncAvatarDrawable asyncDrawable =
  435. new ThumbnailsCacheManager.AsyncAvatarDrawable(
  436. getResources(),
  437. thumbnail,
  438. task
  439. );
  440. menuItem.setIcon(
  441. BitmapUtils.bitmapToCircularBitmapDrawable(
  442. MainApp.getAppContext().getResources(), asyncDrawable.getBitmap())
  443. );
  444. }
  445. task.execute(account.name);
  446. }
  447. }
  448. }
  449. }
  450. /**
  451. * Toggle between standard menu and account list including saving the state.
  452. */
  453. private void toggleAccountList() {
  454. mIsAccountChooserActive = !mIsAccountChooserActive;
  455. showMenu();
  456. }
  457. /**
  458. * depending on the #mIsAccountChooserActive flag shows the account chooser or the standard menu.
  459. */
  460. private void showMenu() {
  461. if (mNavigationView != null) {
  462. if (mIsAccountChooserActive) {
  463. mAccountChooserToggle.setImageResource(R.drawable.ic_up);
  464. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  465. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, false);
  466. } else {
  467. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  468. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  469. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, true);
  470. }
  471. }
  472. }
  473. /**
  474. * checks/highlights the provided menu item if the drawer has been initialized and the menu item exists.
  475. *
  476. * @param menuItemId the menu item to be highlighted
  477. */
  478. protected void setDrawerMenuItemChecked(int menuItemId) {
  479. if (mNavigationView != null && mNavigationView.getMenu() != null && mNavigationView.getMenu().findItem
  480. (menuItemId) != null) {
  481. mNavigationView.getMenu().findItem(menuItemId).setChecked(true);
  482. mCheckedMenuItem = menuItemId;
  483. } else {
  484. Log_OC.w(TAG, "setDrawerMenuItemChecked has been called with invalid menu-item-ID");
  485. }
  486. }
  487. @Override
  488. protected void onCreate(Bundle savedInstanceState) {
  489. super.onCreate(savedInstanceState);
  490. if (savedInstanceState != null) {
  491. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  492. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  493. }
  494. mCurrentAccountAvatarRadiusDimension = getResources()
  495. .getDimension(R.dimen.nav_drawer_header_avatar_radius);
  496. mOtherAccountAvatarRadiusDimension = getResources()
  497. .getDimension(R.dimen.nav_drawer_header_avatar_other_accounts_radius);
  498. mMenuAccountAvatarRadiusDimension = getResources()
  499. .getDimension(R.dimen.nav_drawer_menu_avatar_radius);
  500. }
  501. @Override
  502. protected void onSaveInstanceState(Bundle outState) {
  503. super.onSaveInstanceState(outState);
  504. outState.putBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, mIsAccountChooserActive);
  505. outState.putInt(KEY_CHECKED_MENU_ITEM, mCheckedMenuItem);
  506. }
  507. @Override
  508. public void onRestoreInstanceState(Bundle savedInstanceState) {
  509. super.onRestoreInstanceState(savedInstanceState);
  510. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  511. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  512. // (re-)setup drawer state
  513. showMenu();
  514. // check/highlight the menu item if present
  515. if (mCheckedMenuItem > Menu.NONE || mCheckedMenuItem < Menu.NONE) {
  516. setDrawerMenuItemChecked(mCheckedMenuItem);
  517. }
  518. }
  519. @Override
  520. protected void onPostCreate(Bundle savedInstanceState) {
  521. super.onPostCreate(savedInstanceState);
  522. // Sync the toggle state after onRestoreInstanceState has occurred.
  523. if (mDrawerToggle != null) {
  524. mDrawerToggle.syncState();
  525. if (isDrawerOpen()) {
  526. mDrawerToggle.setDrawerIndicatorEnabled(true);
  527. }
  528. }
  529. updateAccountList();
  530. }
  531. @Override
  532. public void onConfigurationChanged(Configuration newConfig) {
  533. super.onConfigurationChanged(newConfig);
  534. if (mDrawerToggle != null) {
  535. mDrawerToggle.onConfigurationChanged(newConfig);
  536. }
  537. }
  538. @Override
  539. public void onBackPressed() {
  540. if (isDrawerOpen()) {
  541. closeDrawer();
  542. return;
  543. }
  544. super.onBackPressed();
  545. }
  546. @Override
  547. protected void onResume() {
  548. super.onResume();
  549. setDrawerMenuItemChecked(mCheckedMenuItem);
  550. }
  551. @Override
  552. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  553. super.onActivityResult(requestCode, resultCode, data);
  554. // update Account list and active account if Manage Account activity replies with
  555. // - ACCOUNT_LIST_CHANGED = true
  556. // - RESULT_OK
  557. if (requestCode == ACTION_MANAGE_ACCOUNTS
  558. && resultCode == RESULT_OK
  559. && data.getBooleanExtra(ManageAccountsActivity.KEY_ACCOUNT_LIST_CHANGED, false)) {
  560. // current account has changed
  561. if (data.getBooleanExtra(ManageAccountsActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
  562. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  563. restart();
  564. } else {
  565. updateAccountList();
  566. }
  567. }
  568. }
  569. /**
  570. * Finds a view that was identified by the id attribute from the drawer header.
  571. *
  572. * @param id the view's id
  573. * @return The view if found or <code>null</code> otherwise.
  574. */
  575. private View findNavigationViewChildById(int id) {
  576. return ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0).findViewById(id);
  577. }
  578. /**
  579. * restart helper method which is called after a changing the current account.
  580. */
  581. protected abstract void restart();
  582. @Override
  583. protected void onAccountCreationSuccessful(AccountManagerFuture<Bundle> future) {
  584. super.onAccountCreationSuccessful(future);
  585. updateAccountList();
  586. restart();
  587. }
  588. /**
  589. * populates the avatar drawer array with the first three ownCloud {@link Account}s while the first element is
  590. * always the current account.
  591. */
  592. private void populateDrawerOwnCloudAccounts() {
  593. mAvatars = new Account[3];
  594. Account[] accountsAll = AccountManager.get(this).getAccountsByType
  595. (MainApp.getAccountType());
  596. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  597. mAvatars[0] = currentAccount;
  598. int j = 0;
  599. for (int i = 1; i <= 2 && i < accountsAll.length && j < accountsAll.length; j++) {
  600. if (!currentAccount.equals(accountsAll[j])) {
  601. mAvatars[i] = accountsAll[j];
  602. i++;
  603. }
  604. }
  605. }
  606. }