DrawerActivity.java 54 KB

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