ThemeUtils.java 27 KB

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