diff --git a/app/src/main/java/com/knightboot/spwaitkiller/sample/MainActivity.java b/app/src/main/java/com/knightboot/spwaitkiller/sample/MainActivity.java index 9e62517..cedc9fc 100644 --- a/app/src/main/java/com/knightboot/spwaitkiller/sample/MainActivity.java +++ b/app/src/main/java/com/knightboot/spwaitkiller/sample/MainActivity.java @@ -101,14 +101,13 @@ public void run() { @Override public void run() { try { - Log.d(TAG,"run work "+Log.getStackTraceString(new Throwable())); - Log.e(TAG,"run work "+this+" on Thread "+Thread.currentThread().getId()+" begin"); + Log.e(TAG,"run work "+this+" on Thread "+Thread.currentThread().getName()+" begin"); Thread.sleep(blockSeconds*1000); writtenToDiskLatch.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } - Log.e(TAG,"run work "+this+" on Thread "+Thread.currentThread().getId() +" finish"); + Log.e(TAG,"run work "+this+" on Thread "+Thread.currentThread().getName() +" finish"); } }); //触发任务执行 diff --git a/gradle.properties b/gradle.properties index bc04653..df457d0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,7 +20,7 @@ android.enableJetifier=true GROUP=io.github.knight-zxw -VERSION_NAME=0.0.7-SNAPSHOT +VERSION_NAME=0.0.8-SNAPSHOT POM_DESCRIPTION=android sp blocking killer POM_URL=https://github.com/Knight-ZXW/SpWaitKiller/ diff --git a/spwaitkiller/src/main/java/com/knightboot/spwaitkiller/ProxySWork.java b/spwaitkiller/src/main/java/com/knightboot/spwaitkiller/ProxySWork.java index bc19d40..0a38bbc 100644 --- a/spwaitkiller/src/main/java/com/knightboot/spwaitkiller/ProxySWork.java +++ b/spwaitkiller/src/main/java/com/knightboot/spwaitkiller/ProxySWork.java @@ -6,7 +6,6 @@ import androidx.annotation.NonNull; -import java.util.ArrayList; import java.util.LinkedList; /** @@ -17,13 +16,13 @@ class ProxySWork extends LinkedList { private final Handler sHandler; - private final QueueWorkAspect queueWorkAspect; + private final AboveAndroid12Processor aboveAndroid12Processor; public ProxySWork(LinkedList proxy, - Looper looper, QueueWorkAspect queueWorkAspect) { + Looper looper, AboveAndroid12Processor aboveAndroid12Processor) { this.proxy = proxy; sHandler = new Handler(looper); - this.queueWorkAspect = queueWorkAspect; + this.aboveAndroid12Processor = aboveAndroid12Processor; } // is thread safe @@ -65,7 +64,7 @@ public int size() { //Android 12 change: if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.S){ delegateWork(); - this.queueWorkAspect.processPendingWorkDone(); + this.aboveAndroid12Processor.reProxySWork(); return 0; }else { return proxy.size(); @@ -83,10 +82,15 @@ public boolean isEmpty() { return true; } + /** + * Android 12及以上版本的特殊回调处理 + */ + interface AboveAndroid12Processor { - interface QueueWorkAspect { - - public void processPendingWorkDone(); + /** + * 重新代理 sWork字段 + */ + public void reProxySWork(); } } diff --git a/spwaitkiller/src/main/java/com/knightboot/spwaitkiller/SpWaitKiller.java b/spwaitkiller/src/main/java/com/knightboot/spwaitkiller/SpWaitKiller.java index 848c646..e871d2a 100644 --- a/spwaitkiller/src/main/java/com/knightboot/spwaitkiller/SpWaitKiller.java +++ b/spwaitkiller/src/main/java/com/knightboot/spwaitkiller/SpWaitKiller.java @@ -101,12 +101,15 @@ private void realWork() throws Exception { } } - private static class QueueWorksWorkFieldHooker implements ProxySWork.QueueWorkAspect { + private static class QueueWorksWorkFieldHooker implements ProxySWork.AboveAndroid12Processor { - private boolean validate =true; - private Object lock = null; + private boolean reflectionFailed =false; + /** + * sLock对象, 操作 + */ + private Object sLock = null; private Field sWorkField; - private ProxySWork sWorkProxy; + private Looper looper; @SuppressLint("SoonBlockedPrivateApi") public QueueWorksWorkFieldHooker(){ @@ -116,36 +119,40 @@ public QueueWorksWorkFieldHooker(){ method.setAccessible(true); Handler handler = (android.os.Handler) method.invoke(null); - Looper looper = handler.getLooper(); + looper = handler.getLooper(); sWorkField = QueuedWorkClass.getDeclaredField("sWork"); sWorkField.setAccessible(true); Field sLockField = QueuedWorkClass.getDeclaredField("sLock"); sLockField.setAccessible(true); - lock = sLockField.get(null); - LinkedList sWork = (LinkedList) sWorkField.get(null); - sWorkProxy = new ProxySWork(sWork, looper, this); + sLock = sLockField.get(null); } catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException | NoSuchMethodException | InvocationTargetException e) { - validate = false; + reflectionFailed = true; } } - public void proxyWork(){ - if (!validate){ + private void proxyWork(){ + if (reflectionFailed){ return; } - synchronized (lock){ + synchronized (sLock){ + //Android12以下,sWork自始至终是同一个对象 try { + LinkedList sWork = (LinkedList) sWorkField.get(null); + ProxySWork sWorkProxy = new ProxySWork(sWork, looper, this); sWorkField.set(null, sWorkProxy); } catch (IllegalAccessException e) { - validate =false; + reflectionFailed =true; } } } + @Override - public void processPendingWorkDone() { + public void reProxySWork() { + //Android12开始,sWork字段在每次执行ProcessPendingWork时,sWork字段都会重新指向一个新的集合对象 + //因此需要重新代理 proxyWork(); } }