Removing static Context access using LauncherAppState

> This ensures that LauncherAppState is only accessed in the presence of
a valid context

Bug: 33032833
Change-Id: I955e5cb022f8bd6374681ae6c0720a2666d5b750
This commit is contained in:
Sunny Goyal
2017-01-11 10:48:34 -08:00
parent 4130705141
commit 87f784c285
34 changed files with 117 additions and 126 deletions
@@ -320,7 +320,7 @@ public class AppWidgetResizeFrame extends FrameLayout
public static Rect getWidgetSizeRanges(Context context, int spanX, int spanY, Rect rect) {
if (sCellSize == null) {
InvariantDeviceProfile inv = LauncherAppState.getInstance().getInvariantDeviceProfile();
InvariantDeviceProfile inv = LauncherAppState.getIDP(context);
// Initiate cell sizes.
sCellSize = new Point[2];
@@ -79,7 +79,7 @@ public class AutoInstallsLayout {
static AutoInstallsLayout get(Context context, String pkg, Resources targetRes,
AppWidgetHost appWidgetHost, LayoutParserCallback callback) {
InvariantDeviceProfile grid = LauncherAppState.getInstance().getInvariantDeviceProfile();
InvariantDeviceProfile grid = LauncherAppState.getIDP(context);
// Try with grid size and hotseat count
String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
@@ -182,7 +182,7 @@ public class AutoInstallsLayout {
mSourceRes = res;
mLayoutId = layoutId;
mIdp = LauncherAppState.getInstance().getInvariantDeviceProfile();
mIdp = LauncherAppState.getIDP(context);
mRowCount = mIdp.numRows;
mColumnCount = mIdp.numColumns;
}
@@ -587,7 +587,7 @@ public class BubbleTextView extends TextView
if (getTag() instanceof ItemInfoWithIcon) {
ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
if (info.usingLowResIcon) {
mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
.updateIconInBackground(BubbleTextView.this, info);
}
}
+1 -1
View File
@@ -734,7 +734,7 @@ public class IconCache {
if (mAppsToUpdate.isEmpty() && !mUpdatedPackages.isEmpty()) {
// No more app to update. Notify model.
LauncherAppState.getInstance().getModel().onPackageIconsUpdated(
LauncherAppState.getInstance(mContext).getModel().onPackageIconsUpdated(
mUpdatedPackages, mUserManager.getUserForSerialNumber(mUserSerial));
}
@@ -241,7 +241,7 @@ public class InstallShortcutReceiver extends BroadcastReceiver {
private static void queuePendingShortcutInfo(PendingInstallShortcutInfo info, Context context) {
// Queue the item up for adding if launcher has not loaded properly yet
LauncherAppState app = LauncherAppState.getInstance();
LauncherAppState app = LauncherAppState.getInstance(context);
boolean launcherNotLoaded = app.getModel().getCallback() == null;
addToInstallQueue(Utilities.getPrefs(context), info);
@@ -261,7 +261,7 @@ public class InstallShortcutReceiver extends BroadcastReceiver {
static void flushInstallQueue(Context context) {
ArrayList<PendingInstallShortcutInfo> items = getAndClearInstallQueue(context);
if (!items.isEmpty()) {
LauncherAppState.getInstance().getModel().addAndBindAddedWorkspaceItems(
LauncherAppState.getInstance(context).getModel().addAndBindAddedWorkspaceItems(
new LazyShortcutsProvider(context.getApplicationContext(), items));
}
}
@@ -438,7 +438,7 @@ public class InstallShortcutReceiver extends BroadcastReceiver {
public ItemInfo getItemInfo() {
if (activityInfo != null) {
AppInfo appInfo = new AppInfo(mContext, activityInfo, user);
final LauncherAppState app = LauncherAppState.getInstance();
final LauncherAppState app = LauncherAppState.getInstance(mContext);
// Set default values until proper values is loaded.
appInfo.title = "";
appInfo.iconBitmap = app.getIconCache().getDefaultIcon(user);
@@ -464,15 +464,14 @@ public class InstallShortcutReceiver extends BroadcastReceiver {
LauncherAppWidgetInfo widgetInfo = new LauncherAppWidgetInfo(
launchIntent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0),
info.provider);
InvariantDeviceProfile idp = LauncherAppState.getInstance()
.getInvariantDeviceProfile();
InvariantDeviceProfile idp = LauncherAppState.getIDP(mContext);
widgetInfo.minSpanX = info.minSpanX;
widgetInfo.minSpanY = info.minSpanY;
widgetInfo.spanX = Math.min(info.spanX, idp.numColumns);
widgetInfo.spanY = Math.min(info.spanY, idp.numRows);
return widgetInfo;
} else {
return createShortcutInfo(data, LauncherAppState.getInstance());
return createShortcutInfo(data, LauncherAppState.getInstance(mContext));
}
}
+2 -2
View File
@@ -356,7 +356,7 @@ public class Launcher extends Activity
super.onCreate(savedInstanceState);
LauncherAppState app = LauncherAppState.getInstance();
LauncherAppState app = LauncherAppState.getInstance(this);
// Load configuration-specific DeviceProfile
mDeviceProfile =
@@ -1815,7 +1815,7 @@ public class Launcher extends Activity
// been created. In this case, don't interfere with the new Launcher.
if (mModel.isCurrentCallbacks(this)) {
mModel.stopLoader();
LauncherAppState.getInstance().setLauncher(null);
LauncherAppState.getInstance(this).setLauncher(null);
}
if (mRotationPrefChangeHandler != null) {
@@ -48,7 +48,7 @@ public class LauncherAppState {
private InvariantDeviceProfile mInvariantDeviceProfile;
public static LauncherAppState getInstance() {
public static LauncherAppState getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new LauncherAppState();
}
@@ -159,4 +159,11 @@ public class LauncherAppState {
public InvariantDeviceProfile getInvariantDeviceProfile() {
return mInvariantDeviceProfile;
}
/**
* Shorthand for {@link #getInvariantDeviceProfile()}
*/
public static InvariantDeviceProfile getIDP(Context context) {
return LauncherAppState.getInstance(context).getInvariantDeviceProfile();
}
}
@@ -65,7 +65,7 @@ public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
}
public void initSpans(Context context) {
InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile();
InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
Point paddingLand = idp.landscapeProfile.getTotalWorkspacePadding();
Point paddingPort = idp.portraitProfile.getTotalWorkspacePadding();
@@ -107,8 +107,7 @@ public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
if (isCustomWidget) {
return cache.getFullResIcon(provider.getPackageName(), icon);
}
return super.loadIcon(context,
LauncherAppState.getInstance().getInvariantDeviceProfile().fillResIconDpi);
return super.loadIcon(context, LauncherAppState.getIDP(context).fillResIconDpi);
}
public String toString(PackageManager pm) {
+13 -11
View File
@@ -336,9 +336,10 @@ public class LauncherModel extends BroadcastReceiver
final ContentResolver cr = context.getContentResolver();
final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
final Context appContext = context.getApplicationContext();
Runnable r = new Runnable() {
public void run() {
cr.update(uri, writer.getValues(), null, null);
cr.update(uri, writer.getValues(appContext), null, null);
updateItemArrays(item, itemId, stackTrace);
}
};
@@ -553,13 +554,14 @@ public class LauncherModel extends BroadcastReceiver
writer.put(LauncherSettings.Favorites._ID, item.id);
final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
final Context appContext = context.getApplicationContext();
Runnable r = new Runnable() {
public void run() {
cr.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues());
cr.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues(appContext));
synchronized (sBgDataModel) {
checkItemInfoLocked(item.id, item, stackTrace);
sBgDataModel.addItem(item, true);
sBgDataModel.addItem(appContext, item, true);
}
}
};
@@ -588,13 +590,14 @@ public class LauncherModel extends BroadcastReceiver
public static void deleteItemsFromDatabase(Context context,
final Iterable<? extends ItemInfo> items) {
final ContentResolver cr = context.getContentResolver();
final Context appContext = context.getApplicationContext();
Runnable r = new Runnable() {
public void run() {
for (ItemInfo item : items) {
final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
cr.delete(uri, null, null);
sBgDataModel.removeItem(item);
sBgDataModel.removeItem(appContext, item);
}
}
};
@@ -654,16 +657,17 @@ public class LauncherModel extends BroadcastReceiver
*/
public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
final ContentResolver cr = context.getContentResolver();
final Context appContext = context.getApplicationContext();
Runnable r = new Runnable() {
public void run() {
cr.delete(LauncherSettings.Favorites.CONTENT_URI,
LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
sBgDataModel.removeItem(info.contents);
sBgDataModel.removeItem(appContext, info.contents);
info.contents.clear();
cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
sBgDataModel.removeItem(info);
sBgDataModel.removeItem(appContext, info);
}
};
runOnWorkerThread(r);
@@ -1101,8 +1105,7 @@ public class LauncherModel extends BroadcastReceiver
final boolean isSdCardReady = Utilities.isBootCompleted();
final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
LauncherAppState app = LauncherAppState.getInstance();
InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
int countX = profile.numColumns;
int countY = profile.numRows;
@@ -1693,8 +1696,7 @@ public class LauncherModel extends BroadcastReceiver
/** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
* right) */
private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
final LauncherAppState app = LauncherAppState.getInstance();
final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
final int screenCols = profile.numColumns;
final int screenCellCount = profile.numColumns * profile.numRows;
Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
@@ -2261,7 +2263,7 @@ public class LauncherModel extends BroadcastReceiver
bindWidgetsModel(callbacks);
// update the Widget entries inside DB on the worker thread.
LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(allWidgets);
mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
}
});
}
@@ -556,8 +556,7 @@ public class LauncherProvider extends ContentProvider {
}
private DefaultLayoutParser getDefaultLayoutParser(AppWidgetHost widgetHost) {
int defaultLayout = LauncherAppState.getInstance()
.getInvariantDeviceProfile().defaultLayoutId;
int defaultLayout = LauncherAppState.getIDP(getContext()).defaultLayoutId;
return new DefaultLayoutParser(getContext(), widgetHost,
mOpenHelper, getContext().getResources(), defaultLayout);
}
+2 -2
View File
@@ -235,7 +235,7 @@ public class ShortcutInfo extends ItemInfoWithIcon {
disabledMessage = shortcutInfo.getDisabledMessage();
// TODO: Use cache for this
LauncherAppState launcherAppState = LauncherAppState.getInstance();
LauncherAppState launcherAppState = LauncherAppState.getInstance(context);
Drawable unbadgedDrawable = DeepShortcutManager.getInstance(context)
.getShortcutIconDrawable(shortcutInfo,
launcherAppState.getInvariantDeviceProfile().fillResIconDpi);
@@ -249,7 +249,7 @@ public class ShortcutInfo extends ItemInfoWithIcon {
protected Bitmap getBadgedIcon(Bitmap unbadgedBitmap, ShortcutInfoCompat shortcutInfo,
IconCache cache, Context context) {
unbadgedBitmap = LauncherIcons.addShadowToIcon(unbadgedBitmap);
unbadgedBitmap = LauncherIcons.addShadowToIcon(unbadgedBitmap, context);
// Get the app info for the source activity.
AppInfo appInfo = new AppInfo();
appInfo.user = user;
+1 -2
View File
@@ -1613,8 +1613,7 @@ public class Workspace extends PagedView
Utilities.THREAD_POOL_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
final Point size = LauncherAppState.getInstance()
.getInvariantDeviceProfile().defaultWallpaperSize;
final Point size = LauncherAppState.getIDP(getContext()).defaultWallpaperSize;
if (size.x != mWallpaperManager.getDesiredMinimumWidth()
|| size.y != mWallpaperManager.getDesiredMinimumHeight()) {
mWallpaperManager.suggestDesiredDimensions(size.x, size.y);
@@ -42,7 +42,7 @@ public class PackageInstallerCompatVL extends PackageInstallerCompat {
PackageInstallerCompatVL(Context context) {
mInstaller = context.getPackageManager().getPackageInstaller();
mCache = LauncherAppState.getInstance().getIconCache();
mCache = LauncherAppState.getInstance(context).getIconCache();
mWorker = new Handler(LauncherModel.getWorkerLooper());
mInstaller.registerSessionCallback(mCallback, mWorker);
@@ -96,9 +96,7 @@ public class FolderPagedView extends PagedView {
public FolderPagedView(Context context, AttributeSet attrs) {
super(context, attrs);
LauncherAppState app = LauncherAppState.getInstance();
InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
InvariantDeviceProfile profile = LauncherAppState.getIDP(context);
mMaxCountX = profile.numFolderColumns;
mMaxCountY = profile.numFolderRows;
@@ -16,6 +16,7 @@
package com.android.launcher3.graphics;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
@@ -53,9 +54,9 @@ public class IconNormalizer {
private final float[] mLeftBorder;
private final float[] mRightBorder;
private IconNormalizer() {
private IconNormalizer(Context context) {
// Use twice the icon size as maximum size to avoid scaling down twice.
mMaxSize = LauncherAppState.getInstance().getInvariantDeviceProfile().iconBitmapSize * 2;
mMaxSize = LauncherAppState.getIDP(context).iconBitmapSize * 2;
mBitmap = Bitmap.createBitmap(mMaxSize, mMaxSize, Bitmap.Config.ALPHA_8);
mCanvas = new Canvas(mBitmap);
mPixels = new byte[mMaxSize * mMaxSize];
@@ -239,10 +240,10 @@ public class IconNormalizer {
}
}
public static IconNormalizer getInstance() {
public static IconNormalizer getInstance(Context context) {
synchronized (LOCK) {
if (sIconNormalizer == null) {
sIconNormalizer = new IconNormalizer();
sIconNormalizer = new IconNormalizer(context);
}
}
return sIconNormalizer;
@@ -60,9 +60,8 @@ public class LauncherIcons {
Resources resources = packageManager.getResourcesForApplication(iconRes.packageName);
if (resources != null) {
final int id = resources.getIdentifier(iconRes.resourceName, null, null);
return createIconBitmap(
resources.getDrawableForDensity(id, LauncherAppState.getInstance()
.getInvariantDeviceProfile().fillResIconDpi), context);
return createIconBitmap(resources.getDrawableForDensity(
id, LauncherAppState.getIDP(context).fillResIconDpi), context);
}
} catch (Exception e) {
// Icon not found.
@@ -70,15 +69,11 @@ public class LauncherIcons {
return null;
}
private static int getIconBitmapSize() {
return LauncherAppState.getInstance().getInvariantDeviceProfile().iconBitmapSize;
}
/**
* Returns a bitmap which is of the appropriate size to be displayed as an icon
*/
public static Bitmap createIconBitmap(Bitmap icon, Context context) {
final int iconBitmapSize = getIconBitmapSize();
final int iconBitmapSize = LauncherAppState.getIDP(context).iconBitmapSize;
if (iconBitmapSize == icon.getWidth() && iconBitmapSize == icon.getHeight()) {
return icon;
}
@@ -92,7 +87,7 @@ public class LauncherIcons {
public static Bitmap createBadgedIconBitmap(
Drawable icon, UserHandle user, Context context) {
float scale = FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION ?
1 : IconNormalizer.getInstance().getScale(icon, null);
1 : IconNormalizer.getInstance(context).getScale(icon, null);
Bitmap bitmap = createIconBitmap(icon, context, scale);
return badgeIconForUser(bitmap, user, context);
}
@@ -122,7 +117,7 @@ public class LauncherIcons {
public static Bitmap createScaledBitmapWithoutShadow(Drawable icon, Context context) {
RectF iconBounds = new RectF();
float scale = FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION ?
1 : IconNormalizer.getInstance().getScale(icon, iconBounds);
1 : IconNormalizer.getInstance(context).getScale(icon, iconBounds);
scale = Math.min(scale, ShadowGenerator.getScaleForBounds(iconBounds));
return createIconBitmap(icon, context, scale);
}
@@ -131,8 +126,8 @@ public class LauncherIcons {
* Adds a shadow to the provided icon. It assumes that the icon has already been scaled using
* {@link #createScaledBitmapWithoutShadow(Drawable, Context)}
*/
public static Bitmap addShadowToIcon(Bitmap icon) {
return ShadowGenerator.getInstance().recreateIcon(icon);
public static Bitmap addShadowToIcon(Bitmap icon, Context context) {
return ShadowGenerator.getInstance(context).recreateIcon(icon);
}
/**
@@ -163,7 +158,7 @@ public class LauncherIcons {
*/
public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) {
synchronized (sCanvas) {
final int iconBitmapSize = getIconBitmapSize();
final int iconBitmapSize = LauncherAppState.getIDP(context).iconBitmapSize;
int width = iconBitmapSize;
int height = iconBitmapSize;
@@ -16,6 +16,7 @@
package com.android.launcher3.graphics;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BlurMaskFilter;
@@ -52,8 +53,8 @@ public class ShadowGenerator {
private final Paint mBlurPaint;
private final Paint mDrawPaint;
private ShadowGenerator() {
mIconSize = LauncherAppState.getInstance().getInvariantDeviceProfile().iconBitmapSize;
private ShadowGenerator(Context context) {
mIconSize = LauncherAppState.getIDP(context).iconBitmapSize;
mCanvas = new Canvas();
mBlurPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
mBlurPaint.setMaskFilter(new BlurMaskFilter(mIconSize * BLUR_FACTOR, Blur.NORMAL));
@@ -82,11 +83,11 @@ public class ShadowGenerator {
return result;
}
public static ShadowGenerator getInstance() {
public static ShadowGenerator getInstance(Context context) {
Preconditions.assertNonUiThread();
synchronized (LOCK) {
if (sShadowGenerator == null) {
sShadowGenerator = new ShadowGenerator();
sShadowGenerator = new ShadowGenerator(context);
}
}
return sShadowGenerator;
@@ -99,11 +99,11 @@ public class BgDataModel {
deepShortcutMap.clear();
}
public synchronized void removeItem(ItemInfo... items) {
removeItem(Arrays.asList(items));
public synchronized void removeItem(Context context, ItemInfo... items) {
removeItem(context, Arrays.asList(items));
}
public synchronized void removeItem(Iterable<? extends ItemInfo> items) {
public synchronized void removeItem(Context context, Iterable<? extends ItemInfo> items) {
for (ItemInfo item : items) {
switch (item.itemType) {
case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
@@ -125,7 +125,6 @@ public class BgDataModel {
// Decrement pinned shortcut count
ShortcutKey pinnedShortcut = ShortcutKey.fromItemInfo(item);
MutableInt count = pinnedShortcutCounts.get(pinnedShortcut);
Context context = LauncherAppState.getInstance().getContext();
if ((count == null || --count.value == 0)
&& !InstallShortcutReceiver.getPendingShortcuts(context)
.contains(pinnedShortcut)) {
@@ -146,7 +145,7 @@ public class BgDataModel {
}
}
public synchronized void addItem(ItemInfo item, boolean newItem) {
public synchronized void addItem(Context context, ItemInfo item, boolean newItem) {
itemsIdMap.put(item.id, item);
switch (item.itemType) {
case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
@@ -166,8 +165,7 @@ public class BgDataModel {
// Since this is a new item, pin the shortcut in the system server.
if (newItem && count.value == 1) {
DeepShortcutManager.getInstance(LauncherAppState.getInstance().getContext())
.pinShortcut(pinnedShortcut);
DeepShortcutManager.getInstance(context).pinShortcut(pinnedShortcut);
}
// Fall through
}
@@ -895,7 +895,7 @@ public class GridSizeMigrationTask {
*/
public static boolean migrateGridIfNeeded(Context context) {
SharedPreferences prefs = Utilities.getPrefs(context);
InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile();
InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
String gridSizeString = getPointString(idp.numColumns, idp.numRows);
@@ -915,8 +915,7 @@ public class GridSizeMigrationTask {
if (srcHotseatCount != idp.numHotseatIcons) {
// Migrate hotseat.
dbChanged = new GridSizeMigrationTask(context,
LauncherAppState.getInstance().getInvariantDeviceProfile(),
dbChanged = new GridSizeMigrationTask(context, LauncherAppState.getIDP(context),
validPackages, srcHotseatCount, idp.numHotseatIcons).migrateHotseat();
}
@@ -978,9 +977,9 @@ public class GridSizeMigrationTask {
* @return a map with occupied hotseat position set to non-null value.
*/
public static LongArrayMap<Object> removeBrokenHotseatItems(Context context) throws Exception {
GridSizeMigrationTask task = new GridSizeMigrationTask(context,
LauncherAppState.getInstance().getInvariantDeviceProfile(),
getValidPackages(context), Integer.MAX_VALUE, Integer.MAX_VALUE);
GridSizeMigrationTask task = new GridSizeMigrationTask(
context, LauncherAppState.getIDP(context), getValidPackages(context),
Integer.MAX_VALUE, Integer.MAX_VALUE);
// Load all the valid entries
ArrayList<DbEntry> items = task.loadHotseatEntries();
@@ -1038,8 +1037,7 @@ public class GridSizeMigrationTask {
}
protected boolean runStepTask(Point sourceSize, Point nextSize) throws Exception {
return new GridSizeMigrationTask(mContext,
LauncherAppState.getInstance().getInvariantDeviceProfile(),
return new GridSizeMigrationTask(mContext, LauncherAppState.getIDP(mContext),
mValidPackages, sourceSize, nextSize).migrateWorkspace();
}
}
@@ -349,7 +349,7 @@ public class LoaderCursor extends CursorWrapper {
*/
public void checkAndAddItem(ItemInfo info, BgDataModel dataModel) {
if (checkItemPlacement(info, dataModel.workspaceScreens)) {
dataModel.addItem(info, false);
dataModel.addItem(mContext, info, false);
} else {
markDeleted("Item position overlap");
}
@@ -31,16 +31,16 @@ public class WidgetItem extends ComponentKey implements Comparable<WidgetItem> {
public final String label;
public final int spanX, spanY;
public WidgetItem(LauncherAppWidgetProviderInfo info, PackageManager pm) {
public WidgetItem(LauncherAppWidgetProviderInfo info, PackageManager pm,
InvariantDeviceProfile idp) {
super(info.provider, info.getProfile());
label = Utilities.trim(info.getLabel(pm));
widgetInfo = info;
activityInfo = null;
InvariantDeviceProfile idv = LauncherAppState.getInstance().getInvariantDeviceProfile();
spanX = Math.min(info.spanX, idv.numColumns);
spanY = Math.min(info.spanY, idv.numRows);
spanX = Math.min(info.spanX, idp.numColumns);
spanY = Math.min(info.spanY, idp.numRows);
}
public WidgetItem(ResolveInfo info, PackageManager pm) {
@@ -60,12 +60,13 @@ public class WidgetsModel {
final ArrayList<WidgetItem> widgetsAndShortcuts = new ArrayList<>();
try {
PackageManager pm = context.getPackageManager();
InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
// Widgets
for (AppWidgetProviderInfo widgetInfo :
AppWidgetManagerCompat.getInstance(context).getAllProviders()) {
widgetsAndShortcuts.add(new WidgetItem(
LauncherAppWidgetProviderInfo.fromProviderInfo(context, widgetInfo), pm));
widgetsAndShortcuts.add(new WidgetItem(LauncherAppWidgetProviderInfo
.fromProviderInfo(context, widgetInfo), pm, idp));
}
// Shortcuts
@@ -73,7 +74,7 @@ public class WidgetsModel {
pm.queryIntentActivities(new Intent(Intent.ACTION_CREATE_SHORTCUT), 0)) {
widgetsAndShortcuts.add(new WidgetItem(info, pm));
}
setWidgetsAndShortcuts(widgetsAndShortcuts);
setWidgetsAndShortcuts(widgetsAndShortcuts, context);
} catch (Exception e) {
if (!ProviderConfig.IS_DOGFOOD_BUILD && Utilities.isBinderSizeError(e)) {
// the returned value may be incomplete and will not be refreshed until the next
@@ -87,7 +88,8 @@ public class WidgetsModel {
return widgetsAndShortcuts;
}
private void setWidgetsAndShortcuts(ArrayList<WidgetItem> rawWidgetsShortcuts) {
private void setWidgetsAndShortcuts(ArrayList<WidgetItem> rawWidgetsShortcuts,
Context context) {
if (DEBUG) {
Log.d(TAG, "addWidgetsAndShortcuts, widgetsShortcuts#=" + rawWidgetsShortcuts.size());
}
@@ -99,7 +101,7 @@ public class WidgetsModel {
// clear the lists.
mWidgetsList.clear();
InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile();
InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
UserHandle myUser = Process.myUserHandle();
// add and update.
@@ -306,7 +306,7 @@ public class ImportDataTask {
}
LongArrayMap<Object> hotseatItems = GridSizeMigrationTask.removeBrokenHotseatItems(mContext);
int myHotseatCount = LauncherAppState.getInstance().getInvariantDeviceProfile().numHotseatIcons;
int myHotseatCount = LauncherAppState.getIDP(mContext).numHotseatIcons;
if (!FeatureFlags.NO_ALL_APPS_ICON) {
myHotseatCount--;
}
@@ -381,8 +381,8 @@ public class ImportDataTask {
return c.getSharedPreferences(LauncherFiles.DEVICE_PREFERENCES_KEY, Context.MODE_PRIVATE);
}
private static final int getMyHotseatLayoutId() {
return LauncherAppState.getInstance().getInvariantDeviceProfile().numHotseatIcons <= 5
private static final int getMyHotseatLayoutId(Context context) {
return LauncherAppState.getIDP(context).numHotseatIcons <= 5
? R.xml.dw_phone_hotseat
: R.xml.dw_tablet_hotseat;
}
@@ -392,7 +392,7 @@ public class ImportDataTask {
*/
private static class HotseatLayoutParser extends DefaultLayoutParser {
public HotseatLayoutParser(Context context, LayoutParserCallback callback) {
super(context, null, callback, context.getResources(), getMyHotseatLayoutId());
super(context, null, callback, context.getResources(), getMyHotseatLayoutId(context));
}
@Override
@@ -76,8 +76,7 @@ public class LauncherDbUtils {
}
}
new LossyScreenMigrationTask(
context, LauncherAppState.getInstance().getInvariantDeviceProfile(), db)
new LossyScreenMigrationTask(context, LauncherAppState.getIDP(context), db)
.migrateScreen0();
db.setTransactionSuccessful();
return true;
@@ -102,7 +102,7 @@ public class QsbContainerView extends FrameLayout {
}
AppWidgetManagerCompat widgetManager = AppWidgetManagerCompat.getInstance(activity);
InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile();
InvariantDeviceProfile idp = LauncherAppState.getIDP(activity);
Bundle opts = new Bundle();
Rect size = AppWidgetResizeFrame.getWidgetSizeRanges(activity, idp.numColumns, 1, null);
@@ -22,7 +22,7 @@ public class ToggleWeightWatcher extends Activity {
show = !show;
sp.edit().putBoolean(TestingUtils.SHOW_WEIGHT_WATCHER, show).apply();
Launcher launcher = (Launcher) LauncherAppState.getInstance().getModel().getCallback();
Launcher launcher = (Launcher) LauncherAppState.getInstance(this).getModel().getCallback();
if (launcher != null && launcher.mWeightWatcher != null) {
launcher.mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
}
@@ -93,9 +93,9 @@ public class ContentWriter {
* Commits any pending validation and returns the final values.
* Must not be called on UI thread.
*/
public ContentValues getValues() {
public ContentValues getValues(Context context) {
Preconditions.assertNonUiThread();
if (mIcon != null && !LauncherAppState.getInstance().getIconCache()
if (mIcon != null && !LauncherAppState.getInstance(context).getIconCache()
.isDefaultIcon(mIcon, mUser)) {
mValues.put(LauncherSettings.Favorites.ICON, Utilities.flattenBitmap(mIcon));
mIcon = null;
@@ -105,7 +105,7 @@ public class ContentWriter {
public int commit() {
if (mCommitParams != null) {
return mContext.getContentResolver().update(mCommitParams.mUri, getValues(),
return mContext.getContentResolver().update(mCommitParams.mUri, getValues(mContext),
mCommitParams.mWhere, mCommitParams.mSelectionArgs);
}
return 0;
@@ -72,8 +72,8 @@ public class ManagedProfileHeuristic {
private ManagedProfileHeuristic(Context context, UserHandle user) {
mContext = context;
mUser = user;
mModel = LauncherAppState.getInstance().getModel();
mIconCache = LauncherAppState.getInstance().getIconCache();
mModel = LauncherAppState.getInstance(context).getModel();
mIconCache = LauncherAppState.getInstance(context).getIconCache();
}
public void processPackageRemoved(String[] packages) {
@@ -87,7 +87,7 @@ public class WidgetsContainerView extends BaseContainerView
mLauncher = Launcher.getLauncher(context);
mDragController = mLauncher.getDragController();
mAdapter = new WidgetsListAdapter(this, this, context);
mIconCache = (LauncherAppState.getInstance()).getIconCache();
mIconCache = LauncherAppState.getInstance(context).getIconCache();
if (LOGD) {
Log.d(TAG, "WidgetsContainerView constructor");
}
@@ -303,7 +303,7 @@ public class WidgetsContainerView extends BaseContainerView
private WidgetPreviewLoader getWidgetPreviewLoader() {
if (mWidgetPreviewLoader == null) {
mWidgetPreviewLoader = LauncherAppState.getInstance().getWidgetCache();
mWidgetPreviewLoader = LauncherAppState.getInstance(getContext()).getWidgetCache();
}
return mWidgetPreviewLoader;
}
@@ -68,7 +68,7 @@ public class WidgetsListAdapter extends Adapter<WidgetsRowViewHolder> {
View.OnLongClickListener iconLongClickListener,
Context context) {
mLayoutInflater = LayoutInflater.from(context);
mWidgetPreviewLoader = LauncherAppState.getInstance().getWidgetCache();
mWidgetPreviewLoader = LauncherAppState.getInstance(context).getWidgetCache();
mIndexer = new AlphabeticIndexCompat(context);
@@ -16,7 +16,6 @@ import com.android.launcher3.compat.AppWidgetManagerCompat;
import com.android.launcher3.compat.PackageInstallerCompat;
import com.android.launcher3.ui.LauncherInstrumentationTestCase;
import com.android.launcher3.util.ContentWriter;
import com.android.launcher3.util.ManagedProfileHeuristic;
import com.android.launcher3.widget.PendingAddWidgetInfo;
import com.android.launcher3.widget.WidgetHostViewLoader;
@@ -223,20 +222,9 @@ public class BindWidgetTest extends LauncherInstrumentationTestCase {
item.screenId = screenId;
item.onAddToDatabase(writer);
writer.put(LauncherSettings.Favorites._ID, item.id);
mResolver.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues());
mResolver.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues(mTargetContext));
resetLoaderState();
// Reset loader
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
ManagedProfileHeuristic.markExistingUsersForNoFolderCreation(mTargetContext);
LauncherAppState.getInstance().getModel().resetLoadedState(true, true);
}
});
} catch (Throwable t) {
throw new IllegalArgumentException(t);
}
// Launch the home activity
startLauncher();
// Verify UI
@@ -164,7 +164,7 @@ public class AddWorkspaceItemsTaskTest extends BaseModelUpdateTaskTestCase {
info.cellX = x;
info.cellY = y;
info.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
bgDataModel.addItem(info, false);
bgDataModel.addItem(targetContext, info, false);
}
}
return startId;
@@ -128,7 +128,7 @@ public class BaseModelUpdateTaskTestCase extends ProviderTestCase2<TestLauncherP
classMap.put(commands[1], Class.forName(commands[2]));
break;
case "bgItem":
bgDataModel.addItem(
bgDataModel.addItem(targetContext,
(ItemInfo) initItem(classMap.get(commands[1]), commands, 2), false);
break;
case "allApps":
@@ -211,15 +211,22 @@ public class LauncherInstrumentationTestCase extends InstrumentationTestCase {
LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
LauncherSettings.Settings.call(mTargetContext.getContentResolver(),
LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG);
ManagedProfileHeuristic.markExistingUsersForNoFolderCreation(mTargetContext);
resetLoaderState();
}
runTestOnUiThread(new Runnable() {
@Override
public void run() {
// Reset the loader state
LauncherAppState.getInstance().getModel().resetLoadedState(true, true);
}
});
protected void resetLoaderState() {
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
ManagedProfileHeuristic.markExistingUsersForNoFolderCreation(mTargetContext);
LauncherAppState.getInstance(mTargetContext).getModel()
.resetLoadedState(true, true);
}
});
} catch (Throwable t) {
throw new IllegalArgumentException(t);
}
}
/**
@@ -248,8 +255,7 @@ public class LauncherInstrumentationTestCase extends InstrumentationTestCase {
LauncherAppWidgetProviderInfo info = getOnUiThread(new Callable<LauncherAppWidgetProviderInfo>() {
@Override
public LauncherAppWidgetProviderInfo call() throws Exception {
InvariantDeviceProfile idv =
LauncherAppState.getInstance().getInvariantDeviceProfile();
InvariantDeviceProfile idv = LauncherAppState.getIDP(mTargetContext);
ComponentName searchComponent = ((SearchManager) mTargetContext
.getSystemService(Context.SEARCH_SERVICE)).getGlobalSearchActivity();