Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1084 from luca020400/master
Browse files Browse the repository at this point in the history
Don't ANR on Q
  • Loading branch information
nahojjjen authored Dec 18, 2019
2 parents 3a1dc89 + 6861836 commit b1c6621
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
23 changes: 16 additions & 7 deletions app/src/main/java/com/kamron/pogoiv/ScreenGrabber.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ private ScreenGrabber(MediaProjection mediaProjection, DisplayMetrics raw) {
projection = mediaProjection;
imageReader = ImageReader.newInstance(rawDisplayMetrics.widthPixels, rawDisplayMetrics.heightPixels,
PixelFormat.RGBA_8888, 2);
virtualDisplay = projection.createVirtualDisplay("screen-mirror", rawDisplayMetrics.widthPixels,
rawDisplayMetrics.heightPixels,
rawDisplayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, imageReader.getSurface(),
null, null);
new Thread(new Runnable() {
@Override
public void run() {
virtualDisplay = projection.createVirtualDisplay("screen-mirror", rawDisplayMetrics.widthPixels,
rawDisplayMetrics.heightPixels,
rawDisplayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, imageReader.getSurface(),
null, null);
}
}).start();
}

public static ScreenGrabber init(MediaProjection mediaProjection, DisplayMetrics raw) {
Expand Down Expand Up @@ -77,7 +82,8 @@ public void exit() {
}

@WorkerThread
public @Nullable Bitmap grabScreen() {
public @Nullable
Bitmap grabScreen() {
Image image = null;
Bitmap bmp = null;
Integer retries = 60; // Retry for an entire second (given the rendering speed of 60fps)
Expand Down Expand Up @@ -131,7 +137,9 @@ public void exit() {
* @param points array of points representing coordinates to grab
* @return array of colors for the requested pixels, or null if any of them is out-of-bounds
*/
public @Nullable @ColorInt int[] grabPixels(Point[] points) {
public @Nullable
@ColorInt
int[] grabPixels(Point[] points) {
Image image = null;
try {
//Note: imageReader shouldn't be null, but apparently sometimes is.
Expand Down Expand Up @@ -172,7 +180,8 @@ public void exit() {
}

//Inspired by http://stackoverflow.com/a/27655022/53974.
private static @ColorInt int getPixel(ByteBuffer buffer, Point pos, int pixelStride, int rowStride) {
private static @ColorInt
int getPixel(ByteBuffer buffer, Point pos, int pixelStride, int rowStride) {
int offset = pos.y * rowStride + pos.x * pixelStride;
//This works because the image reader is configured with PixelFormat.RGBA_8888.
int r = buffer.get(offset) & 0xff;
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.android.tools.build:gradle:3.5.3'
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Jun 08 08:31:36 CEST 2019
#Mon Dec 09 22:10:46 CET 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

0 comments on commit b1c6621

Please sign in to comment.