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

Commit bbc19b03 authored by Pinyao Ting's avatar Pinyao Ting
Browse files

Consolidate shortcut usage report api.

In S, to simplify shortcut usage report, calling pushDynamicShortcut now
leads to usage report so that developer only needs to deal with one api.

Bug: 151359749
Test: atest ShortcutManagerTest1 ShortcutManagerTest2
ShortcutManagerTest3 ShortcutManagerTest4 ShortcutManagerTest5
ShortcutManagerTest6 ShortcutManagerTest7 ShortcutManagerTest8
ShortcutManagerTest9 ShortcutManagerTest10 ShortcutManagerTest11
Test: atest CtsShortcutManagerTestCases

Change-Id: Ib7b0de0c8343475c2e09078240a87f9b29d55880
parent 00475ff2
Loading
Loading
Loading
Loading
+12 −6
Original line number Original line Diff line number Diff line
@@ -2240,6 +2240,8 @@ public class ShortcutService extends IShortcutService.Stub {


                packageShortcutsChanged(packageName, userId, changedShortcuts, removedShortcuts);
                packageShortcutsChanged(packageName, userId, changedShortcuts, removedShortcuts);


                reportShortcutUsedInternal(packageName, shortcut.getId(), userId);

                verifyStates();
                verifyStates();


                ret.complete(null);
                ret.complete(null);
@@ -2850,12 +2852,7 @@ public class ShortcutService extends IShortcutService.Stub {
                    }
                    }
                }
                }


                final long token = injectClearCallingIdentity();
                reportShortcutUsedInternal(packageName, shortcutId, userId);
                try {
                    mUsageStatsManagerInternal.reportShortcutUsage(packageName, shortcutId, userId);
                } finally {
                    injectRestoreCallingIdentity(token);
                }
                ret.complete(true);
                ret.complete(true);
            } catch (Exception e) {
            } catch (Exception e) {
                ret.completeExceptionally(e);
                ret.completeExceptionally(e);
@@ -2864,6 +2861,15 @@ public class ShortcutService extends IShortcutService.Stub {
        return ret;
        return ret;
    }
    }


    private void reportShortcutUsedInternal(String packageName, String shortcutId, int userId) {
        final long token = injectClearCallingIdentity();
        try {
            mUsageStatsManagerInternal.reportShortcutUsage(packageName, shortcutId, userId);
        } finally {
            injectRestoreCallingIdentity(token);
        }
    }

    @Override
    @Override
    public boolean isRequestPinItemSupported(int callingUserId, int requestType) {
    public boolean isRequestPinItemSupported(int callingUserId, int requestType) {
        final long token = injectClearCallingIdentity();
        final long token = injectClearCallingIdentity();
+34 −0
Original line number Original line Diff line number Diff line
@@ -108,6 +108,7 @@ import com.android.server.pm.ShortcutService.FileOutputStreamWithPath;
import com.android.server.pm.ShortcutUser.PackageWithUser;
import com.android.server.pm.ShortcutUser.PackageWithUser;


import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserException;


@@ -416,8 +417,11 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
        mManager.pushDynamicShortcut(s1);
        mManager.pushDynamicShortcut(s1);
        assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()), "s1");
        assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()), "s1");
        assertEquals(0, getCallerShortcut("s1").getRank());
        assertEquals(0, getCallerShortcut("s1").getRank());
        verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s1"), eq(USER_0));


        // Test push when other shortcuts exist
        // Test push when other shortcuts exist
        Mockito.reset(mMockUsageStatsManagerInternal);
        assertTrue(mManager.setDynamicShortcuts(list(s1, s2)));
        assertTrue(mManager.setDynamicShortcuts(list(s1, s2)));
        assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()), "s1", "s2");
        assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()), "s1", "s2");
        mManager.pushDynamicShortcut(s3);
        mManager.pushDynamicShortcut(s3);
@@ -426,25 +430,38 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
        assertEquals(0, getCallerShortcut("s3").getRank());
        assertEquals(0, getCallerShortcut("s3").getRank());
        assertEquals(1, getCallerShortcut("s1").getRank());
        assertEquals(1, getCallerShortcut("s1").getRank());
        assertEquals(2, getCallerShortcut("s2").getRank());
        assertEquals(2, getCallerShortcut("s2").getRank());
        verify(mMockUsageStatsManagerInternal, times(0)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s1"), eq(USER_0));
        verify(mMockUsageStatsManagerInternal, times(0)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s2"), eq(USER_0));
        verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s3"), eq(USER_0));


        mInjectedCurrentTimeMillis += INTERVAL; // reset
        mInjectedCurrentTimeMillis += INTERVAL; // reset


        // Push with set rank
        // Push with set rank
        Mockito.reset(mMockUsageStatsManagerInternal);
        s4.setRank(2);
        s4.setRank(2);
        mManager.pushDynamicShortcut(s4);
        mManager.pushDynamicShortcut(s4);
        assertEquals(2, getCallerShortcut("s4").getRank());
        assertEquals(2, getCallerShortcut("s4").getRank());
        assertEquals(3, getCallerShortcut("s2").getRank());
        assertEquals(3, getCallerShortcut("s2").getRank());
        verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s4"), eq(USER_0));


        // Push existing shortcut with set rank
        // Push existing shortcut with set rank
        Mockito.reset(mMockUsageStatsManagerInternal);
        final ShortcutInfo s4_2 = makeShortcut("s4");
        final ShortcutInfo s4_2 = makeShortcut("s4");
        s4_2.setRank(4);
        s4_2.setRank(4);
        mManager.pushDynamicShortcut(s4_2);
        mManager.pushDynamicShortcut(s4_2);
        assertEquals(2, getCallerShortcut("s2").getRank());
        assertEquals(2, getCallerShortcut("s2").getRank());
        assertEquals(3, getCallerShortcut("s4").getRank());
        assertEquals(3, getCallerShortcut("s4").getRank());
        verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s4"), eq(USER_0));


        mInjectedCurrentTimeMillis += INTERVAL; // reset
        mInjectedCurrentTimeMillis += INTERVAL; // reset


        // Test push as last
        // Test push as last
        Mockito.reset(mMockUsageStatsManagerInternal);
        mManager.pushDynamicShortcut(s5);
        mManager.pushDynamicShortcut(s5);
        assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()),
        assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()),
                "s1", "s2", "s3", "s4", "s5");
                "s1", "s2", "s3", "s4", "s5");
@@ -453,25 +470,34 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
        assertEquals(2, getCallerShortcut("s1").getRank());
        assertEquals(2, getCallerShortcut("s1").getRank());
        assertEquals(3, getCallerShortcut("s2").getRank());
        assertEquals(3, getCallerShortcut("s2").getRank());
        assertEquals(4, getCallerShortcut("s4").getRank());
        assertEquals(4, getCallerShortcut("s4").getRank());
        verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s5"), eq(USER_0));


        // Push when max has already reached
        // Push when max has already reached
        Mockito.reset(mMockUsageStatsManagerInternal);
        mManager.pushDynamicShortcut(s6);
        mManager.pushDynamicShortcut(s6);
        assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()),
        assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()),
                "s1", "s2", "s3", "s5", "s6");
                "s1", "s2", "s3", "s5", "s6");
        assertEquals(0, getCallerShortcut("s6").getRank());
        assertEquals(0, getCallerShortcut("s6").getRank());
        assertEquals(1, getCallerShortcut("s5").getRank());
        assertEquals(1, getCallerShortcut("s5").getRank());
        assertEquals(4, getCallerShortcut("s2").getRank());
        assertEquals(4, getCallerShortcut("s2").getRank());
        verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s6"), eq(USER_0));


        mInjectedCurrentTimeMillis += INTERVAL; // reset
        mInjectedCurrentTimeMillis += INTERVAL; // reset


        // Push with different activity
        // Push with different activity
        Mockito.reset(mMockUsageStatsManagerInternal);
        s7.setActivity(makeComponent(ShortcutActivity2.class));
        s7.setActivity(makeComponent(ShortcutActivity2.class));
        mManager.pushDynamicShortcut(s7);
        mManager.pushDynamicShortcut(s7);
        assertEquals(makeComponent(ShortcutActivity2.class),
        assertEquals(makeComponent(ShortcutActivity2.class),
                getCallerShortcut("s7").getActivity());
                getCallerShortcut("s7").getActivity());
        assertEquals(0, getCallerShortcut("s7").getRank());
        assertEquals(0, getCallerShortcut("s7").getRank());
        verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s7"), eq(USER_0));


        // Push to update shortcut with different activity
        // Push to update shortcut with different activity
        Mockito.reset(mMockUsageStatsManagerInternal);
        final ShortcutInfo s1_2 = makeShortcut("s1");
        final ShortcutInfo s1_2 = makeShortcut("s1");
        s1_2.setActivity(makeComponent(ShortcutActivity2.class));
        s1_2.setActivity(makeComponent(ShortcutActivity2.class));
        s1_2.setRank(1);
        s1_2.setRank(1);
@@ -482,10 +508,13 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
        assertEquals(1, getCallerShortcut("s5").getRank());
        assertEquals(1, getCallerShortcut("s5").getRank());
        assertEquals(2, getCallerShortcut("s3").getRank());
        assertEquals(2, getCallerShortcut("s3").getRank());
        assertEquals(3, getCallerShortcut("s2").getRank());
        assertEquals(3, getCallerShortcut("s2").getRank());
        verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s1"), eq(USER_0));


        mInjectedCurrentTimeMillis += INTERVAL; // reset
        mInjectedCurrentTimeMillis += INTERVAL; // reset


        // Test push when dropped shortcut is cached
        // Test push when dropped shortcut is cached
        Mockito.reset(mMockUsageStatsManagerInternal);
        s8.setLongLived();
        s8.setLongLived();
        s8.setRank(100);
        s8.setRank(100);
        mManager.pushDynamicShortcut(s8);
        mManager.pushDynamicShortcut(s8);
@@ -494,14 +523,19 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
            mInjectCheckAccessShortcutsPermission = true;
            mInjectCheckAccessShortcutsPermission = true;
            mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s8"), HANDLE_USER_0,
            mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s8"), HANDLE_USER_0,
                    CACHE_OWNER_0);
                    CACHE_OWNER_0);
            verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                    eq(CALLING_PACKAGE_1), eq("s8"), eq(USER_0));
        });
        });


        Mockito.reset(mMockUsageStatsManagerInternal);
        mManager.pushDynamicShortcut(s9);
        mManager.pushDynamicShortcut(s9);
        assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()),
        assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()),
                "s1", "s2", "s3", "s5", "s6", "s7", "s9");
                "s1", "s2", "s3", "s5", "s6", "s7", "s9");
        // Verify s13 stayed as cached
        // Verify s13 stayed as cached
        assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
        assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
                "s8");
                "s8");
        verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                eq(CALLING_PACKAGE_1), eq("s9"), eq(USER_0));
    }
    }


    public void testUnlimitedCalls() {
    public void testUnlimitedCalls() {
+0 −2
Original line number Original line Diff line number Diff line
@@ -2138,7 +2138,6 @@ public class ShortcutManagerTest2 extends BaseShortcutManagerTest {
            mManager.reportShortcutUsed("s2");
            mManager.reportShortcutUsed("s2");
            verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
            verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                    eq(CALLING_PACKAGE_1), eq("s2"), eq(USER_10));
                    eq(CALLING_PACKAGE_1), eq("s2"), eq(USER_10));

        });
        });
        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
            // Try with a different package.
            // Try with a different package.
@@ -2158,7 +2157,6 @@ public class ShortcutManagerTest2 extends BaseShortcutManagerTest {
            mManager.reportShortcutUsed("s3");
            mManager.reportShortcutUsed("s3");
            verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
            verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
                    eq(CALLING_PACKAGE_2), eq("s3"), eq(USER_10));
                    eq(CALLING_PACKAGE_2), eq("s3"), eq(USER_10));

        });
        });
    }
    }