fix(autojs): java.lang.SecurityException Invalid media projection

This commit is contained in:
hyb1996 2018-10-12 21:13:30 +08:00
parent aa7318bfbd
commit 44bc2e9a02
3 changed files with 14 additions and 5 deletions

View File

@ -16,6 +16,7 @@ import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.OrientationEventListener;
import com.stardust.autojs.runtime.exception.ScriptException;
@ -34,7 +35,7 @@ public class ScreenCapturer {
private final Object mCachedImageLock = new Object();
private final MediaProjectionManager mProjectionManager;
private ImageReader mImageReader;
private final MediaProjection mMediaProjection;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private volatile Looper mImageAcquireLooper;
private volatile Image mUnderUsingImage;
@ -55,7 +56,7 @@ public class ScreenCapturer {
mScreenDensity = screenDensity;
mHandler = handler;
mProjectionManager = (MediaProjectionManager) context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
mMediaProjection = mProjectionManager.getMediaProjection(Activity.RESULT_OK, mData);
mMediaProjection = mProjectionManager.getMediaProjection(Activity.RESULT_OK, (Intent) mData.clone());
mHandler = handler;
setOrientation(orientation);
observeOrientation();
@ -88,6 +89,9 @@ public class ScreenCapturer {
private void refreshVirtualDisplay(int orientation) {
if (mImageAcquireLooper != null) {
mImageAcquireLooper.quit();
}
if (mImageReader != null) {
mImageReader.close();
}
@ -95,6 +99,10 @@ public class ScreenCapturer {
mVirtualDisplay.release();
}
mImageAvailable = false;
if (mMediaProjection != null) {
mMediaProjection.stop();
}
mMediaProjection = mProjectionManager.getMediaProjection(Activity.RESULT_OK, (Intent) mData.clone());
int screenHeight = ScreenMetrics.getOrientationAwareScreenHeight(orientation);
int screenWidth = ScreenMetrics.getOrientationAwareScreenWidth(orientation);
initVirtualDisplay(screenWidth, screenHeight, mScreenDensity);
@ -114,10 +122,12 @@ public class ScreenCapturer {
return;
}
new Thread(() -> {
Log.d(LOG_TAG, "AcquireImageLoop: start");
Looper.prepare();
mImageAcquireLooper = Looper.myLooper();
setImageListener(new Handler());
Looper.loop();
Log.d(LOG_TAG, "AcquireImageLoop: stop");
}).start();
}
@ -188,6 +198,7 @@ public class ScreenCapturer {
}
if (mMediaProjection != null) {
mMediaProjection.stop();
mMediaProjection = null;
}
if (mVirtualDisplay != null) {
mVirtualDisplay.release();

View File

@ -20,8 +20,6 @@ public class GlobalAppContext {
private static Handler sHandler;
public static void set(Application a) {
if (sApplicationContext != null)
throw new IllegalStateException();
sHandler = new Handler(Looper.getMainLooper());
sApplicationContext = a.getApplicationContext();
}

View File

@ -51,7 +51,7 @@ public class SplashActivity extends AppCompatActivity {
}
private void main() {
checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE,
checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE);
}