Merge "Cache shortcuts on People Tile creation" into sc-dev

This commit is contained in:
Flavio Fiszman
2021-02-17 13:26:44 +00:00
committed by Android (Google) Code Review
7 changed files with 105 additions and 22 deletions

View File

@@ -167,10 +167,17 @@ public class LauncherApps {
*/
public static final int FLAG_CACHE_BUBBLE_SHORTCUTS = 1;
/**
* Cache shortcuts which are used in People Tile.
* @hide
*/
public static final int FLAG_CACHE_PEOPLE_TILE_SHORTCUTS = 2;
/** @hide */
@IntDef(flag = false, prefix = { "FLAG_CACHE_" }, value = {
FLAG_CACHE_NOTIFICATION_SHORTCUTS,
FLAG_CACHE_BUBBLE_SHORTCUTS,
FLAG_CACHE_PEOPLE_TILE_SHORTCUTS
})
@Retention(RetentionPolicy.SOURCE)
public @interface ShortcutCacheFlags {}
@@ -1179,6 +1186,7 @@ public class LauncherApps {
* <ul>
* <li>{@link #FLAG_CACHE_NOTIFICATION_SHORTCUTS}
* <li>{@link #FLAG_CACHE_BUBBLE_SHORTCUTS}
* <li>{@link #FLAG_CACHE_PEOPLE_TILE_SHORTCUTS}
* </ul>
* @throws IllegalStateException when the user is locked, or when the {@code user} user
* is locked or not running.
@@ -1209,6 +1217,7 @@ public class LauncherApps {
* <ul>
* <li>{@link #FLAG_CACHE_NOTIFICATION_SHORTCUTS}
* <li>{@link #FLAG_CACHE_BUBBLE_SHORTCUTS}
* <li>{@link #FLAG_CACHE_PEOPLE_TILE_SHORTCUTS}
* </ul>
* @throws IllegalStateException when the user is locked, or when the {@code user} user
* is locked or not running.

View File

@@ -129,6 +129,12 @@ public final class ShortcutInfo implements Parcelable {
/** @hide */
public static final int FLAG_HAS_ICON_URI = 1 << 15;
/**
* TODO(b/155135057): This is a quick and temporary fix for b/155135890. ShortcutService doesn't
* need to be aware of the outside world. Replace this with a more extensible solution.
* @hide
*/
public static final int FLAG_CACHED_PEOPLE_TILE = 1 << 29;
/**
* TODO(b/155135057): This is a quick and temporary fix for b/155135890. ShortcutService doesn't
@@ -138,7 +144,8 @@ public final class ShortcutInfo implements Parcelable {
public static final int FLAG_CACHED_BUBBLES = 1 << 30;
/** @hide */
public static final int FLAG_CACHED_ALL = FLAG_CACHED_NOTIFICATIONS | FLAG_CACHED_BUBBLES;
public static final int FLAG_CACHED_ALL =
FLAG_CACHED_NOTIFICATIONS | FLAG_CACHED_BUBBLES | FLAG_CACHED_PEOPLE_TILE;
/** @hide */
@IntDef(flag = true, prefix = { "FLAG_" }, value = {
@@ -159,6 +166,7 @@ public final class ShortcutInfo implements Parcelable {
FLAG_HAS_ICON_URI,
FLAG_CACHED_NOTIFICATIONS,
FLAG_CACHED_BUBBLES,
FLAG_CACHED_PEOPLE_TILE
})
@Retention(RetentionPolicy.SOURCE)
public @interface ShortcutFlags {}

View File

@@ -19,6 +19,8 @@ package com.android.systemui.people;
import static android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID;
import static android.appwidget.AppWidgetManager.INVALID_APPWIDGET_ID;
import static com.android.systemui.people.PeopleSpaceUtils.getUserHandle;
import android.app.Activity;
import android.app.INotificationManager;
import android.app.people.IPeopleManager;
@@ -39,6 +41,7 @@ import com.android.internal.logging.UiEventLoggerImpl;
import com.android.systemui.R;
import com.android.systemui.statusbar.notification.NotificationEntryManager;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
@@ -49,6 +52,7 @@ import javax.inject.Inject;
public class PeopleSpaceActivity extends Activity {
private static final String TAG = "PeopleSpaceActivity";
private static final boolean DEBUG = PeopleSpaceUtils.DEBUG;
private ViewGroup mPeopleSpaceLayout;
private IPeopleManager mPeopleManager;
@@ -134,9 +138,11 @@ public class PeopleSpaceActivity extends Activity {
/** Stores the user selected configuration for {@code mAppWidgetId}. */
private void storeWidgetConfiguration(PeopleSpaceTile tile) {
if (PeopleSpaceUtils.DEBUG) {
Log.d(TAG, "Put " + tile.getUserName() + "'s shortcut ID: "
+ tile.getId() + " for widget ID: "
+ mAppWidgetId);
if (DEBUG) {
Log.d(TAG, "Put " + tile.getUserName() + "'s shortcut ID: "
+ tile.getId() + " for widget ID: "
+ mAppWidgetId);
}
}
PeopleSpaceUtils.setStorageForTile(mContext, tile, mAppWidgetId);
@@ -144,12 +150,22 @@ public class PeopleSpaceActivity extends Activity {
// TODO: Populate new widget with existing conversation notification, if there is any.
PeopleSpaceUtils.updateSingleConversationWidgets(mContext, widgetIds, mAppWidgetManager,
mPeopleManager);
if (mLauncherApps != null) {
try {
if (DEBUG) Log.d(TAG, "Caching shortcut for PeopleTile: " + tile.getId());
mLauncherApps.cacheShortcuts(tile.getPackageName(),
Collections.singletonList(tile.getId()),
getUserHandle(tile), LauncherApps.FLAG_CACHE_PEOPLE_TILE_SHORTCUTS);
} catch (Exception e) {
Log.w(TAG, "Exception caching shortcut:" + e);
}
}
finishActivity();
}
/** Finish activity with a successful widget configuration result. */
private void finishActivity() {
if (PeopleSpaceUtils.DEBUG) Log.d(TAG, "Widget added!");
if (DEBUG) Log.d(TAG, "Widget added!");
mUiEventLogger.log(PeopleSpaceUtils.PeopleSpaceWidgetEvent.PEOPLE_SPACE_WIDGET_ADDED);
setActivityResult(RESULT_OK);
finish();

View File

@@ -287,7 +287,7 @@ public class PeopleSpaceUtils {
SharedPreferences.Editor widgetEditor = widgetSp.edit();
widgetEditor.putString(PeopleSpaceUtils.PACKAGE_NAME, tile.getPackageName());
widgetEditor.putString(PeopleSpaceUtils.SHORTCUT_ID, tile.getId());
int userId = UserHandle.getUserHandleForUid(tile.getUid()).getIdentifier();
int userId = getUserId(tile);
widgetEditor.putInt(PeopleSpaceUtils.USER_ID, userId);
widgetEditor.apply();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
@@ -634,7 +634,7 @@ public class PeopleSpaceUtils {
private static Long getLastInteraction(IPeopleManager peopleManager,
PeopleSpaceTile tile) {
try {
int userId = UserHandle.getUserHandleForUid(tile.getUid()).getIdentifier();
int userId = getUserId(tile);
String pkg = tile.getPackageName();
return peopleManager.getLastInteraction(pkg, userId, tile.getId());
} catch (Exception e) {
@@ -883,4 +883,14 @@ public class PeopleSpaceUtils {
public static String getKey(String shortcutId, String packageName, int userId) {
return shortcutId + "/" + userId + "/" + packageName;
}
/** Returns the userId associated with a {@link PeopleSpaceTile} */
public static int getUserId(PeopleSpaceTile tile) {
return getUserHandle(tile).getIdentifier();
}
/** Returns the {@link UserHandle} associated with a {@link PeopleSpaceTile} */
public static UserHandle getUserHandle(PeopleSpaceTile tile) {
return UserHandle.getUserHandleForUid(tile.getUid());
}
}

View File

@@ -16,13 +16,20 @@
package com.android.systemui.people.widget;
import static com.android.systemui.people.PeopleSpaceUtils.PACKAGE_NAME;
import static com.android.systemui.people.PeopleSpaceUtils.SHORTCUT_ID;
import static com.android.systemui.people.PeopleSpaceUtils.USER_ID;
import android.app.PendingIntent;
import android.app.people.IPeopleManager;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.LauncherApps;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Log;
import android.widget.RemoteViews;
@@ -32,6 +39,8 @@ import com.android.internal.logging.UiEventLoggerImpl;
import com.android.systemui.R;
import com.android.systemui.people.PeopleSpaceUtils;
import java.util.Collections;
/** People Space Widget Provider class. */
public class PeopleSpaceWidgetProvider extends AppWidgetProvider {
private static final String TAG = "PeopleSpaceWidgetPvd";
@@ -88,11 +97,31 @@ public class PeopleSpaceWidgetProvider extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
LauncherApps launcherApps = context.getSystemService(LauncherApps.class);
for (int widgetId : appWidgetIds) {
if (DEBUG) Log.d(TAG, "Widget removed");
mUiEventLogger.log(PeopleSpaceUtils.PeopleSpaceWidgetEvent.PEOPLE_SPACE_WIDGET_DELETED);
if (launcherApps != null) {
SharedPreferences widgetSp = context.getSharedPreferences(String.valueOf(widgetId),
Context.MODE_PRIVATE);
String packageName = widgetSp.getString(PACKAGE_NAME, null);
String shortcutId = widgetSp.getString(SHORTCUT_ID, null);
int userId = widgetSp.getInt(USER_ID, -1);
if (packageName != null && shortcutId != null && userId != -1) {
try {
if (DEBUG) Log.d(TAG, "Uncaching shortcut for PeopleTile: " + shortcutId);
launcherApps.uncacheShortcuts(packageName,
Collections.singletonList(shortcutId),
UserHandle.of(userId),
LauncherApps.FLAG_CACHE_PEOPLE_TILE_SHORTCUTS);
} catch (Exception e) {
Log.d(TAG, "Exception uncaching shortcut:" + e);
}
}
}
PeopleSpaceUtils.removeStorageForTile(context, widgetId);
}
}
}

View File

@@ -22,6 +22,7 @@ import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
import static android.content.pm.LauncherApps.FLAG_CACHE_BUBBLE_SHORTCUTS;
import static android.content.pm.LauncherApps.FLAG_CACHE_NOTIFICATION_SHORTCUTS;
import static android.content.pm.LauncherApps.FLAG_CACHE_PEOPLE_TILE_SHORTCUTS;
import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -1170,6 +1171,8 @@ public class LauncherAppsService extends SystemService {
ret = ShortcutInfo.FLAG_CACHED_NOTIFICATIONS;
} else if (cacheFlags == FLAG_CACHE_BUBBLE_SHORTCUTS) {
ret = ShortcutInfo.FLAG_CACHED_BUBBLES;
} else if (cacheFlags == FLAG_CACHE_PEOPLE_TILE_SHORTCUTS) {
ret = ShortcutInfo.FLAG_CACHED_PEOPLE_TILE;
}
Preconditions.checkArgumentPositive(ret, "Invalid cache owner");

View File

@@ -103,7 +103,6 @@ import android.util.TypedXmlSerializer;
import android.util.Xml;
import com.android.frameworks.servicestests.R;
import com.android.internal.util.FastXmlSerializer;
import com.android.server.pm.ShortcutService.ConfigConstants;
import com.android.server.pm.ShortcutService.FileOutputStreamWithPath;
import com.android.server.pm.ShortcutUser.PackageWithUser;
@@ -111,7 +110,6 @@ import com.android.server.pm.ShortcutUser.PackageWithUser;
import org.mockito.ArgumentCaptor;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -141,6 +139,7 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
private static final int CACHE_OWNER_0 = LauncherApps.FLAG_CACHE_NOTIFICATION_SHORTCUTS;
private static final int CACHE_OWNER_1 = LauncherApps.FLAG_CACHE_BUBBLE_SHORTCUTS;
private static final int CACHE_OWNER_2 = LauncherApps.FLAG_CACHE_PEOPLE_TILE_SHORTCUTS;
@Override
protected void tearDown() throws Exception {
@@ -1531,7 +1530,8 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
assertTrue(mManager.setDynamicShortcuts(list(makeShortcut("s1"),
makeLongLivedShortcut("s2"), makeLongLivedShortcut("s3"),
makeLongLivedShortcut("s4"))));
makeLongLivedShortcut("s4"), makeLongLivedShortcut("s5"),
makeLongLivedShortcut("s6"))));
});
// Pin s2
@@ -1545,28 +1545,30 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
mInjectCheckAccessShortcutsPermission = true;
mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s1", "s2"),
HANDLE_USER_0, CACHE_OWNER_0);
mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s2", "s4"),
mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s2", "s4", "s5"),
HANDLE_USER_0, CACHE_OWNER_1);
mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s5", "s6"),
HANDLE_USER_0, CACHE_OWNER_2);
});
setCaller(CALLING_PACKAGE_1);
// Get dynamic shortcuts
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
"s1", "s2", "s3", "s4");
"s1", "s2", "s3", "s4", "s5", "s6");
// Get pinned shortcuts
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_PINNED),
"s2");
// Get cached shortcuts
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
"s2", "s4");
"s2", "s4", "s5", "s6");
// Remove a dynamic cached shortcut
mManager.removeDynamicShortcuts(list("s4"));
mManager.removeDynamicShortcuts(list("s4", "s5"));
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
"s1", "s2", "s3");
"s1", "s2", "s3", "s6");
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
"s2", "s4");
"s2", "s4", "s5", "s6");
runWithCaller(LAUNCHER_1, USER_0, () -> {
mLauncherApps.uncacheShortcuts(CALLING_PACKAGE_1, list("s2", "s4"),
@@ -1574,15 +1576,21 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
});
// s2 still cached by owner1. s4 wasn't cached by owner0 so didn't get removed.
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
"s2", "s4");
"s2", "s4", "s5", "s6");
// uncache a non-dynamic shortcut. Should be removed.
runWithCaller(LAUNCHER_1, USER_0, () -> {
mLauncherApps.uncacheShortcuts(CALLING_PACKAGE_1, list("s4"),
HANDLE_USER_0, CACHE_OWNER_1);
});
// uncache s6 by its only owner. s5 still cached by owner1
runWithCaller(LAUNCHER_1, USER_0, () -> {
mLauncherApps.uncacheShortcuts(CALLING_PACKAGE_1, list("s5", "s6"),
HANDLE_USER_0, CACHE_OWNER_2);
});
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
"s2");
"s2", "s5");
// Cache another shortcut
runWithCaller(LAUNCHER_1, USER_0, () -> {
@@ -1590,14 +1598,14 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
HANDLE_USER_0, CACHE_OWNER_0);
});
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
"s2", "s3");
"s2", "s3", "s5");
// Remove a dynamic cached pinned long lived shortcut
mManager.removeLongLivedShortcuts(list("s2"));
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
"s1", "s3");
"s1", "s3", "s6");
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
"s3");
"s3", "s5");
assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_PINNED),
"s2");
}