DrawerActivity.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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.AccountManagerCallback;
  23. import android.accounts.AccountManagerFuture;
  24. import android.accounts.OperationCanceledException;
  25. import android.content.Intent;
  26. import android.content.res.Configuration;
  27. import android.os.Bundle;
  28. import android.os.Handler;
  29. import android.support.design.widget.NavigationView;
  30. import android.support.v4.view.GravityCompat;
  31. import android.support.v4.widget.DrawerLayout;
  32. import android.support.v7.app.ActionBarDrawerToggle;
  33. import android.view.Menu;
  34. import android.view.MenuItem;
  35. import android.view.View;
  36. import android.widget.ImageView;
  37. import android.widget.TextView;
  38. import android.widget.Toast;
  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.FileDataStorageManager;
  43. import com.owncloud.android.datamodel.OCFile;
  44. import com.owncloud.android.lib.common.utils.Log_OC;
  45. import com.owncloud.android.lib.resources.status.OCCapability;
  46. import com.owncloud.android.ui.TextDrawable;
  47. import com.owncloud.android.utils.BitmapUtils;
  48. /**
  49. * Base class to handle setup of the drawer implementation.
  50. */
  51. public abstract class DrawerActivity extends ToolbarActivity {
  52. private static final String TAG = DrawerActivity.class.getSimpleName();
  53. private static final String KEY_IS_ACCOUNT_CHOOSER_ACTIVE = "IS_ACCOUNT_CHOOSER_ACTIVE";
  54. private static final int ACTION_MANAGE_ACCOUNTS = 101;
  55. /**
  56. * Reference to the drawer layout.
  57. */
  58. private DrawerLayout mDrawerLayout;
  59. /**
  60. * Reference to the drawer toggle.
  61. */
  62. private ActionBarDrawerToggle mDrawerToggle;
  63. /**
  64. * Reference to the navigation view.
  65. */
  66. private NavigationView mNavigationView;
  67. /**
  68. * Reference to the account chooser toogle.
  69. */
  70. private ImageView mAccountChooserToggle;
  71. /**
  72. * Flag to signal if the account chooser is active.
  73. */
  74. private boolean mIsAccountChooserActive;
  75. /**
  76. * Initializes the drawer and its content.
  77. * This method needs to be called after the content view has been set.
  78. */
  79. protected void setupDrawer() {
  80. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  81. mNavigationView = (NavigationView) findViewById(R.id.nav_view);
  82. if (mNavigationView != null) {
  83. setupDrawerContent(mNavigationView);
  84. mAccountChooserToggle = (ImageView) findNavigationViewChildById(R.id.drawer_account_chooser_toogle);
  85. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  86. mIsAccountChooserActive = false;
  87. findNavigationViewChildById(R.id.drawer_active_user)
  88. .setOnClickListener(new View.OnClickListener() {
  89. @Override
  90. public void onClick(View v) {
  91. toggleAccountList();
  92. }
  93. });
  94. }
  95. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
  96. /** Called when a drawer has settled in a completely closed state. */
  97. public void onDrawerClosed(View view) {
  98. super.onDrawerClosed(view);
  99. // standard behavior of drawer is to switch to the standard menu on closing
  100. if (mIsAccountChooserActive) {
  101. toggleAccountList();
  102. }
  103. updateActionBarTitleAndHomeButton(null);
  104. invalidateOptionsMenu();
  105. }
  106. /** Called when a drawer has settled in a completely open state. */
  107. public void onDrawerOpened(View drawerView) {
  108. super.onDrawerOpened(drawerView);
  109. getSupportActionBar().setTitle(R.string.app_name);
  110. mDrawerToggle.setDrawerIndicatorEnabled(true);
  111. invalidateOptionsMenu();
  112. }
  113. };
  114. // Set the drawer toggle as the DrawerListener
  115. mDrawerLayout.setDrawerListener(mDrawerToggle);
  116. mDrawerToggle.setDrawerIndicatorEnabled(false);
  117. }
  118. /**
  119. * setup drawer content, basically setting the item selected listener.
  120. *
  121. * @param navigationView the drawers navigation view
  122. */
  123. protected void setupDrawerContent(NavigationView navigationView) {
  124. navigationView.setNavigationItemSelectedListener(
  125. new NavigationView.OnNavigationItemSelectedListener() {
  126. @Override
  127. public boolean onNavigationItemSelected(MenuItem menuItem) {
  128. mDrawerLayout.closeDrawers();
  129. switch (menuItem.getItemId()) {
  130. case R.id.nav_all_files:
  131. menuItem.setChecked(true);
  132. allFilesOption();
  133. break;
  134. case R.id.nav_settings:
  135. Intent settingsIntent = new Intent(getApplicationContext(),
  136. Preferences.class);
  137. startActivity(settingsIntent);
  138. break;
  139. case R.id.drawer_menu_account_add:
  140. createAccount();
  141. break;
  142. case R.id.drawer_menu_account_manage:
  143. Intent manageAccountsIntent = new Intent(getApplicationContext(),
  144. ManageAccountsActivity.class);
  145. startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
  146. break;
  147. case Menu.NONE:
  148. // account clicked
  149. AccountUtils.setCurrentOwnCloudAccount(
  150. getApplicationContext(), menuItem.getTitle().toString());
  151. restart();
  152. default:
  153. Log_OC.i(TAG, "Unknown drawer menu item clicked: " + menuItem.getTitle());
  154. }
  155. return true;
  156. }
  157. });
  158. // handle correct state
  159. if (mIsAccountChooserActive) {
  160. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  161. } else {
  162. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  163. }
  164. }
  165. /**
  166. * checks if the drawer exists and is opened.
  167. *
  168. * @return <code>true</code> if the drawer is open, else <code>false</code>
  169. */
  170. public boolean isDrawerOpen() {
  171. return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.START);
  172. }
  173. /**
  174. * closes the drawer.
  175. */
  176. public void closeDrawer() {
  177. if (mDrawerLayout != null) {
  178. mDrawerLayout.closeDrawer(GravityCompat.START);
  179. }
  180. }
  181. /**
  182. * opens the drawer.
  183. */
  184. public void openDrawer() {
  185. if (mDrawerLayout != null) {
  186. mDrawerLayout.openDrawer(GravityCompat.START);
  187. }
  188. }
  189. /**
  190. * Enable or disable interaction with all drawers.
  191. *
  192. * @param lockMode The new lock mode for the given drawer. One of {@link DrawerLayout#LOCK_MODE_UNLOCKED},
  193. * {@link DrawerLayout#LOCK_MODE_LOCKED_CLOSED} or {@link DrawerLayout#LOCK_MODE_LOCKED_OPEN}.
  194. */
  195. public void setDrawerLockMode(int lockMode) {
  196. if (mDrawerLayout != null) {
  197. mDrawerLayout.setDrawerLockMode(lockMode);
  198. }
  199. }
  200. /**
  201. * Enable or disable the drawer indicator.
  202. *
  203. * @param enable <code>true</code> to enable, <code>false</code> to disable
  204. */
  205. public void setDrawerIndicatorEnabled(boolean enable) {
  206. if (mDrawerToggle != null) {
  207. mDrawerToggle.setDrawerIndicatorEnabled(enable);
  208. }
  209. }
  210. /**
  211. * updates the account list in the drawer.
  212. */
  213. public void updateAccountList() {
  214. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  215. if(accounts.length > 0 && mNavigationView != null) {
  216. repopulateAccountList(accounts);
  217. setUsernameInDrawer(AccountUtils.getCurrentOwnCloudAccount(this).name);
  218. }
  219. }
  220. /**
  221. * re-populates the account list.
  222. *
  223. * @param accounts list of accounts
  224. */
  225. private void repopulateAccountList(Account[] accounts) {
  226. // remove all accounts from list
  227. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_accounts);
  228. // add all accounts to list
  229. for (int i = 0; i < accounts.length; i++) {
  230. try {
  231. int[] rgb = BitmapUtils.calculateRGB(accounts[i].name);
  232. TextDrawable icon = new TextDrawable(accounts[i].name.substring(0, 1).toUpperCase()
  233. , rgb[0], rgb[1], rgb[2]);
  234. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, Menu.NONE, 1, accounts[i].name).setIcon(icon);
  235. } catch (Exception e) {
  236. Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
  237. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, Menu.NONE, 1, accounts[i].name).setIcon(R
  238. .drawable.ic_account_circle);
  239. }
  240. }
  241. // re-add add-account and manage-accounts
  242. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_add, 2,
  243. getResources().getString(R.string.prefs_add_account)).setIcon(R.drawable.ic_account_plus);
  244. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_manage, 2,
  245. getResources().getString(R.string.drawer_manage_accounts)).setIcon(R.drawable.ic_settings);
  246. // adding sets menu group back to visible, so safety check and setting invisible
  247. showMenu();
  248. }
  249. /**
  250. * Method that gets called on drawer menu click for 'All Files'.
  251. */
  252. public abstract void allFilesOption();
  253. /**
  254. * Updates title bar and home buttons (state and icon).
  255. * <p/>
  256. * Assumes that navigation drawer is NOT visible.
  257. */
  258. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  259. super.updateActionBarTitleAndHomeButton(chosenFile);
  260. /// set home button properties
  261. mDrawerToggle.setDrawerIndicatorEnabled(isRoot(chosenFile));
  262. }
  263. /**
  264. * sets the given account name in the drawer in case the drawer is available. The account name is shortened
  265. * beginning from the @-sign in the username.
  266. *
  267. * @param accountName the account to be set in the drawer
  268. */
  269. protected void setUsernameInDrawer(String accountName) {
  270. if (mDrawerLayout != null && accountName != null) {
  271. TextView username = (TextView) ((NavigationView) findViewById(R.id.nav_view))
  272. .getHeaderView(0).findViewById(R.id.drawer_username);
  273. TextView usernameFull = (TextView) ((NavigationView) findViewById(R.id.nav_view))
  274. .getHeaderView(0).findViewById(R.id.drawer_username_full);
  275. usernameFull.setText(accountName);
  276. int lastAtPos = accountName.lastIndexOf("@");
  277. username.setText(accountName.substring(0, lastAtPos));
  278. ImageView userIcon = (ImageView) ((NavigationView) findViewById(R.id.nav_view))
  279. .getHeaderView(0).findViewById(R.id.drawer_usericon);
  280. try {
  281. int[] rgb = BitmapUtils.calculateRGB(accountName);
  282. TextDrawable icon = new TextDrawable(
  283. accountName.substring(0, 1).toUpperCase(), rgb[0], rgb[1], rgb[2]);
  284. userIcon.setImageDrawable(icon);
  285. } catch (Exception e) {
  286. Log_OC.e(TAG, "Error calculating RGB value for active account icon.", e);
  287. userIcon.setImageResource(R.drawable.ic_account_circle);
  288. }
  289. }
  290. }
  291. /**
  292. * Toggle between standard menu and account list including saving the state.
  293. */
  294. private void toggleAccountList() {
  295. mIsAccountChooserActive = !mIsAccountChooserActive;
  296. showMenu();
  297. }
  298. /**
  299. * depending on the #mIsAccountChooserActive flag shows the account chooser or the standard menu.
  300. */
  301. private void showMenu() {
  302. if (mIsAccountChooserActive) {
  303. mAccountChooserToggle.setImageResource(R.drawable.ic_up);
  304. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  305. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, false);
  306. } else {
  307. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  308. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  309. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, true);
  310. }
  311. }
  312. @Override
  313. protected void onCreate(Bundle savedInstanceState) {
  314. super.onCreate(savedInstanceState);
  315. if (savedInstanceState != null) {
  316. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  317. }
  318. }
  319. @Override
  320. protected void onSaveInstanceState(Bundle outState) {
  321. super.onSaveInstanceState(outState);
  322. outState.putBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, mIsAccountChooserActive);
  323. }
  324. @Override
  325. public void onRestoreInstanceState(Bundle savedInstanceState) {
  326. super.onRestoreInstanceState(savedInstanceState);
  327. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  328. // (re-)setup drawer state
  329. showMenu();
  330. }
  331. @Override
  332. protected void onPostCreate(Bundle savedInstanceState) {
  333. super.onPostCreate(savedInstanceState);
  334. // Sync the toggle state after onRestoreInstanceState has occurred.
  335. if (mDrawerToggle != null) {
  336. mDrawerToggle.syncState();
  337. if (isDrawerOpen()) {
  338. getSupportActionBar().setTitle(R.string.app_name);
  339. mDrawerToggle.setDrawerIndicatorEnabled(true);
  340. }
  341. }
  342. updateAccountList();
  343. }
  344. @Override
  345. public void onConfigurationChanged(Configuration newConfig) {
  346. super.onConfigurationChanged(newConfig);
  347. if (mDrawerToggle != null) {
  348. mDrawerToggle.onConfigurationChanged(newConfig);
  349. }
  350. }
  351. @Override
  352. public void onBackPressed() {
  353. if (isDrawerOpen()) {
  354. closeDrawer();
  355. return;
  356. }
  357. super.onBackPressed();
  358. }
  359. @Override
  360. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  361. super.onActivityResult(requestCode, resultCode, data);
  362. // update Account list and active account if Manage Account activity replies with
  363. // - ACCOUNT_LIST_CHANGED = true
  364. // - RESULT_OK
  365. if (requestCode == ACTION_MANAGE_ACCOUNTS
  366. && resultCode == RESULT_OK
  367. && data.getBooleanExtra(ManageAccountsActivity.KEY_ACCOUNT_LIST_CHANGED, false)) {
  368. // current account has changed
  369. if(data.getBooleanExtra(ManageAccountsActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
  370. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  371. restart();
  372. } else {
  373. updateAccountList();
  374. }
  375. }
  376. }
  377. /**
  378. * Finds a view that was identified by the id attribute from the drawer header.
  379. *
  380. * @param id the view's id
  381. * @return The view if found or <code>null</code> otherwise.
  382. */
  383. private View findNavigationViewChildById(int id) {
  384. return ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0).findViewById(id);
  385. }
  386. protected void restart() {
  387. Intent i = new Intent(this, FileDisplayActivity.class);
  388. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  389. startActivity(i);
  390. }
  391. @Override
  392. protected void onAccountCreationSuccessful(AccountManagerFuture<Bundle> future) {
  393. super.onAccountCreationSuccessful(future);
  394. updateAccountList();
  395. restart();
  396. }
  397. }