From 01454e9e1eb4605cae5ac0b8be152e73baf99c81 Mon Sep 17 00:00:00 2001 From: liaoheng Date: Mon, 27 Jun 2022 18:42:32 +0800 Subject: [PATCH] fix the live wallpaper service, unable to open the wallpaper check thread in Android 12 --- app/build.gradle | 4 +- .../service/LiveWallpaperService.java | 131 +++++++++--------- .../me/liaoheng/wallpaper/util/Constants.java | 1 - 3 files changed, 65 insertions(+), 71 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7c10ecb..f779e77 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,8 +12,8 @@ android { minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion applicationId "me.liaoheng.wallpaper" - versionCode 10947 - versionName "1.9.47" + versionCode 10948 + versionName "1.9.48" } def isSigning = true diff --git a/app/src/main/java/me/liaoheng/wallpaper/service/LiveWallpaperService.java b/app/src/main/java/me/liaoheng/wallpaper/service/LiveWallpaperService.java index 897ec06..7c0e04d 100644 --- a/app/src/main/java/me/liaoheng/wallpaper/service/LiveWallpaperService.java +++ b/app/src/main/java/me/liaoheng/wallpaper/service/LiveWallpaperService.java @@ -18,9 +18,6 @@ import android.view.SurfaceHolder; import androidx.collection.LruCache; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.Observer; import com.github.liaoheng.common.util.AppUtils; import com.github.liaoheng.common.util.Callback; @@ -31,7 +28,6 @@ import java.io.File; import java.io.IOException; import java.util.UUID; -import java.util.concurrent.atomic.AtomicBoolean; import io.reactivex.Observable; import io.reactivex.ObservableSource; @@ -62,8 +58,9 @@ */ public class LiveWallpaperService extends WallpaperService { private final String TAG = LiveWallpaperService.class.getSimpleName(); - private MutableLiveData mSetWallpaper; - private EnableLiveData mEnableCheck; + public static final String VIEW_LIVE_WALLPAPER = "me.liaoheng.wallpaper.VIEW_LIVE_WALLPAPER"; + public static final String ENABLE_LIVE_WALLPAPER = "me.liaoheng.wallpaper.ENABLE_LIVE_WALLPAPER"; + public static final String EXTRA_ENABLE_LIVE_WALLPAPER = "EXTRA_ENABLE_LIVE_WALLPAPER"; public static final String UPDATE_LIVE_WALLPAPER = "me.liaoheng.wallpaper.UPDATE_LIVE_WALLPAPER"; public static final String PERMISSION_UPDATE_LIVE_WALLPAPER = "me.liaoheng.wallpaper.permission.UPDATE_LIVE_WALLPAPER"; private LiveWallpaperBroadcastReceiver mReceiver; @@ -71,14 +68,14 @@ public class LiveWallpaperService extends WallpaperService { private CompositeDisposable mLoadWallpaperDisposable; private HandlerHelper mCheckHandlerHelper; private Runnable mCheckRunnable; - private long mCheckPeriodic = Constants.DEF_LIVE_WALLPAPER_CHECK_PERIODIC; + private final long mCheckPeriodic = Constants.DEF_LIVE_WALLPAPER_CHECK_PERIODIC; @Override public Engine onCreateEngine() { return new LiveWallpaperEngine(); } - class LiveWallpaperBroadcastReceiver extends BroadcastReceiver { + private final class LiveWallpaperBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { @@ -88,40 +85,17 @@ public void onReceive(Context context, Intent intent) { setBingWallpaper(image, config); } else if (Constants.ACTION_DEBUG_LOG.equals(intent.getAction())) { LogDebugFileUtils.init(getApplicationContext()); + } else if (ENABLE_LIVE_WALLPAPER.equals(intent.getAction())) { + boolean enable = intent.getBooleanExtra(EXTRA_ENABLE_LIVE_WALLPAPER, false); + L.alog().d(TAG, "enable :" + enable); + disable(); + if (enable) { + enable(); + } } } } - static class EnableLiveData extends LiveData { - private final AtomicBoolean mListValue = new AtomicBoolean(); - - public EnableLiveData(Boolean value) { - super(value); - setListValue(value); - } - - @Override - public void postValue(Boolean value) { - if (value != mListValue.get()) { - setListValue(value); - super.postValue(value); - } - } - - @Override - public void setValue(Boolean value) { - if (value != mListValue.get()) { - setListValue(value); - super.setValue(value); - } - } - - private void setListValue(boolean value) { - mListValue.set(value); - } - - } - @Override public void onCreate() { super.onCreate(); @@ -129,25 +103,24 @@ public void onCreate() { LogDebugFileUtils.init(this); mServiceHelper = new SetWallpaperServiceHelper(this, TAG); mLoadWallpaperDisposable = new CompositeDisposable(); - mCheckHandlerHelper = HandlerHelper.create(TAG, Process.THREAD_PRIORITY_FOREGROUND, null); - mCheckRunnable = this::timing; - mSetWallpaper = new MutableLiveData<>(); - mEnableCheck = new EnableLiveData(false); - mEnableCheck.observeForever(enable -> { - disable(); - if (enable) { - enable(); + mCheckHandlerHelper = HandlerHelper.create(TAG, Process.THREAD_PRIORITY_FOREGROUND, msg -> { + L.alog().d(TAG, "HandlerHelper"); + if (msg.what == 1) { + Boolean enable = (Boolean) msg.obj; + L.alog().d(TAG, "chekc :" + enable); + disable(); + if (enable) { + enable(); + } } + return false; }); - - if (ROM.getROM().isEmui()) { - mCheckPeriodic = Constants.DEF_LIVE_WALLPAPER_CHECK_PERIODIC_EMUI; - } - + mCheckRunnable = this::timing; mReceiver = new LiveWallpaperBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(UPDATE_LIVE_WALLPAPER); intentFilter.addAction(Constants.ACTION_DEBUG_LOG); + intentFilter.addAction(ENABLE_LIVE_WALLPAPER); registerReceiver(mReceiver, intentFilter, PERMISSION_UPDATE_LIVE_WALLPAPER, new Handler(getMainLooper())); } @@ -177,7 +150,7 @@ public void enable() { return; } disable(); - mCheckHandlerHelper.postDelayed(this::timing, 300); + mCheckHandlerHelper.postDelayed(this::timing, 500); } private void disable() { @@ -268,7 +241,11 @@ private ObservableTransformer download() { } private void setWallpaper(Config config, DownloadBitmap d) { - mSetWallpaper.postValue(d); + Intent intent = new Intent(VIEW_LIVE_WALLPAPER); + intent.putExtra(Config.EXTRA_SET_WALLPAPER_IMAGE, d.image); + intent.putExtra(Config.EXTRA_SET_WALLPAPER_CONFIG, d.config); + sendBroadcast(intent); + if (config.getWallpaperMode() == Constants.EXTRA_SET_WALLPAPER_MODE_HOME) { return; } @@ -348,14 +325,16 @@ public boolean eq(DownloadBitmap b) { private class LiveWallpaperEngine extends LiveWallpaperService.Engine { public static final int DOWNLOAD_DRAW = 123; public static final int DOWNLOAD_DRAW_DELAY = 400; + public static final int ENABLE = 456; private DownloadBitmap mLastFile; private HandlerHelper mDrawHandlerHelper; private Runnable mDrawRunnable; private DelayedHandler mActionHandler; private Disposable mDisplayDisposable; private Disposable mPreviewDisposable; - private final LruCache mImageCache = new LruCache<>(8); - private final BitmapCache mBitmapCache = new BitmapCache(); + private LiveWallpaperEngineBroadcastReceiver mReceiver; + private LruCache mImageCache; + private BitmapCache mBitmapCache; private String TAG = "LiveWallpaperEngine:"; @Override @@ -368,13 +347,22 @@ public void onCreate(SurfaceHolder surfaceHolder) { mActionHandler = new DelayedHandler(Looper.getMainLooper(), msg -> { if (msg.what == DOWNLOAD_DRAW) { downloadWallpaper((DownloadBitmap) msg.obj); + } else if (msg.what == ENABLE) { + Intent intent = new Intent(ENABLE_LIVE_WALLPAPER); + intent.putExtra(EXTRA_ENABLE_LIVE_WALLPAPER, (boolean) msg.obj); + getDisplayContext().sendBroadcast(intent); } return true; }); mDrawRunnable = this::drawWallpaper; + mReceiver = new LiveWallpaperEngineBroadcastReceiver(); + mImageCache = new LruCache<>(8); + mBitmapCache = new BitmapCache(); + if (!isPreview()) { - mEnableCheck.postValue(true); + mActionHandler.removeMessages(ENABLE); + mActionHandler.sendDelayed(ENABLE, true, 1000); } } @@ -427,7 +415,7 @@ private synchronized void drawWallpaper(DownloadBitmap wallpaper) { } mBitmapCache.put(wallpaper.key(), bitmap); } else { - bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background); + bitmap = BitmapFactory.decodeResource(getDisplayContext().getResources(), R.drawable.background); } } final Bitmap finalBitmap = bitmap; @@ -513,22 +501,28 @@ public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int he public void onSurfaceCreated(SurfaceHolder holder) { super.onSurfaceCreated(holder); L.alog().d(TAG, "onSurfaceCreated"); - if (!isPreview() && mSetWallpaper != null) { - mSetWallpaper.observeForever(mSetWallpaperObserver); + if (!isPreview()) { + getDisplayContext().registerReceiver(mReceiver, new IntentFilter(VIEW_LIVE_WALLPAPER)); } previewBingWallpaper(); } - private final Observer mSetWallpaperObserver = new Observer() { + private final class LiveWallpaperEngineBroadcastReceiver extends BroadcastReceiver { + @Override - public void onChanged(DownloadBitmap info) { - if (info.eq(mLastFile)) { - return; + public void onReceive(Context context, Intent intent) { + if (VIEW_LIVE_WALLPAPER.equals(intent.getAction())) { + Wallpaper image = intent.getParcelableExtra(Config.EXTRA_SET_WALLPAPER_IMAGE); + Config config = intent.getParcelableExtra(Config.EXTRA_SET_WALLPAPER_CONFIG); + DownloadBitmap info = new DownloadBitmap(image, config); + if (info.eq(mLastFile)) { + return; + } + mActionHandler.removeMessages(DOWNLOAD_DRAW); + mActionHandler.sendDelayed(DOWNLOAD_DRAW, info, DOWNLOAD_DRAW_DELAY); } - mActionHandler.removeMessages(DOWNLOAD_DRAW); - mActionHandler.sendDelayed(DOWNLOAD_DRAW, info, DOWNLOAD_DRAW_DELAY); } - }; + } private void previewBingWallpaper() { Config config = new Config.Builder().loadConfig(getApplicationContext()).build(); @@ -573,8 +567,9 @@ public void onSuccess(DownloadBitmap d) { public void onSurfaceDestroyed(SurfaceHolder holder) { super.onSurfaceDestroyed(holder); L.alog().d(TAG, "onSurfaceDestroyed"); - if (mSetWallpaper != null) { - mSetWallpaper.removeObserver(mSetWallpaperObserver); + try { + getDisplayContext().unregisterReceiver(mReceiver); + } catch (Throwable ignored) { } mBitmapPaint = null; mMatrix = null; diff --git a/app/src/main/java/me/liaoheng/wallpaper/util/Constants.java b/app/src/main/java/me/liaoheng/wallpaper/util/Constants.java index 03f894a..f18c8aa 100644 --- a/app/src/main/java/me/liaoheng/wallpaper/util/Constants.java +++ b/app/src/main/java/me/liaoheng/wallpaper/util/Constants.java @@ -24,7 +24,6 @@ public interface Constants { long DEF_SCHEDULER_PERIODIC = 3;//hour String DEF_TIMER_PERIODIC = new LocalTime(0, 35).toString(); long DEF_LIVE_WALLPAPER_CHECK_PERIODIC = TimeUnit.MINUTES.toMillis(35); - long DEF_LIVE_WALLPAPER_CHECK_PERIODIC_EMUI = TimeUnit.MINUTES.toMillis(11); String LOCAL_BASE_URL = "https://www.bing.com"; String GLOBAL_BASE_URL = "https://global.bing.com";