Removing global state from DeepShortcutManager
This makes DeepShortcutManager thread-safe and avoids any locks Change-Id: If4593b3541da6259591ff7a607efa58158006481
This commit is contained in:
parent
273c079761
commit
31ab19f71d
|
@ -26,57 +26,46 @@ import android.os.UserHandle;
|
||||||
|
|
||||||
import com.android.launcher3.ItemInfo;
|
import com.android.launcher3.ItemInfo;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs operations related to deep shortcuts, such as querying for them, pinning them, etc.
|
* Performs operations related to deep shortcuts, such as querying for them, pinning them, etc.
|
||||||
*/
|
*/
|
||||||
public class DeepShortcutManager {
|
public class DeepShortcutManager {
|
||||||
private static DeepShortcutManager sInstance;
|
|
||||||
private static final Object sInstanceLock = new Object();
|
private static final DeepShortcutManager sInstance = new DeepShortcutManager();
|
||||||
|
|
||||||
public static DeepShortcutManager getInstance(Context context) {
|
public static DeepShortcutManager getInstance(Context context) {
|
||||||
synchronized (sInstanceLock) {
|
return sInstance;
|
||||||
if (sInstance == null) {
|
|
||||||
sInstance = new DeepShortcutManager(context.getApplicationContext());
|
|
||||||
}
|
|
||||||
return sInstance;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private DeepShortcutManager(Context context) {
|
private final QueryResult mFailure = new QueryResult();
|
||||||
}
|
|
||||||
|
private DeepShortcutManager() { }
|
||||||
|
|
||||||
public static boolean supportsShortcuts(ItemInfo info) {
|
public static boolean supportsShortcuts(ItemInfo info) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean wasLastCallSuccess() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onShortcutsChanged(List<ShortcutInfo> shortcuts) {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries for the shortcuts with the package name and provided ids.
|
* Queries for the shortcuts with the package name and provided ids.
|
||||||
*
|
*
|
||||||
* This method is intended to get the full details for shortcuts when they are added or updated,
|
* This method is intended to get the full details for shortcuts when they are added or updated,
|
||||||
* because we only get "key" fields in onShortcutsChanged().
|
* because we only get "key" fields in onShortcutsChanged().
|
||||||
*/
|
*/
|
||||||
public List<ShortcutInfo> queryForFullDetails(String packageName,
|
public QueryResult queryForFullDetails(String packageName,
|
||||||
List<String> shortcutIds, UserHandle user) {
|
List<String> shortcutIds, UserHandle user) {
|
||||||
return Collections.emptyList();
|
return mFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all the manifest and dynamic shortcuts associated with the given package and user,
|
* Gets all the manifest and dynamic shortcuts associated with the given package and user,
|
||||||
* to be displayed in the shortcuts container on long press.
|
* to be displayed in the shortcuts container on long press.
|
||||||
*/
|
*/
|
||||||
public List<ShortcutInfo> queryForShortcutsContainer(ComponentName activity,
|
public QueryResult queryForShortcutsContainer(ComponentName activity,
|
||||||
UserHandle user) {
|
UserHandle user) {
|
||||||
return Collections.emptyList();
|
return mFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -106,20 +95,28 @@ public class DeepShortcutManager {
|
||||||
*
|
*
|
||||||
* If packageName is null, returns all pinned shortcuts regardless of package.
|
* If packageName is null, returns all pinned shortcuts regardless of package.
|
||||||
*/
|
*/
|
||||||
public List<ShortcutInfo> queryForPinnedShortcuts(String packageName, UserHandle user) {
|
public QueryResult queryForPinnedShortcuts(String packageName, UserHandle user) {
|
||||||
return Collections.emptyList();
|
return mFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ShortcutInfo> queryForPinnedShortcuts(String packageName,
|
public QueryResult queryForPinnedShortcuts(String packageName,
|
||||||
List<String> shortcutIds, UserHandle user) {
|
List<String> shortcutIds, UserHandle user) {
|
||||||
return Collections.emptyList();
|
return mFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ShortcutInfo> queryForAllShortcuts(UserHandle user) {
|
public QueryResult queryForAllShortcuts(UserHandle user) {
|
||||||
return Collections.emptyList();
|
return mFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasHostPermission() {
|
public boolean hasHostPermission() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class QueryResult extends ArrayList<ShortcutInfo> {
|
||||||
|
|
||||||
|
public boolean wasSuccess() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -317,9 +317,9 @@ public class LoaderTask implements Runnable {
|
||||||
|
|
||||||
// We can only query for shortcuts when the user is unlocked.
|
// We can only query for shortcuts when the user is unlocked.
|
||||||
if (userUnlocked) {
|
if (userUnlocked) {
|
||||||
List<ShortcutInfo> pinnedShortcuts =
|
DeepShortcutManager.QueryResult pinnedShortcuts =
|
||||||
mShortcutManager.queryForPinnedShortcuts(null, user);
|
mShortcutManager.queryForPinnedShortcuts(null, user);
|
||||||
if (mShortcutManager.wasLastCallSuccess()) {
|
if (pinnedShortcuts.wasSuccess()) {
|
||||||
for (ShortcutInfo shortcut : pinnedShortcuts) {
|
for (ShortcutInfo shortcut : pinnedShortcuts) {
|
||||||
shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
|
shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
|
||||||
shortcut);
|
shortcut);
|
||||||
|
|
|
@ -37,7 +37,6 @@ import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Task to handle changing of lock state of the user
|
* Task to handle changing of lock state of the user
|
||||||
|
@ -58,9 +57,9 @@ public class UserLockStateChangedTask extends BaseModelUpdateTask {
|
||||||
|
|
||||||
HashMap<ShortcutKey, ShortcutInfo> pinnedShortcuts = new HashMap<>();
|
HashMap<ShortcutKey, ShortcutInfo> pinnedShortcuts = new HashMap<>();
|
||||||
if (isUserUnlocked) {
|
if (isUserUnlocked) {
|
||||||
List<ShortcutInfo> shortcuts =
|
DeepShortcutManager.QueryResult shortcuts =
|
||||||
deepShortcutManager.queryForPinnedShortcuts(null, mUser);
|
deepShortcutManager.queryForPinnedShortcuts(null, mUser);
|
||||||
if (deepShortcutManager.wasLastCallSuccess()) {
|
if (shortcuts.wasSuccess()) {
|
||||||
for (ShortcutInfo shortcut : shortcuts) {
|
for (ShortcutInfo shortcut : shortcuts) {
|
||||||
pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
|
pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,19 +45,15 @@ public class DeepShortcutManager {
|
||||||
| ShortcutQuery.FLAG_MATCH_MANIFEST | ShortcutQuery.FLAG_MATCH_PINNED;
|
| ShortcutQuery.FLAG_MATCH_MANIFEST | ShortcutQuery.FLAG_MATCH_PINNED;
|
||||||
|
|
||||||
private static DeepShortcutManager sInstance;
|
private static DeepShortcutManager sInstance;
|
||||||
private static final Object sInstanceLock = new Object();
|
|
||||||
|
|
||||||
public static DeepShortcutManager getInstance(Context context) {
|
public static DeepShortcutManager getInstance(Context context) {
|
||||||
synchronized (sInstanceLock) {
|
if (sInstance == null) {
|
||||||
if (sInstance == null) {
|
sInstance = new DeepShortcutManager(context.getApplicationContext());
|
||||||
sInstance = new DeepShortcutManager(context.getApplicationContext());
|
|
||||||
}
|
|
||||||
return sInstance;
|
|
||||||
}
|
}
|
||||||
|
return sInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final LauncherApps mLauncherApps;
|
private final LauncherApps mLauncherApps;
|
||||||
private boolean mWasLastCallSuccess;
|
|
||||||
|
|
||||||
private DeepShortcutManager(Context context) {
|
private DeepShortcutManager(Context context) {
|
||||||
mLauncherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
|
mLauncherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
|
||||||
|
@ -70,17 +66,13 @@ public class DeepShortcutManager {
|
||||||
&& !info.isDisabled() && !isItemPromise;
|
&& !info.isDisabled() && !isItemPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean wasLastCallSuccess() {
|
|
||||||
return mWasLastCallSuccess;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries for the shortcuts with the package name and provided ids.
|
* Queries for the shortcuts with the package name and provided ids.
|
||||||
*
|
*
|
||||||
* This method is intended to get the full details for shortcuts when they are added or updated,
|
* This method is intended to get the full details for shortcuts when they are added or updated,
|
||||||
* because we only get "key" fields in onShortcutsChanged().
|
* because we only get "key" fields in onShortcutsChanged().
|
||||||
*/
|
*/
|
||||||
public List<ShortcutInfo> queryForFullDetails(String packageName,
|
public QueryResult queryForFullDetails(String packageName,
|
||||||
List<String> shortcutIds, UserHandle user) {
|
List<String> shortcutIds, UserHandle user) {
|
||||||
return query(FLAG_GET_ALL, packageName, null, shortcutIds, user);
|
return query(FLAG_GET_ALL, packageName, null, shortcutIds, user);
|
||||||
}
|
}
|
||||||
|
@ -89,7 +81,7 @@ public class DeepShortcutManager {
|
||||||
* Gets all the manifest and dynamic shortcuts associated with the given package and user,
|
* Gets all the manifest and dynamic shortcuts associated with the given package and user,
|
||||||
* to be displayed in the shortcuts container on long press.
|
* to be displayed in the shortcuts container on long press.
|
||||||
*/
|
*/
|
||||||
public List<ShortcutInfo> queryForShortcutsContainer(ComponentName activity,
|
public QueryResult queryForShortcutsContainer(ComponentName activity,
|
||||||
UserHandle user) {
|
UserHandle user) {
|
||||||
return query(ShortcutQuery.FLAG_MATCH_MANIFEST | ShortcutQuery.FLAG_MATCH_DYNAMIC,
|
return query(ShortcutQuery.FLAG_MATCH_MANIFEST | ShortcutQuery.FLAG_MATCH_DYNAMIC,
|
||||||
activity.getPackageName(), activity, null, user);
|
activity.getPackageName(), activity, null, user);
|
||||||
|
@ -107,10 +99,8 @@ public class DeepShortcutManager {
|
||||||
pinnedIds.remove(id);
|
pinnedIds.remove(id);
|
||||||
try {
|
try {
|
||||||
mLauncherApps.pinShortcuts(packageName, pinnedIds, user);
|
mLauncherApps.pinShortcuts(packageName, pinnedIds, user);
|
||||||
mWasLastCallSuccess = true;
|
|
||||||
} catch (SecurityException|IllegalStateException e) {
|
} catch (SecurityException|IllegalStateException e) {
|
||||||
Log.w(TAG, "Failed to unpin shortcut", e);
|
Log.w(TAG, "Failed to unpin shortcut", e);
|
||||||
mWasLastCallSuccess = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,10 +116,8 @@ public class DeepShortcutManager {
|
||||||
pinnedIds.add(id);
|
pinnedIds.add(id);
|
||||||
try {
|
try {
|
||||||
mLauncherApps.pinShortcuts(packageName, pinnedIds, user);
|
mLauncherApps.pinShortcuts(packageName, pinnedIds, user);
|
||||||
mWasLastCallSuccess = true;
|
|
||||||
} catch (SecurityException|IllegalStateException e) {
|
} catch (SecurityException|IllegalStateException e) {
|
||||||
Log.w(TAG, "Failed to pin shortcut", e);
|
Log.w(TAG, "Failed to pin shortcut", e);
|
||||||
mWasLastCallSuccess = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,23 +126,18 @@ public class DeepShortcutManager {
|
||||||
try {
|
try {
|
||||||
mLauncherApps.startShortcut(packageName, id, sourceBounds,
|
mLauncherApps.startShortcut(packageName, id, sourceBounds,
|
||||||
startActivityOptions, user);
|
startActivityOptions, user);
|
||||||
mWasLastCallSuccess = true;
|
|
||||||
} catch (SecurityException|IllegalStateException e) {
|
} catch (SecurityException|IllegalStateException e) {
|
||||||
Log.e(TAG, "Failed to start shortcut", e);
|
Log.e(TAG, "Failed to start shortcut", e);
|
||||||
mWasLastCallSuccess = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Drawable getShortcutIconDrawable(ShortcutInfo shortcutInfo, int density) {
|
public Drawable getShortcutIconDrawable(ShortcutInfo shortcutInfo, int density) {
|
||||||
try {
|
try {
|
||||||
Drawable icon = mLauncherApps.getShortcutIconDrawable(shortcutInfo, density);
|
return mLauncherApps.getShortcutIconDrawable(shortcutInfo, density);
|
||||||
mWasLastCallSuccess = true;
|
|
||||||
return icon;
|
|
||||||
} catch (SecurityException|IllegalStateException e) {
|
} catch (SecurityException|IllegalStateException e) {
|
||||||
Log.e(TAG, "Failed to get shortcut icon", e);
|
Log.e(TAG, "Failed to get shortcut icon", e);
|
||||||
mWasLastCallSuccess = false;
|
return null;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -162,20 +145,20 @@ public class DeepShortcutManager {
|
||||||
*
|
*
|
||||||
* If packageName is null, returns all pinned shortcuts regardless of package.
|
* If packageName is null, returns all pinned shortcuts regardless of package.
|
||||||
*/
|
*/
|
||||||
public List<ShortcutInfo> queryForPinnedShortcuts(String packageName, UserHandle user) {
|
public QueryResult queryForPinnedShortcuts(String packageName, UserHandle user) {
|
||||||
return queryForPinnedShortcuts(packageName, null, user);
|
return queryForPinnedShortcuts(packageName, null, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ShortcutInfo> queryForPinnedShortcuts(String packageName,
|
public QueryResult queryForPinnedShortcuts(String packageName, List<String> shortcutIds,
|
||||||
List<String> shortcutIds, UserHandle user) {
|
UserHandle user) {
|
||||||
return query(ShortcutQuery.FLAG_MATCH_PINNED, packageName, null, shortcutIds, user);
|
return query(ShortcutQuery.FLAG_MATCH_PINNED, packageName, null, shortcutIds, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ShortcutInfo> queryForAllShortcuts(UserHandle user) {
|
public QueryResult queryForAllShortcuts(UserHandle user) {
|
||||||
return query(FLAG_GET_ALL, null, null, null, user);
|
return query(FLAG_GET_ALL, null, null, null, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> extractIds(List<ShortcutInfo> shortcuts) {
|
private static List<String> extractIds(List<ShortcutInfo> shortcuts) {
|
||||||
List<String> shortcutIds = new ArrayList<>(shortcuts.size());
|
List<String> shortcutIds = new ArrayList<>(shortcuts.size());
|
||||||
for (ShortcutInfo shortcut : shortcuts) {
|
for (ShortcutInfo shortcut : shortcuts) {
|
||||||
shortcutIds.add(shortcut.getId());
|
shortcutIds.add(shortcut.getId());
|
||||||
|
@ -189,8 +172,8 @@ public class DeepShortcutManager {
|
||||||
*
|
*
|
||||||
* TODO: Use the cache to optimize this so we don't make an RPC every time.
|
* TODO: Use the cache to optimize this so we don't make an RPC every time.
|
||||||
*/
|
*/
|
||||||
private List<ShortcutInfo> query(int flags, String packageName,
|
private QueryResult query(int flags, String packageName, ComponentName activity,
|
||||||
ComponentName activity, List<String> shortcutIds, UserHandle user) {
|
List<String> shortcutIds, UserHandle user) {
|
||||||
ShortcutQuery q = new ShortcutQuery();
|
ShortcutQuery q = new ShortcutQuery();
|
||||||
q.setQueryFlags(flags);
|
q.setQueryFlags(flags);
|
||||||
if (packageName != null) {
|
if (packageName != null) {
|
||||||
|
@ -198,18 +181,12 @@ public class DeepShortcutManager {
|
||||||
q.setActivity(activity);
|
q.setActivity(activity);
|
||||||
q.setShortcutIds(shortcutIds);
|
q.setShortcutIds(shortcutIds);
|
||||||
}
|
}
|
||||||
List<ShortcutInfo> shortcutInfos = null;
|
|
||||||
try {
|
try {
|
||||||
shortcutInfos = mLauncherApps.getShortcuts(q, user);
|
return new QueryResult(mLauncherApps.getShortcuts(q, user));
|
||||||
mWasLastCallSuccess = true;
|
|
||||||
} catch (SecurityException|IllegalStateException e) {
|
} catch (SecurityException|IllegalStateException e) {
|
||||||
Log.e(TAG, "Failed to query for shortcuts", e);
|
Log.e(TAG, "Failed to query for shortcuts", e);
|
||||||
mWasLastCallSuccess = false;
|
return QueryResult.FAILURE;
|
||||||
}
|
}
|
||||||
if (shortcutInfos == null) {
|
|
||||||
return Collections.EMPTY_LIST;
|
|
||||||
}
|
|
||||||
return shortcutInfos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasHostPermission() {
|
public boolean hasHostPermission() {
|
||||||
|
@ -220,4 +197,25 @@ public class DeepShortcutManager {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class QueryResult extends ArrayList<ShortcutInfo> {
|
||||||
|
|
||||||
|
static QueryResult FAILURE = new QueryResult();
|
||||||
|
|
||||||
|
private final boolean mWasSuccess;
|
||||||
|
|
||||||
|
QueryResult(List<ShortcutInfo> result) {
|
||||||
|
super(result == null ? Collections.emptyList() : result);
|
||||||
|
mWasSuccess = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
QueryResult() {
|
||||||
|
mWasSuccess = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean wasSuccess() {
|
||||||
|
return mWasSuccess;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue