ToolbarActivity.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.animation.AnimatorInflater;
  24. import android.annotation.SuppressLint;
  25. import android.graphics.Bitmap;
  26. import android.graphics.PorterDuff;
  27. import android.graphics.drawable.Drawable;
  28. import android.os.Build;
  29. import android.os.Bundle;
  30. import android.view.View;
  31. import android.widget.FrameLayout;
  32. import android.widget.ImageView;
  33. import android.widget.LinearLayout;
  34. import android.widget.TextView;
  35. import com.google.android.material.appbar.AppBarLayout;
  36. import com.google.android.material.button.MaterialButton;
  37. import com.google.android.material.card.MaterialCardView;
  38. import com.google.android.material.textview.MaterialTextView;
  39. import com.owncloud.android.R;
  40. import com.owncloud.android.datamodel.FileDataStorageManager;
  41. import com.owncloud.android.datamodel.OCFile;
  42. import com.owncloud.android.utils.ThemeUtils;
  43. import androidx.annotation.StringRes;
  44. import androidx.appcompat.app.ActionBar;
  45. import androidx.appcompat.widget.Toolbar;
  46. import androidx.core.view.ViewCompat;
  47. /**
  48. * Base class providing toolbar registration functionality, see {@link #setupToolbar(boolean)}.
  49. */
  50. public abstract class ToolbarActivity extends BaseActivity {
  51. private AppBarLayout mAppBar;
  52. private RelativeLayout mDefaultToolbar;
  53. private MaterialCardView mHomeSearchToolbar;
  54. protected MaterialButton mMenuButton;
  55. protected MaterialTextView mSearchText;
  56. protected MaterialButton mSwitchAccountButton;
  57. private ImageView mPreviewImage;
  58. private FrameLayout mPreviewImageContainer;
  59. private LinearLayout mInfoBox;
  60. private TextView mInfoBoxMessage;
  61. private boolean isHomeSearchToolbarShow = false;
  62. @Override
  63. protected void onCreate(Bundle savedInstanceState) {
  64. super.onCreate(savedInstanceState);
  65. }
  66. /**
  67. * Toolbar setup that must be called in implementer's {@link #onCreate} after {@link #setContentView} if they want
  68. * to use the toolbar.
  69. */
  70. protected void setupToolbar(boolean isHomeSearchToolbarShow) {
  71. int fontColor = ThemeUtils.appBarPrimaryFontColor(this);
  72. Toolbar toolbar = findViewById(R.id.toolbar);
  73. setSupportActionBar(toolbar);
  74. mAppBar = findViewById(R.id.appbar);
  75. mDefaultToolbar = findViewById(R.id.default_toolbar);
  76. mHomeSearchToolbar = findViewById(R.id.home_toolbar);
  77. mMenuButton = findViewById(R.id.menu_button);
  78. mSearchText = findViewById(R.id.search_text);
  79. mSwitchAccountButton = findViewById(R.id.switch_account_button);
  80. this.isHomeSearchToolbarShow = isHomeSearchToolbarShow;
  81. updateActionBarTitleAndHomeButton(null);
  82. mInfoBox = findViewById(R.id.info_box);
  83. mInfoBoxMessage = findViewById(R.id.info_box_message);
  84. mPreviewImage = findViewById(R.id.preview_image);
  85. mPreviewImageContainer = findViewById(R.id.preview_image_frame);
  86. ThemeUtils.colorStatusBar(this);
  87. if (toolbar.getOverflowIcon() != null) {
  88. ThemeUtils.tintDrawable(toolbar.getOverflowIcon(), fontColor);
  89. }
  90. if (toolbar.getNavigationIcon() != null) {
  91. ThemeUtils.tintDrawable(toolbar.getNavigationIcon(), fontColor);
  92. }
  93. }
  94. public void setupToolbar() {
  95. setupToolbar(false);
  96. }
  97. /**
  98. * Updates title bar and home buttons (state and icon).
  99. */
  100. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  101. String title;
  102. boolean isRoot = isRoot(chosenFile);
  103. title = isRoot ? ThemeUtils.getDefaultDisplayNameForRootFolder(this) : chosenFile.getFileName();
  104. updateActionBarTitleAndHomeButtonByString(title);
  105. if (mAppBar != null) {
  106. showHomeSearchToolbar(title, isRoot);
  107. }
  108. }
  109. public void showSearchView() {
  110. if (isHomeSearchToolbarShow) {
  111. showHomeSearchToolbar(false);
  112. }
  113. }
  114. public void hideSearchView(OCFile chosenFile) {
  115. if (isHomeSearchToolbarShow) {
  116. showHomeSearchToolbar(isRoot(chosenFile));
  117. }
  118. }
  119. private void showHomeSearchToolbar(String title, boolean isRoot) {
  120. showHomeSearchToolbar(isHomeSearchToolbarShow && isRoot);
  121. mSearchText.setText(getString(R.string.appbar_search_in, title));
  122. }
  123. @SuppressLint("PrivateResource")
  124. private void showHomeSearchToolbar(boolean isShow) {
  125. if (isShow) {
  126. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  127. mAppBar.setStateListAnimator(AnimatorInflater.loadStateListAnimator(mAppBar.getContext(),
  128. R.animator.appbar_elevation_off));
  129. } else {
  130. ViewCompat.setElevation(mAppBar, 0);
  131. }
  132. mDefaultToolbar.setVisibility(View.GONE);
  133. mHomeSearchToolbar.setVisibility(View.VISIBLE);
  134. } else {
  135. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  136. mAppBar.setStateListAnimator(AnimatorInflater.loadStateListAnimator(mAppBar.getContext(),
  137. R.animator.appbar_elevation_on));
  138. } else {
  139. ViewCompat.setElevation(mAppBar, getResources().getDimension(R.dimen.design_appbar_elevation));
  140. }
  141. mDefaultToolbar.setVisibility(View.VISIBLE);
  142. mHomeSearchToolbar.setVisibility(View.GONE);
  143. }
  144. }
  145. /**
  146. * Updates title bar and home buttons (state and icon).
  147. */
  148. protected void updateActionBarTitleAndHomeButtonByString(String title) {
  149. String titleToSet = getString(R.string.app_name); // default
  150. if (title != null) {
  151. titleToSet = title;
  152. }
  153. // set & color the chosen title
  154. ActionBar actionBar = getSupportActionBar();
  155. ThemeUtils.setColoredTitle(actionBar, titleToSet, this);
  156. // set home button properties
  157. if (actionBar != null) {
  158. actionBar.setDisplayShowTitleEnabled(true);
  159. }
  160. }
  161. /**
  162. * checks if the given file is the root folder.
  163. *
  164. * @param file file to be checked if it is the root folder
  165. * @return <code>true</code> if it is <code>null</code> or the root folder, else returns <code>false</code>
  166. */
  167. public boolean isRoot(OCFile file) {
  168. return file == null || (file.isFolder() && file.getParentId() == FileDataStorageManager.ROOT_PARENT_ID);
  169. }
  170. /**
  171. * shows the toolbar's info box with the given text.
  172. *
  173. * @param text the text to be displayed
  174. */
  175. protected final void showInfoBox(@StringRes int text) {
  176. mInfoBox.setVisibility(View.VISIBLE);
  177. mInfoBoxMessage.setText(text);
  178. }
  179. /**
  180. * Hides the toolbar's info box.
  181. */
  182. protected final void hideInfoBox() {
  183. mInfoBox.setVisibility(View.GONE);
  184. }
  185. /**
  186. * Change the visibility for the toolbar's preview image.
  187. *
  188. * @param visibility visibility of the preview image
  189. */
  190. public void setPreviewImageVisibility(int visibility) {
  191. if (mPreviewImage != null && mPreviewImageContainer != null) {
  192. mPreviewImageContainer.setVisibility(visibility);
  193. }
  194. }
  195. /**
  196. * Change the bitmap for the toolbar's preview image.
  197. *
  198. * @param bitmap bitmap of the preview image
  199. */
  200. public void setPreviewImageBitmap(Bitmap bitmap) {
  201. if (mPreviewImage != null) {
  202. mPreviewImage.setImageBitmap(bitmap);
  203. }
  204. }
  205. /**
  206. * Change the drawable for the toolbar's preview image.
  207. *
  208. * @param drawable drawable of the preview image
  209. */
  210. public void setPreviewImageDrawable(Drawable drawable) {
  211. if (mPreviewImage != null) {
  212. mPreviewImage.setImageDrawable(drawable);
  213. }
  214. }
  215. /**
  216. * get the toolbar's preview image view.
  217. */
  218. public ImageView getPreviewImageView() {
  219. return mPreviewImage;
  220. }
  221. }