ToolbarActivity.java 9.0 KB

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