Utils.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.cooleshow.base.utils;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.Application;
  5. import android.graphics.drawable.Drawable;
  6. import android.text.Spannable;
  7. import android.text.SpannableString;
  8. import android.text.Spanned;
  9. import android.text.style.ForegroundColorSpan;
  10. import android.text.style.ImageSpan;
  11. import android.util.Log;
  12. import androidx.annotation.NonNull;
  13. import androidx.lifecycle.Lifecycle;
  14. /**
  15. * <pre>
  16. * author:
  17. * ___ ___ ___ ___
  18. * _____ / /\ /__/\ /__/| / /\
  19. * / /::\ / /::\ \ \:\ | |:| / /:/
  20. * / /:/\:\ ___ ___ / /:/\:\ \ \:\ | |:| /__/::\
  21. * / /:/~/::\ /__/\ / /\ / /:/~/::\ _____\__\:\ __| |:| \__\/\:\
  22. * /__/:/ /:/\:| \ \:\ / /:/ /__/:/ /:/\:\ /__/::::::::\ /__/\_|:|____ \ \:\
  23. * \ \:\/:/~/:/ \ \:\ /:/ \ \:\/:/__\/ \ \:\~~\~~\/ \ \:\/:::::/ \__\:\
  24. * \ \::/ /:/ \ \:\/:/ \ \::/ \ \:\ ~~~ \ \::/~~~~ / /:/
  25. * \ \:\/:/ \ \::/ \ \:\ \ \:\ \ \:\ /__/:/
  26. * \ \::/ \__\/ \ \:\ \ \:\ \ \:\ \__\/
  27. * \__\/ \__\/ \__\/ \__\/
  28. * blog : http://blankj.com
  29. * time : 16/12/08
  30. * desc : utils about initialization
  31. * </pre>
  32. */
  33. public final class Utils {
  34. @SuppressLint("StaticFieldLeak")
  35. private static Application sApp;
  36. private Utils() {
  37. throw new UnsupportedOperationException("u can't instantiate me...");
  38. }
  39. /**
  40. * Init utils.
  41. * <p>Init it in the class of UtilsFileProvider.</p>
  42. *
  43. * @param app application
  44. */
  45. public static void init(final Application app) {
  46. if (app == null) {
  47. Log.e("Utils", "app is null.");
  48. return;
  49. }
  50. if (sApp == null) {
  51. sApp = app;
  52. UtilsBridge.init(sApp);
  53. UtilsBridge.preLoad();
  54. return;
  55. }
  56. if (sApp.equals(app)) return;
  57. UtilsBridge.unInit(sApp);
  58. sApp = app;
  59. UtilsBridge.init(sApp);
  60. }
  61. /**
  62. * Return the Application object.
  63. * <p>Main process get app by UtilsFileProvider,
  64. * and other process get app by reflect.</p>
  65. *
  66. * @return the Application object
  67. */
  68. public static Application getApp() {
  69. if (sApp != null) return sApp;
  70. init(UtilsBridge.getApplicationByReflect());
  71. if (sApp == null) throw new NullPointerException("reflect failed.");
  72. Log.i("Utils", UtilsBridge.getCurrentProcessName() + " reflect app success.");
  73. return sApp;
  74. }
  75. ///////////////////////////////////////////////////////////////////////////
  76. // interface
  77. ///////////////////////////////////////////////////////////////////////////
  78. public abstract static class Task<Result> extends ThreadUtils.SimpleTask<Result> {
  79. private Consumer<Result> mConsumer;
  80. public Task(final Consumer<Result> consumer) {
  81. mConsumer = consumer;
  82. }
  83. @Override
  84. public void onSuccess(Result result) {
  85. if (mConsumer != null) {
  86. mConsumer.accept(result);
  87. }
  88. }
  89. }
  90. public interface OnAppStatusChangedListener {
  91. void onForeground(Activity activity);
  92. void onBackground(Activity activity);
  93. }
  94. public static class ActivityLifecycleCallbacks {
  95. public void onActivityCreated(@NonNull Activity activity) {/**/}
  96. public void onActivityStarted(@NonNull Activity activity) {/**/}
  97. public void onActivityResumed(@NonNull Activity activity) {/**/}
  98. public void onActivityPaused(@NonNull Activity activity) {/**/}
  99. public void onActivityStopped(@NonNull Activity activity) {/**/}
  100. public void onActivityDestroyed(@NonNull Activity activity) {/**/}
  101. public void onLifecycleChanged(@NonNull Activity activity, Lifecycle.Event event) {/**/}
  102. }
  103. public interface Consumer<T> {
  104. void accept(T t);
  105. }
  106. public interface Supplier<T> {
  107. T get();
  108. }
  109. public interface Func1<Ret, Par> {
  110. Ret call(Par param);
  111. }
  112. public static SpannableString diffColorString(String bigSizeStr, String centerStr, String lastStr, int firstColor, int centerColor) {
  113. String tmpStr = bigSizeStr + centerStr + lastStr;
  114. SpannableString result = new SpannableString(tmpStr);
  115. try{
  116. result.setSpan(new ForegroundColorSpan(firstColor), 0, bigSizeStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  117. result.setSpan(new ForegroundColorSpan(centerColor), bigSizeStr.length(), bigSizeStr.length() + centerStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  118. result.setSpan(new ForegroundColorSpan(firstColor), bigSizeStr.length() + centerStr.length(), tmpStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  119. }catch (Exception e){
  120. e.printStackTrace();
  121. }
  122. return result;
  123. }
  124. public static SpannableString diffColorString(String bigSizeStr, String lastStr, int firstColor, int lastColor) {
  125. String tmpStr = bigSizeStr + lastStr;
  126. SpannableString result = new SpannableString(tmpStr);
  127. try {
  128. result.setSpan(new ForegroundColorSpan(firstColor), 0, bigSizeStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  129. result.setSpan(new ForegroundColorSpan(lastColor), bigSizeStr.length(), tmpStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. }
  133. return result;
  134. }
  135. }