DrawerActivity.java 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * @author Tobias Kaminsky
  6. * Copyright (C) 2016 Andy Scherzinger
  7. * Copyright (C) 2017 Tobias Kaminsky
  8. * Copyright (C) 2016 Nextcloud
  9. * Copyright (C) 2016 ownCloud Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public
  22. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package com.owncloud.android.ui.activity;
  25. import android.accounts.Account;
  26. import android.accounts.AccountManager;
  27. import android.accounts.AccountManagerFuture;
  28. import android.content.Intent;
  29. import android.content.SharedPreferences;
  30. import android.content.res.Configuration;
  31. import android.graphics.drawable.ColorDrawable;
  32. import android.graphics.drawable.Drawable;
  33. import android.graphics.drawable.LayerDrawable;
  34. import android.os.Build;
  35. import android.os.Bundle;
  36. import android.os.Handler;
  37. import android.preference.PreferenceManager;
  38. import android.support.design.widget.NavigationView;
  39. import android.support.v4.view.GravityCompat;
  40. import android.support.v4.widget.DrawerLayout;
  41. import android.support.v7.app.ActionBarDrawerToggle;
  42. import android.text.Html;
  43. import android.text.TextUtils;
  44. import android.view.Menu;
  45. import android.view.MenuItem;
  46. import android.view.View;
  47. import android.view.ViewGroup;
  48. import android.webkit.URLUtil;
  49. import android.widget.ImageView;
  50. import android.widget.LinearLayout;
  51. import android.widget.ProgressBar;
  52. import android.widget.TextView;
  53. import com.bumptech.glide.Glide;
  54. import com.bumptech.glide.request.animation.GlideAnimation;
  55. import com.bumptech.glide.request.target.SimpleTarget;
  56. import com.owncloud.android.MainApp;
  57. import com.owncloud.android.R;
  58. import com.owncloud.android.authentication.AccountUtils;
  59. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  60. import com.owncloud.android.datamodel.ExternalLinksProvider;
  61. import com.owncloud.android.datamodel.OCFile;
  62. import com.owncloud.android.lib.common.ExternalLink;
  63. import com.owncloud.android.lib.common.ExternalLinkType;
  64. import com.owncloud.android.lib.common.OwnCloudAccount;
  65. import com.owncloud.android.lib.common.Quota;
  66. import com.owncloud.android.lib.common.UserInfo;
  67. import com.owncloud.android.lib.common.accounts.ExternalLinksOperation;
  68. import com.owncloud.android.lib.common.operations.RemoteOperation;
  69. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  70. import com.owncloud.android.lib.common.utils.Log_OC;
  71. import com.owncloud.android.lib.resources.files.SearchOperation;
  72. import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
  73. import com.owncloud.android.operations.GetCapabilitiesOperarion;
  74. import com.owncloud.android.ui.TextDrawable;
  75. import com.owncloud.android.ui.events.AccountRemovedEvent;
  76. import com.owncloud.android.ui.events.ChangeMenuEvent;
  77. import com.owncloud.android.ui.events.DummyDrawerEvent;
  78. import com.owncloud.android.ui.events.MenuItemClickEvent;
  79. import com.owncloud.android.ui.events.SearchEvent;
  80. import com.owncloud.android.ui.fragment.OCFileListFragment;
  81. import com.owncloud.android.utils.DisplayUtils;
  82. import com.owncloud.android.utils.ThemeUtils;
  83. import com.owncloud.android.utils.svg.MenuSimpleTarget;
  84. import org.greenrobot.eventbus.EventBus;
  85. import org.greenrobot.eventbus.Subscribe;
  86. import org.greenrobot.eventbus.ThreadMode;
  87. import org.parceler.Parcels;
  88. import java.util.ArrayList;
  89. /**
  90. * Base class to handle setup of the drawer implementation including user switching and avatar fetching and fallback
  91. * generation.
  92. */
  93. public abstract class DrawerActivity extends ToolbarActivity implements DisplayUtils.AvatarGenerationListener {
  94. private static final String TAG = DrawerActivity.class.getSimpleName();
  95. private static final String KEY_IS_ACCOUNT_CHOOSER_ACTIVE = "IS_ACCOUNT_CHOOSER_ACTIVE";
  96. private static final String KEY_CHECKED_MENU_ITEM = "CHECKED_MENU_ITEM";
  97. private static final String EXTERNAL_LINKS_COUNT = "EXTERNAL_LINKS_COUNT";
  98. private static final int ACTION_MANAGE_ACCOUNTS = 101;
  99. private static final int MENU_ORDER_ACCOUNT = 1;
  100. private static final int MENU_ORDER_ACCOUNT_FUNCTION = 2;
  101. private static final int MENU_ORDER_EXTERNAL_LINKS = 3;
  102. private static final int MENU_ITEM_EXTERNAL_LINK = 111;
  103. /**
  104. * menu account avatar radius.
  105. */
  106. private float mMenuAccountAvatarRadiusDimension;
  107. /**
  108. * current account avatar radius.
  109. */
  110. private float mCurrentAccountAvatarRadiusDimension;
  111. /**
  112. * other accounts avatar radius.
  113. */
  114. private float mOtherAccountAvatarRadiusDimension;
  115. /**
  116. * Reference to the drawer layout.
  117. */
  118. protected DrawerLayout mDrawerLayout;
  119. /**
  120. * Reference to the drawer toggle.
  121. */
  122. protected ActionBarDrawerToggle mDrawerToggle;
  123. /**
  124. * Reference to the navigation view.
  125. */
  126. private NavigationView mNavigationView;
  127. /**
  128. * Reference to the account chooser toggle.
  129. */
  130. private ImageView mAccountChooserToggle;
  131. /**
  132. * Reference to the middle account avatar.
  133. */
  134. private ImageView mAccountMiddleAccountAvatar;
  135. /**
  136. * Reference to the end account avatar.
  137. */
  138. private ImageView mAccountEndAccountAvatar;
  139. /**
  140. * Flag to signal if the account chooser is active.
  141. */
  142. private boolean mIsAccountChooserActive;
  143. /**
  144. * Id of the checked menu item.
  145. */
  146. private int mCheckedMenuItem = Menu.NONE;
  147. /**
  148. * accounts for the (max) three displayed accounts in the drawer header.
  149. */
  150. private Account[] mAvatars = new Account[3];
  151. /**
  152. * container layout of the quota view.
  153. */
  154. private LinearLayout mQuotaView;
  155. /**
  156. * progress bar of the quota view.
  157. */
  158. private ProgressBar mQuotaProgressBar;
  159. /**
  160. * text view of the quota view.
  161. */
  162. private TextView mQuotaTextPercentage;
  163. private TextView mQuotaTextLink;
  164. /**
  165. * runnable that will be executed after the drawer has been closed.
  166. */
  167. private Runnable pendingRunnable;
  168. private ExternalLinksProvider externalLinksProvider;
  169. private SharedPreferences sharedPreferences;
  170. private ArbitraryDataProvider arbitraryDataProvider;
  171. /**
  172. * Initializes the drawer, its content and highlights the menu item with the given id.
  173. * This method needs to be called after the content view has been set.
  174. *
  175. * @param menuItemId the menu item to be checked/highlighted
  176. */
  177. protected void setupDrawer(int menuItemId) {
  178. setupDrawer();
  179. setDrawerMenuItemChecked(menuItemId);
  180. }
  181. /**
  182. * Initializes the drawer and its content.
  183. * This method needs to be called after the content view has been set.
  184. */
  185. protected void setupDrawer() {
  186. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  187. mNavigationView = (NavigationView) findViewById(R.id.nav_view);
  188. if (mNavigationView != null) {
  189. setupDrawerHeader();
  190. setupDrawerMenu(mNavigationView);
  191. setupQuotaElement();
  192. }
  193. setupDrawerToggle();
  194. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  195. }
  196. /**
  197. * initializes and sets up the drawer toggle.
  198. */
  199. private void setupDrawerToggle() {
  200. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
  201. /** Called when a drawer has settled in a completely closed state. */
  202. public void onDrawerClosed(View view) {
  203. super.onDrawerClosed(view);
  204. // standard behavior of drawer is to switch to the standard menu on closing
  205. if (mIsAccountChooserActive) {
  206. toggleAccountList();
  207. }
  208. supportInvalidateOptionsMenu();
  209. mDrawerToggle.setDrawerIndicatorEnabled(isDrawerIndicatorAvailable());
  210. if (pendingRunnable != null) {
  211. new Handler().post(pendingRunnable);
  212. pendingRunnable = null;
  213. }
  214. closeDrawer();
  215. }
  216. /** Called when a drawer has settled in a completely open state. */
  217. public void onDrawerOpened(View drawerView) {
  218. super.onDrawerOpened(drawerView);
  219. mDrawerToggle.setDrawerIndicatorEnabled(true);
  220. supportInvalidateOptionsMenu();
  221. }
  222. };
  223. // Set the drawer toggle as the DrawerListener
  224. mDrawerLayout.addDrawerListener(mDrawerToggle);
  225. mDrawerToggle.setDrawerIndicatorEnabled(true);
  226. mDrawerToggle.getDrawerArrowDrawable().setColor(ThemeUtils.fontColor());
  227. }
  228. /**
  229. * initializes and sets up the drawer header.
  230. */
  231. private void setupDrawerHeader() {
  232. mIsAccountChooserActive = false;
  233. mAccountMiddleAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_middle);
  234. mAccountEndAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_end);
  235. mAccountChooserToggle = (ImageView) findNavigationViewChildById(R.id.drawer_account_chooser_toggle);
  236. mAccountChooserToggle.setColorFilter(ThemeUtils.fontColor());
  237. if (getResources().getBoolean(R.bool.allow_profile_click)) {
  238. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  239. findNavigationViewChildById(R.id.drawer_active_user)
  240. .setOnClickListener(new View.OnClickListener() {
  241. @Override
  242. public void onClick(View v) {
  243. toggleAccountList();
  244. }
  245. });
  246. } else {
  247. mAccountChooserToggle.setVisibility(View.GONE);
  248. }
  249. }
  250. /**
  251. * setup quota elements of the drawer.
  252. */
  253. private void setupQuotaElement() {
  254. mQuotaView = (LinearLayout) findQuotaViewById(R.id.drawer_quota);
  255. mQuotaProgressBar = (ProgressBar) findQuotaViewById(R.id.drawer_quota_ProgressBar);
  256. mQuotaTextPercentage = (TextView) findQuotaViewById(R.id.drawer_quota_percentage);
  257. mQuotaTextLink = (TextView) findQuotaViewById(R.id.drawer_quota_link);
  258. ThemeUtils.colorHorizontalProgressBar(mQuotaProgressBar, ThemeUtils.primaryAccentColor());
  259. }
  260. /**
  261. * setup drawer content, basically setting the item selected listener.
  262. *
  263. * @param navigationView the drawers navigation view
  264. */
  265. protected void setupDrawerMenu(NavigationView navigationView) {
  266. navigationView.setItemIconTintList(null);
  267. // setup actions for drawer menu items
  268. navigationView.setNavigationItemSelectedListener(
  269. new NavigationView.OnNavigationItemSelectedListener() {
  270. @Override
  271. public boolean onNavigationItemSelected(final MenuItem menuItem) {
  272. mDrawerLayout.closeDrawers();
  273. // pending runnable will be executed after the drawer has been closed
  274. pendingRunnable = new Runnable() {
  275. @Override
  276. public void run() {
  277. selectNavigationItem(menuItem);
  278. }
  279. };
  280. return true;
  281. }
  282. });
  283. // handle correct state
  284. if (mIsAccountChooserActive) {
  285. navigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  286. } else {
  287. navigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  288. }
  289. Account account = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
  290. boolean searchSupported = AccountUtils.hasSearchSupport(account);
  291. if (getResources().getBoolean(R.bool.bottom_toolbar_enabled) && account != null) {
  292. navigationView.getMenu().removeItem(R.id.nav_all_files);
  293. navigationView.getMenu().removeItem(R.id.nav_settings);
  294. navigationView.getMenu().removeItem(R.id.nav_favorites);
  295. navigationView.getMenu().removeItem(R.id.nav_photos);
  296. }
  297. if (!searchSupported && account != null) {
  298. navigationView.getMenu().removeItem(R.id.nav_photos);
  299. navigationView.getMenu().removeItem(R.id.nav_favorites);
  300. navigationView.getMenu().removeItem(R.id.nav_videos);
  301. }
  302. if (getResources().getBoolean(R.bool.use_home) && navigationView.getMenu().findItem(R.id.nav_all_files) !=
  303. null) {
  304. navigationView.getMenu().findItem(R.id.nav_all_files).setTitle(getResources().
  305. getString(R.string.drawer_item_home));
  306. navigationView.getMenu().findItem(R.id.nav_all_files).setIcon(R.drawable.ic_home);
  307. }
  308. if (!getResources().getBoolean(R.bool.participate_enabled)) {
  309. navigationView.getMenu().removeItem(R.id.nav_participate);
  310. }
  311. if (!getResources().getBoolean(R.bool.shared_enabled)) {
  312. navigationView.getMenu().removeItem(R.id.nav_shared);
  313. }
  314. if (!getResources().getBoolean(R.bool.contacts_backup)
  315. || !getResources().getBoolean(R.bool.show_drawer_contacts_backup)) {
  316. navigationView.getMenu().removeItem(R.id.nav_contacts);
  317. }
  318. if (getResources().getBoolean(R.bool.syncedFolder_light)) {
  319. navigationView.getMenu().removeItem(R.id.nav_synced_folders);
  320. }
  321. if (!getResources().getBoolean(R.bool.show_drawer_logout)) {
  322. navigationView.getMenu().removeItem(R.id.nav_logout);
  323. }
  324. if (AccountUtils.hasSearchSupport(account)) {
  325. if (!getResources().getBoolean(R.bool.recently_added_enabled)) {
  326. navigationView.getMenu().removeItem(R.id.nav_recently_added);
  327. }
  328. if (!getResources().getBoolean(R.bool.recently_modified_enabled)) {
  329. navigationView.getMenu().removeItem(R.id.nav_recently_modified);
  330. }
  331. if (!getResources().getBoolean(R.bool.videos_enabled)) {
  332. navigationView.getMenu().removeItem(R.id.nav_videos);
  333. }
  334. } else if (account != null) {
  335. navigationView.getMenu().removeItem(R.id.nav_recently_added);
  336. navigationView.getMenu().removeItem(R.id.nav_recently_modified);
  337. navigationView.getMenu().removeItem(R.id.nav_videos);
  338. }
  339. }
  340. @Subscribe(threadMode = ThreadMode.MAIN)
  341. public void onMessageEvent(MenuItemClickEvent event) {
  342. unsetAllDrawerMenuItems();
  343. switch (event.menuItem.getItemId()) {
  344. case R.id.nav_bar_files:
  345. showFiles(false);
  346. break;
  347. case R.id.nav_bar_settings:
  348. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  349. startActivity(settingsIntent);
  350. break;
  351. default:
  352. break;
  353. }
  354. }
  355. @Subscribe(threadMode = ThreadMode.MAIN)
  356. public void onMessageEvent(DummyDrawerEvent event) {
  357. unsetAllDrawerMenuItems();
  358. }
  359. private void selectNavigationItem(final MenuItem menuItem) {
  360. setDrawerMenuItemChecked(menuItem.getItemId());
  361. switch (menuItem.getItemId()) {
  362. case R.id.nav_all_files:
  363. showFiles(false);
  364. EventBus.getDefault().post(new ChangeMenuEvent());
  365. break;
  366. case R.id.nav_favorites:
  367. switchToSearchFragment(new SearchEvent("", SearchOperation.SearchType.FAVORITE_SEARCH,
  368. SearchEvent.UnsetType.NO_UNSET), menuItem);
  369. break;
  370. case R.id.nav_photos:
  371. switchToSearchFragment(new SearchEvent("image/%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  372. SearchEvent.UnsetType.NO_UNSET), menuItem);
  373. break;
  374. case R.id.nav_on_device:
  375. EventBus.getDefault().post(new ChangeMenuEvent());
  376. showFiles(true);
  377. break;
  378. case R.id.nav_uploads:
  379. Intent uploadListIntent = new Intent(getApplicationContext(), UploadListActivity.class);
  380. uploadListIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  381. startActivity(uploadListIntent);
  382. break;
  383. case R.id.nav_activity:
  384. Intent activityIntent = new Intent(getApplicationContext(), ActivitiesListActivity.class);
  385. activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  386. startActivity(activityIntent);
  387. break;
  388. case R.id.nav_notifications:
  389. Intent notificationsIntent = new Intent(getApplicationContext(), NotificationsActivity.class);
  390. startActivity(notificationsIntent);
  391. break;
  392. case R.id.nav_synced_folders:
  393. Intent syncedFoldersIntent = new Intent(getApplicationContext(), SyncedFoldersActivity.class);
  394. startActivity(syncedFoldersIntent);
  395. break;
  396. case R.id.nav_contacts:
  397. Intent contactsIntent = new Intent(getApplicationContext(), ContactsPreferenceActivity.class);
  398. startActivity(contactsIntent);
  399. break;
  400. case R.id.nav_settings:
  401. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  402. startActivity(settingsIntent);
  403. break;
  404. case R.id.nav_participate:
  405. Intent participateIntent = new Intent(getApplicationContext(), ParticipateActivity.class);
  406. startActivity(participateIntent);
  407. break;
  408. case R.id.nav_logout:
  409. mCheckedMenuItem = -1;
  410. menuItem.setChecked(false);
  411. UserInfoActivity.openAccountRemovalConfirmationDialog(getAccount(), getFragmentManager(), true);
  412. break;
  413. case R.id.drawer_menu_account_add:
  414. createAccount(false);
  415. break;
  416. case R.id.drawer_menu_account_manage:
  417. Intent manageAccountsIntent = new Intent(getApplicationContext(), ManageAccountsActivity.class);
  418. startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
  419. break;
  420. case R.id.nav_recently_added:
  421. switchToSearchFragment(new SearchEvent("%",SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  422. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  423. break;
  424. case R.id.nav_recently_modified:
  425. switchToSearchFragment(new SearchEvent("", SearchOperation.SearchType.RECENTLY_MODIFIED_SEARCH,
  426. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  427. break;
  428. case R.id.nav_shared:
  429. switchToSearchFragment(new SearchEvent("", SearchOperation.SearchType.SHARED_SEARCH,
  430. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  431. break;
  432. case R.id.nav_videos:
  433. switchToSearchFragment(new SearchEvent("video/%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  434. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  435. break;
  436. case Menu.NONE:
  437. // account clicked
  438. accountClicked(menuItem.getTitle().toString());
  439. break;
  440. default:
  441. break;
  442. }
  443. if (menuItem.getItemId() >= MENU_ITEM_EXTERNAL_LINK && menuItem.getItemId() <= MENU_ITEM_EXTERNAL_LINK + 100) {
  444. // external link clicked
  445. externalLinkClicked(menuItem);
  446. } else {
  447. Log_OC.i(TAG, "Unknown drawer menu item clicked: " + menuItem.getTitle());
  448. }
  449. }
  450. private void switchToSearchFragment(SearchEvent event, MenuItem menuItem) {
  451. Intent recentlyAddedIntent = new Intent(getBaseContext(), FileDisplayActivity.class);
  452. recentlyAddedIntent.putExtra(OCFileListFragment.SEARCH_EVENT, Parcels.wrap(event));
  453. recentlyAddedIntent.putExtra(FileDisplayActivity.DRAWER_MENU_ID, menuItem.getItemId());
  454. recentlyAddedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  455. startActivity(recentlyAddedIntent);
  456. }
  457. /**
  458. * show the file list to the user.
  459. *
  460. * @param onDeviceOnly flag to decide if all files or only the ones on the device should be shown
  461. */
  462. public abstract void showFiles(boolean onDeviceOnly);
  463. /**
  464. * sets the new/current account and restarts. In case the given account equals the actual/current account the
  465. * call will be ignored.
  466. *
  467. * @param accountName The account name to be set
  468. */
  469. private void accountClicked(String accountName) {
  470. if (!AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()).name.equals(accountName)) {
  471. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), accountName);
  472. fetchExternalLinks(true);
  473. restart();
  474. }
  475. }
  476. private void externalLinkClicked(MenuItem menuItem){
  477. for (ExternalLink link : externalLinksProvider.getExternalLink(ExternalLinkType.LINK)) {
  478. if (menuItem.getTitle().toString().equalsIgnoreCase(link.name)) {
  479. Intent externalWebViewIntent = new Intent(getApplicationContext(),
  480. ExternalSiteWebView.class);
  481. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, link.name);
  482. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, link.url);
  483. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, true);
  484. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, menuItem.getItemId());
  485. startActivity(externalWebViewIntent);
  486. }
  487. }
  488. }
  489. /**
  490. * click method for mini avatars in drawer header.
  491. *
  492. * @param view the clicked ImageView
  493. */
  494. public void onAccountDrawerClick(View view) {
  495. accountClicked(view.getContentDescription().toString());
  496. }
  497. /**
  498. * checks if the drawer exists and is opened.
  499. *
  500. * @return <code>true</code> if the drawer is open, else <code>false</code>
  501. */
  502. public boolean isDrawerOpen() {
  503. return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.START);
  504. }
  505. /**
  506. * closes the drawer.
  507. */
  508. public void closeDrawer() {
  509. if (mDrawerLayout != null) {
  510. mDrawerLayout.closeDrawer(GravityCompat.START);
  511. }
  512. }
  513. /**
  514. * opens the drawer.
  515. */
  516. public void openDrawer() {
  517. if (mDrawerLayout != null) {
  518. mDrawerLayout.openDrawer(GravityCompat.START);
  519. updateExternalLinksInDrawer();
  520. updateQuotaLink();
  521. }
  522. }
  523. /**
  524. * Enable or disable interaction with all drawers.
  525. *
  526. * @param lockMode The new lock mode for the given drawer. One of {@link DrawerLayout#LOCK_MODE_UNLOCKED},
  527. * {@link DrawerLayout#LOCK_MODE_LOCKED_CLOSED} or {@link DrawerLayout#LOCK_MODE_LOCKED_OPEN}.
  528. */
  529. public void setDrawerLockMode(int lockMode) {
  530. if (mDrawerLayout != null) {
  531. mDrawerLayout.setDrawerLockMode(lockMode);
  532. }
  533. }
  534. /**
  535. * Enable or disable the drawer indicator.
  536. *
  537. * @param enable true to enable, false to disable
  538. */
  539. public void setDrawerIndicatorEnabled(boolean enable) {
  540. if (mDrawerToggle != null) {
  541. mDrawerToggle.setDrawerIndicatorEnabled(enable);
  542. }
  543. }
  544. /**
  545. * updates the account list in the drawer.
  546. */
  547. public void updateAccountList() {
  548. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  549. ArrayList<Account> persistingAccounts = new ArrayList<>();
  550. for (Account acc: accounts) {
  551. boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(acc,
  552. ManageAccountsActivity.PENDING_FOR_REMOVAL);
  553. if (!pendingForRemoval) {
  554. persistingAccounts.add(acc);
  555. }
  556. }
  557. if (mNavigationView != null && mDrawerLayout != null) {
  558. if (persistingAccounts.size() > 0) {
  559. repopulateAccountList(persistingAccounts);
  560. setAccountInDrawer(AccountUtils.getCurrentOwnCloudAccount(this));
  561. populateDrawerOwnCloudAccounts();
  562. // activate second/end account avatar
  563. if (mAvatars[1] != null) {
  564. DisplayUtils.setAvatar(mAvatars[1], this,
  565. mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  566. findNavigationViewChildById(R.id.drawer_account_end));
  567. mAccountEndAccountAvatar.setVisibility(View.VISIBLE);
  568. } else {
  569. mAccountEndAccountAvatar.setVisibility(View.GONE);
  570. }
  571. // activate third/middle account avatar
  572. if (mAvatars[2] != null) {
  573. DisplayUtils.setAvatar(mAvatars[2], this,
  574. mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  575. findNavigationViewChildById(R.id.drawer_account_middle));
  576. mAccountMiddleAccountAvatar.setVisibility(View.VISIBLE);
  577. } else {
  578. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  579. }
  580. } else {
  581. mAccountEndAccountAvatar.setVisibility(View.GONE);
  582. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  583. }
  584. }
  585. }
  586. /**
  587. * re-populates the account list.
  588. *
  589. * @param accounts list of accounts
  590. */
  591. private void repopulateAccountList(ArrayList<Account> accounts) {
  592. // remove all accounts from list
  593. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_accounts);
  594. // add all accounts to list
  595. for (Account account: accounts) {
  596. try {
  597. // show all accounts except the currently active one and those pending for removal
  598. if (!getAccount().name.equals(account.name)) {
  599. MenuItem accountMenuItem = mNavigationView.getMenu().add(
  600. R.id.drawer_menu_accounts,
  601. Menu.NONE,
  602. MENU_ORDER_ACCOUNT,
  603. account.name)
  604. .setIcon(TextDrawable.createAvatar(account.name, mMenuAccountAvatarRadiusDimension));
  605. DisplayUtils.setAvatar(account, this, mMenuAccountAvatarRadiusDimension, getResources(),
  606. getStorageManager(), accountMenuItem);
  607. }
  608. } catch (Exception e) {
  609. Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
  610. mNavigationView.getMenu().add(
  611. R.id.drawer_menu_accounts,
  612. Menu.NONE,
  613. MENU_ORDER_ACCOUNT,
  614. account.name)
  615. .setIcon(R.drawable.ic_user);
  616. }
  617. }
  618. // re-add add-account and manage-accounts
  619. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_add,
  620. MENU_ORDER_ACCOUNT_FUNCTION,
  621. getResources().getString(R.string.prefs_add_account)).setIcon(R.drawable.ic_account_plus);
  622. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_manage,
  623. MENU_ORDER_ACCOUNT_FUNCTION,
  624. getResources().getString(R.string.drawer_manage_accounts)).setIcon(R.drawable.ic_settings);
  625. // adding sets menu group back to visible, so safety check and setting invisible
  626. showMenu();
  627. }
  628. /**
  629. * Updates title bar and home buttons (state and icon).
  630. * <p/>
  631. * Assumes that navigation drawer is NOT visible.
  632. */
  633. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  634. super.updateActionBarTitleAndHomeButton(chosenFile);
  635. /// set home button properties
  636. if (mDrawerToggle != null && chosenFile != null) {
  637. mDrawerToggle.setDrawerIndicatorEnabled(isRoot(chosenFile));
  638. } else if (mDrawerToggle != null){
  639. mDrawerToggle.setDrawerIndicatorEnabled(false);
  640. }
  641. }
  642. /**
  643. * sets the given account name in the drawer in case the drawer is available. The account name is shortened
  644. * beginning from the @-sign in the username.
  645. *
  646. * @param account the account to be set in the drawer
  647. */
  648. protected void setAccountInDrawer(Account account) {
  649. if (mDrawerLayout != null && account != null) {
  650. TextView username = (TextView) findNavigationViewChildById(R.id.drawer_username);
  651. TextView usernameFull = (TextView) findNavigationViewChildById(R.id.drawer_username_full);
  652. usernameFull.setText(account.name);
  653. usernameFull.setTextColor(ThemeUtils.fontColor());
  654. try {
  655. OwnCloudAccount oca = new OwnCloudAccount(account, this);
  656. username.setText(oca.getDisplayName());
  657. username.setTextColor(ThemeUtils.fontColor());
  658. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  659. Log_OC.w(TAG, "Couldn't read display name of account fallback to account name");
  660. username.setText(AccountUtils.getAccountUsername(account.name));
  661. }
  662. DisplayUtils.setAvatar(account, this,
  663. mCurrentAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  664. findNavigationViewChildById(R.id.drawer_current_account));
  665. // check and show quota info if available
  666. getAndDisplayUserQuota();
  667. }
  668. }
  669. /**
  670. * Toggle between standard menu and account list including saving the state.
  671. */
  672. private void toggleAccountList() {
  673. mIsAccountChooserActive = !mIsAccountChooserActive;
  674. showMenu();
  675. }
  676. /**
  677. * depending on the #mIsAccountChooserActive flag shows the account chooser or the standard menu.
  678. */
  679. private void showMenu() {
  680. if (mNavigationView != null) {
  681. if (mIsAccountChooserActive) {
  682. if (mAccountChooserToggle != null) {
  683. mAccountChooserToggle.setImageResource(R.drawable.ic_up);
  684. }
  685. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  686. if (!getResources().getBoolean(R.bool.multiaccount_support) &&
  687. mNavigationView.getMenu().findItem(R.id.drawer_menu_account_add) != null) {
  688. mNavigationView.getMenu().removeItem(R.id.drawer_menu_account_add);
  689. }
  690. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, false);
  691. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_external_links, false);
  692. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_bottom, false);
  693. } else {
  694. if (mAccountChooserToggle != null) {
  695. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  696. }
  697. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  698. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, true);
  699. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_external_links, true);
  700. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_bottom, true);
  701. }
  702. }
  703. }
  704. /**
  705. * shows or hides the quota UI elements.
  706. *
  707. * @param showQuota show/hide quota information
  708. */
  709. private void showQuota(boolean showQuota) {
  710. if (showQuota) {
  711. mQuotaView.setVisibility(View.VISIBLE);
  712. } else {
  713. mQuotaView.setVisibility(View.GONE);
  714. }
  715. }
  716. /**
  717. * configured the quota to be displayed.
  718. *
  719. * @param usedSpace the used space
  720. * @param totalSpace the total space
  721. * @param relative the percentage of space already used
  722. */
  723. private void setQuotaInformation(long usedSpace, long totalSpace, int relative) {
  724. mQuotaProgressBar.setProgress(relative);
  725. ThemeUtils.colorHorizontalProgressBar(mQuotaProgressBar, DisplayUtils.getRelativeInfoColor(this, relative));
  726. updateQuotaLink();
  727. mQuotaTextPercentage.setText(String.format(
  728. getString(R.string.drawer_quota),
  729. DisplayUtils.bytesToHumanReadable(usedSpace),
  730. DisplayUtils.bytesToHumanReadable(totalSpace)));
  731. showQuota(true);
  732. }
  733. protected void unsetAllDrawerMenuItems() {
  734. if (mNavigationView != null && mNavigationView.getMenu() != null) {
  735. Menu menu = mNavigationView.getMenu();
  736. for (int i = 0; i < menu.size(); i++) {
  737. menu.getItem(i).setChecked(false);
  738. }
  739. }
  740. mCheckedMenuItem = Menu.NONE;
  741. }
  742. private void updateQuotaLink() {
  743. if (mQuotaTextLink != null) {
  744. if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  745. ArrayList<ExternalLink> quotas = externalLinksProvider.getExternalLink(ExternalLinkType.QUOTA);
  746. float density = getResources().getDisplayMetrics().density;
  747. final int size = Math.round(24 * density);
  748. if (quotas.size() > 0) {
  749. final ExternalLink firstQuota = quotas.get(0);
  750. mQuotaTextLink.setText(firstQuota.name);
  751. mQuotaTextLink.setClickable(true);
  752. mQuotaTextLink.setVisibility(View.VISIBLE);
  753. mQuotaTextLink.setOnClickListener(new View.OnClickListener() {
  754. @Override
  755. public void onClick(View v) {
  756. Intent externalWebViewIntent = new Intent(getApplicationContext(), ExternalSiteWebView.class);
  757. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, firstQuota.name);
  758. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, firstQuota.url);
  759. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, true);
  760. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, -1);
  761. startActivity(externalWebViewIntent);
  762. }
  763. });
  764. SimpleTarget target = new SimpleTarget<Drawable>() {
  765. @Override
  766. public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
  767. Drawable test = resource.getCurrent();
  768. test.setBounds(0, 0, size, size);
  769. mQuotaTextLink.setCompoundDrawablesWithIntrinsicBounds(test, null, null, null);
  770. }
  771. @Override
  772. public void onLoadFailed(Exception e, Drawable errorDrawable) {
  773. super.onLoadFailed(e, errorDrawable);
  774. Drawable test = errorDrawable.getCurrent();
  775. test.setBounds(0, 0, size, size);
  776. mQuotaTextLink.setCompoundDrawablesWithIntrinsicBounds(test, null, null, null);
  777. }
  778. };
  779. DisplayUtils.downloadIcon(this, firstQuota.iconUrl, target, R.drawable.ic_link_grey, size, size);
  780. } else {
  781. mQuotaTextLink.setVisibility(View.GONE);
  782. }
  783. } else {
  784. mQuotaTextLink.setVisibility(View.GONE);
  785. }
  786. }
  787. }
  788. /**
  789. * checks/highlights the provided menu item if the drawer has been initialized and the menu item exists.
  790. *
  791. * @param menuItemId the menu item to be highlighted
  792. */
  793. protected void setDrawerMenuItemChecked(int menuItemId) {
  794. if (mNavigationView != null && mNavigationView.getMenu() != null &&
  795. mNavigationView.getMenu().findItem(menuItemId) != null) {
  796. MenuItem item = mNavigationView.getMenu().findItem(menuItemId);
  797. item.setChecked(true);
  798. // reset all tinted icons
  799. for (int i = 0; i < mNavigationView.getMenu().size(); i++) {
  800. MenuItem menuItem = mNavigationView.getMenu().getItem(i);
  801. if (menuItem.getIcon() != null) {
  802. menuItem.getIcon().clearColorFilter();
  803. menuItem.setTitle(Html.fromHtml("<font color='#000000'>" + menuItem.getTitle() + "</font>"));
  804. }
  805. }
  806. int elementColor = ThemeUtils.elementColor();
  807. ThemeUtils.tintDrawable(item.getIcon(), elementColor);
  808. String colorHex = ThemeUtils.colorToHexString(elementColor);
  809. item.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + item.getTitle() + "</font>"));
  810. mCheckedMenuItem = menuItemId;
  811. } else {
  812. Log_OC.w(TAG, "setDrawerMenuItemChecked has been called with invalid menu-item-ID");
  813. }
  814. }
  815. /**
  816. * Retrieves and shows the user quota if available
  817. */
  818. private void getAndDisplayUserQuota() {
  819. // set user space information
  820. Thread t = new Thread(new Runnable() {
  821. public void run() {
  822. AccountManager mAccountMgr = AccountManager.get(MainApp.getAppContext());
  823. String userId = mAccountMgr.getUserData(AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this),
  824. com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
  825. RemoteOperation getQuotaInfoOperation;
  826. if (TextUtils.isEmpty(userId)) {
  827. getQuotaInfoOperation = new GetRemoteUserInfoOperation();
  828. } else {
  829. getQuotaInfoOperation = new GetRemoteUserInfoOperation(userId);
  830. }
  831. RemoteOperationResult result = getQuotaInfoOperation.execute(
  832. AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this), DrawerActivity.this);
  833. if (result.isSuccess() && result.getData() != null) {
  834. final UserInfo userInfo = (UserInfo) result.getData().get(0);
  835. final Quota quota = userInfo.getQuota();
  836. // Since we always call this method, might as well put it here
  837. if (userInfo.getId() != null) {
  838. mAccountMgr.setUserData(
  839. AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this),
  840. com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID,
  841. userInfo.getId()
  842. );
  843. }
  844. if (quota != null) {
  845. final long used = quota.getUsed();
  846. final long total = quota.getTotal();
  847. final int relative = (int) Math.ceil(quota.getRelative());
  848. final long quotaValue = quota.getQuota();
  849. runOnUiThread(new Runnable() {
  850. @Override
  851. public void run() {
  852. if (quotaValue > 0
  853. || quotaValue == GetRemoteUserInfoOperation.QUOTA_LIMIT_INFO_NOT_AVAILABLE) {
  854. /**
  855. * show quota in case
  856. * it is available and calculated (> 0) or
  857. * in case of legacy servers (==QUOTA_LIMIT_INFO_NOT_AVAILABLE)
  858. */
  859. setQuotaInformation(used, total, relative);
  860. } else {
  861. /**
  862. * quotaValue < 0 means special cases like
  863. * {@link RemoteGetUserQuotaOperation.SPACE_NOT_COMPUTED},
  864. * {@link RemoteGetUserQuotaOperation.SPACE_UNKNOWN} or
  865. * {@link RemoteGetUserQuotaOperation.SPACE_UNLIMITED}
  866. * thus don't display any quota information.
  867. */
  868. showQuota(false);
  869. }
  870. }
  871. });
  872. }
  873. }
  874. }
  875. });
  876. t.start();
  877. }
  878. public void updateExternalLinksInDrawer() {
  879. if (mNavigationView != null && getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  880. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_external_links);
  881. float density = getResources().getDisplayMetrics().density;
  882. final int size = Math.round(24 * density);
  883. int greyColor = getResources().getColor(R.color.standard_grey);
  884. for (final ExternalLink link : externalLinksProvider.getExternalLink(ExternalLinkType.LINK)) {
  885. int id = mNavigationView.getMenu().add(R.id.drawer_menu_external_links,
  886. MENU_ITEM_EXTERNAL_LINK + link.id, MENU_ORDER_EXTERNAL_LINKS, link.name)
  887. .setCheckable(true).getItemId();
  888. MenuSimpleTarget target = new MenuSimpleTarget<Drawable>(id) {
  889. @Override
  890. public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
  891. mNavigationView.getMenu().findItem(getIdMenuItem()).setIcon(
  892. ThemeUtils.tintDrawable(resource, greyColor));
  893. }
  894. @Override
  895. public void onLoadFailed(Exception e, Drawable errorDrawable) {
  896. super.onLoadFailed(e, errorDrawable);
  897. mNavigationView.getMenu().findItem(getIdMenuItem()).setIcon(errorDrawable.getCurrent());
  898. }
  899. };
  900. DisplayUtils.downloadIcon(this, link.iconUrl, target, R.drawable.ic_link_grey, size, size);
  901. }
  902. }
  903. }
  904. public void updateHeaderBackground() {
  905. if (getAccount() != null &&
  906. getStorageManager().getCapability(getAccount().name).getServerBackground() != null) {
  907. final ViewGroup navigationHeader = (ViewGroup) findNavigationViewChildById(R.id.drawer_header_view);
  908. if (navigationHeader != null) {
  909. String background = getStorageManager().getCapability(getAccount().name).getServerBackground();
  910. int primaryColor = ThemeUtils.primaryColor(getAccount());
  911. if (URLUtil.isValidUrl(background) || background.isEmpty()) {
  912. // background image
  913. SimpleTarget target = new SimpleTarget<Drawable>() {
  914. @Override
  915. public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
  916. Drawable[] drawables = {new ColorDrawable(primaryColor), resource};
  917. LayerDrawable layerDrawable = new LayerDrawable(drawables);
  918. setNavigationHeaderBackground(layerDrawable, navigationHeader);
  919. }
  920. @Override
  921. public void onLoadFailed(Exception e, Drawable errorDrawable) {
  922. Drawable[] drawables = {new ColorDrawable(primaryColor),
  923. getResources().getDrawable(R.drawable.background)};
  924. LayerDrawable layerDrawable = new LayerDrawable(drawables);
  925. setNavigationHeaderBackground(layerDrawable, navigationHeader);
  926. }
  927. };
  928. Glide.with(this)
  929. .load(background)
  930. .centerCrop()
  931. .placeholder(R.drawable.background)
  932. .error(R.drawable.background)
  933. .crossFade()
  934. .into(target);
  935. } else {
  936. // plain color
  937. setNavigationHeaderBackground(new ColorDrawable(primaryColor), navigationHeader);
  938. }
  939. }
  940. }
  941. }
  942. private void setNavigationHeaderBackground(Drawable drawable, ViewGroup navigationHeader) {
  943. final ImageView background = navigationHeader.findViewById(R.id.drawer_header_background);
  944. background.setImageDrawable(drawable);
  945. }
  946. @Override
  947. protected void onCreate(Bundle savedInstanceState) {
  948. super.onCreate(savedInstanceState);
  949. if (savedInstanceState != null) {
  950. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  951. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  952. }
  953. mCurrentAccountAvatarRadiusDimension = getResources()
  954. .getDimension(R.dimen.nav_drawer_header_avatar_radius);
  955. mOtherAccountAvatarRadiusDimension = getResources()
  956. .getDimension(R.dimen.nav_drawer_header_avatar_other_accounts_radius);
  957. mMenuAccountAvatarRadiusDimension = getResources()
  958. .getDimension(R.dimen.nav_drawer_menu_avatar_radius);
  959. externalLinksProvider = new ExternalLinksProvider(MainApp.getAppContext().getContentResolver());
  960. arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  961. sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  962. }
  963. @Override
  964. protected void onSaveInstanceState(Bundle outState) {
  965. super.onSaveInstanceState(outState);
  966. outState.putBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, mIsAccountChooserActive);
  967. outState.putInt(KEY_CHECKED_MENU_ITEM, mCheckedMenuItem);
  968. }
  969. @Override
  970. public void onRestoreInstanceState(Bundle savedInstanceState) {
  971. super.onRestoreInstanceState(savedInstanceState);
  972. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  973. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  974. // (re-)setup drawer state
  975. showMenu();
  976. // check/highlight the menu item if present
  977. if (mCheckedMenuItem > Menu.NONE || mCheckedMenuItem < Menu.NONE) {
  978. setDrawerMenuItemChecked(mCheckedMenuItem);
  979. }
  980. }
  981. @Override
  982. protected void onPostCreate(Bundle savedInstanceState) {
  983. super.onPostCreate(savedInstanceState);
  984. // Sync the toggle state after onRestoreInstanceState has occurred.
  985. if (mDrawerToggle != null) {
  986. mDrawerToggle.syncState();
  987. if (isDrawerOpen()) {
  988. mDrawerToggle.setDrawerIndicatorEnabled(true);
  989. }
  990. }
  991. updateAccountList();
  992. updateExternalLinksInDrawer();
  993. updateQuotaLink();
  994. updateHeaderBackground();
  995. }
  996. @Override
  997. public void onConfigurationChanged(Configuration newConfig) {
  998. super.onConfigurationChanged(newConfig);
  999. if (mDrawerToggle != null) {
  1000. mDrawerToggle.onConfigurationChanged(newConfig);
  1001. }
  1002. }
  1003. @Override
  1004. public void onBackPressed() {
  1005. if (isDrawerOpen()) {
  1006. closeDrawer();
  1007. return;
  1008. }
  1009. super.onBackPressed();
  1010. }
  1011. @Override
  1012. protected void onResume() {
  1013. super.onResume();
  1014. setDrawerMenuItemChecked(mCheckedMenuItem);
  1015. }
  1016. @Override
  1017. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  1018. super.onActivityResult(requestCode, resultCode, data);
  1019. // update Account list and active account if Manage Account activity replies with
  1020. // - ACCOUNT_LIST_CHANGED = true
  1021. // - RESULT_OK
  1022. if (requestCode == ACTION_MANAGE_ACCOUNTS && resultCode == RESULT_OK
  1023. && data.getBooleanExtra(ManageAccountsActivity.KEY_ACCOUNT_LIST_CHANGED, false)) {
  1024. // current account has changed
  1025. if (data.getBooleanExtra(ManageAccountsActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
  1026. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  1027. updateAccountList();
  1028. restart();
  1029. } else {
  1030. updateAccountList();
  1031. }
  1032. }
  1033. }
  1034. /**
  1035. * Finds a view that was identified by the id attribute from the drawer header.
  1036. *
  1037. * @param id the view's id
  1038. * @return The view if found or <code>null</code> otherwise.
  1039. */
  1040. private View findNavigationViewChildById(int id) {
  1041. NavigationView view = ((NavigationView) findViewById(R.id.nav_view));
  1042. if (view != null) {
  1043. return view.getHeaderView(0).findViewById(id);
  1044. } else {
  1045. return null;
  1046. }
  1047. }
  1048. /**
  1049. * Quota view can be either at navigation bottom or header
  1050. *
  1051. * @param id the view's id
  1052. * @return The view if found or <code>null</code> otherwise.
  1053. */
  1054. private View findQuotaViewById(int id) {
  1055. View v = ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0).findViewById(id);
  1056. if (v != null) {
  1057. return v;
  1058. } else {
  1059. return findViewById(id);
  1060. }
  1061. }
  1062. /**
  1063. * restart helper method which is called after a changing the current account.
  1064. */
  1065. protected abstract void restart();
  1066. @Override
  1067. protected void onAccountCreationSuccessful(AccountManagerFuture<Bundle> future) {
  1068. super.onAccountCreationSuccessful(future);
  1069. updateAccountList();
  1070. restart();
  1071. }
  1072. /**
  1073. * populates the avatar drawer array with the first three ownCloud {@link Account}s while the first element is
  1074. * always the current account.
  1075. */
  1076. private void populateDrawerOwnCloudAccounts() {
  1077. mAvatars = new Account[3];
  1078. Account[] accountsAll = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  1079. ArrayList<Account> persistingAccounts = new ArrayList<>();
  1080. for (Account acc: accountsAll) {
  1081. boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(acc,
  1082. ManageAccountsActivity.PENDING_FOR_REMOVAL);
  1083. if (!pendingForRemoval) {
  1084. persistingAccounts.add(acc);
  1085. }
  1086. }
  1087. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  1088. mAvatars[0] = currentAccount;
  1089. int j = 0;
  1090. for (int i = 1; i <= 2 && i < persistingAccounts.size() && j < persistingAccounts.size(); j++) {
  1091. if (!currentAccount.equals(persistingAccounts.get(j))) {
  1092. mAvatars[i] = persistingAccounts.get(j);
  1093. i++;
  1094. }
  1095. }
  1096. }
  1097. @Override
  1098. public void avatarGenerated(Drawable avatarDrawable, Object callContext) {
  1099. if (callContext instanceof MenuItem) {
  1100. MenuItem mi = (MenuItem) callContext;
  1101. mi.setIcon(avatarDrawable);
  1102. } else if (callContext instanceof ImageView) {
  1103. ImageView iv = (ImageView) callContext;
  1104. iv.setImageDrawable(avatarDrawable);
  1105. }
  1106. }
  1107. @Override
  1108. public boolean shouldCallGeneratedCallback(String tag, Object callContext) {
  1109. if (callContext instanceof MenuItem) {
  1110. MenuItem mi = (MenuItem) callContext;
  1111. return String.valueOf(mi.getTitle()).equals(tag);
  1112. } else if (callContext instanceof ImageView) {
  1113. ImageView iv = (ImageView) callContext;
  1114. return String.valueOf(iv.getTag()).equals(tag);
  1115. }
  1116. return false;
  1117. }
  1118. /**
  1119. * Adds other listeners to react on changes of the drawer layout.
  1120. *
  1121. * @param listener Object interested in changes of the drawer layout.
  1122. */
  1123. public void addDrawerListener(DrawerLayout.DrawerListener listener) {
  1124. if (mDrawerLayout != null) {
  1125. mDrawerLayout.addDrawerListener(listener);
  1126. } else {
  1127. Log_OC.e(TAG, "Drawer layout not ready to add drawer listener");
  1128. }
  1129. }
  1130. public boolean isDrawerIndicatorAvailable() {
  1131. return true;
  1132. }
  1133. @Override
  1134. protected void onStart() {
  1135. super.onStart();
  1136. EventBus.getDefault().register(this);
  1137. }
  1138. @Override
  1139. protected void onStop() {
  1140. EventBus.getDefault().unregister(this);
  1141. super.onStop();
  1142. }
  1143. @Subscribe(threadMode = ThreadMode.MAIN)
  1144. public void onAccountRemovedEvent(AccountRemovedEvent event) {
  1145. updateAccountList();
  1146. }
  1147. /**
  1148. * Retrieves external links via api from 'external' app
  1149. */
  1150. public void fetchExternalLinks(final boolean force) {
  1151. if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  1152. Thread t = new Thread(() -> {
  1153. // fetch capabilities as early as possible
  1154. if ((getCapabilities() == null || getCapabilities().getAccountName().isEmpty())
  1155. && getStorageManager() != null) {
  1156. GetCapabilitiesOperarion getCapabilities = new GetCapabilitiesOperarion();
  1157. getCapabilities.execute(getStorageManager(), getBaseContext());
  1158. }
  1159. Account account = AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this);
  1160. externalLinksProvider.deleteAllExternalLinks();
  1161. if (account != null && getStorageManager() != null &&
  1162. getStorageManager().getCapability(account.name) != null &&
  1163. getStorageManager().getCapability(account.name).getExternalLinks().isTrue()) {
  1164. int count = sharedPreferences.getInt(EXTERNAL_LINKS_COUNT, -1);
  1165. if (count > 10 || count == -1 || force) {
  1166. if (force) {
  1167. Log_OC.d("ExternalLinks", "force update");
  1168. }
  1169. sharedPreferences.edit().putInt(EXTERNAL_LINKS_COUNT, 0).apply();
  1170. Log_OC.d("ExternalLinks", "update via api");
  1171. RemoteOperation getExternalLinksOperation = new ExternalLinksOperation();
  1172. RemoteOperationResult result = getExternalLinksOperation.execute(account, DrawerActivity.this);
  1173. if (result.isSuccess() && result.getData() != null) {
  1174. externalLinksProvider.deleteAllExternalLinks();
  1175. ArrayList<ExternalLink> externalLinks = (ArrayList<ExternalLink>) (Object) result.getData();
  1176. for (ExternalLink link : externalLinks) {
  1177. externalLinksProvider.storeExternalLink(link);
  1178. }
  1179. }
  1180. } else {
  1181. sharedPreferences.edit().putInt(EXTERNAL_LINKS_COUNT, count + 1).apply();
  1182. }
  1183. } else {
  1184. Log_OC.d("ExternalLinks", "links disabled");
  1185. }
  1186. runOnUiThread(() -> updateExternalLinksInDrawer());
  1187. });
  1188. t.start();
  1189. }
  1190. }
  1191. }