ThemeUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2017 Tobias Kaminsky
  7. * Copyright (C) 2017 Andy Scherzinger
  8. * Copyright (C) 2017 Nextcloud GmbH.
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. package com.owncloud.android.utils;
  24. import android.accounts.Account;
  25. import android.content.Context;
  26. import android.content.res.ColorStateList;
  27. import android.graphics.Color;
  28. import android.graphics.PorterDuff;
  29. import android.graphics.drawable.Drawable;
  30. import android.os.Build;
  31. import android.support.annotation.ColorInt;
  32. import android.support.annotation.DrawableRes;
  33. import android.support.design.widget.Snackbar;
  34. import android.support.v4.app.FragmentActivity;
  35. import android.support.v4.content.ContextCompat;
  36. import android.support.v4.content.res.ResourcesCompat;
  37. import android.support.v4.graphics.ColorUtils;
  38. import android.support.v4.graphics.drawable.DrawableCompat;
  39. import android.support.v4.widget.CompoundButtonCompat;
  40. import android.support.v7.app.ActionBar;
  41. import android.support.v7.widget.AppCompatCheckBox;
  42. import android.support.v7.widget.SwitchCompat;
  43. import android.text.Html;
  44. import android.text.Spanned;
  45. import android.widget.ImageButton;
  46. import android.widget.ProgressBar;
  47. import android.widget.SeekBar;
  48. import com.owncloud.android.MainApp;
  49. import com.owncloud.android.R;
  50. import com.owncloud.android.authentication.AccountUtils;
  51. import com.owncloud.android.datamodel.FileDataStorageManager;
  52. import com.owncloud.android.lib.resources.status.OCCapability;
  53. import com.owncloud.android.ui.activity.ToolbarActivity;
  54. /**
  55. * Utility class with methods for client side theming.
  56. */
  57. public class ThemeUtils {
  58. public static int primaryAccentColor() {
  59. OCCapability capability = getCapability();
  60. try {
  61. float adjust;
  62. if (darkTheme()){
  63. adjust = +0.1f;
  64. } else {
  65. adjust = -0.1f;
  66. }
  67. return adjustLightness(adjust, Color.parseColor(capability.getServerColor()));
  68. } catch (Exception e) {
  69. return MainApp.getAppContext().getResources().getColor(R.color.color_accent);
  70. }
  71. }
  72. public static int primaryDarkColor() {
  73. return primaryDarkColor(null);
  74. }
  75. public static int primaryDarkColor(Account account) {
  76. OCCapability capability = getCapability(account);
  77. try {
  78. return adjustLightness(-0.2f, Color.parseColor(capability.getServerColor()));
  79. } catch (Exception e) {
  80. return MainApp.getAppContext().getResources().getColor(R.color.primary_dark);
  81. }
  82. }
  83. public static int primaryColor() {
  84. return primaryColor(null);
  85. }
  86. public static int primaryColor(Account account) {
  87. OCCapability capability = getCapability(account);
  88. try {
  89. return Color.parseColor(capability.getServerColor());
  90. } catch (Exception e) {
  91. return MainApp.getAppContext().getResources().getColor(R.color.primary);
  92. }
  93. }
  94. public static boolean themingEnabled() {
  95. return getCapability().getServerColor() != null && !getCapability().getServerColor().isEmpty();
  96. }
  97. /**
  98. * @return int font color to use
  99. * adapted from https://github.com/nextcloud/server/blob/master/apps/theming/lib/Util.php#L90-L102
  100. */
  101. public static int fontColor() {
  102. if (darkTheme()) {
  103. return Color.WHITE;
  104. } else {
  105. return Color.BLACK;
  106. }
  107. }
  108. /**
  109. * Tests if dark color is set
  110. * @return true if dark theme -> e.g.use light font color, darker accent color
  111. */
  112. public static boolean darkTheme() {
  113. int primaryColor = primaryColor();
  114. float[] hsl = colorToHSL(primaryColor);
  115. return hsl[2] <= 0.55;
  116. }
  117. /**
  118. * Set color of title to white/black depending on background color
  119. *
  120. * @param actionBar actionBar to be used
  121. * @param title title to be shown
  122. */
  123. public static void setColoredTitle(ActionBar actionBar, String title) {
  124. if (actionBar != null) {
  125. String colorHex = colorToHexString(fontColor());
  126. actionBar.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>"));
  127. }
  128. }
  129. public static Spanned getColoredTitle(String title, int color) {
  130. String colorHex = colorToHexString(color);
  131. return Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>");
  132. }
  133. /**
  134. * Set color of title to white/black depending on background color
  135. *
  136. * @param actionBar actionBar to be used
  137. * @param titleId title to be shown
  138. */
  139. public static void setColoredTitle(ActionBar actionBar, int titleId, Context context) {
  140. String colorHex = colorToHexString(fontColor());
  141. String title = context.getString(titleId);
  142. actionBar.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>"));
  143. }
  144. public static String getDefaultDisplayNameForRootFolder() {
  145. OCCapability capability = getCapability();
  146. if (capability.getServerName() == null || capability.getServerName().isEmpty()) {
  147. return MainApp.getAppContext().getResources().getString(R.string.default_display_name_for_root_folder);
  148. } else {
  149. return capability.getServerName();
  150. }
  151. }
  152. public static int adjustLightness(float lightnessDelta, int color) {
  153. float[] hsl = colorToHSL(color);
  154. hsl[2] += lightnessDelta;
  155. return ColorUtils.HSLToColor(hsl);
  156. }
  157. private static float[] colorToHSL(int color) {
  158. float[] hsl = new float[3];
  159. ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl);
  160. return hsl;
  161. }
  162. /**
  163. * sets the tinting of the given ImageButton's icon to color_accent.
  164. *
  165. * @param imageButton the image button who's icon should be colored
  166. */
  167. public static void colorImageButton(ImageButton imageButton, @ColorInt int color) {
  168. if (imageButton != null) {
  169. imageButton.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  170. }
  171. }
  172. /**
  173. * sets the coloring of the given progress bar to color_accent.
  174. *
  175. * @param progressBar the progress bar to be colored
  176. * @param color the color to be used
  177. */
  178. public static void colorHorizontalProgressBar(ProgressBar progressBar, @ColorInt int color) {
  179. if (progressBar != null) {
  180. progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  181. progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  182. }
  183. }
  184. /**
  185. * sets the coloring of the given seek bar to color_accent.
  186. *
  187. * @param seekBar the seek bar to be colored
  188. */
  189. public static void colorHorizontalSeekBar(SeekBar seekBar) {
  190. int color = ThemeUtils.primaryAccentColor();
  191. colorHorizontalProgressBar(seekBar, color);
  192. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  193. seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  194. }
  195. }
  196. /**
  197. * set the Nextcloud standard colors for the snackbar.
  198. *
  199. * @param context the context relevant for setting the color according to the context's theme
  200. * @param snackbar the snackbar to be colored
  201. */
  202. public static void colorSnackbar(Context context, Snackbar snackbar) {
  203. // Changing action button text color
  204. snackbar.setActionTextColor(ContextCompat.getColor(context, R.color.white));
  205. }
  206. /**
  207. * Sets the color of the status bar to {@code color} on devices with OS version lollipop or higher.
  208. *
  209. * @param fragmentActivity fragment activity
  210. * @param color the color
  211. */
  212. public static void colorStatusBar(FragmentActivity fragmentActivity, @ColorInt int color) {
  213. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  214. fragmentActivity.getWindow().setStatusBarColor(color);
  215. }
  216. }
  217. /**
  218. * Sets the color of the progressbar to {@code color} within the given toolbar.
  219. *
  220. * @param activity the toolbar activity instance
  221. * @param progressBarColor the color to be used for the toolbar's progress bar
  222. */
  223. public static void colorToolbarProgressBar(FragmentActivity activity, int progressBarColor) {
  224. if (activity instanceof ToolbarActivity) {
  225. ((ToolbarActivity) activity).setProgressBarBackgroundColor(progressBarColor);
  226. }
  227. }
  228. public static void tintCheckbox(AppCompatCheckBox checkBox, int color) {
  229. CompoundButtonCompat.setButtonTintList(checkBox, new ColorStateList(
  230. new int[][]{
  231. new int[]{-android.R.attr.state_checked},
  232. new int[]{android.R.attr.state_checked},
  233. },
  234. new int[]{
  235. Color.GRAY,
  236. color
  237. }
  238. ));
  239. }
  240. public static void tintSwitch(SwitchCompat switchView, int color) {
  241. tintSwitch(switchView, color, false);
  242. }
  243. public static void tintSwitch(SwitchCompat switchView, int color, boolean colorText) {
  244. if (colorText) {
  245. switchView.setTextColor(color);
  246. }
  247. int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
  248. // setting the thumb color
  249. DrawableCompat.setTintList(switchView.getThumbDrawable(), new ColorStateList(
  250. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  251. new int[]{color, Color.WHITE}));
  252. // setting the track color
  253. DrawableCompat.setTintList(switchView.getTrackDrawable(), new ColorStateList(
  254. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  255. new int[]{trackColor, Color.parseColor("#4D000000")}));
  256. }
  257. public static Drawable tintDrawable(@DrawableRes int id, int color) {
  258. Drawable drawable = ResourcesCompat.getDrawable(MainApp.getAppContext().getResources(), id, null);
  259. return tintDrawable(drawable, color);
  260. }
  261. public static Drawable tintDrawable(Drawable drawable, int color) {
  262. if (drawable != null) {
  263. Drawable wrap = DrawableCompat.wrap(drawable);
  264. wrap.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  265. return wrap;
  266. } else {
  267. return drawable;
  268. }
  269. }
  270. public static String colorToHexString(int color) {
  271. return String.format("#%06X", 0xFFFFFF & color);
  272. }
  273. private static OCCapability getCapability() {
  274. return getCapability(null);
  275. }
  276. private static OCCapability getCapability(Account acc) {
  277. Account account;
  278. if (acc != null) {
  279. account = acc;
  280. } else {
  281. account = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
  282. }
  283. if (account != null) {
  284. Context context = MainApp.getAppContext();
  285. FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
  286. return storageManager.getCapability(account.name);
  287. } else {
  288. return new OCCapability();
  289. }
  290. }
  291. }