ThemeUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 Nextcloud GmbH
  8. * Copyright (C) 2018 Andy Scherzinger
  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.app.Activity;
  26. import android.content.Context;
  27. import android.content.res.ColorStateList;
  28. import android.graphics.Color;
  29. import android.graphics.PorterDuff;
  30. import android.graphics.drawable.Drawable;
  31. import android.os.Build;
  32. import android.support.annotation.ColorInt;
  33. import android.support.annotation.DrawableRes;
  34. import android.support.annotation.Nullable;
  35. import android.support.design.widget.FloatingActionButton;
  36. import android.support.design.widget.Snackbar;
  37. import android.support.v4.app.FragmentActivity;
  38. import android.support.v4.content.ContextCompat;
  39. import android.support.v4.content.res.ResourcesCompat;
  40. import android.support.v4.graphics.ColorUtils;
  41. import android.support.v4.graphics.drawable.DrawableCompat;
  42. import android.support.v4.widget.CompoundButtonCompat;
  43. import android.support.v7.app.ActionBar;
  44. import android.support.v7.widget.AppCompatCheckBox;
  45. import android.support.v7.widget.SwitchCompat;
  46. import android.text.Html;
  47. import android.text.Spanned;
  48. import android.widget.ImageButton;
  49. import android.widget.ProgressBar;
  50. import android.widget.SeekBar;
  51. import com.owncloud.android.MainApp;
  52. import com.owncloud.android.R;
  53. import com.owncloud.android.authentication.AccountUtils;
  54. import com.owncloud.android.datamodel.FileDataStorageManager;
  55. import com.owncloud.android.lib.resources.status.OCCapability;
  56. import com.owncloud.android.ui.activity.ToolbarActivity;
  57. /**
  58. * Utility class with methods for client side theming.
  59. */
  60. public class ThemeUtils {
  61. public static int primaryAccentColor(Context context) {
  62. OCCapability capability = getCapability(context);
  63. try {
  64. float adjust;
  65. if (darkTheme(context)) {
  66. adjust = +0.1f;
  67. } else {
  68. adjust = -0.1f;
  69. }
  70. return adjustLightness(adjust, Color.parseColor(capability.getServerColor()), 0.35f);
  71. } catch (Exception e) {
  72. return context.getResources().getColor(R.color.color_accent);
  73. }
  74. }
  75. public static int primaryDarkColor(Context context) {
  76. return primaryDarkColor(null, context);
  77. }
  78. public static int primaryDarkColor(Account account, Context context) {
  79. OCCapability capability = getCapability(account, context);
  80. try {
  81. return adjustLightness(-0.2f, Color.parseColor(capability.getServerColor()), -1f);
  82. } catch (Exception e) {
  83. return context.getResources().getColor(R.color.primary_dark);
  84. }
  85. }
  86. public static int primaryColor(Context context) {
  87. return primaryColor(context, false);
  88. }
  89. public static int primaryColor(Context context, boolean replaceWhite) {
  90. return primaryColor(null, replaceWhite, context);
  91. }
  92. public static int primaryColor(Account account, boolean replaceWhite, Context context) {
  93. OCCapability capability = getCapability(account, context);
  94. try {
  95. int color = Color.parseColor(capability.getServerColor());
  96. if (replaceWhite && Color.WHITE == color) {
  97. return Color.GRAY;
  98. } else {
  99. return color;
  100. }
  101. } catch (Exception e) {
  102. return context.getResources().getColor(R.color.primary);
  103. }
  104. }
  105. public static int elementColor(Context context) {
  106. return elementColor(null, context);
  107. }
  108. @NextcloudServer(max = 12)
  109. public static int elementColor(Account account, Context context) {
  110. OCCapability capability = getCapability(account, context);
  111. try {
  112. return Color.parseColor(capability.getServerElementColor());
  113. } catch (Exception e) {
  114. int primaryColor;
  115. try {
  116. primaryColor = Color.parseColor(capability.getServerColor());
  117. } catch (Exception e1) {
  118. primaryColor = context.getResources().getColor(R.color.primary);
  119. }
  120. float[] hsl = colorToHSL(primaryColor);
  121. if (hsl[2] > 0.8) {
  122. return context.getResources().getColor(R.color.elementFallbackColor);
  123. } else {
  124. return primaryColor;
  125. }
  126. }
  127. }
  128. public static boolean themingEnabled(Context context) {
  129. return getCapability(context).getServerColor() != null && !getCapability(context).getServerColor().isEmpty();
  130. }
  131. /**
  132. * @return int font color to use
  133. * adapted from https://github.com/nextcloud/server/blob/master/apps/theming/lib/Util.php#L90-L102
  134. */
  135. public static int fontColor(Context context) {
  136. try {
  137. return Color.parseColor(getCapability(context).getServerTextColor());
  138. } catch (Exception e) {
  139. if (darkTheme(context)) {
  140. return Color.WHITE;
  141. } else {
  142. return Color.BLACK;
  143. }
  144. }
  145. }
  146. /**
  147. * Tests if dark color is set
  148. * @return true if dark theme -> e.g.use light font color, darker accent color
  149. */
  150. public static boolean darkTheme(Context context) {
  151. int primaryColor = primaryColor(context);
  152. float[] hsl = colorToHSL(primaryColor);
  153. return hsl[2] <= 0.55;
  154. }
  155. /**
  156. * Set color of title to white/black depending on background color
  157. *
  158. * @param actionBar actionBar to be used
  159. * @param title title to be shown
  160. */
  161. public static void setColoredTitle(@Nullable ActionBar actionBar, String title, Context context) {
  162. if (actionBar != null) {
  163. if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
  164. actionBar.setTitle(title);
  165. } else {
  166. String colorHex = colorToHexString(fontColor(context));
  167. actionBar.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>"));
  168. }
  169. }
  170. }
  171. public static Spanned getColoredTitle(String title, int color) {
  172. String colorHex = colorToHexString(color);
  173. return Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>");
  174. }
  175. /**
  176. * Set color of title to white/black depending on background color
  177. *
  178. * @param actionBar actionBar to be used
  179. * @param titleId title to be shown
  180. */
  181. public static void setColoredTitle(@Nullable ActionBar actionBar, int titleId, Context context) {
  182. if (actionBar != null) {
  183. if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
  184. actionBar.setTitle(titleId);
  185. } else {
  186. String colorHex = colorToHexString(fontColor(context));
  187. String title = context.getString(titleId);
  188. actionBar.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>"));
  189. }
  190. }
  191. }
  192. public static String getDefaultDisplayNameForRootFolder(Context context) {
  193. OCCapability capability = getCapability(context);
  194. if (capability.getServerName() == null || capability.getServerName().isEmpty()) {
  195. return MainApp.getAppContext().getResources().getString(R.string.default_display_name_for_root_folder);
  196. } else {
  197. return capability.getServerName();
  198. }
  199. }
  200. public static void setStatusBarColor(Activity activity, @ColorInt int color) {
  201. if (activity != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  202. activity.getWindow().setStatusBarColor(color);
  203. }
  204. }
  205. /**
  206. * Adjust lightness of given color
  207. *
  208. * @param lightnessDelta values -1..+1
  209. * @param color
  210. * @param threshold 0..1 as maximum value, -1 to disable
  211. * @return color adjusted by lightness
  212. */
  213. public static int adjustLightness(float lightnessDelta, int color, float threshold) {
  214. float[] hsl = colorToHSL(color);
  215. if (threshold == -1f) {
  216. hsl[2] += lightnessDelta;
  217. } else {
  218. hsl[2] = Math.min(hsl[2] + lightnessDelta, threshold);
  219. }
  220. return ColorUtils.HSLToColor(hsl);
  221. }
  222. private static float[] colorToHSL(int color) {
  223. float[] hsl = new float[3];
  224. ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl);
  225. return hsl;
  226. }
  227. /**
  228. * sets the tinting of the given ImageButton's icon to color_accent.
  229. *
  230. * @param imageButton the image button who's icon should be colored
  231. */
  232. public static void colorImageButton(ImageButton imageButton, @ColorInt int color) {
  233. if (imageButton != null) {
  234. imageButton.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  235. }
  236. }
  237. /**
  238. * sets the coloring of the given progress bar to color_accent.
  239. *
  240. * @param progressBar the progress bar to be colored
  241. * @param color the color to be used
  242. */
  243. public static void colorHorizontalProgressBar(ProgressBar progressBar, @ColorInt int color) {
  244. if (progressBar != null) {
  245. progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  246. progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  247. }
  248. }
  249. /**
  250. * sets the coloring of the given seek bar to color_accent.
  251. *
  252. * @param seekBar the seek bar to be colored
  253. */
  254. public static void colorHorizontalSeekBar(SeekBar seekBar, Context context) {
  255. int color = ThemeUtils.primaryAccentColor(context);
  256. colorHorizontalProgressBar(seekBar, color);
  257. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  258. seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  259. }
  260. }
  261. /**
  262. * set the Nextcloud standard colors for the snackbar.
  263. *
  264. * @param context the context relevant for setting the color according to the context's theme
  265. * @param snackbar the snackbar to be colored
  266. */
  267. public static void colorSnackbar(Context context, Snackbar snackbar) {
  268. // Changing action button text color
  269. snackbar.setActionTextColor(ContextCompat.getColor(context, R.color.white));
  270. }
  271. /**
  272. * Sets the color of the status bar to {@code color} on devices with OS version lollipop or higher.
  273. *
  274. * @param fragmentActivity fragment activity
  275. * @param color the color
  276. */
  277. public static void colorStatusBar(FragmentActivity fragmentActivity, @ColorInt int color) {
  278. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && fragmentActivity.getWindow() != null) {
  279. fragmentActivity.getWindow().setStatusBarColor(color);
  280. }
  281. }
  282. /**
  283. * Sets the color of the progressbar to {@code color} within the given toolbar.
  284. *
  285. * @param activity the toolbar activity instance
  286. * @param progressBarColor the color to be used for the toolbar's progress bar
  287. */
  288. public static void colorToolbarProgressBar(FragmentActivity activity, int progressBarColor) {
  289. if (activity instanceof ToolbarActivity) {
  290. ((ToolbarActivity) activity).setProgressBarBackgroundColor(progressBarColor);
  291. }
  292. }
  293. public static void tintCheckbox(AppCompatCheckBox checkBox, int color) {
  294. CompoundButtonCompat.setButtonTintList(checkBox, new ColorStateList(
  295. new int[][]{
  296. new int[]{-android.R.attr.state_checked},
  297. new int[]{android.R.attr.state_checked},
  298. },
  299. new int[]{
  300. Color.GRAY,
  301. color
  302. }
  303. ));
  304. }
  305. public static void tintSwitch(SwitchCompat switchView, int color) {
  306. tintSwitch(switchView, color, false);
  307. }
  308. public static void tintSwitch(SwitchCompat switchView, int color, boolean colorText) {
  309. if (colorText) {
  310. switchView.setTextColor(color);
  311. }
  312. int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
  313. // setting the thumb color
  314. DrawableCompat.setTintList(switchView.getThumbDrawable(), new ColorStateList(
  315. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  316. new int[]{color, Color.WHITE}));
  317. // setting the track color
  318. DrawableCompat.setTintList(switchView.getTrackDrawable(), new ColorStateList(
  319. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  320. new int[]{trackColor, Color.parseColor("#4D000000")}));
  321. }
  322. public static Drawable tintDrawable(@DrawableRes int id, int color) {
  323. Drawable drawable = ResourcesCompat.getDrawable(MainApp.getAppContext().getResources(), id, null);
  324. return tintDrawable(drawable, color);
  325. }
  326. public static Drawable tintDrawable(Drawable drawable, int color) {
  327. if (drawable != null) {
  328. Drawable wrap = DrawableCompat.wrap(drawable);
  329. wrap.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  330. return wrap;
  331. } else {
  332. return drawable;
  333. }
  334. }
  335. public static String colorToHexString(int color) {
  336. return String.format("#%06X", 0xFFFFFF & color);
  337. }
  338. public static void tintFloatingActionButton(FloatingActionButton button, @DrawableRes int
  339. drawable, Context context) {
  340. button.setBackgroundTintList(ColorStateList.valueOf(ThemeUtils.primaryColor(context)));
  341. button.setRippleColor(ThemeUtils.primaryDarkColor(context));
  342. button.setImageDrawable(ThemeUtils.tintDrawable(drawable, ThemeUtils.fontColor(context)));
  343. }
  344. private static OCCapability getCapability(Context context) {
  345. return getCapability(null, context);
  346. }
  347. private static OCCapability getCapability(Account acc, Context context) {
  348. Account account;
  349. if (acc != null) {
  350. account = acc;
  351. } else {
  352. account = AccountUtils.getCurrentOwnCloudAccount(context);
  353. }
  354. if (account != null) {
  355. FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
  356. return storageManager.getCapability(account.name);
  357. } else {
  358. return new OCCapability();
  359. }
  360. }
  361. }