diff --git a/autojs/src/main/java/com/stardust/autojs/core/image/capture/ScreenCapturer.java b/autojs/src/main/java/com/stardust/autojs/core/image/capture/ScreenCapturer.java index 74e92327..55fc9b18 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/image/capture/ScreenCapturer.java +++ b/autojs/src/main/java/com/stardust/autojs/core/image/capture/ScreenCapturer.java @@ -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(); diff --git a/common/src/main/java/com/stardust/app/GlobalAppContext.java b/common/src/main/java/com/stardust/app/GlobalAppContext.java index ae5c9952..3731bb29 100644 --- a/common/src/main/java/com/stardust/app/GlobalAppContext.java +++ b/common/src/main/java/com/stardust/app/GlobalAppContext.java @@ -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(); } diff --git a/inrt/src/main/java/com/stardust/auojs/inrt/SplashActivity.java b/inrt/src/main/java/com/stardust/auojs/inrt/SplashActivity.java index 6b35b91e..3930f6c1 100644 --- a/inrt/src/main/java/com/stardust/auojs/inrt/SplashActivity.java +++ b/inrt/src/main/java/com/stardust/auojs/inrt/SplashActivity.java @@ -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); }