[DO NOT MERGE] Convert TakeScreenshot finishers Runnables to Consumer<Uri>

Test: n/a
Bug: b/131082115
Change-Id: Ia84d034d08ff4d004fc9852dc5156d46059e8b00
This commit is contained in:
James O'Leary
2019-11-14 12:27:40 -05:00
parent 3ea014ca6d
commit 712eb51dc9
4 changed files with 31 additions and 26 deletions

View File

@@ -6,6 +6,7 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Message; import android.os.Message;
@@ -58,7 +59,7 @@ public class ScreenshotHelper {
*/ */
public void takeScreenshot(final int screenshotType, final boolean hasStatus, public void takeScreenshot(final int screenshotType, final boolean hasStatus,
final boolean hasNav, @NonNull Handler handler, final boolean hasNav, @NonNull Handler handler,
@Nullable Consumer<Boolean> completionConsumer) { @Nullable Consumer<Uri> completionConsumer) {
takeScreenshot(screenshotType, hasStatus, hasNav, SCREENSHOT_TIMEOUT_MS, handler, takeScreenshot(screenshotType, hasStatus, hasNav, SCREENSHOT_TIMEOUT_MS, handler,
completionConsumer); completionConsumer);
} }
@@ -83,12 +84,12 @@ public class ScreenshotHelper {
* the screenshot attempt will be cancelled and `completionConsumer` * the screenshot attempt will be cancelled and `completionConsumer`
* will be run. * will be run.
* @param handler A handler used in case the screenshot times out * @param handler A handler used in case the screenshot times out
* @param completionConsumer Consumes `false` if a screenshot was not taken, and `true` if the * @param completionConsumer Consumes `null` if a screenshot was not taken, and the URI of the
* screenshot was taken. * screenshot if the screenshot was taken.
*/ */
public void takeScreenshot(final int screenshotType, final boolean hasStatus, public void takeScreenshot(final int screenshotType, final boolean hasStatus,
final boolean hasNav, long timeoutMs, @NonNull Handler handler, final boolean hasNav, long timeoutMs, @NonNull Handler handler,
@Nullable Consumer<Boolean> completionConsumer) { @Nullable Consumer<Uri> completionConsumer) {
synchronized (mScreenshotLock) { synchronized (mScreenshotLock) {
if (mScreenshotConnection != null) { if (mScreenshotConnection != null) {
return; return;
@@ -108,7 +109,7 @@ public class ScreenshotHelper {
} }
} }
if (completionConsumer != null) { if (completionConsumer != null) {
completionConsumer.accept(false); completionConsumer.accept(null);
} }
} }
}; };
@@ -134,8 +135,9 @@ public class ScreenshotHelper {
handler.removeCallbacks(mScreenshotTimeout); handler.removeCallbacks(mScreenshotTimeout);
} }
} }
if (completionConsumer != null) { if (completionConsumer != null) {
completionConsumer.accept(true); completionConsumer.accept((Uri) msg.obj);
} }
} }
}; };
@@ -148,7 +150,7 @@ public class ScreenshotHelper {
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Couldn't take screenshot: " + e); Log.e(TAG, "Couldn't take screenshot: " + e);
if (completionConsumer != null) { if (completionConsumer != null) {
completionConsumer.accept(false); completionConsumer.accept(null);
} }
} }
} }

View File

@@ -19,7 +19,7 @@ package com.android.internal.util;
import static android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN; import static android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN;
import static android.view.WindowManager.TAKE_SCREENSHOT_SELECTED_REGION; import static android.view.WindowManager.TAKE_SCREENSHOT_SELECTED_REGION;
import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.fail; import static junit.framework.Assert.fail;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@@ -80,8 +80,8 @@ public final class ScreenshotHelperTest {
CountDownLatch lock = new CountDownLatch(1); CountDownLatch lock = new CountDownLatch(1);
mScreenshotHelper.takeScreenshot(TAKE_SCREENSHOT_FULLSCREEN, false, false, timeoutMs, mScreenshotHelper.takeScreenshot(TAKE_SCREENSHOT_FULLSCREEN, false, false, timeoutMs,
mHandler, mHandler,
worked -> { uri -> {
assertFalse(worked); assertNull(uri);
lock.countDown(); lock.countDown();
}); });

View File

@@ -95,7 +95,7 @@ import java.util.Date;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
/** /**
@@ -105,7 +105,7 @@ class SaveImageInBackgroundData {
Context context; Context context;
Bitmap image; Bitmap image;
Uri imageUri; Uri imageUri;
Runnable finisher; Consumer<Uri> finisher;
int iconSize; int iconSize;
int previewWidth; int previewWidth;
int previewheight; int previewheight;
@@ -406,7 +406,7 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
mNotificationManager.notify(SystemMessage.NOTE_GLOBAL_SCREENSHOT, mNotificationManager.notify(SystemMessage.NOTE_GLOBAL_SCREENSHOT,
mNotificationBuilder.build()); mNotificationBuilder.build());
} }
mParams.finisher.run(); mParams.finisher.accept(mParams.imageUri);
mParams.clearContext(); mParams.clearContext();
} }
@@ -415,7 +415,7 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
// If we are cancelled while the task is running in the background, we may get null params. // If we are cancelled while the task is running in the background, we may get null params.
// The finisher is expected to always be called back, so just use the baked-in params from // The finisher is expected to always be called back, so just use the baked-in params from
// the ctor in any case. // the ctor in any case.
mParams.finisher.run(); mParams.finisher.accept(null);
mParams.clearImage(); mParams.clearImage();
mParams.clearContext(); mParams.clearContext();
@@ -566,7 +566,7 @@ class GlobalScreenshot {
/** /**
* Creates a new worker thread and saves the screenshot to the media store. * Creates a new worker thread and saves the screenshot to the media store.
*/ */
private void saveScreenshotInWorkerThread(Runnable finisher) { private void saveScreenshotInWorkerThread(Consumer<Uri> finisher) {
SaveImageInBackgroundData data = new SaveImageInBackgroundData(); SaveImageInBackgroundData data = new SaveImageInBackgroundData();
data.context = mContext; data.context = mContext;
data.image = mScreenBitmap; data.image = mScreenBitmap;
@@ -584,8 +584,8 @@ class GlobalScreenshot {
/** /**
* Takes a screenshot of the current display and shows an animation. * Takes a screenshot of the current display and shows an animation.
*/ */
private void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible, private void takeScreenshot(Consumer<Uri> finisher, boolean statusBarVisible,
Rect crop) { boolean navBarVisible, Rect crop) {
int rot = mDisplay.getRotation(); int rot = mDisplay.getRotation();
int width = crop.width(); int width = crop.width();
int height = crop.height(); int height = crop.height();
@@ -595,7 +595,7 @@ class GlobalScreenshot {
if (mScreenBitmap == null) { if (mScreenBitmap == null) {
notifyScreenshotError(mContext, mNotificationManager, notifyScreenshotError(mContext, mNotificationManager,
R.string.screenshot_failed_to_capture_text); R.string.screenshot_failed_to_capture_text);
finisher.run(); finisher.accept(null);
return; return;
} }
@@ -608,7 +608,7 @@ class GlobalScreenshot {
statusBarVisible, navBarVisible); statusBarVisible, navBarVisible);
} }
void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) { void takeScreenshot(Consumer<Uri> finisher, boolean statusBarVisible, boolean navBarVisible) {
mDisplay.getRealMetrics(mDisplayMetrics); mDisplay.getRealMetrics(mDisplayMetrics);
takeScreenshot(finisher, statusBarVisible, navBarVisible, takeScreenshot(finisher, statusBarVisible, navBarVisible,
new Rect(0, 0, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels)); new Rect(0, 0, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels));
@@ -617,7 +617,7 @@ class GlobalScreenshot {
/** /**
* Displays a screenshot selector * Displays a screenshot selector
*/ */
void takeScreenshotPartial(final Runnable finisher, final boolean statusBarVisible, void takeScreenshotPartial(final Consumer<Uri> finisher, final boolean statusBarVisible,
final boolean navBarVisible) { final boolean navBarVisible) {
mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams); mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams);
mScreenshotSelectorView.setOnTouchListener(new View.OnTouchListener() { mScreenshotSelectorView.setOnTouchListener(new View.OnTouchListener() {
@@ -677,8 +677,8 @@ class GlobalScreenshot {
/** /**
* Starts the animation after taking the screenshot * Starts the animation after taking the screenshot
*/ */
private void startAnimation(final Runnable finisher, int w, int h, boolean statusBarVisible, private void startAnimation(final Consumer<Uri> finisher, int w, int h,
boolean navBarVisible) { boolean statusBarVisible, boolean navBarVisible) {
// If power save is on, show a toast so there is some visual indication that a screenshot // If power save is on, show a toast so there is some visual indication that a screenshot
// has been taken. // has been taken.
PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);

View File

@@ -18,6 +18,7 @@ package com.android.systemui.screenshot;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Message; import android.os.Message;
@@ -27,6 +28,8 @@ import android.os.UserManager;
import android.util.Log; import android.util.Log;
import android.view.WindowManager; import android.view.WindowManager;
import java.util.function.Consumer;
public class TakeScreenshotService extends Service { public class TakeScreenshotService extends Service {
private static final String TAG = "TakeScreenshotService"; private static final String TAG = "TakeScreenshotService";
@@ -36,10 +39,10 @@ public class TakeScreenshotService extends Service {
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
final Messenger callback = msg.replyTo; final Messenger callback = msg.replyTo;
Runnable finisher = new Runnable() { Consumer<Uri> finisher = new Consumer<Uri>() {
@Override @Override
public void run() { public void accept(Uri uri) {
Message reply = Message.obtain(null, 1); Message reply = Message.obtain(null, 1, uri);
try { try {
callback.send(reply); callback.send(reply);
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -52,7 +55,7 @@ public class TakeScreenshotService extends Service {
// animation and error notification. // animation and error notification.
if (!getSystemService(UserManager.class).isUserUnlocked()) { if (!getSystemService(UserManager.class).isUserUnlocked()) {
Log.w(TAG, "Skipping screenshot because storage is locked!"); Log.w(TAG, "Skipping screenshot because storage is locked!");
post(finisher); post(() -> finisher.accept(null));
return; return;
} }