diff options
| author | 2021-02-03 14:27:52 +0000 | |
|---|---|---|
| committer | 2021-02-03 14:27:52 +0000 | |
| commit | e0135acaa6a971b08a4cbe1cf012733beafd92d0 (patch) | |
| tree | 2014b8ae09f3c3becd2e53d145df66107e77d217 | |
| parent | 77d6da84357dd97d7098876ab819b901712428bf (diff) | |
| parent | 9908e1a14575b5543fef15032f9fe495382fce87 (diff) | |
Merge "[DO NOT MERGE] Close screenshot process on user switched" into qt-dev
| -rw-r--r-- | core/java/com/android/internal/util/ScreenshotHelper.java | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/core/java/com/android/internal/util/ScreenshotHelper.java b/core/java/com/android/internal/util/ScreenshotHelper.java index 7fd94c6859fb..9ab89328a075 100644 --- a/core/java/com/android/internal/util/ScreenshotHelper.java +++ b/core/java/com/android/internal/util/ScreenshotHelper.java @@ -1,9 +1,13 @@ package com.android.internal.util; +import static android.content.Intent.ACTION_USER_SWITCHED; + import android.annotation.NonNull; +import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Handler; import android.os.IBinder; @@ -29,8 +33,21 @@ public class ScreenshotHelper { private ServiceConnection mScreenshotConnection = null; 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) { mContext = context; + IntentFilter filter = new IntentFilter(ACTION_USER_SWITCHED); + mContext.registerReceiver(mBroadcastReceiver, filter); } /** @@ -53,14 +70,12 @@ public class ScreenshotHelper { SYSUI_SCREENSHOT_SERVICE); final Intent serviceIntent = new Intent(); - final Runnable mScreenshotTimeout = new Runnable() { - @Override public void run() { - synchronized (mScreenshotLock) { - if (mScreenshotConnection != null) { - mContext.unbindService(mScreenshotConnection); - mScreenshotConnection = null; - notifyScreenshotError(); - } + final Runnable mScreenshotTimeout = () -> { + synchronized (mScreenshotLock) { + if (mScreenshotConnection != null) { + Log.e(TAG, "Timed out before getting screenshot capture response"); + resetConnection(); + notifyScreenshotError(); } } }; @@ -81,8 +96,7 @@ public class ScreenshotHelper { public void handleMessage(Message msg) { synchronized (mScreenshotLock) { if (mScreenshotConnection == myConn) { - mContext.unbindService(mScreenshotConnection); - mScreenshotConnection = null; + resetConnection(); handler.removeCallbacks(mScreenshotTimeout); } } @@ -103,8 +117,7 @@ public class ScreenshotHelper { public void onServiceDisconnected(ComponentName name) { synchronized (mScreenshotLock) { if (mScreenshotConnection != null) { - mContext.unbindService(mScreenshotConnection); - mScreenshotConnection = null; + resetConnection(); handler.removeCallbacks(mScreenshotTimeout); notifyScreenshotError(); } @@ -121,6 +134,16 @@ public class ScreenshotHelper { } /** + * Unbinds the current screenshot connection (if any). + */ + private void resetConnection() { + if (mScreenshotConnection != null) { + mContext.unbindService(mScreenshotConnection); + mScreenshotConnection = null; + } + } + + /** * Notifies the screenshot service to show an error. */ private void notifyScreenshotError() { |