Merge "Changing the dogfood check to a static boolean to better handle proguard optimizations" into ub-launcher3-master
diff --git a/Android.mk b/Android.mk
index 2e57478..ec675c6 100644
--- a/Android.mk
+++ b/Android.mk
@@ -23,20 +23,29 @@
 
 LOCAL_MODULE_TAGS := optional
 
+LOCAL_STATIC_JAVA_LIBRARIES := \
+    android-support-v4 \
+    android-support-v7-recyclerview
+
 LOCAL_SRC_FILES := $(call all-java-files-under, src) \
     $(call all-java-files-under, WallpaperPicker/src) \
     $(call all-proto-files-under, protos)
-LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/WallpaperPicker/res $(LOCAL_PATH)/res
+
+LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/WallpaperPicker/res \
+    $(LOCAL_PATH)/res \
+    $(LOCAL_PATH)/../../../prebuilts/sdk/current/support/v7/recyclerview/res
+
 LOCAL_PROGUARD_FLAG_FILES := proguard.flags
-LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4 android-support-v7-recyclerview
 
 LOCAL_PROTOC_OPTIMIZE_TYPE := nano
 LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/protos/
-LOCAL_AAPT_FLAGS := --auto-add-overlay
+LOCAL_AAPT_FLAGS := \
+    --auto-add-overlay \
+    --extra-packages android.support.v7.recyclerview
 
 LOCAL_SDK_VERSION := current
 LOCAL_PACKAGE_NAME := Launcher3
-LOCAL_OVERRIDES_PACKAGES := Launcher2
+LOCAL_OVERRIDES_PACKAGES := Home Launcher2
 
 include $(BUILD_PACKAGE)
 
diff --git a/proguard.flags b/proguard.flags
index 6a9d6f3..a8e2b60 100644
--- a/proguard.flags
+++ b/proguard.flags
@@ -1,3 +1,7 @@
+-keep,allowshrinking,allowoptimization class com.android.launcher3.** {
+  *;
+}
+
 -keep class com.android.launcher3.BaseRecyclerViewFastScrollBar {
   public void setWidth(int);
   public int getWidth();
@@ -63,4 +67,4 @@
 -keep class com.android.launcher3.Workspace {
   public float getBackgroundAlpha();
   public void setBackgroundAlpha(float);
-}
\ No newline at end of file
+}
diff --git a/protos/backup.proto b/protos/backup.proto
index 09330ee..d8d94e8 100644
--- a/protos/backup.proto
+++ b/protos/backup.proto
@@ -102,17 +102,17 @@
   optional string iconResource = 17;
   optional bytes icon = 18;
   optional TargetType targetType = 19 [default = TARGET_NONE];
- }
+}
 
 message Screen {
   required int64 id = 1;
   optional int32 rank = 2;
- }
+}
 
 message Resource {
   required int32 dpi = 1;
   required bytes data = 2;
- }
+}
 
 message Widget {
   required string provider = 1;
@@ -120,4 +120,8 @@
   optional bool configure = 3;
   optional Resource icon = 4;
   optional Resource preview = 5;
- }
+
+  // Assume that a widget is resizable upto 2x2 if no data is available
+  optional int32 minSpanX = 6 [default = 2];
+  optional int32 minSpanY = 7 [default = 2];
+}
diff --git a/src/com/android/launcher3/CellLayout.java b/src/com/android/launcher3/CellLayout.java
index 5f8b71c..31e2a7f 100644
--- a/src/com/android/launcher3/CellLayout.java
+++ b/src/com/android/launcher3/CellLayout.java
@@ -2688,8 +2688,12 @@
      * @param result An array of length 2 in which to store the result (may be null).
      */
     public static int[] rectToCell(Launcher launcher, int width, int height, int[] result) {
-        DeviceProfile grid = launcher.getDeviceProfile();
-        Rect padding = grid.getWorkspacePadding(Utilities.isRtl(launcher.getResources()));
+        return rectToCell(launcher.getDeviceProfile(), launcher, width, height, result);
+    }
+
+    public static int[] rectToCell(DeviceProfile grid, Context context, int width, int height,
+            int[] result) {
+        Rect padding = grid.getWorkspacePadding(Utilities.isRtl(context.getResources()));
 
         // Always assume we're working with the smallest span to make sure we
         // reserve enough space in both orientations.
diff --git a/src/com/android/launcher3/IconCache.java b/src/com/android/launcher3/IconCache.java
index 916418f..ea1c0fd 100644
--- a/src/com/android/launcher3/IconCache.java
+++ b/src/com/android/launcher3/IconCache.java
@@ -540,7 +540,7 @@
             mCache.put(cacheKey, entry);
 
             // Check the DB first.
-            if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
+            if (!getEntryFromDB(cacheKey, entry, useLowResIcon)) {
                 if (info != null) {
                     entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
                 } else {
@@ -579,7 +579,14 @@
             Bitmap icon, CharSequence title) {
         removeFromMemCacheLocked(packageName, user);
 
-        CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
+        ComponentKey cacheKey = getPackageKey(packageName, user);
+        CacheEntry entry = mCache.get(cacheKey);
+
+        // For icon caching, do not go through DB. Just update the in-memory entry.
+        if (entry == null) {
+            entry = new CacheEntry();
+            mCache.put(cacheKey, entry);
+        }
         if (!TextUtils.isEmpty(title)) {
             entry.title = title;
         }
@@ -588,15 +595,18 @@
         }
     }
 
+    private static ComponentKey getPackageKey(String packageName, UserHandleCompat user) {
+        ComponentName cn = new ComponentName(packageName, packageName + EMPTY_CLASS_NAME);
+        return new ComponentKey(cn, user);
+    }
+
     /**
      * Gets an entry for the package, which can be used as a fallback entry for various components.
      * This method is not thread safe, it must be called from a synchronized method.
-     *
      */
     private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
             boolean useLowResIcon) {
-        ComponentName cn = new ComponentName(packageName, packageName + EMPTY_CLASS_NAME);
-        ComponentKey cacheKey = new ComponentKey(cn, user);
+        ComponentKey cacheKey = getPackageKey(packageName, user);
         CacheEntry entry = mCache.get(cacheKey);
 
         if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
@@ -604,7 +614,7 @@
             boolean entryUpdated = true;
 
             // Check the DB first.
-            if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
+            if (!getEntryFromDB(cacheKey, entry, useLowResIcon)) {
                 try {
                     PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
                     ApplicationInfo appInfo = info.applicationInfo;
@@ -622,7 +632,8 @@
                     // package updates.
                     ContentValues values =
                             newContentValues(entry.icon, entry.title.toString(), mPackageBgColor);
-                    addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
+                    addIconToDB(values, cacheKey.componentName, info,
+                            mUserManager.getSerialNumberForUser(user));
 
                 } catch (NameNotFoundException e) {
                     if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
@@ -649,7 +660,7 @@
      * @param dpi the native density of the icon
      */
     public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
-            long userSerial) {
+            long userSerial, InvariantDeviceProfile idp) {
         // TODO rescale to the correct native DPI
         try {
             PackageManager packageManager = mContext.getPackageManager();
@@ -660,21 +671,22 @@
             // pass
         }
 
-        ContentValues values = newContentValues(icon, label, Color.TRANSPARENT);
+        ContentValues values = newContentValues(
+                Bitmap.createScaledBitmap(icon, idp.iconBitmapSize, idp.iconBitmapSize, true),
+                label, Color.TRANSPARENT);
         values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
         values.put(IconDB.COLUMN_USER, userSerial);
         mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
                 SQLiteDatabase.CONFLICT_REPLACE);
     }
 
-    private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
-            CacheEntry entry, boolean lowRes) {
+    private boolean getEntryFromDB(ComponentKey cacheKey, CacheEntry entry, boolean lowRes) {
         Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
                 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
                         IconDB.COLUMN_LABEL},
                 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
-                new String[] {component.flattenToString(),
-                    Long.toString(mUserManager.getSerialNumberForUser(user))},
+                new String[] {cacheKey.componentName.flattenToString(),
+                    Long.toString(mUserManager.getSerialNumberForUser(cacheKey.user))},
                 null, null, null);
         try {
             if (c.moveToNext()) {
@@ -685,7 +697,8 @@
                     entry.title = "";
                     entry.contentDescription = "";
                 } else {
-                    entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
+                    entry.contentDescription = mUserManager.getBadgedLabelForUser(
+                            entry.title, cacheKey.user);
                 }
                 return true;
             }
diff --git a/src/com/android/launcher3/LauncherBackupAgentHelper.java b/src/com/android/launcher3/LauncherBackupAgentHelper.java
index e607a0f..a92a889 100644
--- a/src/com/android/launcher3/LauncherBackupAgentHelper.java
+++ b/src/com/android/launcher3/LauncherBackupAgentHelper.java
@@ -92,7 +92,7 @@
             LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);
 
             // TODO: Update the backup set to include rank.
-            if (mHelper.restoredBackupVersion <= 2) {
+            if (mHelper.restoredBackupVersion <= 3) {
                 LauncherAppState.getLauncherProvider().updateFolderItemsRank();
                 LauncherAppState.getLauncherProvider().convertShortcutsToLauncherActivities();
             }
diff --git a/src/com/android/launcher3/LauncherBackupHelper.java b/src/com/android/launcher3/LauncherBackupHelper.java
index f209736..38a4bde 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,10 +148,15 @@
 
     private IconCache mIconCache;
     private DeviceProfieData mDeviceProfileData;
+    private InvariantDeviceProfile mIdp;
 
     boolean restoreSuccessful;
     int restoredBackupVersion = 1;
 
+    // When migrating from a device which different hotseat configuration, the icons are shifted
+    // to center along the new all-apps icon.
+    private int mHotseatShift = 0;
+
     public LauncherBackupHelper(Context context) {
         mContext = context;
         mExistingKeys = new HashSet<String>();
@@ -178,6 +183,7 @@
                 mExistingKeys.add(keyToBackupKey(key));
             }
         }
+        restoredBackupVersion = journal.backupVersion;
     }
 
     /**
@@ -206,7 +212,8 @@
 
         if (mDeviceProfileData == null) {
             LauncherAppState app = LauncherAppState.getInstance();
-            mDeviceProfileData = initDeviceProfileData(app.getInvariantDeviceProfile());
+            mIdp = app.getInvariantDeviceProfile();
+            mDeviceProfileData = initDeviceProfileData(mIdp);
             mIconCache = app.getIconCache();
         }
 
@@ -282,10 +289,15 @@
         boolean isHotsetCompatible = false;
         if (currentProfile.allappsRank >= oldProfile.hotseatCount) {
             isHotsetCompatible = true;
+            mHotseatShift = 0;
         }
-        if ((currentProfile.hotseatCount >= oldProfile.hotseatCount) &&
-                (currentProfile.allappsRank == oldProfile.allappsRank)) {
+
+        if ((currentProfile.allappsRank >= oldProfile.allappsRank)
+                && ((currentProfile.hotseatCount - currentProfile.allappsRank) >=
+                        (oldProfile.hotseatCount - oldProfile.allappsRank))) {
+            // There is enough space on both sides of the hotseat.
             isHotsetCompatible = true;
+            mHotseatShift = currentProfile.allappsRank - oldProfile.allappsRank;
         }
 
         return isHotsetCompatible && (currentProfile.desktopCols >= oldProfile.desktopCols)
@@ -308,9 +320,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 +347,6 @@
                 MessageNano.mergeFrom(journal, readCheckedBytes(mBuffer, dataSize));
                 applyJournal(journal);
                 restoreSuccessful = isBackupCompatible(journal);
-                restoredBackupVersion = journal.backupVersion;
                 return;
             }
 
@@ -599,10 +610,11 @@
         Bitmap icon = BitmapFactory.decodeByteArray(res.data, 0, res.data.length);
         if (icon == null) {
             Log.w(TAG, "failed to unpack icon for " + key.name);
+        } else {
+            if (VERBOSE) Log.v(TAG, "saving restored icon as: " + key.name);
+            mIconCache.preloadIcon(ComponentName.unflattenFromString(key.name), icon, res.dpi,
+                    "" /* label */, mUserSerial, mIdp);
         }
-        if (VERBOSE) Log.v(TAG, "saving restored icon as: " + key.name);
-        mIconCache.preloadIcon(ComponentName.unflattenFromString(key.name), icon, res.dpi,
-                "" /* label */, mUserSerial);
     }
 
     /**
@@ -636,7 +648,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
@@ -683,7 +695,7 @@
                 Log.w(TAG, "failed to unpack widget icon for " + key.name);
             } else {
                 mIconCache.preloadIcon(ComponentName.unflattenFromString(widget.provider),
-                        icon, widget.icon.dpi, widget.label, mUserSerial);
+                        icon, widget.icon.dpi, widget.label, mUserSerial, mIdp);
             }
         }
 
@@ -845,6 +857,11 @@
             throws IOException {
         Favorite favorite = unpackProto(new Favorite(), buffer, dataSize);
 
+        // If it is a hotseat item, move it accordingly.
+        if (favorite.container == Favorites.CONTAINER_HOTSEAT) {
+            favorite.screen += mHotseatShift;
+        }
+
         ContentValues values = new ContentValues();
         values.put(Favorites._ID, favorite.id);
         values.put(Favorites.SCREEN, favorite.screen);
@@ -969,6 +986,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;
     }
 
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index 4972138..a836e90 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -2159,7 +2159,11 @@
                                         appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
                                                 provider.provider);
 
-                                        int status = restoreStatus;
+                                        // The provider is available. So the widget is either
+                                        // available or not available. We do not need to track
+                                        // any future restore updates.
+                                        int status = restoreStatus &
+                                                ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
                                         if (!wasProviderReady) {
                                             // If provider was not previously ready, update the
                                             // status and UI flag.
@@ -3168,7 +3172,9 @@
                             if (mUser.equals(widgetInfo.user)
                                     && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
                                     && packageSet.contains(widgetInfo.providerName.getPackageName())) {
-                                widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
+                                widgetInfo.restoreStatus &=
+                                        ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
+                                        ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
                                 widgets.add(widgetInfo);
                                 updateItemInDatabase(context, widgetInfo);
                             }
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index e3d4136..7eca760 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -2282,6 +2282,8 @@
             dragRect = new Rect(left, top, right, bottom);
         } else if (child instanceof FolderIcon) {
             int previewSize = grid.folderIconSizePx;
+            dragVisualizeOffset = new Point(-padding.get() / 2,
+                    padding.get() / 2 - child.getPaddingTop());
             dragRect = new Rect(0, child.getPaddingTop(), child.getWidth(), previewSize);
         }
 
diff --git a/src/com/android/launcher3/model/WidgetsModel.java b/src/com/android/launcher3/model/WidgetsModel.java
index 15d3717..185dfca 100644
--- a/src/com/android/launcher3/model/WidgetsModel.java
+++ b/src/com/android/launcher3/model/WidgetsModel.java
@@ -8,10 +8,10 @@
 
 import com.android.launcher3.AppFilter;
 import com.android.launcher3.IconCache;
-import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.LauncherAppWidgetProviderInfo;
 import com.android.launcher3.Utilities;
 import com.android.launcher3.compat.AlphabeticIndexCompat;
+import com.android.launcher3.compat.AppWidgetManagerCompat;
 import com.android.launcher3.compat.UserHandleCompat;
 
 import java.util.ArrayList;
@@ -38,6 +38,7 @@
 
     private ArrayList<Object> mRawList;
 
+    private final AppWidgetManagerCompat mAppWidgetMgr;
     private final Comparator mWidgetAndShortcutNameComparator;
     private final Comparator mAppNameComparator;
     private final IconCache mIconCache;
@@ -45,6 +46,7 @@
     private AlphabeticIndexCompat mIndexer;
 
     public WidgetsModel(Context context,  IconCache iconCache, AppFilter appFilter) {
+        mAppWidgetMgr = AppWidgetManagerCompat.getInstance(context);
         mWidgetAndShortcutNameComparator = new WidgetsAndShortcutNameComparator(context);
         mAppNameComparator = (new AppNameComparator(context)).getAppInfoComparator();
         mIconCache = iconCache;
@@ -53,6 +55,7 @@
     }
 
     private WidgetsModel(WidgetsModel model) {
+        mAppWidgetMgr = model.mAppWidgetMgr;
         mPackageItemInfos = (ArrayList<PackageItemInfo>) model.mPackageItemInfos.clone();
         mWidgetsList = (HashMap<PackageItemInfo, ArrayList<Object>>) model.mWidgetsList.clone();
         mRawList = (ArrayList<Object>) model.mRawList.clone();
@@ -104,21 +107,23 @@
         // add and update.
         for (Object o: rawWidgetsShortcuts) {
             String packageName = "";
+            UserHandleCompat userHandle = null;
             ComponentName componentName = null;
             if (o instanceof LauncherAppWidgetProviderInfo) {
                 LauncherAppWidgetProviderInfo widgetInfo = (LauncherAppWidgetProviderInfo) o;
                 componentName = widgetInfo.provider;
                 packageName = widgetInfo.provider.getPackageName();
+                userHandle = mAppWidgetMgr.getUser(widgetInfo);
             } else if (o instanceof ResolveInfo) {
                 ResolveInfo resolveInfo = (ResolveInfo) o;
                 componentName = new ComponentName(resolveInfo.activityInfo.packageName,
                         resolveInfo.activityInfo.name);
                 packageName = resolveInfo.activityInfo.packageName;
+                userHandle = UserHandleCompat.myUserHandle();
             }
 
-            if (componentName == null) {
-                Log.e(TAG, String.format("Widget cannot be set for class=%s",
-                        o.getClass().toString()));
+            if (componentName == null || userHandle == null) {
+                Log.e(TAG, String.format("Widget cannot be set for %s.", o.getClass().toString()));
                 continue;
             }
             if (mAppFilter != null && !mAppFilter.shouldShowApp(componentName)) {
@@ -137,7 +142,7 @@
                 widgetsShortcutsList = new ArrayList<Object>();
                 widgetsShortcutsList.add(o);
                 pInfo = new PackageItemInfo(packageName);
-                mIconCache.getTitleAndIconForApp(packageName, UserHandleCompat.myUserHandle(),
+                mIconCache.getTitleAndIconForApp(packageName, userHandle,
                         true /* userLowResIcon */, pInfo);
                 pInfo.titleSectionName = mIndexer.computeSectionName(pInfo.title);
                 mWidgetsList.put(pInfo, widgetsShortcutsList);
diff --git a/src/com/android/launcher3/widget/PendingAddWidgetInfo.java b/src/com/android/launcher3/widget/PendingAddWidgetInfo.java
index 88a6ca4..758287a 100644
--- a/src/com/android/launcher3/widget/PendingAddWidgetInfo.java
+++ b/src/com/android/launcher3/widget/PendingAddWidgetInfo.java
@@ -23,6 +23,7 @@
 import com.android.launcher3.LauncherAppWidgetProviderInfo;
 import com.android.launcher3.LauncherSettings;
 import com.android.launcher3.PendingAddItemInfo;
+import com.android.launcher3.compat.AppWidgetManagerCompat;
 
 /**
  * Meta data used for late binding of {@link LauncherAppWidgetProviderInfo}.
@@ -47,6 +48,7 @@
             itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
         }
         this.info = i;
+        user = AppWidgetManagerCompat.getInstance(launcher).getUser(i);
         componentName = i.provider;
         minWidth = i.minWidth;
         minHeight = i.minHeight;
diff --git a/src/com/android/launcher3/widget/WidgetsListAdapter.java b/src/com/android/launcher3/widget/WidgetsListAdapter.java
index d2ea252..a54626a 100644
--- a/src/com/android/launcher3/widget/WidgetsListAdapter.java
+++ b/src/com/android/launcher3/widget/WidgetsListAdapter.java
@@ -87,6 +87,9 @@
 
     @Override
     public int getItemCount() {
+        if (mWidgetsModel == null) {
+            return 0;
+        }
         return mWidgetsModel.getPackageSize();
     }