ToolbarActivity.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.graphics.Bitmap;
  24. import android.graphics.PorterDuff;
  25. import android.graphics.drawable.Drawable;
  26. import android.os.Bundle;
  27. import android.view.View;
  28. import android.widget.ImageView;
  29. import android.widget.LinearLayout;
  30. import android.widget.ProgressBar;
  31. import android.widget.TextView;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.datamodel.FileDataStorageManager;
  34. import com.owncloud.android.datamodel.OCFile;
  35. import com.owncloud.android.utils.ThemeUtils;
  36. import androidx.annotation.ColorInt;
  37. import androidx.annotation.StringRes;
  38. import androidx.appcompat.app.ActionBar;
  39. import androidx.appcompat.widget.Toolbar;
  40. import androidx.core.content.ContextCompat;
  41. /**
  42. * Base class providing toolbar registration functionality, see {@link #setupToolbar()}.
  43. */
  44. public abstract class ToolbarActivity extends BaseActivity {
  45. private ProgressBar mProgressBar;
  46. private ImageView mPreviewImage;
  47. private LinearLayout mInfoBox;
  48. private TextView mInfoBoxMessage;
  49. @Override
  50. protected void onCreate(Bundle savedInstanceState) {
  51. super.onCreate(savedInstanceState);
  52. }
  53. /**
  54. * Toolbar setup that must be called in implementer's {@link #onCreate} after {@link #setContentView} if they
  55. * want to use the toolbar.
  56. */
  57. protected void setupToolbar(boolean useBackgroundImage) {
  58. int primaryColor = ThemeUtils.primaryColor(this, false);
  59. int fontColor = ThemeUtils.fontColor(this, true);
  60. Toolbar toolbar = findViewById(R.id.toolbar);
  61. setSupportActionBar(toolbar);
  62. mProgressBar = findViewById(R.id.progressBar);
  63. if (mProgressBar != null) {
  64. mProgressBar.setIndeterminateDrawable(
  65. ContextCompat.getDrawable(this, R.drawable.actionbar_progress_indeterminate_horizontal));
  66. ThemeUtils.colorToolbarProgressBar(this, primaryColor);
  67. }
  68. mInfoBox = findViewById(R.id.info_box);
  69. mInfoBoxMessage = findViewById(R.id.info_box_message);
  70. mPreviewImage = findViewById(R.id.preview_image);
  71. ThemeUtils.colorStatusBar(this, primaryColor);
  72. if (toolbar.getOverflowIcon() != null) {
  73. ThemeUtils.tintDrawable(toolbar.getOverflowIcon(), fontColor);
  74. }
  75. if (toolbar.getNavigationIcon() != null) {
  76. ThemeUtils.tintDrawable(toolbar.getNavigationIcon(), fontColor);
  77. }
  78. if (!useBackgroundImage) {
  79. toolbar.setBackgroundColor(primaryColor);
  80. }
  81. }
  82. public void setupToolbar() {
  83. setupToolbar(false);
  84. }
  85. /**
  86. * Updates title bar and home buttons (state and icon).
  87. */
  88. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  89. String title = ThemeUtils.getDefaultDisplayNameForRootFolder(this); // default
  90. boolean inRoot;
  91. // choose the appropriate title
  92. inRoot = chosenFile == null ||
  93. (chosenFile.isFolder() && chosenFile.getParentId() == FileDataStorageManager.ROOT_PARENT_ID);
  94. if (!inRoot) {
  95. title = chosenFile.getFileName();
  96. }
  97. updateActionBarTitleAndHomeButtonByString(title);
  98. }
  99. /**
  100. * Updates title bar and home buttons (state and icon).
  101. */
  102. protected void updateActionBarTitleAndHomeButtonByString(String title) {
  103. String titleToSet = getString(R.string.app_name); // default
  104. if (title != null) {
  105. titleToSet = title;
  106. }
  107. // set & color the chosen title
  108. ActionBar actionBar = getSupportActionBar();
  109. ThemeUtils.setColoredTitle(actionBar, titleToSet, this);
  110. // set home button properties
  111. if (actionBar != null) {
  112. actionBar.setDisplayHomeAsUpEnabled(true);
  113. actionBar.setDisplayShowTitleEnabled(true);
  114. }
  115. Toolbar toolbar = findViewById(R.id.toolbar);
  116. if (toolbar != null && toolbar.getNavigationIcon() != null) {
  117. ThemeUtils.tintDrawable(toolbar.getNavigationIcon(), ThemeUtils.fontColor(this));
  118. }
  119. }
  120. /**
  121. * checks if the given file is the root folder.
  122. *
  123. * @param file file to be checked if it is the root folder
  124. * @return <code>true</code> if it is <code>null</code> or the root folder, else returns <code>false</code>
  125. */
  126. public boolean isRoot(OCFile file) {
  127. return file == null || (file.isFolder() && file.getParentId() == FileDataStorageManager.ROOT_PARENT_ID);
  128. }
  129. /**
  130. * shows the toolbar's info box with the given text.
  131. *
  132. * @param text the text to be displayed
  133. */
  134. protected final void showInfoBox(@StringRes int text) {
  135. mInfoBox.setVisibility(View.VISIBLE);
  136. mInfoBoxMessage.setText(text);
  137. }
  138. /**
  139. * Hides the toolbar's info box.
  140. */
  141. protected final void hideInfoBox() {
  142. mInfoBox.setVisibility(View.GONE);
  143. }
  144. /**
  145. * Change the indeterminate mode for the toolbar's progress bar.
  146. *
  147. * @param indeterminate <code>true</code> to enable the indeterminate mode
  148. */
  149. public void setIndeterminate(boolean indeterminate) {
  150. if (mProgressBar != null) {
  151. mProgressBar.setIndeterminate(indeterminate);
  152. }
  153. }
  154. /**
  155. * Change the visibility for the toolbar's progress bar.
  156. *
  157. * @param visibility visibility of the progress bar
  158. */
  159. public void setProgressBarVisibility(int visibility) {
  160. if (mProgressBar != null) {
  161. mProgressBar.setVisibility(visibility);
  162. }
  163. }
  164. /**
  165. * Change the visibility for the toolbar's preview image.
  166. *
  167. * @param visibility visibility of the preview image
  168. */
  169. public void setPreviewImageVisibility(int visibility) {
  170. if (mPreviewImage != null) {
  171. mPreviewImage.setVisibility(visibility);
  172. }
  173. }
  174. /**
  175. * Change the bitmap for the toolbar's preview image.
  176. *
  177. * @param bitmap bitmap of the preview image
  178. */
  179. public void setPreviewImageBitmap(Bitmap bitmap) {
  180. if (mPreviewImage != null) {
  181. mPreviewImage.setImageBitmap(bitmap);
  182. }
  183. }
  184. /**
  185. * Change the drawable for the toolbar's preview image.
  186. *
  187. * @param drawable drawable of the preview image
  188. */
  189. public void setPreviewImageDrawable(Drawable drawable) {
  190. if (mPreviewImage != null) {
  191. mPreviewImage.setImageDrawable(drawable);
  192. }
  193. }
  194. /**
  195. * get the toolbar's preview image view.
  196. */
  197. public ImageView getPreviewImageView() {
  198. return mPreviewImage;
  199. }
  200. /**
  201. * Set the background to to progress bar of the toolbar. The resource should refer to
  202. * a Drawable object or 0 to remove the background.#
  203. *
  204. * @param color The identifier of the color.
  205. */
  206. public void setProgressBarBackgroundColor(@ColorInt int color) {
  207. mProgressBar.setBackgroundColor(color);
  208. mProgressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  209. }
  210. }