DrawerActivity.java 50 KB

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