Removing static access to Context through LauncherAppState

Bug: 33032833
Change-Id: I09baaa6d79187b3096a2ab3a89d7dcaeaf9eee68
diff --git a/src/com/android/launcher3/InfoDropTarget.java b/src/com/android/launcher3/InfoDropTarget.java
index 6a6d285..db14e2e 100644
--- a/src/com/android/launcher3/InfoDropTarget.java
+++ b/src/com/android/launcher3/InfoDropTarget.java
@@ -18,7 +18,6 @@
 
 import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
-import android.content.ContentResolver;
 import android.content.Context;
 import android.provider.Settings;
 import android.util.AttributeSet;
@@ -90,13 +89,12 @@
 
     @Override
     protected boolean supportsDrop(DragSource source, ItemInfo info) {
-        return source.supportsAppInfoDropTarget() && supportsDrop(info);
+        return source.supportsAppInfoDropTarget() && supportsDrop(getContext(), info);
     }
 
-    public static boolean supportsDrop(ItemInfo info) {
+    public static boolean supportsDrop(Context context, ItemInfo info) {
         // Only show the App Info drop target if developer settings are enabled.
-        ContentResolver resolver = LauncherAppState.getInstance().getContext().getContentResolver();
-        boolean developmentSettingsEnabled = Settings.Global.getInt(resolver,
+        boolean developmentSettingsEnabled = Settings.Global.getInt(context.getContentResolver(),
                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1;
         return developmentSettingsEnabled
                 && (info instanceof AppInfo || info instanceof ShortcutInfo
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index f618546..7dd8450 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -735,7 +735,7 @@
             } else if (resultCode == RESULT_OK) {
                 addAppWidgetImpl(
                         appWidgetId, requestArgs, null,
-                        requestArgs.getWidgetProvider(),
+                        requestArgs.getWidgetProvider(this),
                         ON_ACTIVITY_RESULT_ANIMATION_DELAY);
             }
             return;
@@ -894,7 +894,7 @@
         if (resultCode == RESULT_OK) {
             animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
             final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
-                    requestArgs.getWidgetProvider());
+                    requestArgs.getWidgetProvider(this));
             boundWidget = layout;
             onCompleteRunnable = new Runnable() {
                 @Override
diff --git a/src/com/android/launcher3/LauncherAppWidgetHost.java b/src/com/android/launcher3/LauncherAppWidgetHost.java
index 657b024..6e8c59b 100644
--- a/src/com/android/launcher3/LauncherAppWidgetHost.java
+++ b/src/com/android/launcher3/LauncherAppWidgetHost.java
@@ -128,7 +128,7 @@
         super.onProviderChanged(appWidgetId, info);
         // The super method updates the dimensions of the providerInfo. Update the
         // launcher spans accordingly.
-        info.initSpans();
+        info.initSpans(mLauncher);
     }
 
     @Override
diff --git a/src/com/android/launcher3/LauncherAppWidgetProviderInfo.java b/src/com/android/launcher3/LauncherAppWidgetProviderInfo.java
index 01e2624..ab8f395 100644
--- a/src/com/android/launcher3/LauncherAppWidgetProviderInfo.java
+++ b/src/com/android/launcher3/LauncherAppWidgetProviderInfo.java
@@ -29,22 +29,27 @@
 
     public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
             AppWidgetProviderInfo info) {
+        final LauncherAppWidgetProviderInfo launcherInfo;
+        if (info instanceof LauncherAppWidgetProviderInfo) {
+            launcherInfo = (LauncherAppWidgetProviderInfo) info;
+        } else {
 
-        // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
-        // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
-        // associated super parcel constructor. This allows us to copy non-public members without
-        // using reflection.
-        Parcel p = Parcel.obtain();
-        info.writeToParcel(p, 0);
-        p.setDataPosition(0);
-        LauncherAppWidgetProviderInfo lawpi = new LauncherAppWidgetProviderInfo(p);
-        p.recycle();
-        return lawpi;
+            // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
+            // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
+            // associated super parcel constructor. This allows us to copy non-public members without
+            // using reflection.
+            Parcel p = Parcel.obtain();
+            info.writeToParcel(p, 0);
+            p.setDataPosition(0);
+            launcherInfo = new LauncherAppWidgetProviderInfo(p);
+            p.recycle();
+        }
+        launcherInfo.initSpans(context);
+        return launcherInfo;
     }
 
-    public LauncherAppWidgetProviderInfo(Parcel in) {
+    private LauncherAppWidgetProviderInfo(Parcel in) {
         super(in);
-        initSpans();
     }
 
     public LauncherAppWidgetProviderInfo(Context context, CustomAppWidget widget) {
@@ -56,12 +61,11 @@
         previewImage = widget.getPreviewImage();
         initialLayout = widget.getWidgetLayout();
         resizeMode = widget.getResizeMode();
-        initSpans();
+        initSpans(context);
     }
 
-    public void initSpans() {
-        LauncherAppState app = LauncherAppState.getInstance();
-        InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
+    public void initSpans(Context context) {
+        InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile();
 
         Point paddingLand = idp.landscapeProfile.getTotalWorkspacePadding();
         Point paddingPort = idp.portraitProfile.getTotalWorkspacePadding();
@@ -80,7 +84,7 @@
         // We want to account for the extra amount of padding that we are adding to the widget
         // to ensure that it gets the full amount of space that it has requested.
         Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
-                app.getContext(), provider, null);
+                context, provider, null);
         spanX = Math.max(1, (int) Math.ceil(
                         (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
         spanY = Math.max(1, (int) Math.ceil(
diff --git a/src/com/android/launcher3/LauncherProvider.java b/src/com/android/launcher3/LauncherProvider.java
index 0865925..8f56eb6 100644
--- a/src/com/android/launcher3/LauncherProvider.java
+++ b/src/com/android/launcher3/LauncherProvider.java
@@ -796,7 +796,7 @@
                 case 26:
                     // QSB was moved to the grid. Clear the first row on screen 0.
                     if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
-                            !LauncherDbUtils.prepareScreenZeroToHostQsb(db)) {
+                            !LauncherDbUtils.prepareScreenZeroToHostQsb(mContext, db)) {
                         break;
                     }
                 case 27: {
diff --git a/src/com/android/launcher3/accessibility/LauncherAccessibilityDelegate.java b/src/com/android/launcher3/accessibility/LauncherAccessibilityDelegate.java
index f5ffec7..9347852 100644
--- a/src/com/android/launcher3/accessibility/LauncherAccessibilityDelegate.java
+++ b/src/com/android/launcher3/accessibility/LauncherAccessibilityDelegate.java
@@ -115,7 +115,7 @@
         if (UninstallDropTarget.supportsDrop(host.getContext(), item)) {
             info.addAction(mActions.get(UNINSTALL));
         }
-        if (InfoDropTarget.supportsDrop(item)) {
+        if (InfoDropTarget.supportsDrop(host.getContext(), item)) {
             info.addAction(mActions.get(INFO));
         }
 
diff --git a/src/com/android/launcher3/provider/LauncherDbUtils.java b/src/com/android/launcher3/provider/LauncherDbUtils.java
index 2f82419..89fd99b 100644
--- a/src/com/android/launcher3/provider/LauncherDbUtils.java
+++ b/src/com/android/launcher3/provider/LauncherDbUtils.java
@@ -17,6 +17,7 @@
 package com.android.launcher3.provider;
 
 import android.content.ContentValues;
+import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.util.Log;
@@ -42,7 +43,7 @@
      * the first row. The items in the first screen are moved and resized but the carry-forward
      * items are simply deleted.
      */
-    public static boolean prepareScreenZeroToHostQsb(SQLiteDatabase db) {
+    public static boolean prepareScreenZeroToHostQsb(Context context, SQLiteDatabase db) {
         db.beginTransaction();
         try {
             // Get the existing screens
@@ -75,8 +76,8 @@
                 }
             }
 
-            LauncherAppState app = LauncherAppState.getInstance();
-            new LossyScreenMigrationTask(app.getContext(), app.getInvariantDeviceProfile(), db)
+            new LossyScreenMigrationTask(
+                    context, LauncherAppState.getInstance().getInvariantDeviceProfile(), db)
                     .migrateScreen0();
             db.setTransactionSuccessful();
             return true;
diff --git a/src/com/android/launcher3/util/PendingRequestArgs.java b/src/com/android/launcher3/util/PendingRequestArgs.java
index 8eea28b..9452fbd 100644
--- a/src/com/android/launcher3/util/PendingRequestArgs.java
+++ b/src/com/android/launcher3/util/PendingRequestArgs.java
@@ -15,7 +15,9 @@
  */
 package com.android.launcher3.util;
 
+import android.appwidget.AppWidgetProviderInfo;
 import android.content.ContentValues;
+import android.content.Context;
 import android.content.Intent;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -57,9 +59,8 @@
         mArg1 = parcel.readInt();
         mObjectType = parcel.readInt();
         if (parcel.readInt() != 0) {
-            mObject = mObjectType == TYPE_INTENT
-                    ? Intent.CREATOR.createFromParcel(parcel)
-                    : new LauncherAppWidgetProviderInfo(parcel);
+            mObject = (mObjectType == TYPE_INTENT ? Intent.CREATOR : AppWidgetProviderInfo.CREATOR)
+                    .createFromParcel(parcel);
         } else {
             mObject = null;
         }
@@ -86,8 +87,10 @@
         }
     }
 
-    public LauncherAppWidgetProviderInfo getWidgetProvider() {
-        return mObjectType == TYPE_APP_WIDGET ? (LauncherAppWidgetProviderInfo) mObject : null;
+    public LauncherAppWidgetProviderInfo getWidgetProvider(Context context) {
+        return mObjectType == TYPE_APP_WIDGET ?
+                LauncherAppWidgetProviderInfo.fromProviderInfo(
+                        context, (AppWidgetProviderInfo) mObject) : null;
     }
 
     public int getWidgetId() {
@@ -103,7 +106,7 @@
     }
 
     public static PendingRequestArgs forWidgetInfo(
-            int appWidgetId, LauncherAppWidgetProviderInfo widgetInfo, ItemInfo info) {
+            int appWidgetId, AppWidgetProviderInfo widgetInfo, ItemInfo info) {
         PendingRequestArgs args = new PendingRequestArgs(appWidgetId, TYPE_APP_WIDGET, widgetInfo);
         args.copyFrom(info);
         return args;