Merge "[DO NOT MERGE] Close screenshot process on user switched" into rvc-qpr-dev

This commit is contained in:
Miranda Kephart
2021-02-03 16:50:04 +00:00
committed by Android (Google) Code Review

View File

@@ -1,12 +1,15 @@
package com.android.internal.util; package com.android.internal.util;
import static android.content.Intent.ACTION_USER_SWITCHED;
import static android.view.WindowManager.ScreenshotSource.SCREENSHOT_OTHER; import static android.view.WindowManager.ScreenshotSource.SCREENSHOT_OTHER;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.content.BroadcastReceiver;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.graphics.Insets; import android.graphics.Insets;
import android.graphics.Rect; import android.graphics.Rect;
@@ -161,8 +164,21 @@ public class ScreenshotHelper {
private ServiceConnection mScreenshotConnection = null; private ServiceConnection mScreenshotConnection = null;
private final Context mContext; private final Context mContext;
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
synchronized (mScreenshotLock) {
if (ACTION_USER_SWITCHED.equals(intent.getAction())) {
resetConnection();
}
}
}
};
public ScreenshotHelper(Context context) { public ScreenshotHelper(Context context) {
mContext = context; mContext = context;
IntentFilter filter = new IntentFilter(ACTION_USER_SWITCHED);
mContext.registerReceiver(mBroadcastReceiver, filter);
} }
/** /**
@@ -279,9 +295,8 @@ public class ScreenshotHelper {
final Runnable mScreenshotTimeout = () -> { final Runnable mScreenshotTimeout = () -> {
synchronized (mScreenshotLock) { synchronized (mScreenshotLock) {
if (mScreenshotConnection != null) { if (mScreenshotConnection != null) {
mContext.unbindService(mScreenshotConnection); Log.e(TAG, "Timed out before getting screenshot capture response");
mScreenshotConnection = null; resetConnection();
mScreenshotService = null;
notifyScreenshotError(); notifyScreenshotError();
} }
} }
@@ -304,11 +319,7 @@ public class ScreenshotHelper {
break; break;
case SCREENSHOT_MSG_PROCESS_COMPLETE: case SCREENSHOT_MSG_PROCESS_COMPLETE:
synchronized (mScreenshotLock) { synchronized (mScreenshotLock) {
if (mScreenshotConnection != null) { resetConnection();
mContext.unbindService(mScreenshotConnection);
mScreenshotConnection = null;
mScreenshotService = null;
}
} }
break; break;
} }
@@ -348,9 +359,7 @@ public class ScreenshotHelper {
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
synchronized (mScreenshotLock) { synchronized (mScreenshotLock) {
if (mScreenshotConnection != null) { if (mScreenshotConnection != null) {
mContext.unbindService(mScreenshotConnection); resetConnection();
mScreenshotConnection = null;
mScreenshotService = null;
// only log an error if we're still within the timeout period // only log an error if we're still within the timeout period
if (handler.hasCallbacks(mScreenshotTimeout)) { if (handler.hasCallbacks(mScreenshotTimeout)) {
handler.removeCallbacks(mScreenshotTimeout); handler.removeCallbacks(mScreenshotTimeout);
@@ -382,6 +391,17 @@ public class ScreenshotHelper {
} }
} }
/**
* Unbinds the current screenshot connection (if any).
*/
private void resetConnection() {
if (mScreenshotConnection != null) {
mContext.unbindService(mScreenshotConnection);
mScreenshotConnection = null;
mScreenshotService = null;
}
}
/** /**
* Notifies the screenshot service to show an error. * Notifies the screenshot service to show an error.
*/ */