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

View File

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

View File

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