DrawerActivity.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package com.owncloud.android.ui.activity;
  2. import android.accounts.Account;
  3. import android.content.Intent;
  4. import android.content.res.Configuration;
  5. import android.os.Bundle;
  6. import android.support.design.widget.NavigationView;
  7. import android.support.v4.view.GravityCompat;
  8. import android.support.v4.widget.DrawerLayout;
  9. import android.support.v7.app.ActionBar;
  10. import android.support.v7.app.ActionBarDrawerToggle;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.widget.TextView;
  14. import com.owncloud.android.R;
  15. import com.owncloud.android.datamodel.FileDataStorageManager;
  16. import com.owncloud.android.datamodel.OCFile;
  17. /**
  18. * Base class to handle setup of the drawer implementation.
  19. */
  20. public abstract class DrawerActivity extends ToolbarActivity {
  21. // Navigation Drawer
  22. protected DrawerLayout mDrawerLayout;
  23. protected ActionBarDrawerToggle mDrawerToggle;
  24. /**
  25. * Initializes the drawer and its content. This method needs to be called after the content view has been set.
  26. */
  27. protected void setupDrawer() {
  28. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  29. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  30. if (navigationView != null) {
  31. setupDrawerContent(navigationView);
  32. }
  33. // TODO re-enable when "Accounts" is available in Navigation Drawer
  34. // // load Account in the Drawer Title
  35. // // User-Icon
  36. // ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon);
  37. // userIcon.setImageResource(DisplayUtils.getSeasonalIconId());
  38. //
  39. // // Username
  40. // TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username);
  41. // Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  42. //
  43. // if (account != null) {
  44. // int lastAtPos = account.name.lastIndexOf("@");
  45. // username.setText(account.name.substring(0, lastAtPos));
  46. // }
  47. /*
  48. // Display username in drawer
  49. setUsernameInDrawer(navigationDrawerLayout, AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()));
  50. // load slide menu items
  51. mDrawerTitles = getResources().getStringArray(R.array.drawer_items);
  52. // nav drawer content description from resources
  53. mDrawerContentDescriptions = getResources().
  54. getStringArray(R.array.drawer_content_descriptions);
  55. // nav drawer items
  56. mDrawerItems = new ArrayList<NavigationDrawerItem>();
  57. // adding nav drawer items to array
  58. // TODO re-enable when "Accounts" is available in Navigation Drawer
  59. // Accounts
  60. // mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0],
  61. // mDrawerContentDescriptions[0]));
  62. // All Files
  63. mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0], mDrawerContentDescriptions[0],
  64. R.drawable.ic_folder_open));
  65. // TODO Enable when "On Device" is recovered
  66. // On Device
  67. //mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2],
  68. // mDrawerContentDescriptions[2]));
  69. */
  70. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
  71. /** Called when a drawer has settled in a completely closed state. */
  72. public void onDrawerClosed(View view) {
  73. super.onDrawerClosed(view);
  74. updateActionBarTitleAndHomeButton(null);
  75. invalidateOptionsMenu();
  76. }
  77. /** Called when a drawer has settled in a completely open state. */
  78. public void onDrawerOpened(View drawerView) {
  79. super.onDrawerOpened(drawerView);
  80. getSupportActionBar().setTitle(R.string.app_name);
  81. mDrawerToggle.setDrawerIndicatorEnabled(true);
  82. invalidateOptionsMenu();
  83. }
  84. };
  85. /*
  86. // Set the list's click listener
  87. mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
  88. */
  89. // Set the drawer toggle as the DrawerListener
  90. mDrawerLayout.setDrawerListener(mDrawerToggle);
  91. mDrawerToggle.setDrawerIndicatorEnabled(false);
  92. }
  93. /**
  94. * setup drawer content, basically setting the item selected listener.
  95. *
  96. * @param navigationView the drawers navigation view
  97. */
  98. protected void setupDrawerContent(NavigationView navigationView) {
  99. navigationView.setNavigationItemSelectedListener(
  100. new NavigationView.OnNavigationItemSelectedListener() {
  101. @Override
  102. public boolean onNavigationItemSelected(MenuItem menuItem) {
  103. mDrawerLayout.closeDrawers();
  104. switch (menuItem.getItemId()) {
  105. case R.id.nav_all_files:
  106. menuItem.setChecked(true);
  107. allFilesOption();
  108. break;
  109. case R.id.nav_settings:
  110. Intent settingsIntent = new Intent(getApplicationContext(),
  111. Preferences.class);
  112. startActivity(settingsIntent);
  113. break;
  114. }
  115. return true;
  116. }
  117. });
  118. }
  119. /**
  120. * checks if the drawer exists and is opened.
  121. *
  122. * @return <code>true</code> if the drawer is open, else <code>false</code>
  123. */
  124. public boolean isDrawerOpen() {
  125. if(mDrawerLayout != null) {
  126. return mDrawerLayout.isDrawerOpen(GravityCompat.START);
  127. } else {
  128. return false;
  129. }
  130. }
  131. /**
  132. * closes the navigation drawer.
  133. */
  134. public void closeNavDrawer() {
  135. if(mDrawerLayout != null) {
  136. mDrawerLayout.closeDrawer(GravityCompat.START);
  137. }
  138. }
  139. /**
  140. * Method that gets called on drawer menu click for 'All Files'.
  141. */
  142. public abstract void allFilesOption();
  143. /**
  144. * Updates title bar and home buttons (state and icon).
  145. * <p/>
  146. * Assumes that navigation drawer is NOT visible.
  147. */
  148. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  149. super.updateActionBarTitleAndHomeButton(chosenFile);
  150. /// set home button properties
  151. mDrawerToggle.setDrawerIndicatorEnabled(isRoot(chosenFile));
  152. }
  153. /**
  154. * sets the given account name in the drawer in case the drawer is available. The account name is shortened
  155. * beginning from the @-sign in the username.
  156. *
  157. * @param accountName the account to be set in the drawer
  158. */
  159. protected void setUsernameInDrawer(String accountName) {
  160. if (mDrawerLayout != null && accountName != null) {
  161. TextView username = (TextView) ((NavigationView) findViewById(R.id.nav_view))
  162. .getHeaderView(0).findViewById(R.id.drawer_username);
  163. TextView usernameFull = (TextView) ((NavigationView) findViewById(R.id.nav_view))
  164. .getHeaderView(0).findViewById(R.id.drawer_username_full);
  165. usernameFull.setText(accountName);
  166. int lastAtPos = accountName.lastIndexOf("@");
  167. username.setText(accountName.substring(0, lastAtPos));
  168. }
  169. }
  170. @Override
  171. protected void onPostCreate(Bundle savedInstanceState) {
  172. super.onPostCreate(savedInstanceState);
  173. // Sync the toggle state after onRestoreInstanceState has occurred.
  174. if (mDrawerToggle != null) {
  175. mDrawerToggle.syncState();
  176. if (isDrawerOpen()) {
  177. getSupportActionBar().setTitle(R.string.app_name);
  178. mDrawerToggle.setDrawerIndicatorEnabled(true);
  179. }
  180. }
  181. }
  182. @Override
  183. public void onConfigurationChanged(Configuration newConfig) {
  184. super.onConfigurationChanged(newConfig);
  185. if (mDrawerToggle != null) {
  186. mDrawerToggle.onConfigurationChanged(newConfig);
  187. }
  188. }
  189. @Override
  190. public void onBackPressed() {
  191. if (isDrawerOpen()) {
  192. closeNavDrawer();
  193. return;
  194. }
  195. super.onBackPressed();
  196. }
  197. }