ToolbarActivity.java 7.1 KB

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