DrawerActivity.java 50 KB

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