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

Commit 0e1209be authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Introduce isForegroundDefaultLauncher in ShortcutServiceInternal"

parents 1b09d2ff ed6ef62b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -81,4 +81,7 @@ public abstract class ShortcutServiceInternal {
            @Nullable IntentSender resultIntent, int userId);

    public abstract boolean isRequestPinItemSupported(int callingUserId, int requestType);

    public abstract boolean isForegroundDefaultLauncher(@NonNull String callingPackage,
            int callingUid);
}
+20 −0
Original line number Diff line number Diff line
@@ -2739,6 +2739,26 @@ public class ShortcutService extends IShortcutService.Stub {
        public boolean isRequestPinItemSupported(int callingUserId, int requestType) {
            return ShortcutService.this.isRequestPinItemSupported(callingUserId, requestType);
        }

        @Override
        public boolean isForegroundDefaultLauncher(@NonNull String callingPackage, int callingUid) {
            Preconditions.checkNotNull(callingPackage);

            final int userId = UserHandle.getUserId(callingUid);
            final ComponentName defaultLauncher = getDefaultLauncher(userId);
            if (defaultLauncher == null) {
                return false;
            }
            if (!callingPackage.equals(defaultLauncher.getPackageName())) {
                return false;
            }
            synchronized (mLock) {
                if (!isUidForegroundLocked(callingUid)) {
                    return false;
                }
            }
            return true;
        }
    }

    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+12 −4
Original line number Diff line number Diff line
@@ -2121,24 +2121,32 @@ public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
                PACKAGE_FALLBACK_LAUNCHER_PRIORITY);
    }

    protected void makeCallerForeground() {
    protected void makeUidForeground(int uid) {
        try {
            mService.mUidObserver.onUidStateChanged(
                    mInjectedCallingUid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0);
                    uid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0);
        } catch (RemoteException e) {
            e.rethrowAsRuntimeException();
        }
    }

    protected void makeCallerBackground() {
    protected void makeCallerForeground() {
        makeUidForeground(mInjectedCallingUid);
    }

    protected void makeUidBackground(int uid) {
        try {
            mService.mUidObserver.onUidStateChanged(
                    mInjectedCallingUid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0);
                    uid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0);
        } catch (RemoteException e) {
            e.rethrowAsRuntimeException();
        }
    }

    protected void makeCallerBackground() {
        makeUidBackground(mInjectedCallingUid);
    }

    protected void publishManifestShortcutsAsCaller(int resId) {
        addManifestShortcutResource(
                new ComponentName(getCallingPackage(), ShortcutActivity.class.getName()),
+31 −0
Original line number Diff line number Diff line
@@ -7912,4 +7912,35 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
                    .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
        });
    }

    public void testIsForegroundDefaultLauncher_true() {
        final ComponentName defaultLauncher = new ComponentName("default", "launcher");
        final int uid = 1024;

        setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
        makeUidForeground(uid);

        assertTrue(mInternal.isForegroundDefaultLauncher("default", uid));
    }


    public void testIsForegroundDefaultLauncher_defaultButNotForeground() {
        final ComponentName defaultLauncher = new ComponentName("default", "launcher");
        final int uid = 1024;

        setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
        makeUidBackground(uid);

        assertFalse(mInternal.isForegroundDefaultLauncher("default", uid));
    }

    public void testIsForegroundDefaultLauncher_foregroundButNotDefault() {
        final ComponentName defaultLauncher = new ComponentName("default", "launcher");
        final int uid = 1024;

        setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
        makeUidForeground(uid);

        assertFalse(mInternal.isForegroundDefaultLauncher("another", uid));
    }
}