ThemeUtils.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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.text.Html;
  33. import android.text.Spanned;
  34. import android.view.View;
  35. import android.view.Window;
  36. import android.widget.EditText;
  37. import android.widget.ImageButton;
  38. import android.widget.ProgressBar;
  39. import android.widget.SeekBar;
  40. import android.widget.TextView;
  41. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  42. import com.google.android.material.snackbar.Snackbar;
  43. import com.google.android.material.textfield.TextInputLayout;
  44. import com.owncloud.android.MainApp;
  45. import com.owncloud.android.R;
  46. import com.owncloud.android.authentication.AccountUtils;
  47. import com.owncloud.android.datamodel.FileDataStorageManager;
  48. import com.owncloud.android.lib.resources.status.OCCapability;
  49. import com.owncloud.android.ui.activity.ToolbarActivity;
  50. import java.lang.reflect.Field;
  51. import androidx.annotation.ColorInt;
  52. import androidx.annotation.DrawableRes;
  53. import androidx.annotation.Nullable;
  54. import androidx.appcompat.app.ActionBar;
  55. import androidx.appcompat.widget.AppCompatCheckBox;
  56. import androidx.appcompat.widget.SearchView;
  57. import androidx.appcompat.widget.SwitchCompat;
  58. import androidx.core.content.ContextCompat;
  59. import androidx.core.content.res.ResourcesCompat;
  60. import androidx.core.graphics.ColorUtils;
  61. import androidx.core.graphics.drawable.DrawableCompat;
  62. import androidx.core.widget.CompoundButtonCompat;
  63. import androidx.fragment.app.FragmentActivity;
  64. /**
  65. * Utility class with methods for client side theming.
  66. */
  67. public final class ThemeUtils {
  68. private ThemeUtils() {
  69. // utility class -> private constructor
  70. }
  71. public static int primaryAccentColor(Context context) {
  72. OCCapability capability = getCapability(context);
  73. try {
  74. float adjust;
  75. if (darkTheme(context)) {
  76. adjust = +0.1f;
  77. } else {
  78. adjust = -0.1f;
  79. }
  80. return adjustLightness(adjust, Color.parseColor(capability.getServerColor()), 0.35f);
  81. } catch (Exception e) {
  82. return context.getResources().getColor(R.color.color_accent);
  83. }
  84. }
  85. public static int primaryContrastColor(Context context) {
  86. OCCapability capability = getCapability(context);
  87. try {
  88. float adjust = 0;
  89. // if (darkTheme(context)) {
  90. // adjust = +0.1f;
  91. // } else {
  92. // adjust = -0.1f;
  93. // }
  94. return adjustLightness(adjust, Color.parseColor(capability.getServerColor()), 0.75f);
  95. } catch (Exception e) {
  96. return context.getResources().getColor(R.color.color_accent);
  97. }
  98. }
  99. public static int primaryDarkColor(Context context) {
  100. return primaryDarkColor(null, context);
  101. }
  102. public static int primaryDarkColor(Account account, Context context) {
  103. OCCapability capability = getCapability(account, context);
  104. try {
  105. return adjustLightness(-0.2f, Color.parseColor(capability.getServerColor()), -1f);
  106. } catch (Exception e) {
  107. return context.getResources().getColor(R.color.primary_dark);
  108. }
  109. }
  110. public static int primaryColor(Context context) {
  111. return primaryColor(context, false);
  112. }
  113. public static int primaryColor(Context context, boolean replaceWhite) {
  114. return primaryColor(null, replaceWhite, context);
  115. }
  116. public static int primaryColor(Account account, boolean replaceWhite, Context context) {
  117. OCCapability capability = getCapability(account, context);
  118. try {
  119. int color = Color.parseColor(capability.getServerColor());
  120. if (replaceWhite && Color.WHITE == color) {
  121. return Color.GRAY;
  122. } else {
  123. return color;
  124. }
  125. } catch (Exception e) {
  126. return context.getResources().getColor(R.color.primary);
  127. }
  128. }
  129. public static int elementColor(Context context) {
  130. return elementColor(null, context);
  131. }
  132. @NextcloudServer(max = 12)
  133. public static int elementColor(Account account, Context context) {
  134. OCCapability capability = getCapability(account, context);
  135. try {
  136. return Color.parseColor(capability.getServerElementColor());
  137. } catch (Exception e) {
  138. int primaryColor;
  139. try {
  140. primaryColor = Color.parseColor(capability.getServerColor());
  141. } catch (Exception e1) {
  142. primaryColor = context.getResources().getColor(R.color.primary);
  143. }
  144. float[] hsl = colorToHSL(primaryColor);
  145. if (hsl[2] > 0.8) {
  146. return context.getResources().getColor(R.color.elementFallbackColor);
  147. } else {
  148. return primaryColor;
  149. }
  150. }
  151. }
  152. public static boolean themingEnabled(Context context) {
  153. return getCapability(context).getServerColor() != null && !getCapability(context).getServerColor().isEmpty();
  154. }
  155. /**
  156. * @return int font color to use
  157. * adapted from https://github.com/nextcloud/server/blob/master/apps/theming/lib/Util.php#L90-L102
  158. */
  159. public static int fontColor(Context context) {
  160. try {
  161. return Color.parseColor(getCapability(context).getServerTextColor());
  162. } catch (Exception e) {
  163. if (darkTheme(context)) {
  164. return Color.WHITE;
  165. } else {
  166. return Color.BLACK;
  167. }
  168. }
  169. }
  170. /**
  171. * Tests if dark color is set
  172. * @return true if dark theme -> e.g.use light font color, darker accent color
  173. */
  174. public static boolean darkTheme(Context context) {
  175. int primaryColor = primaryColor(context);
  176. float[] hsl = colorToHSL(primaryColor);
  177. return hsl[2] <= 0.55;
  178. }
  179. /**
  180. * Set color of title to white/black depending on background color
  181. *
  182. * @param actionBar actionBar to be used
  183. * @param title title to be shown
  184. */
  185. public static void setColoredTitle(@Nullable ActionBar actionBar, String title, Context context) {
  186. if (actionBar != null) {
  187. if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
  188. actionBar.setTitle(title);
  189. } else {
  190. String colorHex = colorToHexString(fontColor(context));
  191. actionBar.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>"));
  192. }
  193. }
  194. }
  195. public static Spanned getColoredTitle(String title, int color) {
  196. String colorHex = colorToHexString(color);
  197. return Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>");
  198. }
  199. /**
  200. * Set color of title to white/black depending on background color
  201. *
  202. * @param actionBar actionBar to be used
  203. * @param titleId title to be shown
  204. */
  205. public static void setColoredTitle(@Nullable ActionBar actionBar, int titleId, Context context) {
  206. if (actionBar != null) {
  207. if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
  208. actionBar.setTitle(titleId);
  209. } else {
  210. String colorHex = colorToHexString(fontColor(context));
  211. String title = context.getString(titleId);
  212. actionBar.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>"));
  213. }
  214. }
  215. }
  216. public static String getDefaultDisplayNameForRootFolder(Context context) {
  217. OCCapability capability = getCapability(context);
  218. if (MainApp.isOnlyOnDevice()) {
  219. return MainApp.getAppContext().getString(R.string.drawer_item_on_device);
  220. } else {
  221. if (capability.getServerName() == null || capability.getServerName().isEmpty()) {
  222. return MainApp.getAppContext().getResources().getString(R.string.default_display_name_for_root_folder);
  223. } else {
  224. return capability.getServerName();
  225. }
  226. }
  227. }
  228. public static void setStatusBarColor(Activity activity, @ColorInt int color) {
  229. if (activity != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  230. activity.getWindow().setStatusBarColor(color);
  231. }
  232. }
  233. /**
  234. * Adjust lightness of given color
  235. *
  236. * @param lightnessDelta values -1..+1
  237. * @param color original color
  238. * @param threshold 0..1 as maximum value, -1 to disable
  239. * @return color adjusted by lightness
  240. */
  241. public static int adjustLightness(float lightnessDelta, int color, float threshold) {
  242. float[] hsl = colorToHSL(color);
  243. if (threshold == -1f) {
  244. hsl[2] += lightnessDelta;
  245. } else {
  246. hsl[2] = Math.min(hsl[2] + lightnessDelta, threshold);
  247. }
  248. return ColorUtils.HSLToColor(hsl);
  249. }
  250. private static float[] colorToHSL(int color) {
  251. float[] hsl = new float[3];
  252. ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl);
  253. return hsl;
  254. }
  255. /**
  256. * sets the tinting of the given ImageButton's icon to color_accent.
  257. *
  258. * @param imageButton the image button who's icon should be colored
  259. */
  260. public static void colorImageButton(ImageButton imageButton, @ColorInt int color) {
  261. if (imageButton != null) {
  262. imageButton.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  263. }
  264. }
  265. public static void colorEditText(EditText editText, int elementColor) {
  266. if (editText != null) {
  267. editText.setTextColor(elementColor);
  268. editText.getBackground().setColorFilter(elementColor, PorterDuff.Mode.SRC_ATOP);
  269. }
  270. }
  271. /**
  272. * sets the coloring of the given progress bar to given color.
  273. *
  274. * @param progressBar the progress bar to be colored
  275. * @param color the color to be used
  276. */
  277. public static void colorHorizontalProgressBar(ProgressBar progressBar, @ColorInt int color) {
  278. if (progressBar != null) {
  279. progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  280. progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  281. }
  282. }
  283. /**
  284. * sets the coloring of the given progress bar's progress to given color.
  285. *
  286. * @param progressBar the progress bar to be colored
  287. * @param color the color to be used
  288. */
  289. public static void colorProgressBar(ProgressBar progressBar, @ColorInt int color) {
  290. if (progressBar != null) {
  291. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  292. progressBar.setProgressTintList(ColorStateList.valueOf(color));
  293. } else {
  294. ThemeUtils.colorHorizontalProgressBar(progressBar, color);
  295. }
  296. }
  297. }
  298. /**
  299. * sets the coloring of the given seek bar to color_accent.
  300. *
  301. * @param seekBar the seek bar to be colored
  302. */
  303. public static void colorHorizontalSeekBar(SeekBar seekBar, Context context) {
  304. int color = ThemeUtils.primaryAccentColor(context);
  305. colorHorizontalProgressBar(seekBar, color);
  306. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  307. seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  308. }
  309. }
  310. /**
  311. * set the Nextcloud standard colors for the snackbar.
  312. *
  313. * @param context the context relevant for setting the color according to the context's theme
  314. * @param snackbar the snackbar to be colored
  315. */
  316. public static void colorSnackbar(Context context, Snackbar snackbar) {
  317. // Changing action button text color
  318. snackbar.setActionTextColor(ContextCompat.getColor(context, R.color.fg_inverse));
  319. }
  320. /**
  321. * Sets the color of the status bar to {@code color} on devices with OS version lollipop or higher.
  322. *
  323. * @param fragmentActivity fragment activity
  324. * @param color the color
  325. */
  326. public static void colorStatusBar(FragmentActivity fragmentActivity, @ColorInt int color) {
  327. Window window = fragmentActivity.getWindow();
  328. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && window != null) {
  329. window.setStatusBarColor(color);
  330. }
  331. }
  332. /**
  333. * Sets the color of the progressbar to {@code color} within the given toolbar.
  334. *
  335. * @param activity the toolbar activity instance
  336. * @param progressBarColor the color to be used for the toolbar's progress bar
  337. */
  338. public static void colorToolbarProgressBar(FragmentActivity activity, int progressBarColor) {
  339. if (activity instanceof ToolbarActivity) {
  340. ((ToolbarActivity) activity).setProgressBarBackgroundColor(progressBarColor);
  341. }
  342. }
  343. /**
  344. * Sets the color of the TextInputLayout to {@code color} for hint text and box stroke.
  345. *
  346. * @param textInputLayout the TextInputLayout instance
  347. * @param color the color to be used for the hint text and box stroke
  348. */
  349. public static void colorTextInputLayout(TextInputLayout textInputLayout, int color) {
  350. textInputLayout.setBoxStrokeColor(color);
  351. textInputLayout.setDefaultHintTextColor(new ColorStateList(
  352. new int[][]{
  353. new int[]{-android.R.attr.state_focused},
  354. new int[]{android.R.attr.state_focused},
  355. },
  356. new int[]{
  357. Color.GRAY,
  358. color
  359. }
  360. ));
  361. }
  362. public static void themeEditText(Context context, EditText editText, boolean themedBackground) {
  363. if (editText == null) { return; }
  364. int color = primaryColor(context);
  365. // Don't theme the view when it is already on a theme'd background
  366. if (themedBackground) {
  367. if (darkTheme(context)) {
  368. color = ContextCompat.getColor(context, R.color.themed_fg);
  369. } else {
  370. color = ContextCompat.getColor(context, R.color.themed_fg_inverse);
  371. }
  372. } else {
  373. float[] colorHSL = colorToHSL(color);
  374. if (colorHSL[2] >= 0.92) {
  375. color = ContextCompat.getColor(context, R.color.themed_fg_inverse);
  376. }
  377. }
  378. editText.setHighlightColor(context.getResources().getColor(R.color.fg_contrast));
  379. setTextViewCursorColor(editText, color);
  380. setTextViewHandlesColor(context, editText, color);
  381. }
  382. public static void themeSearchView(Context context, SearchView searchView, boolean themedBackground) {
  383. if (searchView == null) { return; }
  384. SearchView.SearchAutoComplete editText = searchView.findViewById(R.id.search_src_text);
  385. themeEditText(context, editText, themedBackground);
  386. }
  387. public static void tintCheckbox(AppCompatCheckBox checkBox, int color) {
  388. CompoundButtonCompat.setButtonTintList(checkBox, new ColorStateList(
  389. new int[][]{
  390. new int[]{-android.R.attr.state_checked},
  391. new int[]{android.R.attr.state_checked},
  392. },
  393. new int[]{
  394. Color.GRAY,
  395. color
  396. }
  397. ));
  398. }
  399. public static void tintSwitch(SwitchCompat switchView, int color) {
  400. tintSwitch(switchView, color, false);
  401. }
  402. public static void tintSwitch(SwitchCompat switchView, int color, boolean colorText) {
  403. if (colorText) {
  404. switchView.setTextColor(color);
  405. }
  406. int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
  407. // setting the thumb color
  408. DrawableCompat.setTintList(switchView.getThumbDrawable(), new ColorStateList(
  409. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  410. new int[]{color, Color.WHITE}));
  411. // setting the track color
  412. DrawableCompat.setTintList(switchView.getTrackDrawable(), new ColorStateList(
  413. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  414. new int[]{trackColor, Color.parseColor("#4D000000")}));
  415. }
  416. public static Drawable tintDrawable(@DrawableRes int id, int color) {
  417. Drawable drawable = ResourcesCompat.getDrawable(MainApp.getAppContext().getResources(), id, null);
  418. return tintDrawable(drawable, color);
  419. }
  420. @Nullable
  421. public static Drawable tintDrawable(Drawable drawable, int color) {
  422. if (drawable != null) {
  423. Drawable wrap = DrawableCompat.wrap(drawable);
  424. wrap.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  425. return wrap;
  426. }
  427. return null;
  428. }
  429. public static String colorToHexString(int color) {
  430. return String.format("#%06X", 0xFFFFFF & color);
  431. }
  432. public static void tintFloatingActionButton(FloatingActionButton button, @DrawableRes int
  433. drawable, Context context) {
  434. button.setBackgroundTintList(ColorStateList.valueOf(ThemeUtils.primaryColor(context)));
  435. button.setRippleColor(ThemeUtils.primaryDarkColor(context));
  436. button.setImageDrawable(ThemeUtils.tintDrawable(drawable, ThemeUtils.fontColor(context)));
  437. }
  438. private static OCCapability getCapability(Context context) {
  439. return getCapability(null, context);
  440. }
  441. private static OCCapability getCapability(Account acc, Context context) {
  442. Account account = null;
  443. if (acc != null) {
  444. account = acc;
  445. } else if (context != null) {
  446. account = AccountUtils.getCurrentOwnCloudAccount(context);
  447. }
  448. if (account != null) {
  449. FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
  450. return storageManager.getCapability(account.name);
  451. } else {
  452. return new OCCapability();
  453. }
  454. }
  455. /**
  456. * Lifted from SO.
  457. * @see https://stackoverflow.com/questions/25996032/how-to-change-programmatically-edittext-cursor-color-in-android#26543290
  458. * @param view TextView to be styled
  459. * @param color The desired cursor colour
  460. */
  461. public static void setTextViewCursorColor(EditText view, @ColorInt int color) {
  462. try {
  463. // Get the cursor resource id
  464. Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
  465. field.setAccessible(true);
  466. int drawableResId = field.getInt(view);
  467. // Get the editor
  468. // TODO check this in API 15
  469. field = TextView.class.getDeclaredField("mEditor");
  470. field.setAccessible(true);
  471. Object editor = field.get(view);
  472. // Get the drawable and set a color filter
  473. Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableResId);
  474. drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
  475. Drawable[] drawables = {drawable, drawable};
  476. // Set the drawables
  477. field = editor.getClass().getDeclaredField("mCursorDrawable");
  478. field.setAccessible(true);
  479. field.set(editor, drawables);
  480. } catch (Exception ignored) { }
  481. }
  482. /**
  483. * Set the color of the handles when you select text in a
  484. * {@link android.widget.EditText} or other view that extends {@link TextView}.
  485. *
  486. * @param view
  487. * The {@link TextView} or a {@link View} that extends {@link TextView}.
  488. * @param color
  489. * The color to set for the text handles
  490. *
  491. * @see https://gist.github.com/jaredrummler/2317620559d10ac39b8218a1152ec9d4
  492. */
  493. public static void setTextViewHandlesColor(Context context, TextView view, int color) {
  494. try {
  495. Field editorField = TextView.class.getDeclaredField("mEditor");
  496. if (!editorField.isAccessible()) {
  497. editorField.setAccessible(true);
  498. }
  499. Object editor = editorField.get(view);
  500. Class<?> editorClass = editor.getClass();
  501. String[] handleNames = {"mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter"};
  502. String[] resNames = {"mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes"};
  503. for (int i = 0; i < handleNames.length; i++) {
  504. Field handleField = editorClass.getDeclaredField(handleNames[i]);
  505. if (!handleField.isAccessible()) {
  506. handleField.setAccessible(true);
  507. }
  508. Drawable handleDrawable = (Drawable) handleField.get(editor);
  509. if (handleDrawable == null) {
  510. Field resField = TextView.class.getDeclaredField(resNames[i]);
  511. if (!resField.isAccessible()) {
  512. resField.setAccessible(true);
  513. }
  514. int resId = resField.getInt(view);
  515. // handleDrawable = view.getResources().getDrawable(resId);
  516. handleDrawable = ContextCompat.getDrawable(context, resId);
  517. }
  518. if (handleDrawable != null) {
  519. Drawable drawable = handleDrawable.mutate();
  520. drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
  521. handleField.set(editor, drawable);
  522. }
  523. }
  524. } catch (Exception e) {
  525. e.printStackTrace();
  526. }
  527. }
  528. }