Skip to content

Commit

Permalink
fix issue#7
Browse files Browse the repository at this point in the history
    在Android12及以上版本,每次调用ProcessPendingWork时,在 work.size()函数执行时, sWork字段已被指向为一个新的集合对象,因此
    在执行代理work.size()的逻辑后,应当重新设置sWork字段,并且需要代理这个新的集合对象
  • Loading branch information
Knight-ZXW committed Sep 5, 2022
1 parent 62fafae commit 294ea63
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
});
//触发任务执行
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.LinkedList;

/**
Expand All @@ -17,13 +16,13 @@ class ProxySWork<T> extends LinkedList<T> {

private final Handler sHandler;

private final QueueWorkAspect queueWorkAspect;
private final AboveAndroid12Processor aboveAndroid12Processor;

public ProxySWork(LinkedList<T> 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
Expand Down Expand Up @@ -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();
Expand All @@ -83,10 +82,15 @@ public boolean isEmpty() {
return true;
}

/**
* Android 12及以上版本的特殊回调处理
*/
interface AboveAndroid12Processor {

interface QueueWorkAspect {

public void processPendingWorkDone();
/**
* 重新代理 sWork字段
*/
public void reProxySWork();

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand All @@ -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();
}
}
Expand Down

0 comments on commit 294ea63

Please sign in to comment.