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

Commit ed6ef62b authored by Tony Mak's avatar Tony Mak
Browse files

Introduce isForegroundDefaultLauncher in ShortcutServiceInternal

BUG: 70212757

Test: bit FrameworksServicesTests:com.android.server.pm.ShortcutManagerTest{1..10}

Change-Id: Ia85cf990e3fcf1dae129ca9b1fc4c3af2838a284
parent 4d51f445
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));
    }
}