Adding minSpanX and minSpanY for all the launcher widgets

Bug: 22353460
Change-Id: Id4450dee42a83e4603dcd56e1c4dec2b0e405858
diff --git a/src/com/android/launcher3/LauncherBackupHelper.java b/src/com/android/launcher3/LauncherBackupHelper.java
index f209736..8c6fedb 100644
--- a/src/com/android/launcher3/LauncherBackupHelper.java
+++ b/src/com/android/launcher3/LauncherBackupHelper.java
@@ -75,7 +75,7 @@
     private static final boolean VERBOSE = LauncherBackupAgentHelper.VERBOSE;
     private static final boolean DEBUG = LauncherBackupAgentHelper.DEBUG;
 
-    private static final int BACKUP_VERSION = 2;
+    private static final int BACKUP_VERSION = 3;
     private static final int MAX_JOURNAL_SIZE = 1000000;
 
     // Journal key is such that it is always smaller than any dynamically generated
@@ -148,6 +148,7 @@
 
     private IconCache mIconCache;
     private DeviceProfieData mDeviceProfileData;
+    private InvariantDeviceProfile mIdp;
 
     boolean restoreSuccessful;
     int restoredBackupVersion = 1;
@@ -178,6 +179,7 @@
                 mExistingKeys.add(keyToBackupKey(key));
             }
         }
+        restoredBackupVersion = journal.backupVersion;
     }
 
     /**
@@ -206,7 +208,8 @@
 
         if (mDeviceProfileData == null) {
             LauncherAppState app = LauncherAppState.getInstance();
-            mDeviceProfileData = initDeviceProfileData(app.getInvariantDeviceProfile());
+            mIdp = app.getInvariantDeviceProfile();
+            mDeviceProfileData = initDeviceProfileData(mIdp);
             mIconCache = app.getIconCache();
         }
 
@@ -308,9 +311,9 @@
         if (mDeviceProfileData == null) {
             // This call does not happen on a looper thread. So LauncherAppState
             // can't be created . Instead initialize required dependencies directly.
-            InvariantDeviceProfile profile = new InvariantDeviceProfile(mContext);
-            mDeviceProfileData = initDeviceProfileData(profile);
-            mIconCache = new IconCache(mContext, profile);
+            mIdp = new InvariantDeviceProfile(mContext);
+            mDeviceProfileData = initDeviceProfileData(mIdp);
+            mIconCache = new IconCache(mContext, mIdp);
         }
 
         int dataSize = data.size();
@@ -335,7 +338,6 @@
                 MessageNano.mergeFrom(journal, readCheckedBytes(mBuffer, dataSize));
                 applyJournal(journal);
                 restoreSuccessful = isBackupCompatible(journal);
-                restoredBackupVersion = journal.backupVersion;
                 return;
             }
 
@@ -636,7 +638,7 @@
                 } else {
                     Log.w(TAG, "empty intent on appwidget: " + id);
                 }
-                if (mExistingKeys.contains(backupKey)) {
+                if (mExistingKeys.contains(backupKey) && restoredBackupVersion >= BACKUP_VERSION) {
                     if (DEBUG) Log.d(TAG, "already saved widget " + backupKey);
 
                     // remember that we already backed this up previously
@@ -969,6 +971,16 @@
             widget.icon.data = Utilities.flattenBitmap(icon);
             widget.icon.dpi = dpi;
         }
+
+        // Calculate the spans corresponding to any one of the orientations as it should not change
+        // based on orientation.
+        int[] minSpans = CellLayout.rectToCell(
+                mIdp.portraitProfile, mContext, info.minResizeWidth, info.minResizeHeight, null);
+        widget.minSpanX = (info.resizeMode & LauncherAppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0
+                ? minSpans[0] : -1;
+        widget.minSpanY = (info.resizeMode & LauncherAppWidgetProviderInfo.RESIZE_VERTICAL) != 0
+                ? minSpans[1] : -1;
+
         return widget;
     }