DrawerActivity.java 47 KB

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