Merge "Keep the conversation shortcut criteria in People Service consistent with the one in Notification Manager" into rvc-dev am: 068558b44b

Change-Id: I426df513a4228724eeb3dc199409e852e696413a
This commit is contained in:
Danning Chen
2020-04-21 20:12:15 +00:00
committed by Automerger Merge Worker
5 changed files with 55 additions and 20 deletions

View File

@@ -21,7 +21,6 @@ import static android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC;
import static android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED;
import android.annotation.NonNull;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.LauncherApps;
import android.content.pm.ShortcutInfo;
@@ -41,9 +40,18 @@ import java.util.List;
/**
* Helper for querying shortcuts.
*/
class ShortcutHelper {
public class ShortcutHelper {
private static final String TAG = "ShortcutHelper";
private static final IntentFilter SHARING_FILTER = new IntentFilter();
static {
try {
SHARING_FILTER.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
Slog.e(TAG, "Bad mime type", e);
}
}
/**
* Listener to call when a shortcut we're tracking has been removed.
*/
@@ -54,7 +62,6 @@ class ShortcutHelper {
private LauncherApps mLauncherAppsService;
private ShortcutListener mShortcutListener;
private ShortcutServiceInternal mShortcutServiceInternal;
private IntentFilter mSharingFilter;
// Key: packageName Value: <shortcutId, notifId>
private HashMap<String, HashMap<String, String>> mActiveShortcutBubbles = new HashMap<>();
@@ -122,12 +129,6 @@ class ShortcutHelper {
ShortcutServiceInternal shortcutServiceInternal) {
mLauncherAppsService = launcherApps;
mShortcutListener = listener;
mSharingFilter = new IntentFilter();
try {
mSharingFilter.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
Slog.e(TAG, "Bad mime type", e);
}
mShortcutServiceInternal = shortcutServiceInternal;
}
@@ -142,7 +143,21 @@ class ShortcutHelper {
}
/**
* Only returns shortcut info if it's found and if it's {@link ShortcutInfo#isLongLived()}.
* Returns whether the given shortcut info is a conversation shortcut.
*/
public static boolean isConversationShortcut(
ShortcutInfo shortcutInfo, ShortcutServiceInternal mShortcutServiceInternal,
int callingUserId) {
if (shortcutInfo == null || !shortcutInfo.isLongLived() || !shortcutInfo.isEnabled()) {
return false;
}
return mShortcutServiceInternal.isSharingShortcut(callingUserId, "android",
shortcutInfo.getPackage(), shortcutInfo.getId(), shortcutInfo.getUserId(),
SHARING_FILTER);
}
/**
* Only returns shortcut info if it's found and if it's a conversation shortcut.
*/
ShortcutInfo getValidShortcutInfo(String shortcutId, String packageName, UserHandle user) {
if (mLauncherAppsService == null) {
@@ -161,11 +176,7 @@ class ShortcutHelper {
ShortcutInfo info = shortcuts != null && shortcuts.size() > 0
? shortcuts.get(0)
: null;
if (info == null || !info.isLongLived() || !info.isEnabled()) {
return null;
}
if (mShortcutServiceInternal.isSharingShortcut(user.getIdentifier(),
"android", packageName, shortcutId, user.getIdentifier(), mSharingFilter)) {
if (isConversationShortcut(info, mShortcutServiceInternal, user.getIdentifier())) {
return info;
}
return null;

View File

@@ -67,6 +67,7 @@ import com.android.internal.os.BackgroundThread;
import com.android.internal.telephony.SmsApplication;
import com.android.server.LocalServices;
import com.android.server.notification.NotificationManagerInternal;
import com.android.server.notification.ShortcutHelper;
import java.util.ArrayList;
import java.util.Collections;
@@ -497,10 +498,6 @@ public class DataManager {
EventStore.CATEGORY_SHORTCUT_BASED, shortcutId);
}
private boolean isPersonShortcut(@NonNull ShortcutInfo shortcutInfo) {
return shortcutInfo.getPersons() != null && shortcutInfo.getPersons().length != 0;
}
@VisibleForTesting
@WorkerThread
void addOrUpdateConversationInfo(@NonNull ShortcutInfo shortcutInfo) {
@@ -712,7 +709,8 @@ public class DataManager {
@NonNull List<ShortcutInfo> shortcuts, @NonNull UserHandle user) {
mInjector.getBackgroundExecutor().execute(() -> {
for (ShortcutInfo shortcut : shortcuts) {
if (isPersonShortcut(shortcut)) {
if (ShortcutHelper.isConversationShortcut(
shortcut, mShortcutServiceInternal, user.getIdentifier())) {
addOrUpdateConversationInfo(shortcut);
}
}

View File

@@ -215,6 +215,8 @@ public final class DataManagerTest {
mDataManager = new DataManager(mContext, mInjector);
mDataManager.initialize();
when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(),
anyString(), anyInt(), any())).thenReturn(true);
verify(mShortcutServiceInternal).addShortcutChangeCallback(
mShortcutChangeCallbackCaptor.capture());
mShortcutChangeCallback = mShortcutChangeCallbackCaptor.getValue();

View File

@@ -6074,6 +6074,9 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
// Pretend the shortcut exists
List<ShortcutInfo> shortcutInfos = new ArrayList<>();
ShortcutInfo info = mock(ShortcutInfo.class);
when(info.getPackage()).thenReturn(PKG);
when(info.getId()).thenReturn("someshortcutId");
when(info.getUserId()).thenReturn(USER_SYSTEM);
when(info.isLongLived()).thenReturn(true);
when(info.isEnabled()).thenReturn(true);
shortcutInfos.add(info);
@@ -6137,6 +6140,9 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
// Pretend the shortcut exists
List<ShortcutInfo> shortcutInfos = new ArrayList<>();
ShortcutInfo info = mock(ShortcutInfo.class);
when(info.getPackage()).thenReturn(PKG);
when(info.getId()).thenReturn("someshortcutId");
when(info.getUserId()).thenReturn(USER_SYSTEM);
when(info.isLongLived()).thenReturn(true);
when(info.isEnabled()).thenReturn(true);
shortcutInfos.add(info);
@@ -6483,6 +6489,9 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
when(mPreferencesHelper.getConversations(anyString(), anyInt())).thenReturn(convos);
ShortcutInfo si = mock(ShortcutInfo.class);
when(si.getPackage()).thenReturn(PKG_P);
when(si.getId()).thenReturn("convo");
when(si.getUserId()).thenReturn(USER_SYSTEM);
when(si.getShortLabel()).thenReturn("Hello");
when(si.isLongLived()).thenReturn(true);
when(si.isEnabled()).thenReturn(true);
@@ -6514,6 +6523,9 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
when(mPreferencesHelper.getConversations(anyString(), anyInt())).thenReturn(convos);
ShortcutInfo si = mock(ShortcutInfo.class);
when(si.getPackage()).thenReturn(PKG_P);
when(si.getId()).thenReturn("convo");
when(si.getUserId()).thenReturn(USER_SYSTEM);
when(si.getShortLabel()).thenReturn("Hello");
when(si.isLongLived()).thenReturn(false);
when(mLauncherApps.getShortcuts(any(), any())).thenReturn(Arrays.asList(si));

View File

@@ -170,6 +170,9 @@ public class ShortcutHelperTest extends UiServiceTestCase {
@Test
public void testGetValidShortcutInfo_notLongLived() {
ShortcutInfo si = mock(ShortcutInfo.class);
when(si.getPackage()).thenReturn(PKG);
when(si.getId()).thenReturn(SHORTCUT_ID);
when(si.getUserId()).thenReturn(UserHandle.USER_SYSTEM);
when(si.isLongLived()).thenReturn(false);
when(si.isEnabled()).thenReturn(true);
ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
@@ -184,6 +187,9 @@ public class ShortcutHelperTest extends UiServiceTestCase {
@Test
public void testGetValidShortcutInfo_notSharingShortcut() {
ShortcutInfo si = mock(ShortcutInfo.class);
when(si.getPackage()).thenReturn(PKG);
when(si.getId()).thenReturn(SHORTCUT_ID);
when(si.getUserId()).thenReturn(UserHandle.USER_SYSTEM);
when(si.isLongLived()).thenReturn(true);
when(si.isEnabled()).thenReturn(true);
ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
@@ -198,6 +204,9 @@ public class ShortcutHelperTest extends UiServiceTestCase {
@Test
public void testGetValidShortcutInfo_notEnabled() {
ShortcutInfo si = mock(ShortcutInfo.class);
when(si.getPackage()).thenReturn(PKG);
when(si.getId()).thenReturn(SHORTCUT_ID);
when(si.getUserId()).thenReturn(UserHandle.USER_SYSTEM);
when(si.isLongLived()).thenReturn(true);
when(si.isEnabled()).thenReturn(false);
ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
@@ -212,6 +221,9 @@ public class ShortcutHelperTest extends UiServiceTestCase {
@Test
public void testGetValidShortcutInfo_isValid() {
ShortcutInfo si = mock(ShortcutInfo.class);
when(si.getPackage()).thenReturn(PKG);
when(si.getId()).thenReturn(SHORTCUT_ID);
when(si.getUserId()).thenReturn(UserHandle.USER_SYSTEM);
when(si.isLongLived()).thenReturn(true);
when(si.isEnabled()).thenReturn(true);
ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();