ThemeUtils.java 13 KB

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