Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 41a97a3f authored by Miranda Kephart's avatar Miranda Kephart
Browse files

[DO NOT MERGE] Close screenshot process on user switched

Currently, we keep the process up even if the user switches,
meaning that in some cases (if the user is switched while the
screenshot UI is up) we will save images to the wrong profile.
This change makes ScreenshotHelper listen for user switches and
close the screenshot service, so that a new screenshot is
guaranteed to be constructed with the correct user's context.

Bug: 170474245
Fix: 170474245
Test: manual -- verified bad state occurs if user switches within
the timeout period, ensured that screenshots work immediately
after switching with this change.

Change-Id: I9d32d0928e6c2bda161d04555438d0dd7afef0ba
(cherry picked from commit 7ef1a5dd)
parent d3eb04e7
Loading
Loading
Loading
Loading
+31 −6
Original line number Diff line number Diff line
package com.android.internal.util;

import static android.content.Intent.ACTION_USER_SWITCHED;

import android.annotation.NonNull;
import android.annotation.Nullable;
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.net.Uri;
import android.os.Handler;
@@ -33,8 +37,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);
    }

    /**
@@ -103,8 +120,8 @@ public class ScreenshotHelper {
                public void run() {
                    synchronized (mScreenshotLock) {
                        if (mScreenshotConnection != null) {
                            mContext.unbindService(mScreenshotConnection);
                            mScreenshotConnection = null;
                            Log.e(TAG, "Timed out before getting screenshot capture response");
                            resetConnection();
                            notifyScreenshotError();
                        }
                    }
@@ -130,8 +147,7 @@ public class ScreenshotHelper {
                            public void handleMessage(Message msg) {
                                synchronized (mScreenshotLock) {
                                    if (mScreenshotConnection == myConn) {
                                        mContext.unbindService(mScreenshotConnection);
                                        mScreenshotConnection = null;
                                        resetConnection();
                                        handler.removeCallbacks(mScreenshotTimeout);
                                    }
                                }
@@ -160,8 +176,7 @@ public class ScreenshotHelper {
                public void onServiceDisconnected(ComponentName name) {
                    synchronized (mScreenshotLock) {
                        if (mScreenshotConnection != null) {
                            mContext.unbindService(mScreenshotConnection);
                            mScreenshotConnection = null;
                            resetConnection();
                            handler.removeCallbacks(mScreenshotTimeout);
                            notifyScreenshotError();
                        }
@@ -177,6 +192,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.
     */