DrawerActivity.java 25 KB

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