ThemeUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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] / 100) <= 0.5;
  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. String colorHex = colorToHexString(fontColor());
  125. actionBar.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>"));
  126. }
  127. public static Spanned getColoredTitle(String title, int color) {
  128. String colorHex = colorToHexString(color);
  129. return Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>");
  130. }
  131. /**
  132. * Set color of title to white/black depending on background color
  133. *
  134. * @param actionBar actionBar to be used
  135. * @param titleId title to be shown
  136. */
  137. public static void setColoredTitle(ActionBar actionBar, int titleId, Context context) {
  138. String colorHex = colorToHexString(fontColor());
  139. String title = context.getString(titleId);
  140. actionBar.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>"));
  141. }
  142. public static String getDefaultDisplayNameForRootFolder() {
  143. OCCapability capability = getCapability();
  144. if (capability.getServerName() == null || capability.getServerName().isEmpty()) {
  145. return MainApp.getAppContext().getResources().getString(R.string.default_display_name_for_root_folder);
  146. } else {
  147. return capability.getServerName();
  148. }
  149. }
  150. public static int adjustLightness(float lightnessDelta, int color) {
  151. float[] hsl = colorToHSL(color);
  152. hsl[2] += lightnessDelta;
  153. return ColorUtils.HSLToColor(hsl);
  154. }
  155. private static float[] colorToHSL(int color) {
  156. float[] hsl = new float[3];
  157. ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl);
  158. return hsl;
  159. }
  160. /**
  161. * sets the tinting of the given ImageButton's icon to color_accent.
  162. *
  163. * @param imageButton the image button who's icon should be colored
  164. */
  165. public static void colorImageButton(ImageButton imageButton, @ColorInt int color) {
  166. if (imageButton != null) {
  167. imageButton.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  168. }
  169. }
  170. /**
  171. * sets the coloring of the given progress bar to color_accent.
  172. *
  173. * @param progressBar the progress bar to be colored
  174. * @param color the color to be used
  175. */
  176. public static void colorHorizontalProgressBar(ProgressBar progressBar, @ColorInt int color) {
  177. if (progressBar != null) {
  178. progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  179. progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  180. }
  181. }
  182. /**
  183. * sets the coloring of the given seek bar to color_accent.
  184. *
  185. * @param seekBar the seek bar to be colored
  186. */
  187. public static void colorHorizontalSeekBar(SeekBar seekBar) {
  188. int color = ThemeUtils.primaryAccentColor();
  189. colorHorizontalProgressBar(seekBar, color);
  190. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  191. seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  192. seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  193. }
  194. }
  195. /**
  196. * set the Nextcloud standard colors for the snackbar.
  197. *
  198. * @param context the context relevant for setting the color according to the context's theme
  199. * @param snackbar the snackbar to be colored
  200. */
  201. public static void colorSnackbar(Context context, Snackbar snackbar) {
  202. // Changing action button text color
  203. snackbar.setActionTextColor(ContextCompat.getColor(context, R.color.white));
  204. }
  205. /**
  206. * Sets the color of the status bar to {@code color} on devices with OS version lollipop or higher.
  207. *
  208. * @param fragmentActivity fragment activity
  209. * @param color the color
  210. */
  211. public static void colorStatusBar(FragmentActivity fragmentActivity, @ColorInt int color) {
  212. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  213. fragmentActivity.getWindow().setStatusBarColor(color);
  214. }
  215. }
  216. /**
  217. * Sets the color of the progressbar to {@code color} within the given toolbar.
  218. *
  219. * @param activity the toolbar activity instance
  220. * @param progressBarColor the color to be used for the toolbar's progress bar
  221. */
  222. public static void colorToolbarProgressBar(FragmentActivity activity, int progressBarColor) {
  223. if (activity instanceof ToolbarActivity) {
  224. ((ToolbarActivity) activity).setProgressBarBackgroundColor(progressBarColor);
  225. }
  226. }
  227. public static void tintCheckbox(AppCompatCheckBox checkBox, int color) {
  228. CompoundButtonCompat.setButtonTintList(checkBox, new ColorStateList(
  229. new int[][]{
  230. new int[]{-android.R.attr.state_checked},
  231. new int[]{android.R.attr.state_checked},
  232. },
  233. new int[]{
  234. Color.GRAY,
  235. color
  236. }
  237. ));
  238. }
  239. public static void tintSwitch(SwitchCompat switchView, int color) {
  240. tintSwitch(switchView, color, false);
  241. }
  242. public static void tintSwitch(SwitchCompat switchView, int color, boolean colorText) {
  243. if (colorText) {
  244. switchView.setTextColor(color);
  245. }
  246. int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
  247. // setting the thumb color
  248. DrawableCompat.setTintList(switchView.getThumbDrawable(), new ColorStateList(
  249. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  250. new int[]{color, Color.WHITE}));
  251. // setting the track color
  252. DrawableCompat.setTintList(switchView.getTrackDrawable(), new ColorStateList(
  253. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  254. new int[]{trackColor, Color.parseColor("#4D000000")}));
  255. }
  256. public static Drawable tintDrawable(@DrawableRes int id, int color) {
  257. Drawable drawable = ResourcesCompat.getDrawable(MainApp.getAppContext().getResources(), id, null);
  258. return tintDrawable(drawable, color);
  259. }
  260. public static Drawable tintDrawable(Drawable drawable, int color) {
  261. if (drawable != null) {
  262. Drawable wrap = DrawableCompat.wrap(drawable);
  263. wrap.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  264. return wrap;
  265. } else {
  266. return drawable;
  267. }
  268. }
  269. public static String colorToHexString(int color) {
  270. return String.format("#%06X", 0xFFFFFF & color);
  271. }
  272. private static OCCapability getCapability() {
  273. return getCapability(null);
  274. }
  275. private static OCCapability getCapability(Account acc) {
  276. Account account;
  277. if (acc != null) {
  278. account = acc;
  279. } else {
  280. account = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
  281. }
  282. if (account != null) {
  283. Context context = MainApp.getAppContext();
  284. FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
  285. return storageManager.getCapability(account.name);
  286. } else {
  287. return new OCCapability();
  288. }
  289. }
  290. }