DrawerActivity.java 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. mAccountChooserToggle = (ImageView) findNavigationViewChildById(R.id.drawer_account_chooser_toogle);
  223. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  224. mIsAccountChooserActive = false;
  225. mAccountMiddleAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_middle);
  226. mAccountEndAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_end);
  227. findNavigationViewChildById(R.id.drawer_active_user)
  228. .setOnClickListener(new View.OnClickListener() {
  229. @Override
  230. public void onClick(View v) {
  231. toggleAccountList();
  232. }
  233. });
  234. }
  235. /**
  236. * setup quota elements of the drawer.
  237. */
  238. private void setupQuotaElement() {
  239. mQuotaView = (LinearLayout) findViewById(R.id.drawer_quota);
  240. mQuotaProgressBar = (ProgressBar) findViewById(R.id.drawer_quota_ProgressBar);
  241. mQuotaTextPercentage = (TextView) findViewById(R.id.drawer_quota_percentage);
  242. mQuotaTextLink = (TextView) findViewById(R.id.drawer_quota_link);
  243. DisplayUtils.colorPreLollipopHorizontalProgressBar(mQuotaProgressBar);
  244. }
  245. /**
  246. * setup drawer content, basically setting the item selected listener.
  247. *
  248. * @param navigationView the drawers navigation view
  249. */
  250. protected void setupDrawerMenu(NavigationView navigationView) {
  251. // on pre lollipop the light theme adds a black tint to icons with white coloring
  252. // ruining the generic avatars, so tinting for icons is deactivated pre lollipop
  253. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  254. navigationView.setItemIconTintList(null);
  255. }
  256. // setup actions for drawer menu items
  257. navigationView.setNavigationItemSelectedListener(
  258. new NavigationView.OnNavigationItemSelectedListener() {
  259. @Override
  260. public boolean onNavigationItemSelected(final MenuItem menuItem) {
  261. mDrawerLayout.closeDrawers();
  262. // pending runnable will be executed after the drawer has been closed
  263. pendingRunnable = new Runnable() {
  264. @Override
  265. public void run() {
  266. selectNavigationItem(menuItem);
  267. }
  268. };
  269. return true;
  270. }
  271. });
  272. // handle correct state
  273. if (mIsAccountChooserActive) {
  274. navigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, true);
  275. } else {
  276. navigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  277. }
  278. Account account = AccountUtils.
  279. getCurrentOwnCloudAccount(MainApp.getAppContext());
  280. boolean searchSupported = AccountUtils.hasSearchSupport(account);
  281. if ((getResources().getBoolean(R.bool.bottom_toolbar_enabled) || (!searchSupported)) &&
  282. (account != null)){
  283. navigationView.getMenu().removeItem(R.id.nav_photos);
  284. if (getResources().getBoolean(R.bool.bottom_toolbar_enabled)) {
  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. }
  289. if (!searchSupported) {
  290. navigationView.getMenu().removeItem(R.id.nav_videos);
  291. }
  292. }
  293. if (getResources().getBoolean(R.bool.use_home) && navigationView.getMenu().findItem(R.id.nav_all_files) !=
  294. null) {
  295. navigationView.getMenu().findItem(R.id.nav_all_files).setTitle(getResources().
  296. getString(R.string.drawer_item_home));
  297. navigationView.getMenu().findItem(R.id.nav_all_files).setIcon(R.drawable.ic_home);
  298. }
  299. if (!getResources().getBoolean(R.bool.participate_enabled)) {
  300. navigationView.getMenu().removeItem(R.id.nav_participate);
  301. }
  302. if (!getResources().getBoolean(R.bool.shared_enabled)) {
  303. navigationView.getMenu().removeItem(R.id.nav_shared);
  304. }
  305. if (!getResources().getBoolean(R.bool.contacts_backup)) {
  306. navigationView.getMenu().removeItem(R.id.nav_contacts);
  307. }
  308. if (!getResources().getBoolean(R.bool.show_drawer_logout)) {
  309. navigationView.getMenu().removeItem(R.id.nav_logout);
  310. }
  311. if (AccountUtils.hasSearchSupport(account)) {
  312. if (!getResources().getBoolean(R.bool.recently_added_enabled)) {
  313. navigationView.getMenu().removeItem(R.id.nav_recently_added);
  314. }
  315. if (!getResources().getBoolean(R.bool.recently_modified_enabled)) {
  316. navigationView.getMenu().removeItem(R.id.nav_recently_modified);
  317. }
  318. if (!getResources().getBoolean(R.bool.videos_enabled)) {
  319. navigationView.getMenu().removeItem(R.id.nav_videos);
  320. }
  321. } else if (account != null) {
  322. navigationView.getMenu().removeItem(R.id.nav_recently_added);
  323. navigationView.getMenu().removeItem(R.id.nav_recently_modified);
  324. navigationView.getMenu().removeItem(R.id.nav_videos);
  325. }
  326. }
  327. @Subscribe(threadMode = ThreadMode.MAIN)
  328. public void onMessageEvent(MenuItemClickEvent event) {
  329. unsetAllDrawerMenuItems();
  330. switch (event.menuItem.getItemId()) {
  331. case R.id.nav_bar_files:
  332. showFiles(false);
  333. break;
  334. case R.id.nav_bar_settings:
  335. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  336. startActivity(settingsIntent);
  337. break;
  338. default:
  339. break;
  340. }
  341. }
  342. @Subscribe(threadMode = ThreadMode.MAIN)
  343. public void onMessageEvent(DummyDrawerEvent event) {
  344. unsetAllDrawerMenuItems();
  345. }
  346. private void selectNavigationItem(final MenuItem menuItem) {
  347. switch (menuItem.getItemId()) {
  348. case R.id.nav_all_files:
  349. menuItem.setChecked(true);
  350. mCheckedMenuItem = menuItem.getItemId();
  351. showFiles(false);
  352. EventBus.getDefault().post(new ChangeMenuEvent());
  353. break;
  354. case R.id.nav_favorites:
  355. menuItem.setChecked(true);
  356. mCheckedMenuItem = menuItem.getItemId();
  357. switchToSearchFragment(new SearchEvent("", SearchOperation.SearchType.FAVORITE_SEARCH,
  358. SearchEvent.UnsetType.NO_UNSET), menuItem);
  359. break;
  360. case R.id.nav_photos:
  361. menuItem.setChecked(true);
  362. mCheckedMenuItem = menuItem.getItemId();
  363. switchToSearchFragment(new SearchEvent("image/%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  364. SearchEvent.UnsetType.NO_UNSET), menuItem);
  365. break;
  366. case R.id.nav_on_device:
  367. menuItem.setChecked(true);
  368. mCheckedMenuItem = menuItem.getItemId();
  369. EventBus.getDefault().post(new ChangeMenuEvent());
  370. showFiles(true);
  371. break;
  372. case R.id.nav_uploads:
  373. Intent uploadListIntent = new Intent(getApplicationContext(),
  374. UploadListActivity.class);
  375. uploadListIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  376. startActivity(uploadListIntent);
  377. break;
  378. case R.id.nav_activity:
  379. Intent activityIntent = new Intent(getApplicationContext(), ActivitiesListActivity.class);
  380. activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  381. startActivity(activityIntent);
  382. break;
  383. case R.id.nav_notifications:
  384. Intent notificationsIntent = new Intent(getApplicationContext(), NotificationsActivity.class);
  385. startActivity(notificationsIntent);
  386. break;
  387. case R.id.nav_folder_sync:
  388. Intent folderSyncIntent = new Intent(getApplicationContext(), FolderSyncActivity.class);
  389. startActivity(folderSyncIntent);
  390. break;
  391. case R.id.nav_contacts:
  392. Intent contactsIntent = new Intent(getApplicationContext(), ContactsPreferenceActivity.class);
  393. startActivity(contactsIntent);
  394. break;
  395. case R.id.nav_settings:
  396. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  397. startActivity(settingsIntent);
  398. break;
  399. case R.id.nav_participate:
  400. Intent participateIntent = new Intent(getApplicationContext(),
  401. ParticipateActivity.class);
  402. startActivity(participateIntent);
  403. break;
  404. case R.id.nav_logout:
  405. mCheckedMenuItem = -1;
  406. menuItem.setChecked(false);
  407. UserInfoActivity.openAccountRemovalConfirmationDialog(getAccount(), getFragmentManager(), true);
  408. break;
  409. case R.id.drawer_menu_account_add:
  410. createAccount(false);
  411. break;
  412. case R.id.drawer_menu_account_manage:
  413. Intent manageAccountsIntent = new Intent(getApplicationContext(),
  414. ManageAccountsActivity.class);
  415. startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
  416. break;
  417. case R.id.nav_recently_added:
  418. menuItem.setChecked(true);
  419. mCheckedMenuItem = menuItem.getItemId();
  420. switchToSearchFragment(new SearchEvent("%",SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  421. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  422. break;
  423. case R.id.nav_recently_modified:
  424. menuItem.setChecked(true);
  425. mCheckedMenuItem = menuItem.getItemId();
  426. switchToSearchFragment(new SearchEvent("", SearchOperation.SearchType.RECENTLY_MODIFIED_SEARCH,
  427. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  428. break;
  429. case R.id.nav_shared:
  430. menuItem.setChecked(true);
  431. mCheckedMenuItem = menuItem.getItemId();
  432. switchToSearchFragment(new SearchEvent("", SearchOperation.SearchType.SHARED_SEARCH,
  433. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  434. break;
  435. case R.id.nav_videos:
  436. menuItem.setChecked(true);
  437. mCheckedMenuItem = menuItem.getItemId();
  438. switchToSearchFragment(new SearchEvent("video/%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  439. SearchEvent.UnsetType.UNSET_BOTTOM_NAV_BAR), menuItem);
  440. break;
  441. case MENU_ITEM_EXTERNAL_LINK:
  442. // external link clicked
  443. externalLinkClicked(menuItem);
  444. break;
  445. case Menu.NONE:
  446. // account clicked
  447. accountClicked(menuItem.getTitle().toString());
  448. break;
  449. default:
  450. Log_OC.i(TAG, "Unknown drawer menu item clicked: " + menuItem.getTitle());
  451. }
  452. }
  453. private void switchToSearchFragment(SearchEvent event, MenuItem menuItem) {
  454. Intent recentlyAddedIntent = new Intent(getBaseContext(), FileDisplayActivity.class);
  455. recentlyAddedIntent.putExtra(OCFileListFragment.SEARCH_EVENT, Parcels.wrap(event));
  456. recentlyAddedIntent.putExtra(FileDisplayActivity.DRAWER_MENU_ID, menuItem.getItemId());
  457. recentlyAddedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  458. startActivity(recentlyAddedIntent);
  459. }
  460. /**
  461. * show the file list to the user.
  462. *
  463. * @param onDeviceOnly flag to decide if all files or only the ones on the device should be shown
  464. */
  465. public abstract void showFiles(boolean onDeviceOnly);
  466. /**
  467. * sets the new/current account and restarts. In case the given account equals the actual/current account the
  468. * call will be ignored.
  469. *
  470. * @param accountName The account name to be set
  471. */
  472. private void accountClicked(String accountName) {
  473. if (!AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()).name.equals(accountName)) {
  474. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), accountName);
  475. fetchExternalLinks(true);
  476. restart();
  477. }
  478. }
  479. private void externalLinkClicked(MenuItem menuItem){
  480. for (ExternalLink link : externalLinksProvider.getExternalLink(ExternalLinkType.LINK)) {
  481. if (menuItem.getTitle().toString().equalsIgnoreCase(link.name)) {
  482. Intent externalWebViewIntent = new Intent(getApplicationContext(),
  483. ExternalSiteWebView.class);
  484. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, link.name);
  485. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, link.url);
  486. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, true);
  487. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, menuItem.getItemId());
  488. startActivity(externalWebViewIntent);
  489. }
  490. }
  491. }
  492. /**
  493. * click method for mini avatars in drawer header.
  494. *
  495. * @param view the clicked ImageView
  496. */
  497. public void onAccountDrawerClick(View view) {
  498. accountClicked(view.getContentDescription().toString());
  499. }
  500. /**
  501. * checks if the drawer exists and is opened.
  502. *
  503. * @return <code>true</code> if the drawer is open, else <code>false</code>
  504. */
  505. public boolean isDrawerOpen() {
  506. return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.START);
  507. }
  508. /**
  509. * closes the drawer.
  510. */
  511. public void closeDrawer() {
  512. if (mDrawerLayout != null) {
  513. mDrawerLayout.closeDrawer(GravityCompat.START);
  514. }
  515. }
  516. /**
  517. * opens the drawer.
  518. */
  519. public void openDrawer() {
  520. if (mDrawerLayout != null) {
  521. mDrawerLayout.openDrawer(GravityCompat.START);
  522. updateExternalLinksInDrawer();
  523. updateQuotaLink();
  524. }
  525. }
  526. /**
  527. * Enable or disable interaction with all drawers.
  528. *
  529. * @param lockMode The new lock mode for the given drawer. One of {@link DrawerLayout#LOCK_MODE_UNLOCKED},
  530. * {@link DrawerLayout#LOCK_MODE_LOCKED_CLOSED} or {@link DrawerLayout#LOCK_MODE_LOCKED_OPEN}.
  531. */
  532. public void setDrawerLockMode(int lockMode) {
  533. if (mDrawerLayout != null) {
  534. mDrawerLayout.setDrawerLockMode(lockMode);
  535. }
  536. }
  537. /**
  538. * Enable or disable the drawer indicator.
  539. *
  540. * @param enable <code>true</code> to enable, <code>false</code> to disable
  541. */
  542. public void setDrawerIndicatorEnabled(boolean enable) {
  543. if (mDrawerToggle != null) {
  544. mDrawerToggle.setDrawerIndicatorEnabled(enable);
  545. }
  546. }
  547. /**
  548. * updates the account list in the drawer.
  549. */
  550. public void updateAccountList() {
  551. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  552. if (mNavigationView != null && mDrawerLayout != null) {
  553. if (accounts.length > 0) {
  554. repopulateAccountList(accounts);
  555. setAccountInDrawer(AccountUtils.getCurrentOwnCloudAccount(this));
  556. populateDrawerOwnCloudAccounts();
  557. // activate second/end account avatar
  558. if (mAvatars[1] != null) {
  559. DisplayUtils.setAvatar(mAvatars[1], this,
  560. mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  561. findNavigationViewChildById(R.id.drawer_account_end));
  562. mAccountEndAccountAvatar.setVisibility(View.VISIBLE);
  563. } else {
  564. mAccountEndAccountAvatar.setVisibility(View.GONE);
  565. }
  566. // activate third/middle account avatar
  567. if (mAvatars[2] != null) {
  568. DisplayUtils.setAvatar(mAvatars[2], this,
  569. mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  570. findNavigationViewChildById(R.id.drawer_account_middle));
  571. mAccountMiddleAccountAvatar.setVisibility(View.VISIBLE);
  572. } else {
  573. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  574. }
  575. } else {
  576. mAccountEndAccountAvatar.setVisibility(View.GONE);
  577. mAccountMiddleAccountAvatar.setVisibility(View.GONE);
  578. }
  579. }
  580. }
  581. /**
  582. * re-populates the account list.
  583. *
  584. * @param accounts list of accounts
  585. */
  586. private void repopulateAccountList(Account[] accounts) {
  587. // remove all accounts from list
  588. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_accounts);
  589. // add all accounts to list
  590. for (int i = 0; i < accounts.length; i++) {
  591. try {
  592. // show all accounts except the currently active one
  593. if (!getAccount().name.equals(accounts[i].name)) {
  594. MenuItem accountMenuItem = mNavigationView.getMenu().add(
  595. R.id.drawer_menu_accounts,
  596. Menu.NONE,
  597. MENU_ORDER_ACCOUNT,
  598. accounts[i].name)
  599. .setIcon(TextDrawable.createAvatar(
  600. accounts[i].name,
  601. mMenuAccountAvatarRadiusDimension)
  602. );
  603. DisplayUtils.setAvatar(accounts[i], this, mMenuAccountAvatarRadiusDimension, getResources(), getStorageManager(), accountMenuItem);
  604. }
  605. } catch (Exception e) {
  606. Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
  607. mNavigationView.getMenu().add(
  608. R.id.drawer_menu_accounts,
  609. Menu.NONE,
  610. MENU_ORDER_ACCOUNT,
  611. accounts[i].name)
  612. .setIcon(R.drawable.ic_user);
  613. }
  614. }
  615. // re-add add-account and manage-accounts
  616. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_add,
  617. MENU_ORDER_ACCOUNT_FUNCTION,
  618. getResources().getString(R.string.prefs_add_account)).setIcon(R.drawable.ic_account_plus);
  619. mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_manage,
  620. MENU_ORDER_ACCOUNT_FUNCTION,
  621. getResources().getString(R.string.drawer_manage_accounts)).setIcon(R.drawable.ic_settings);
  622. // adding sets menu group back to visible, so safety check and setting invisible
  623. showMenu();
  624. }
  625. /**
  626. * Updates title bar and home buttons (state and icon).
  627. * <p/>
  628. * Assumes that navigation drawer is NOT visible.
  629. */
  630. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  631. super.updateActionBarTitleAndHomeButton(chosenFile);
  632. /// set home button properties
  633. if (mDrawerToggle != null && chosenFile != null) {
  634. mDrawerToggle.setDrawerIndicatorEnabled(isRoot(chosenFile));
  635. } else if (mDrawerToggle != null){
  636. mDrawerToggle.setDrawerIndicatorEnabled(false);
  637. }
  638. }
  639. /**
  640. * sets the given account name in the drawer in case the drawer is available. The account name is shortened
  641. * beginning from the @-sign in the username.
  642. *
  643. * @param account the account to be set in the drawer
  644. */
  645. protected void setAccountInDrawer(Account account) {
  646. if (mDrawerLayout != null && account != null) {
  647. TextView username = (TextView) findNavigationViewChildById(R.id.drawer_username);
  648. TextView usernameFull = (TextView) findNavigationViewChildById(R.id.drawer_username_full);
  649. usernameFull.setText(account.name);
  650. try {
  651. OwnCloudAccount oca = new OwnCloudAccount(account, this);
  652. username.setText(oca.getDisplayName());
  653. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  654. Log_OC.w(TAG, "Couldn't read display name of account fallback to account name");
  655. username.setText(AccountUtils.getAccountUsername(account.name));
  656. }
  657. DisplayUtils.setAvatar(account, this,
  658. mCurrentAccountAvatarRadiusDimension, getResources(), getStorageManager(),
  659. findNavigationViewChildById(R.id.drawer_current_account));
  660. // check and show quota info if available
  661. getAndDisplayUserQuota();
  662. }
  663. }
  664. /**
  665. * Toggle between standard menu and account list including saving the state.
  666. */
  667. private void toggleAccountList() {
  668. mIsAccountChooserActive = !mIsAccountChooserActive;
  669. showMenu();
  670. }
  671. /**
  672. * depending on the #mIsAccountChooserActive flag shows the account chooser or the standard menu.
  673. */
  674. private void showMenu() {
  675. if (mNavigationView != null) {
  676. if (mIsAccountChooserActive) {
  677. mAccountChooserToggle.setImageResource(R.drawable.ic_up);
  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. mAccountChooserToggle.setImageResource(R.drawable.ic_down);
  688. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
  689. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_standard, true);
  690. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_external_links, true);
  691. mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_bottom, true);
  692. }
  693. }
  694. }
  695. /**
  696. * shows or hides the quota UI elements.
  697. *
  698. * @param showQuota show/hide quota information
  699. */
  700. private void showQuota(boolean showQuota) {
  701. if (showQuota) {
  702. mQuotaView.setVisibility(View.VISIBLE);
  703. } else {
  704. mQuotaView.setVisibility(View.GONE);
  705. }
  706. }
  707. /**
  708. * configured the quota to be displayed.
  709. *
  710. * @param usedSpace the used space
  711. * @param totalSpace the total space
  712. * @param relative the percentage of space already used
  713. */
  714. private void setQuotaInformation(long usedSpace, long totalSpace, int relative) {
  715. mQuotaProgressBar.setProgress(relative);
  716. DisplayUtils.colorHorizontalProgressBar(mQuotaProgressBar, DisplayUtils.getRelativeInfoColor(this, relative));
  717. updateQuotaLink();
  718. mQuotaTextPercentage.setText(String.format(
  719. getString(R.string.drawer_quota),
  720. DisplayUtils.bytesToHumanReadable(usedSpace),
  721. DisplayUtils.bytesToHumanReadable(totalSpace)));
  722. showQuota(true);
  723. }
  724. protected void unsetAllDrawerMenuItems() {
  725. if (mNavigationView != null && mNavigationView.getMenu() != null) {
  726. Menu menu = mNavigationView.getMenu();
  727. for (int i = 0; i < menu.size(); i++) {
  728. menu.getItem(i).setChecked(false);
  729. }
  730. }
  731. mCheckedMenuItem = Menu.NONE;
  732. }
  733. private void updateQuotaLink() {
  734. if (mQuotaTextLink != null) {
  735. if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  736. ArrayList<ExternalLink> quotas = externalLinksProvider.getExternalLink(ExternalLinkType.QUOTA);
  737. float density = getResources().getDisplayMetrics().density;
  738. final int size = Math.round(24 * density);
  739. if (quotas.size() > 0) {
  740. final ExternalLink firstQuota = quotas.get(0);
  741. mQuotaTextLink.setText(firstQuota.name);
  742. mQuotaTextLink.setClickable(true);
  743. mQuotaTextLink.setVisibility(View.VISIBLE);
  744. mQuotaTextLink.setOnClickListener(new View.OnClickListener() {
  745. @Override
  746. public void onClick(View v) {
  747. Intent externalWebViewIntent = new Intent(getApplicationContext(), ExternalSiteWebView.class);
  748. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, firstQuota.name);
  749. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, firstQuota.url);
  750. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, true);
  751. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, -1);
  752. startActivity(externalWebViewIntent);
  753. }
  754. });
  755. SimpleTarget target = new SimpleTarget<Drawable>() {
  756. @Override
  757. public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
  758. Drawable test = resource.getCurrent();
  759. test.setBounds(0, 0, size, size);
  760. mQuotaTextLink.setCompoundDrawablesWithIntrinsicBounds(test, null, null, null);
  761. }
  762. @Override
  763. public void onLoadFailed(Exception e, Drawable errorDrawable) {
  764. super.onLoadFailed(e, errorDrawable);
  765. Drawable test = errorDrawable.getCurrent();
  766. test.setBounds(0, 0, size, size);
  767. mQuotaTextLink.setCompoundDrawablesWithIntrinsicBounds(test, null, null, null);
  768. }
  769. };
  770. DisplayUtils.downloadIcon(this, firstQuota.iconUrl, target, R.drawable.ic_link_grey, size, size);
  771. } else {
  772. mQuotaTextLink.setVisibility(View.INVISIBLE);
  773. }
  774. } else {
  775. mQuotaTextLink.setVisibility(View.INVISIBLE);
  776. }
  777. }
  778. }
  779. /**
  780. * checks/highlights the provided menu item if the drawer has been initialized and the menu item exists.
  781. *
  782. * @param menuItemId the menu item to be highlighted
  783. */
  784. protected void setDrawerMenuItemChecked(int menuItemId) {
  785. if (mNavigationView != null && mNavigationView.getMenu() != null && mNavigationView.getMenu().findItem
  786. (menuItemId) != null) {
  787. mNavigationView.getMenu().findItem(menuItemId).setChecked(true);
  788. mCheckedMenuItem = menuItemId;
  789. } else {
  790. Log_OC.w(TAG, "setDrawerMenuItemChecked has been called with invalid menu-item-ID");
  791. }
  792. }
  793. /**
  794. * Retrieves and shows the user quota if available
  795. */
  796. private void getAndDisplayUserQuota() {
  797. // set user space information
  798. Thread t = new Thread(new Runnable() {
  799. public void run() {
  800. RemoteOperation getQuotaInfoOperation = new GetRemoteUserInfoOperation();
  801. RemoteOperationResult result = getQuotaInfoOperation.execute(
  802. AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this), DrawerActivity.this);
  803. if (result.isSuccess() && result.getData() != null) {
  804. final UserInfo userInfo = (UserInfo) result.getData().get(0);
  805. final Quota quota = userInfo.getQuota();
  806. if (quota != null) {
  807. final long used = quota.getUsed();
  808. final long total = quota.getTotal();
  809. final int relative = (int) Math.ceil(quota.getRelative());
  810. final long quotaValue = quota.getQuota();
  811. runOnUiThread(new Runnable() {
  812. @Override
  813. public void run() {
  814. if (quotaValue > 0
  815. || quotaValue == GetRemoteUserInfoOperation.QUOTA_LIMIT_INFO_NOT_AVAILABLE) {
  816. /**
  817. * show quota in case
  818. * it is available and calculated (> 0) or
  819. * in case of legacy servers (==QUOTA_LIMIT_INFO_NOT_AVAILABLE)
  820. */
  821. setQuotaInformation(used, total, relative);
  822. } else {
  823. /**
  824. * quotaValue < 0 means special cases like
  825. * {@link RemoteGetUserQuotaOperation.SPACE_NOT_COMPUTED},
  826. * {@link RemoteGetUserQuotaOperation.SPACE_UNKNOWN} or
  827. * {@link RemoteGetUserQuotaOperation.SPACE_UNLIMITED}
  828. * thus don't display any quota information.
  829. */
  830. showQuota(false);
  831. }
  832. }
  833. });
  834. }
  835. }
  836. }
  837. });
  838. t.start();
  839. }
  840. public void updateExternalLinksInDrawer() {
  841. if (mNavigationView != null && getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  842. mNavigationView.getMenu().removeGroup(R.id.drawer_menu_external_links);
  843. float density = getResources().getDisplayMetrics().density;
  844. final int size = Math.round(24 * density);
  845. for (final ExternalLink link : externalLinksProvider.getExternalLink(ExternalLinkType.LINK)) {
  846. int id=mNavigationView.getMenu().add(R.id.drawer_menu_external_links, MENU_ITEM_EXTERNAL_LINK,
  847. MENU_ORDER_EXTERNAL_LINKS, link.name).setCheckable(true).getItemId();
  848. MenuSimpleTarget target = new MenuSimpleTarget<Drawable>(id) {
  849. @Override
  850. public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
  851. mNavigationView.getMenu().findItem(getIdMenuItem()).setIcon(resource);
  852. }
  853. @Override
  854. public void onLoadFailed(Exception e, Drawable errorDrawable) {
  855. super.onLoadFailed(e, errorDrawable);
  856. mNavigationView.getMenu().findItem(getIdMenuItem()).setIcon(errorDrawable.getCurrent());
  857. }
  858. };
  859. DisplayUtils.downloadIcon(this, link.iconUrl, target, R.drawable.ic_link_grey, size, size);
  860. }
  861. }
  862. }
  863. @Override
  864. protected void onCreate(Bundle savedInstanceState) {
  865. super.onCreate(savedInstanceState);
  866. if (savedInstanceState != null) {
  867. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  868. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  869. }
  870. mCurrentAccountAvatarRadiusDimension = getResources()
  871. .getDimension(R.dimen.nav_drawer_header_avatar_radius);
  872. mOtherAccountAvatarRadiusDimension = getResources()
  873. .getDimension(R.dimen.nav_drawer_header_avatar_other_accounts_radius);
  874. mMenuAccountAvatarRadiusDimension = getResources()
  875. .getDimension(R.dimen.nav_drawer_menu_avatar_radius);
  876. externalLinksProvider = new ExternalLinksProvider(MainApp.getAppContext().getContentResolver());
  877. sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  878. }
  879. @Override
  880. protected void onSaveInstanceState(Bundle outState) {
  881. super.onSaveInstanceState(outState);
  882. outState.putBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, mIsAccountChooserActive);
  883. outState.putInt(KEY_CHECKED_MENU_ITEM, mCheckedMenuItem);
  884. }
  885. @Override
  886. public void onRestoreInstanceState(Bundle savedInstanceState) {
  887. super.onRestoreInstanceState(savedInstanceState);
  888. mIsAccountChooserActive = savedInstanceState.getBoolean(KEY_IS_ACCOUNT_CHOOSER_ACTIVE, false);
  889. mCheckedMenuItem = savedInstanceState.getInt(KEY_CHECKED_MENU_ITEM, Menu.NONE);
  890. // (re-)setup drawer state
  891. showMenu();
  892. // check/highlight the menu item if present
  893. if (mCheckedMenuItem > Menu.NONE || mCheckedMenuItem < Menu.NONE) {
  894. setDrawerMenuItemChecked(mCheckedMenuItem);
  895. }
  896. }
  897. @Override
  898. protected void onPostCreate(Bundle savedInstanceState) {
  899. super.onPostCreate(savedInstanceState);
  900. // Sync the toggle state after onRestoreInstanceState has occurred.
  901. if (mDrawerToggle != null) {
  902. mDrawerToggle.syncState();
  903. if (isDrawerOpen()) {
  904. mDrawerToggle.setDrawerIndicatorEnabled(true);
  905. }
  906. }
  907. updateAccountList();
  908. updateExternalLinksInDrawer();
  909. updateQuotaLink();
  910. }
  911. @Override
  912. public void onConfigurationChanged(Configuration newConfig) {
  913. super.onConfigurationChanged(newConfig);
  914. if (mDrawerToggle != null) {
  915. mDrawerToggle.onConfigurationChanged(newConfig);
  916. }
  917. }
  918. @Override
  919. public void onBackPressed() {
  920. if (isDrawerOpen()) {
  921. closeDrawer();
  922. return;
  923. }
  924. super.onBackPressed();
  925. }
  926. @Override
  927. protected void onResume() {
  928. super.onResume();
  929. setDrawerMenuItemChecked(mCheckedMenuItem);
  930. }
  931. @Override
  932. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  933. super.onActivityResult(requestCode, resultCode, data);
  934. // update Account list and active account if Manage Account activity replies with
  935. // - ACCOUNT_LIST_CHANGED = true
  936. // - RESULT_OK
  937. if (requestCode == ACTION_MANAGE_ACCOUNTS
  938. && resultCode == RESULT_OK
  939. && data.getBooleanExtra(ManageAccountsActivity.KEY_ACCOUNT_LIST_CHANGED, false)) {
  940. // current account has changed
  941. if (data.getBooleanExtra(ManageAccountsActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
  942. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  943. restart();
  944. } else {
  945. updateAccountList();
  946. }
  947. }
  948. }
  949. /**
  950. * Finds a view that was identified by the id attribute from the drawer header.
  951. *
  952. * @param id the view's id
  953. * @return The view if found or <code>null</code> otherwise.
  954. */
  955. private View findNavigationViewChildById(int id) {
  956. return ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0).findViewById(id);
  957. }
  958. /**
  959. * restart helper method which is called after a changing the current account.
  960. */
  961. protected abstract void restart();
  962. @Override
  963. protected void onAccountCreationSuccessful(AccountManagerFuture<Bundle> future) {
  964. super.onAccountCreationSuccessful(future);
  965. updateAccountList();
  966. restart();
  967. }
  968. /**
  969. * populates the avatar drawer array with the first three ownCloud {@link Account}s while the first element is
  970. * always the current account.
  971. */
  972. private void populateDrawerOwnCloudAccounts() {
  973. mAvatars = new Account[3];
  974. Account[] accountsAll = AccountManager.get(this).getAccountsByType
  975. (MainApp.getAccountType());
  976. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  977. mAvatars[0] = currentAccount;
  978. int j = 0;
  979. for (int i = 1; i <= 2 && i < accountsAll.length && j < accountsAll.length; j++) {
  980. if (!currentAccount.equals(accountsAll[j])) {
  981. mAvatars[i] = accountsAll[j];
  982. i++;
  983. }
  984. }
  985. }
  986. @Override
  987. public void avatarGenerated(Drawable avatarDrawable, Object callContext) {
  988. if (callContext instanceof MenuItem) {
  989. MenuItem mi = (MenuItem) callContext;
  990. mi.setIcon(avatarDrawable);
  991. } else if (callContext instanceof ImageView) {
  992. ImageView iv = (ImageView) callContext;
  993. iv.setImageDrawable(avatarDrawable);
  994. }
  995. }
  996. @Override
  997. public boolean shouldCallGeneratedCallback(String tag, Object callContext) {
  998. if (callContext instanceof MenuItem) {
  999. MenuItem mi = (MenuItem) callContext;
  1000. return String.valueOf(mi.getTitle()).equals(tag);
  1001. } else if (callContext instanceof ImageView) {
  1002. ImageView iv = (ImageView) callContext;
  1003. return String.valueOf(iv.getTag()).equals(tag);
  1004. }
  1005. return false;
  1006. }
  1007. /**
  1008. * Adds other listeners to react on changes of the drawer layout.
  1009. *
  1010. * @param listener Object interested in changes of the drawer layout.
  1011. */
  1012. public void addDrawerListener(DrawerLayout.DrawerListener listener) {
  1013. if (mDrawerLayout != null) {
  1014. mDrawerLayout.addDrawerListener(listener);
  1015. } else {
  1016. Log_OC.e(TAG, "Drawer layout not ready to add drawer listener");
  1017. }
  1018. }
  1019. public boolean isDrawerIndicatorAvailable() {
  1020. return true;
  1021. }
  1022. @Override
  1023. protected void onStart() {
  1024. super.onStart();
  1025. EventBus.getDefault().register(this);
  1026. }
  1027. @Override
  1028. protected void onStop() {
  1029. EventBus.getDefault().unregister(this);
  1030. super.onStop();
  1031. }
  1032. /**
  1033. * Retrieves external links via api from 'external' app
  1034. */
  1035. public void fetchExternalLinks(final boolean force) {
  1036. if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  1037. Thread t = new Thread(new Runnable() {
  1038. public void run() {
  1039. // fetch capabilities as early as possible
  1040. if ((getCapabilities() == null || getCapabilities().getAccountName().isEmpty())
  1041. && getStorageManager() != null) {
  1042. GetCapabilitiesOperarion getCapabilities = new GetCapabilitiesOperarion();
  1043. getCapabilities.execute(getStorageManager(), getBaseContext());
  1044. }
  1045. Account account = AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this);
  1046. if (account != null && getStorageManager() != null &&
  1047. getStorageManager().getCapability(account.name) != null &&
  1048. getStorageManager().getCapability(account.name).getExternalLinks().isTrue()) {
  1049. int count = sharedPreferences.getInt(EXTERNAL_LINKS_COUNT, -1);
  1050. if (count > 10 || count == -1 || force) {
  1051. if (force) {
  1052. Log_OC.d("ExternalLinks", "force update");
  1053. }
  1054. sharedPreferences.edit().putInt(EXTERNAL_LINKS_COUNT, 0).apply();
  1055. Log_OC.d("ExternalLinks", "update via api");
  1056. ExternalLinksProvider externalLinksProvider = new ExternalLinksProvider(getContentResolver());
  1057. RemoteOperation getExternalLinksOperation = new ExternalLinksOperation();
  1058. RemoteOperationResult result = getExternalLinksOperation.execute(account, DrawerActivity.this);
  1059. if (result.isSuccess() && result.getData() != null) {
  1060. externalLinksProvider.deleteAllExternalLinks();
  1061. ArrayList<ExternalLink> externalLinks = (ArrayList<ExternalLink>) (Object) result.getData();
  1062. for (ExternalLink link : externalLinks) {
  1063. externalLinksProvider.storeExternalLink(link);
  1064. }
  1065. }
  1066. } else {
  1067. sharedPreferences.edit().putInt(EXTERNAL_LINKS_COUNT, count + 1).apply();
  1068. }
  1069. } else {
  1070. Log_OC.d("ExternalLinks", "links disabled");
  1071. }
  1072. }
  1073. });
  1074. t.start();
  1075. }
  1076. }
  1077. }