ToolbarActivity.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.PorterDuff;
  24. import android.os.Bundle;
  25. import android.support.annotation.ColorInt;
  26. import android.support.v4.content.ContextCompat;
  27. import android.support.v7.app.ActionBar;
  28. import android.support.v7.widget.Toolbar;
  29. import android.widget.ProgressBar;
  30. import com.owncloud.android.R;
  31. import com.owncloud.android.datamodel.FileDataStorageManager;
  32. import com.owncloud.android.datamodel.OCFile;
  33. import com.owncloud.android.utils.ThemeUtils;
  34. /**
  35. * Base class providing toolbar registration functionality, see {@link #setupToolbar()}.
  36. */
  37. public abstract class ToolbarActivity extends BaseActivity {
  38. private ProgressBar mProgressBar;
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. }
  43. /**
  44. * Toolbar setup that must be called in implementer's {@link #onCreate} after {@link #setContentView} if they
  45. * want to use the toolbar.
  46. */
  47. protected void setupToolbar(boolean useBackgroundImage) {
  48. int primaryColor = ThemeUtils.primaryColor(this);
  49. int primaryDarkColor = ThemeUtils.primaryDarkColor(this);
  50. int fontColor = ThemeUtils.fontColor(this);
  51. Toolbar toolbar = findViewById(R.id.toolbar);
  52. setSupportActionBar(toolbar);
  53. mProgressBar = findViewById(R.id.progressBar);
  54. if (mProgressBar != null) {
  55. mProgressBar.setIndeterminateDrawable(
  56. ContextCompat.getDrawable(this, R.drawable.actionbar_progress_indeterminate_horizontal));
  57. ThemeUtils.colorToolbarProgressBar(this, ThemeUtils.primaryColor(this));
  58. }
  59. ThemeUtils.colorStatusBar(this, primaryDarkColor);
  60. if (toolbar.getOverflowIcon() != null) {
  61. ThemeUtils.tintDrawable(toolbar.getOverflowIcon(), fontColor);
  62. }
  63. if (toolbar.getNavigationIcon() != null) {
  64. ThemeUtils.tintDrawable(toolbar.getNavigationIcon(), fontColor);
  65. }
  66. if (!useBackgroundImage) {
  67. toolbar.setBackgroundColor(primaryColor);
  68. }
  69. }
  70. protected void setupToolbar() {
  71. setupToolbar(false);
  72. }
  73. /**
  74. * Updates title bar and home buttons (state and icon).
  75. */
  76. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  77. String title = ThemeUtils.getDefaultDisplayNameForRootFolder(this); // default
  78. boolean inRoot;
  79. // choose the appropriate title
  80. inRoot = (
  81. chosenFile == null ||
  82. (chosenFile.isFolder() && chosenFile.getParentId() == FileDataStorageManager.ROOT_PARENT_ID)
  83. );
  84. if (!inRoot) {
  85. title = chosenFile.getFileName();
  86. }
  87. updateActionBarTitleAndHomeButtonByString(title);
  88. }
  89. /**
  90. * Updates title bar and home buttons (state and icon).
  91. */
  92. protected void updateActionBarTitleAndHomeButtonByString(String title) {
  93. String titleToSet = getString(R.string.app_name); // default
  94. if (title != null) {
  95. titleToSet = title;
  96. }
  97. // set & color the chosen title
  98. ActionBar actionBar = getSupportActionBar();
  99. ThemeUtils.setColoredTitle(actionBar, titleToSet, this);
  100. // set home button properties
  101. if (actionBar != null) {
  102. actionBar.setDisplayHomeAsUpEnabled(true);
  103. actionBar.setDisplayShowTitleEnabled(true);
  104. }
  105. Toolbar toolbar = findViewById(R.id.toolbar);
  106. if (toolbar != null && toolbar.getNavigationIcon() != null) {
  107. ThemeUtils.tintDrawable(toolbar.getNavigationIcon(), ThemeUtils.fontColor(this));
  108. }
  109. }
  110. /**
  111. * checks if the given file is the root folder.
  112. *
  113. * @param file file to be checked if it is the root folder
  114. * @return <code>true</code> if it is <code>null</code> or the root folder, else returns <code>false</code>
  115. */
  116. public boolean isRoot(OCFile file) {
  117. return file == null || (file.isFolder() && file.getParentId() == FileDataStorageManager.ROOT_PARENT_ID);
  118. }
  119. /**
  120. * Change the indeterminate mode for the toolbar's progress bar.
  121. *
  122. * @param indeterminate <code>true</code> to enable the indeterminate mode
  123. */
  124. public void setIndeterminate(boolean indeterminate) {
  125. mProgressBar.setIndeterminate(indeterminate);
  126. }
  127. /**
  128. * Set the background to to progress bar of the toolbar. The resource should refer to
  129. * a Drawable object or 0 to remove the background.#
  130. *
  131. * @param color The identifier of the color.
  132. */
  133. public void setProgressBarBackgroundColor(@ColorInt int color) {
  134. mProgressBar.setBackgroundColor(color);
  135. mProgressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  136. }
  137. }