DrawerActivity.java 53 KB

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