DrawerActivity.java 50 KB

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