DrawerActivity.java 54 KB

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