DrawerActivity.java 49 KB

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