ThemeUtils.java 33 KB

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