DrawerActivity.java 57 KB

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