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

Commit 34611854 authored by Suprabh Shukla's avatar Suprabh Shukla
Browse files

Suspend dialog improvememts

Finishing the suspend dialog when the package for which it was shown
gets unsuspended for any reason.

Also, attributing any interaction with this dialog to the suspending
package, as the system is showing the dialog on its behalf.

Test: Manual, from an unrooted shell:
adb shell pm suspend com.android.chrome
Tap to start chrome and wait for the dialog to appear. Then:
adb shell pm unsuspend com.android.chrome
The dialog should disappear. Further,
adb shell dumpsys usagestats
should show USER_INTERACTION for com.android.shell.

Bug: 169137795
Fixes: 180336589
Change-Id: I70b6f23e2994d8802335e35932ed09209754697c
parent 7d27c36d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ interface IUsageStatsManager {
    void reportPastUsageStart(in IBinder activity, String token, long timeAgoMs,
            String callingPackage);
    void reportUsageStop(in IBinder activity, String token, String callingPackage);
    void reportUserInteraction(String packageName, int userId);
    int getUsageSource();
    void forceUsageSourceSettingRead();
    long getLastTimeAnyComponentUsed(String packageName);
+14 −0
Original line number Diff line number Diff line
@@ -981,6 +981,20 @@ public final class UsageStatsManager {
        }
    }

    /**
     * Reports user interaction with a given package in the given user.
     *
     * <p><em>This method is only for use by the system</em>
     * @hide
     */
    public void reportUserInteraction(@NonNull String packageName, int userId) {
        try {
            mService.reportUserInteraction(packageName, userId);
        } catch (RemoteException re) {
            throw re.rethrowFromSystemServer();
        }
    }

    /**
     * Report usage associated with a particular {@code token} has started. Tokens are app defined
     * strings used to represent usage of in-app features. Apps with the {@link
+33 −0
Original line number Diff line number Diff line
@@ -27,8 +27,12 @@ import android.annotation.Nullable;
import android.app.AlertDialog;
import android.app.AppGlobals;
import android.app.KeyguardManager;
import android.app.usage.UsageStatsManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
@@ -64,9 +68,26 @@ public class SuspendedAppActivity extends AlertActivity
    private int mNeutralButtonAction;
    private int mUserId;
    private PackageManager mPm;
    private UsageStatsManager mUsm;
    private Resources mSuspendingAppResources;
    private SuspendDialogInfo mSuppliedDialogInfo;
    private Bundle mOptions;
    private BroadcastReceiver mUnsuspendReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_PACKAGES_UNSUSPENDED.equals(intent.getAction())) {
                final String[] unsuspended = intent.getStringArrayExtra(
                        Intent.EXTRA_CHANGED_PACKAGE_LIST);
                if (ArrayUtils.contains(unsuspended, mSuspendedPackage)) {
                    if (!isFinishing()) {
                        Slog.w(TAG, "Package " + mSuspendedPackage
                                + " got unsuspended while the dialog was visible. Finishing.");
                        SuspendedAppActivity.this.finish();
                    }
                }
            }
        }
    };

    private CharSequence getAppLabel(String packageName) {
        try {
@@ -183,6 +204,7 @@ public class SuspendedAppActivity extends AlertActivity
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mPm = getPackageManager();
        mUsm = getSystemService(UsageStatsManager.class);
        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);

        final Intent intent = getIntent();
@@ -222,6 +244,16 @@ public class SuspendedAppActivity extends AlertActivity
        requestDismissKeyguardIfNeeded(ap.mMessage);

        setupAlert();

        final IntentFilter unsuspendFilter = new IntentFilter(Intent.ACTION_PACKAGES_UNSUSPENDED);
        registerReceiverAsUser(mUnsuspendReceiver, UserHandle.of(mUserId), unsuspendFilter, null,
                null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUnsuspendReceiver);
    }

    private void requestDismissKeyguardIfNeeded(CharSequence dismissMessage) {
@@ -292,6 +324,7 @@ public class SuspendedAppActivity extends AlertActivity
                }
                break;
        }
        mUsm.reportUserInteraction(mSuspendingPackage, mUserId);
        finish();
    }

+13 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static android.app.usage.UsageEvents.Event.FLUSH_TO_DISK;
import static android.app.usage.UsageEvents.Event.LOCUS_ID_SET;
import static android.app.usage.UsageEvents.Event.NOTIFICATION_INTERRUPTION;
import static android.app.usage.UsageEvents.Event.SHORTCUT_INVOCATION;
import static android.app.usage.UsageEvents.Event.USER_INTERACTION;
import static android.app.usage.UsageEvents.Event.USER_STOPPED;
import static android.app.usage.UsageEvents.Event.USER_UNLOCKED;
import static android.app.usage.UsageStatsManager.USAGE_SOURCE_CURRENT_ACTIVITY;
@@ -112,6 +113,7 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;
@@ -1987,6 +1989,17 @@ public class UsageStatsService extends SystemService implements
            reportEventOrAddToQueue(userId, event);
        }

        @Override
        public void reportUserInteraction(String packageName, int userId) {
            Objects.requireNonNull(packageName);
            if (!isCallingUidSystem()) {
                throw new SecurityException("Only system is allowed to call reportUserInteraction");
            }
            final Event event = new Event(USER_INTERACTION, SystemClock.elapsedRealtime());
            event.mPackage = packageName;
            reportEventOrAddToQueue(userId, event);
        }

        @Override
        public void registerAppUsageObserver(int observerId,
                String[] packages, long timeLimitMs, PendingIntent