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
This commit is contained in:
Pinyao Ting
2021-04-15 13:34:54 -07:00
parent 00475ff23d
commit bbc19b03c2
3 changed files with 46 additions and 8 deletions

View File

@@ -2240,6 +2240,8 @@ public class ShortcutService extends IShortcutService.Stub {
packageShortcutsChanged(packageName, userId, changedShortcuts, removedShortcuts);
reportShortcutUsedInternal(packageName, shortcut.getId(), userId);
verifyStates();
ret.complete(null);
@@ -2850,12 +2852,7 @@ public class ShortcutService extends IShortcutService.Stub {
}
}
final long token = injectClearCallingIdentity();
try {
mUsageStatsManagerInternal.reportShortcutUsage(packageName, shortcutId, userId);
} finally {
injectRestoreCallingIdentity(token);
}
reportShortcutUsedInternal(packageName, shortcutId, userId);
ret.complete(true);
} catch (Exception e) {
ret.completeExceptionally(e);
@@ -2864,6 +2861,15 @@ public class ShortcutService extends IShortcutService.Stub {
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
public boolean isRequestPinItemSupported(int callingUserId, int requestType) {
final long token = injectClearCallingIdentity();

View File

@@ -108,6 +108,7 @@ import com.android.server.pm.ShortcutService.FileOutputStreamWithPath;
import com.android.server.pm.ShortcutUser.PackageWithUser;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@@ -416,8 +417,11 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
mManager.pushDynamicShortcut(s1);
assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()), "s1");
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
Mockito.reset(mMockUsageStatsManagerInternal);
assertTrue(mManager.setDynamicShortcuts(list(s1, s2)));
assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()), "s1", "s2");
mManager.pushDynamicShortcut(s3);
@@ -426,25 +430,38 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
assertEquals(0, getCallerShortcut("s3").getRank());
assertEquals(1, getCallerShortcut("s1").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
// Push with set rank
Mockito.reset(mMockUsageStatsManagerInternal);
s4.setRank(2);
mManager.pushDynamicShortcut(s4);
assertEquals(2, getCallerShortcut("s4").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
Mockito.reset(mMockUsageStatsManagerInternal);
final ShortcutInfo s4_2 = makeShortcut("s4");
s4_2.setRank(4);
mManager.pushDynamicShortcut(s4_2);
assertEquals(2, getCallerShortcut("s2").getRank());
assertEquals(3, getCallerShortcut("s4").getRank());
verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
eq(CALLING_PACKAGE_1), eq("s4"), eq(USER_0));
mInjectedCurrentTimeMillis += INTERVAL; // reset
// Test push as last
Mockito.reset(mMockUsageStatsManagerInternal);
mManager.pushDynamicShortcut(s5);
assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()),
"s1", "s2", "s3", "s4", "s5");
@@ -453,25 +470,34 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
assertEquals(2, getCallerShortcut("s1").getRank());
assertEquals(3, getCallerShortcut("s2").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
Mockito.reset(mMockUsageStatsManagerInternal);
mManager.pushDynamicShortcut(s6);
assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()),
"s1", "s2", "s3", "s5", "s6");
assertEquals(0, getCallerShortcut("s6").getRank());
assertEquals(1, getCallerShortcut("s5").getRank());
assertEquals(4, getCallerShortcut("s2").getRank());
verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
eq(CALLING_PACKAGE_1), eq("s6"), eq(USER_0));
mInjectedCurrentTimeMillis += INTERVAL; // reset
// Push with different activity
Mockito.reset(mMockUsageStatsManagerInternal);
s7.setActivity(makeComponent(ShortcutActivity2.class));
mManager.pushDynamicShortcut(s7);
assertEquals(makeComponent(ShortcutActivity2.class),
getCallerShortcut("s7").getActivity());
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
Mockito.reset(mMockUsageStatsManagerInternal);
final ShortcutInfo s1_2 = makeShortcut("s1");
s1_2.setActivity(makeComponent(ShortcutActivity2.class));
s1_2.setRank(1);
@@ -482,10 +508,13 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
assertEquals(1, getCallerShortcut("s5").getRank());
assertEquals(2, getCallerShortcut("s3").getRank());
assertEquals(3, getCallerShortcut("s2").getRank());
verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
eq(CALLING_PACKAGE_1), eq("s1"), eq(USER_0));
mInjectedCurrentTimeMillis += INTERVAL; // reset
// Test push when dropped shortcut is cached
Mockito.reset(mMockUsageStatsManagerInternal);
s8.setLongLived();
s8.setRank(100);
mManager.pushDynamicShortcut(s8);
@@ -494,14 +523,19 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
mInjectCheckAccessShortcutsPermission = true;
mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s8"), HANDLE_USER_0,
CACHE_OWNER_0);
verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
eq(CALLING_PACKAGE_1), eq("s8"), eq(USER_0));
});
Mockito.reset(mMockUsageStatsManagerInternal);
mManager.pushDynamicShortcut(s9);
assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()),
"s1", "s2", "s3", "s5", "s6", "s7", "s9");
// Verify s13 stayed as cached
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
"s8");
verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
eq(CALLING_PACKAGE_1), eq("s9"), eq(USER_0));
}
public void testUnlimitedCalls() {

View File

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