ThemeUtils.java 27 KB

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