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

Commit 348dcc01 authored by Willie Koomson's avatar Willie Koomson
Browse files

Handle IllegalArgumentException in ShortcutService.isEnabled

Adds a catch block for IllegalArgumentException and a test for
isEnabled.

Bug: 438456778
Test: FrameworksServicesTests:ShortcutManagerTest5
Flag: EXEMPT bugfix
Change-Id: Id0737510720de779d38084141dd4799739192b2d
parent 99fcf858
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -4052,7 +4052,8 @@ public class ShortcutService extends IShortcutService.Stub {

    // Due to b/38267327, ActivityInfo.enabled may not reflect the current state of the component
    // and we need to check the enabled state via PackageManager.getComponentEnabledSetting.
    private boolean isEnabled(@Nullable ActivityInfo ai, int userId) {
    @VisibleForTesting
    boolean isEnabled(@Nullable ActivityInfo ai, int userId) {
        if (ai == null) {
            return false;
        }
@@ -4062,6 +4063,9 @@ public class ShortcutService extends IShortcutService.Stub {
        try {
            enabledFlag = mIPackageManager.getComponentEnabledSetting(
                    ai.getComponentName(), userId);
        } catch (IllegalArgumentException e) {
            Slog.w(TAG, "Failed to get component enabled setting of " + ai.getComponentName(), e);
            return false;
        } catch (RemoteException e) {
            // Shouldn't happen.
            Slog.wtf(TAG, "RemoteException", e);
+28 −0
Original line number Diff line number Diff line
@@ -17,6 +17,9 @@ package com.android.server.pm;

import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.ActivityInfo;
@@ -202,6 +205,31 @@ public class ShortcutManagerTest5 extends BaseShortcutManagerTest {
                new ComponentName(mMyPackage, "a.ShortcutDisabled"), mMyUserId));
        assertFalse(mShortcutService.injectIsActivityEnabledAndExported(
                new ComponentName(mMyPackage, "a.ShortcutUnexported"), mMyUserId));
    }

    public void testIsEnabled() {
        ActivityInfo ai = mShortcutService.getActivityInfoWithMetadata(
            new ComponentName(mMyPackage, "a.Shortcut1"), mMyUserId);

        // Should return true for an existing and enabled activity.
        assertTrue(mShortcutService.isEnabled(ai, mMyUserId));

        // Should return false for an existing and disabled activity.
        ai = mShortcutService.getActivityInfoWithMetadata(
            new ComponentName(mMyPackage, "a.ShortcutDisabled"), mMyUserId);
        assertFalse(mShortcutService.isEnabled(ai, mMyUserId));

        // Should return false (not throw) for a nonexistent or null activity.
        ai = mock(ActivityInfo.class);
        when(ai.getComponentName()).thenReturn(
                ComponentName.unflattenFromString("com.android.settings/.xxx"));
        assertFalse(mShortcutService.isEnabled(ai, mMyUserId));

        ai = mock(ActivityInfo.class);
        when(ai.getComponentName()).thenReturn(
                ComponentName.unflattenFromString("no.such.package/.xxx"));
        assertFalse(mShortcutService.isEnabled(ai, mMyUserId));

        assertFalse(mShortcutService.isEnabled(null, mMyUserId));
    }
}