123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package com.cooleshow.base.utils;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.Application;
- import android.graphics.drawable.Drawable;
- import android.text.Spannable;
- import android.text.SpannableString;
- import android.text.Spanned;
- import android.text.style.ForegroundColorSpan;
- import android.text.style.ImageSpan;
- import android.util.Log;
- import androidx.annotation.NonNull;
- import androidx.lifecycle.Lifecycle;
- /**
- * <pre>
- * author:
- * ___ ___ ___ ___
- * _____ / /\ /__/\ /__/| / /\
- * / /::\ / /::\ \ \:\ | |:| / /:/
- * / /:/\:\ ___ ___ / /:/\:\ \ \:\ | |:| /__/::\
- * / /:/~/::\ /__/\ / /\ / /:/~/::\ _____\__\:\ __| |:| \__\/\:\
- * /__/:/ /:/\:| \ \:\ / /:/ /__/:/ /:/\:\ /__/::::::::\ /__/\_|:|____ \ \:\
- * \ \:\/:/~/:/ \ \:\ /:/ \ \:\/:/__\/ \ \:\~~\~~\/ \ \:\/:::::/ \__\:\
- * \ \::/ /:/ \ \:\/:/ \ \::/ \ \:\ ~~~ \ \::/~~~~ / /:/
- * \ \:\/:/ \ \::/ \ \:\ \ \:\ \ \:\ /__/:/
- * \ \::/ \__\/ \ \:\ \ \:\ \ \:\ \__\/
- * \__\/ \__\/ \__\/ \__\/
- * blog : http://blankj.com
- * time : 16/12/08
- * desc : utils about initialization
- * </pre>
- */
- public final class Utils {
- @SuppressLint("StaticFieldLeak")
- private static Application sApp;
- private Utils() {
- throw new UnsupportedOperationException("u can't instantiate me...");
- }
- /**
- * Init utils.
- * <p>Init it in the class of UtilsFileProvider.</p>
- *
- * @param app application
- */
- public static void init(final Application app) {
- if (app == null) {
- Log.e("Utils", "app is null.");
- return;
- }
- if (sApp == null) {
- sApp = app;
- UtilsBridge.init(sApp);
- UtilsBridge.preLoad();
- return;
- }
- if (sApp.equals(app)) return;
- UtilsBridge.unInit(sApp);
- sApp = app;
- UtilsBridge.init(sApp);
- }
- /**
- * Return the Application object.
- * <p>Main process get app by UtilsFileProvider,
- * and other process get app by reflect.</p>
- *
- * @return the Application object
- */
- public static Application getApp() {
- if (sApp != null) return sApp;
- init(UtilsBridge.getApplicationByReflect());
- if (sApp == null) throw new NullPointerException("reflect failed.");
- Log.i("Utils", UtilsBridge.getCurrentProcessName() + " reflect app success.");
- return sApp;
- }
- ///////////////////////////////////////////////////////////////////////////
- // interface
- ///////////////////////////////////////////////////////////////////////////
- public abstract static class Task<Result> extends ThreadUtils.SimpleTask<Result> {
- private Consumer<Result> mConsumer;
- public Task(final Consumer<Result> consumer) {
- mConsumer = consumer;
- }
- @Override
- public void onSuccess(Result result) {
- if (mConsumer != null) {
- mConsumer.accept(result);
- }
- }
- }
- public interface OnAppStatusChangedListener {
- void onForeground(Activity activity);
- void onBackground(Activity activity);
- }
- public static class ActivityLifecycleCallbacks {
- public void onActivityCreated(@NonNull Activity activity) {/**/}
- public void onActivityStarted(@NonNull Activity activity) {/**/}
- public void onActivityResumed(@NonNull Activity activity) {/**/}
- public void onActivityPaused(@NonNull Activity activity) {/**/}
- public void onActivityStopped(@NonNull Activity activity) {/**/}
- public void onActivityDestroyed(@NonNull Activity activity) {/**/}
- public void onLifecycleChanged(@NonNull Activity activity, Lifecycle.Event event) {/**/}
- }
- public interface Consumer<T> {
- void accept(T t);
- }
- public interface Supplier<T> {
- T get();
- }
- public interface Func1<Ret, Par> {
- Ret call(Par param);
- }
- public static SpannableString diffColorString(String bigSizeStr, String centerStr, String lastStr, int firstColor, int centerColor) {
- String tmpStr = bigSizeStr + centerStr + lastStr;
- SpannableString result = new SpannableString(tmpStr);
- try{
- result.setSpan(new ForegroundColorSpan(firstColor), 0, bigSizeStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- result.setSpan(new ForegroundColorSpan(centerColor), bigSizeStr.length(), bigSizeStr.length() + centerStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- result.setSpan(new ForegroundColorSpan(firstColor), bigSizeStr.length() + centerStr.length(), tmpStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- }catch (Exception e){
- e.printStackTrace();
- }
- return result;
- }
- public static SpannableString diffColorString(String bigSizeStr, String lastStr, int firstColor, int lastColor) {
- String tmpStr = bigSizeStr + lastStr;
- SpannableString result = new SpannableString(tmpStr);
- try {
- result.setSpan(new ForegroundColorSpan(firstColor), 0, bigSizeStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- result.setSpan(new ForegroundColorSpan(lastColor), bigSizeStr.length(), tmpStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- }
|