ThemeUtils.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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.Spannable;
  33. import android.text.SpannableString;
  34. import android.text.Spanned;
  35. import android.text.style.ForegroundColorSpan;
  36. import android.view.View;
  37. import android.view.Window;
  38. import android.widget.EditText;
  39. import android.widget.ImageButton;
  40. import android.widget.ImageView;
  41. import android.widget.ProgressBar;
  42. import android.widget.SeekBar;
  43. import android.widget.TextView;
  44. import com.google.android.material.button.MaterialButton;
  45. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  46. import com.google.android.material.snackbar.Snackbar;
  47. import com.google.android.material.textfield.TextInputLayout;
  48. import com.nextcloud.client.account.UserAccountManagerImpl;
  49. import com.owncloud.android.MainApp;
  50. import com.owncloud.android.R;
  51. import com.owncloud.android.datamodel.FileDataStorageManager;
  52. import com.owncloud.android.lib.common.utils.Log_OC;
  53. import com.owncloud.android.lib.resources.status.OCCapability;
  54. import java.lang.reflect.Field;
  55. import androidx.annotation.ColorInt;
  56. import androidx.annotation.DrawableRes;
  57. import androidx.annotation.Nullable;
  58. import androidx.appcompat.app.ActionBar;
  59. import androidx.appcompat.app.AppCompatDelegate;
  60. import androidx.appcompat.widget.AppCompatCheckBox;
  61. import androidx.appcompat.widget.SearchView;
  62. import androidx.appcompat.widget.SwitchCompat;
  63. import androidx.core.content.ContextCompat;
  64. import androidx.core.content.res.ResourcesCompat;
  65. import androidx.core.graphics.ColorUtils;
  66. import androidx.core.graphics.drawable.DrawableCompat;
  67. import androidx.core.widget.CompoundButtonCompat;
  68. import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
  69. import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
  70. /**
  71. * Utility class with methods for client side theming.
  72. */
  73. public final class ThemeUtils {
  74. private static final String TAG = ThemeUtils.class.getSimpleName();
  75. private static final int INDEX_LUMINATION = 2;
  76. private static final double MAX_LIGHTNESS = 0.92;
  77. public static final double LUMINATION_THRESHOLD = 0.8;
  78. private ThemeUtils() {
  79. // utility class -> private constructor
  80. }
  81. public static int primaryAccentColor(Context context) {
  82. OCCapability capability = getCapability(context);
  83. try {
  84. float adjust;
  85. if (darkTheme(context)) {
  86. if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
  87. adjust = +0.5f;
  88. // return adjustLightness(adjust, Color.parseColor(capability.getServerColor()), -1);
  89. } else {
  90. adjust = +0.1f;
  91. }
  92. } else {
  93. adjust = -0.1f;
  94. }
  95. return adjustLightness(adjust, Color.parseColor(capability.getServerColor()), 0.35f);
  96. } catch (Exception e) {
  97. return context.getResources().getColor(R.color.color_accent);
  98. }
  99. }
  100. public static int primaryDarkColor(Context context) {
  101. return primaryDarkColor(null, context);
  102. }
  103. public static int primaryDarkColor(Account account, Context context) {
  104. OCCapability capability = getCapability(account, context);
  105. try {
  106. return adjustLightness(-0.2f, Color.parseColor(capability.getServerColor()), -1f);
  107. } catch (Exception e) {
  108. return context.getResources().getColor(R.color.primary_dark);
  109. }
  110. }
  111. public static int primaryColor(Context context) {
  112. return primaryColor(context, false);
  113. }
  114. public static int primaryColor(Context context, boolean replaceWhite) {
  115. return primaryColor(null, replaceWhite, context);
  116. }
  117. public static int primaryColor(Account account, boolean replaceWhite, Context context) {
  118. if (context == null) {
  119. return Color.GRAY;
  120. }
  121. try {
  122. int color = Color.parseColor(getCapability(account, context).getServerColor());
  123. if (replaceWhite && Color.WHITE == color) {
  124. return getNeutralGrey(context);
  125. } else {
  126. return color;
  127. }
  128. } catch (Exception e) {
  129. return context.getResources().getColor(R.color.primary);
  130. }
  131. }
  132. public static int getNeutralGrey(Context context) {
  133. return darkTheme(context) ? context.getResources().getColor(R.color.fg_contrast) : Color.GRAY;
  134. }
  135. public static int elementColor(Context context) {
  136. return elementColor(null, context);
  137. }
  138. @NextcloudServer(max = 12)
  139. public static int elementColor(Account account, Context context) {
  140. OCCapability capability = getCapability(account, context);
  141. try {
  142. return Color.parseColor(capability.getServerElementColor());
  143. } catch (Exception e) {
  144. int primaryColor;
  145. try {
  146. primaryColor = Color.parseColor(capability.getServerColor());
  147. } catch (Exception e1) {
  148. primaryColor = context.getResources().getColor(R.color.primary);
  149. }
  150. float[] hsl = colorToHSL(primaryColor);
  151. if (hsl[INDEX_LUMINATION] > LUMINATION_THRESHOLD) {
  152. return context.getResources().getColor(R.color.element_fallback_color);
  153. } else {
  154. return primaryColor;
  155. }
  156. }
  157. }
  158. public static boolean themingEnabled(Context context) {
  159. return getCapability(context).getServerColor() != null && !getCapability(context).getServerColor().isEmpty();
  160. }
  161. /**
  162. * returns the font color based on the server side theming and uses black/white as a fallback based on
  163. * replaceWhite.
  164. *
  165. * @param context the context
  166. * @param replaceWhite FLAG to return white/black if server side color isn't available
  167. * @return int font color to use
  168. */
  169. public static int fontColor(Context context, boolean replaceWhite) {
  170. if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
  171. if (replaceWhite) {
  172. return Color.BLACK;
  173. } else {
  174. return Color.WHITE;
  175. }
  176. }
  177. try {
  178. return Color.parseColor(getCapability(context).getServerTextColor());
  179. } catch (Exception e) {
  180. if (darkTheme(context)) {
  181. return Color.WHITE;
  182. } else {
  183. return Color.BLACK;
  184. }
  185. }
  186. }
  187. public static int fontColor(Context context) {
  188. return fontColor(context, false);
  189. }
  190. /**
  191. * Tests if light color is set
  192. *
  193. * @param color the color
  194. * @return true if primaryColor is lighter than MAX_LIGHTNESS
  195. */
  196. public static boolean lightTheme(int color) {
  197. float[] hsl = colorToHSL(color);
  198. return hsl[INDEX_LUMINATION] >= MAX_LIGHTNESS;
  199. }
  200. /**
  201. * Tests if dark color is set
  202. *
  203. * @return true if dark theme -> e.g.use light font color, darker accent color
  204. */
  205. public static boolean darkTheme(Context context) {
  206. int primaryColor = primaryColor(context);
  207. float[] hsl = colorToHSL(primaryColor);
  208. return hsl[INDEX_LUMINATION] <= 0.55;
  209. }
  210. public static int primaryAppbarColor(Context context) {
  211. return ContextCompat.getColor(context, R.color.appbar);
  212. }
  213. public static int appBarPrimaryFontColor(Context context) {
  214. return ContextCompat.getColor(context, R.color.fontAppbar);
  215. }
  216. public static int appBarSecondaryFontColor(Context context) {
  217. return ContextCompat.getColor(context, R.color.fontSecondaryAppbar);
  218. }
  219. public static int actionModeColor(Context context) {
  220. return ContextCompat.getColor(context, R.color.action_mode_background);
  221. }
  222. /**
  223. * Set color of title to white/black depending on background color
  224. *
  225. * @param actionBar actionBar to be used
  226. * @param title title to be shown
  227. */
  228. public static void setColoredTitle(@Nullable ActionBar actionBar, String title, Context context) {
  229. if (actionBar != null) {
  230. if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
  231. actionBar.setTitle(title);
  232. } else {
  233. Spannable text = new SpannableString(title);
  234. text.setSpan(new ForegroundColorSpan(appBarPrimaryFontColor(context)),
  235. 0,
  236. text.length(),
  237. Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  238. actionBar.setTitle(text);
  239. }
  240. }
  241. }
  242. public static void setColoredTitle(@Nullable ActionBar actionBar, int titleId, Context context) {
  243. setColoredTitle(actionBar, context.getString(titleId), context);
  244. }
  245. /**
  246. * Set color of subtitle to white/black depending on background color
  247. *
  248. * @param actionBar actionBar to be used
  249. * @param title title to be shown
  250. */
  251. public static void setColoredSubtitle(@Nullable ActionBar actionBar, String title, Context context) {
  252. if (actionBar != null) {
  253. if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
  254. actionBar.setSubtitle(title);
  255. } else {
  256. Spannable text = new SpannableString(title);
  257. text.setSpan(new ForegroundColorSpan(appBarSecondaryFontColor(context)),
  258. 0,
  259. text.length(),
  260. Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  261. actionBar.setSubtitle(text);
  262. }
  263. }
  264. }
  265. /**
  266. * For activities that do not use drawer, e.g. Settings, this can be used to correctly tint back button based on
  267. * theme
  268. *
  269. * @param supportActionBar
  270. */
  271. public static void tintBackButton(@Nullable ActionBar supportActionBar, Context context) {
  272. tintBackButton(supportActionBar, context, ThemeUtils.appBarPrimaryFontColor(context));
  273. }
  274. public static void tintBackButton(@Nullable ActionBar supportActionBar, Context context, @ColorInt int color) {
  275. if (supportActionBar == null) {
  276. return;
  277. }
  278. Drawable backArrow = context.getResources().getDrawable(R.drawable.ic_arrow_back);
  279. supportActionBar.setHomeAsUpIndicator(ThemeUtils.tintDrawable(backArrow, color));
  280. }
  281. public static Spanned getColoredTitle(String title, int color) {
  282. Spannable text = new SpannableString(title);
  283. text.setSpan(new ForegroundColorSpan(color),
  284. 0,
  285. text.length(),
  286. Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  287. return text;
  288. }
  289. public static String getDefaultDisplayNameForRootFolder(Context context) {
  290. OCCapability capability = getCapability(context);
  291. if (MainApp.isOnlyOnDevice()) {
  292. return MainApp.getAppContext().getString(R.string.drawer_item_on_device);
  293. } else {
  294. if (capability.getServerName() == null || capability.getServerName().isEmpty()) {
  295. return MainApp.getAppContext().getResources().getString(R.string.default_display_name_for_root_folder);
  296. } else {
  297. return capability.getServerName();
  298. }
  299. }
  300. }
  301. public static void setStatusBarColor(Activity activity, @ColorInt int color) {
  302. if (activity != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  303. activity.getWindow().setStatusBarColor(color);
  304. }
  305. }
  306. /**
  307. * Adjust lightness of given color
  308. *
  309. * @param lightnessDelta values -1..+1
  310. * @param color original color
  311. * @param threshold 0..1 as maximum value, -1 to disable
  312. * @return color adjusted by lightness
  313. */
  314. public static int adjustLightness(float lightnessDelta, int color, float threshold) {
  315. float[] hsl = colorToHSL(color);
  316. if (threshold == -1f) {
  317. hsl[INDEX_LUMINATION] += lightnessDelta;
  318. } else {
  319. hsl[INDEX_LUMINATION] = Math.min(hsl[INDEX_LUMINATION] + lightnessDelta, threshold);
  320. }
  321. return ColorUtils.HSLToColor(hsl);
  322. }
  323. private static float[] colorToHSL(int color) {
  324. float[] hsl = new float[3];
  325. ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl);
  326. return hsl;
  327. }
  328. /**
  329. * sets the tinting of the given ImageButton's icon to color_accent.
  330. *
  331. * @param imageButton the image button who's icon should be colored
  332. */
  333. public static void colorImageButton(ImageButton imageButton, @ColorInt int color) {
  334. if (imageButton != null) {
  335. imageButton.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  336. }
  337. }
  338. public static void colorEditText(EditText editText, int elementColor) {
  339. if (editText != null) {
  340. editText.setTextColor(elementColor);
  341. editText.getBackground().setColorFilter(elementColor, PorterDuff.Mode.SRC_ATOP);
  342. }
  343. }
  344. /**
  345. * sets the coloring of the given progress bar to given color.
  346. *
  347. * @param progressBar the progress bar to be colored
  348. * @param color the color to be used
  349. */
  350. public static void colorHorizontalProgressBar(ProgressBar progressBar, @ColorInt int color) {
  351. if (progressBar != null) {
  352. progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  353. progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  354. }
  355. }
  356. /**
  357. * sets the coloring of the given progress bar's progress to given color.
  358. *
  359. * @param progressBar the progress bar to be colored
  360. * @param color the color to be used
  361. */
  362. public static void colorProgressBar(ProgressBar progressBar, @ColorInt int color) {
  363. if (progressBar != null) {
  364. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  365. progressBar.setProgressTintList(ColorStateList.valueOf(color));
  366. } else {
  367. ThemeUtils.colorHorizontalProgressBar(progressBar, color);
  368. }
  369. }
  370. }
  371. /**
  372. * sets the coloring of the given seek bar to color_accent.
  373. *
  374. * @param seekBar the seek bar to be colored
  375. */
  376. public static void colorHorizontalSeekBar(SeekBar seekBar, Context context) {
  377. int color = ThemeUtils.primaryAccentColor(context);
  378. colorHorizontalProgressBar(seekBar, color);
  379. seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  380. }
  381. public static void colorSwipeRefreshLayout(Context context, SwipeRefreshLayout swipeRefreshLayout) {
  382. int primaryColor = ThemeUtils.primaryColor(context);
  383. int darkColor = ThemeUtils.primaryDarkColor(context);
  384. int accentColor = ThemeUtils.primaryAccentColor(context);
  385. swipeRefreshLayout.setColorSchemeColors(accentColor, primaryColor, darkColor);
  386. swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.bg_elevation_one);
  387. }
  388. /**
  389. * set the Nextcloud standard colors for the snackbar.
  390. *
  391. * @param context the context relevant for setting the color according to the context's theme
  392. * @param snackbar the snackbar to be colored
  393. */
  394. public static void colorSnackbar(Context context, Snackbar snackbar) {
  395. // Changing action button text color
  396. snackbar.setActionTextColor(ContextCompat.getColor(context, R.color.fg_inverse));
  397. }
  398. /**
  399. * Sets the color of the status bar to {@code color} on devices with OS version lollipop or higher.
  400. *
  401. * @param fragmentActivity fragment activity
  402. * @param color the color
  403. */
  404. public static void colorStatusBar(Activity fragmentActivity, @ColorInt int color) {
  405. Window window = fragmentActivity.getWindow();
  406. boolean isLightTheme = lightTheme(color);
  407. if (window != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  408. window.setStatusBarColor(color);
  409. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  410. View decor = window.getDecorView();
  411. if (isLightTheme) {
  412. int systemUiFlagLightStatusBar;
  413. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  414. systemUiFlagLightStatusBar = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
  415. } else {
  416. systemUiFlagLightStatusBar = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
  417. }
  418. decor.setSystemUiVisibility(systemUiFlagLightStatusBar);
  419. } else {
  420. decor.setSystemUiVisibility(0);
  421. }
  422. } else if (isLightTheme) {
  423. window.setStatusBarColor(Color.BLACK);
  424. }
  425. }
  426. }
  427. public static void colorStatusBar(Activity fragmentActivity) {
  428. colorStatusBar(fragmentActivity, primaryAppbarColor(fragmentActivity));
  429. }
  430. /**
  431. * Sets the color of the TextInputLayout to {@code color} for hint text and box stroke.
  432. *
  433. * @param textInputLayout the TextInputLayout instance
  434. * @param color the color to be used for the hint text and box stroke
  435. */
  436. public static void colorTextInputLayout(TextInputLayout textInputLayout, int color) {
  437. textInputLayout.setBoxStrokeColor(color);
  438. textInputLayout.setDefaultHintTextColor(new ColorStateList(
  439. new int[][]{
  440. new int[]{-android.R.attr.state_focused},
  441. new int[]{android.R.attr.state_focused},
  442. },
  443. new int[]{
  444. Color.GRAY,
  445. color
  446. }
  447. ));
  448. }
  449. public static void themeDialogActionButton(MaterialButton button) {
  450. if (button == null) {
  451. return;
  452. }
  453. Context context = button.getContext();
  454. int accentColor = ThemeUtils.primaryAccentColor(button.getContext());
  455. int disabledColor = ContextCompat.getColor(context, R.color.disabled_text);
  456. button.setTextColor(new ColorStateList(
  457. new int[][]{
  458. new int[]{android.R.attr.state_enabled}, // enabled
  459. new int[]{-android.R.attr.state_enabled}, // disabled
  460. },
  461. new int[]{
  462. accentColor,
  463. disabledColor
  464. }
  465. ));
  466. }
  467. public static void themeEditText(Context context, EditText editText, boolean themedBackground) {
  468. if (editText == null) {
  469. return;
  470. }
  471. int color = ContextCompat.getColor(context, R.color.text_color);
  472. if (themedBackground) {
  473. if (darkTheme(context)) {
  474. color = ContextCompat.getColor(context, R.color.themed_fg);
  475. } else {
  476. color = ContextCompat.getColor(context, R.color.themed_fg_inverse);
  477. }
  478. }
  479. setEditTextColor(context, editText, color);
  480. }
  481. private static void setEditTextColor(Context context, EditText editText, int color) {
  482. editText.setTextColor(color);
  483. editText.setHighlightColor(context.getResources().getColor(R.color.fg_contrast));
  484. setEditTextCursorColor(editText, color);
  485. setTextViewHandlesColor(context, editText, color);
  486. }
  487. /**
  488. * Theme search view
  489. *
  490. * @param searchView searchView to be changed
  491. * @param context the app's context
  492. */
  493. public static void themeSearchView(SearchView searchView, Context context) {
  494. // hacky as no default way is provided
  495. int fontColor = appBarPrimaryFontColor(context);
  496. SearchView.SearchAutoComplete editText = searchView.findViewById(R.id.search_src_text);
  497. setEditTextColor(context, editText, fontColor);
  498. editText.setHintTextColor(appBarSecondaryFontColor(context));
  499. ImageView closeButton = searchView.findViewById(androidx.appcompat.R.id.search_close_btn);
  500. closeButton.setColorFilter(fontColor);
  501. ImageView searchButton = searchView.findViewById(androidx.appcompat.R.id.search_button);
  502. searchButton.setColorFilter(fontColor);
  503. }
  504. public static void themeProgressBar(Context context, ProgressBar progressBar) {
  505. int color = ThemeUtils.primaryAccentColor(context);
  506. progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
  507. }
  508. public static void tintCheckbox(AppCompatCheckBox checkBox, int color) {
  509. CompoundButtonCompat.setButtonTintList(checkBox, new ColorStateList(
  510. new int[][]{
  511. new int[]{-android.R.attr.state_checked},
  512. new int[]{android.R.attr.state_checked},
  513. },
  514. new int[]{
  515. Color.GRAY,
  516. color
  517. }
  518. ));
  519. }
  520. public static void tintSwitch(SwitchCompat switchView, int color) {
  521. tintSwitch(switchView, color, false);
  522. }
  523. public static void tintSwitch(SwitchCompat switchView, int color, boolean colorText) {
  524. if (colorText) {
  525. switchView.setTextColor(color);
  526. }
  527. int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
  528. // setting the thumb color
  529. DrawableCompat.setTintList(switchView.getThumbDrawable(), new ColorStateList(
  530. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  531. new int[]{color, Color.WHITE}));
  532. // setting the track color
  533. DrawableCompat.setTintList(switchView.getTrackDrawable(), new ColorStateList(
  534. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  535. new int[]{trackColor, MainApp.getAppContext().getResources().getColor(R.color.switch_track_color_unchecked)}));
  536. }
  537. public static Drawable tintDrawable(@DrawableRes int id, int color) {
  538. Drawable drawable = ResourcesCompat.getDrawable(MainApp.getAppContext().getResources(), id, null);
  539. return tintDrawable(drawable, color);
  540. }
  541. @Nullable
  542. public static Drawable tintDrawable(Drawable drawable, int color) {
  543. if (drawable != null) {
  544. Drawable wrap = DrawableCompat.wrap(drawable);
  545. wrap.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  546. return wrap;
  547. }
  548. return null;
  549. }
  550. public static String colorToHexString(int color) {
  551. return String.format("#%06X", 0xFFFFFF & color);
  552. }
  553. public static void tintFloatingActionButton(FloatingActionButton button, Context context) {
  554. button.setBackgroundTintList(ColorStateList.valueOf(ThemeUtils.primaryColor(context)));
  555. button.setRippleColor(ThemeUtils.primaryDarkColor(context));
  556. }
  557. public static void drawableFloatingActionButton(FloatingActionButton button, @DrawableRes int
  558. drawable, Context context) {
  559. button.setImageDrawable(ThemeUtils.tintDrawable(drawable, ThemeUtils.fontColor(context)));
  560. }
  561. private static OCCapability getCapability(Context context) {
  562. return getCapability(null, context);
  563. }
  564. private static OCCapability getCapability(Account acc, Context context) {
  565. Account account = null;
  566. if (acc != null) {
  567. account = acc;
  568. } else if (context != null) {
  569. // TODO: refactor when dark theme work is completed
  570. account = UserAccountManagerImpl.fromContext(context).getCurrentAccount();
  571. }
  572. if (account != null) {
  573. FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
  574. return storageManager.getCapability(account.name);
  575. } else {
  576. return new OCCapability();
  577. }
  578. }
  579. public static Drawable setIconColor(Drawable drawable) {
  580. int color;
  581. if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
  582. color = Color.WHITE;
  583. } else {
  584. color = Color.BLACK;
  585. }
  586. return tintDrawable(drawable, color);
  587. }
  588. /**
  589. * Lifted from SO. FindBugs surpressed because of lack of public API to alter the cursor color.
  590. *
  591. * @param editText TextView to be styled
  592. * @param color The desired cursor colour
  593. * @see <a href="https://stackoverflow.com/a/52564925">StackOverflow url</a>
  594. */
  595. @SuppressFBWarnings
  596. public static void setEditTextCursorColor(EditText editText, int color) {
  597. try {
  598. // Get the cursor resource id
  599. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {//set differently in Android P (API 28)
  600. Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
  601. field.setAccessible(true);
  602. int drawableResId = field.getInt(editText);
  603. // Get the editor
  604. field = TextView.class.getDeclaredField("mEditor");
  605. field.setAccessible(true);
  606. Object editor = field.get(editText);
  607. // Get the drawable and set a color filter
  608. Drawable drawable = ContextCompat.getDrawable(editText.getContext(), drawableResId);
  609. drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
  610. // Set the drawables
  611. field = editor.getClass().getDeclaredField("mDrawableForCursor");
  612. field.setAccessible(true);
  613. field.set(editor, drawable);
  614. } else {
  615. Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
  616. field.setAccessible(true);
  617. int drawableResId = field.getInt(editText);
  618. // Get the editor
  619. field = TextView.class.getDeclaredField("mEditor");
  620. field.setAccessible(true);
  621. Object editor = field.get(editText);
  622. // Get the drawable and set a color filter
  623. Drawable drawable = ContextCompat.getDrawable(editText.getContext(), drawableResId);
  624. drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
  625. Drawable[] drawables = {drawable, drawable};
  626. // Set the drawables
  627. field = editor.getClass().getDeclaredField("mCursorDrawable");
  628. field.setAccessible(true);
  629. field.set(editor, drawables);
  630. }
  631. } catch (Exception exception) {
  632. // we do not log this
  633. }
  634. }
  635. /**
  636. * Set the color of the handles when you select text in a {@link android.widget.EditText} or other view that extends
  637. * {@link TextView}. FindBugs surpressed because of lack of public API to alter the {@link TextView} handles color.
  638. *
  639. * @param view The {@link TextView} or a {@link View} that extends {@link TextView}.
  640. * @param color The color to set for the text handles
  641. * @see <a href="https://gist.github.com/jaredrummler/2317620559d10ac39b8218a1152ec9d4">External reference</a>
  642. */
  643. @SuppressFBWarnings
  644. private static void setTextViewHandlesColor(Context context, TextView view, int color) {
  645. try {
  646. Field editorField = TextView.class.getDeclaredField("mEditor");
  647. if (!editorField.isAccessible()) {
  648. editorField.setAccessible(true);
  649. }
  650. Object editor = editorField.get(view);
  651. Class<?> editorClass = editor.getClass();
  652. String[] handleNames = {"mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter"};
  653. String[] resNames = {"mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes"};
  654. for (int i = 0; i < handleNames.length; i++) {
  655. Field handleField = editorClass.getDeclaredField(handleNames[i]);
  656. if (!handleField.isAccessible()) {
  657. handleField.setAccessible(true);
  658. }
  659. Drawable handleDrawable = (Drawable) handleField.get(editor);
  660. if (handleDrawable == null) {
  661. Field resField = TextView.class.getDeclaredField(resNames[i]);
  662. if (!resField.isAccessible()) {
  663. resField.setAccessible(true);
  664. }
  665. int resId = resField.getInt(view);
  666. handleDrawable = ContextCompat.getDrawable(context, resId);
  667. }
  668. if (handleDrawable != null) {
  669. Drawable drawable = handleDrawable.mutate();
  670. drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
  671. handleField.set(editor, drawable);
  672. }
  673. }
  674. } catch (Exception e) {
  675. Log_OC.e(TAG, "Error setting TextView handles color", e);
  676. }
  677. }
  678. }