summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/LauncherBackupHelper.java
diff options
context:
space:
mode:
authorAdam Cohen <adamcohen@google.com>2015-05-06 11:42:25 -0700
committerAdam Cohen <adamcohen@google.com>2015-05-21 16:16:40 -0700
commit2e6da1539bc7286336b3c24d96ab76434939ce4d (patch)
tree38c7387dc9a5f6f084c4998aac88495189423d3b /src/com/android/launcher3/LauncherBackupHelper.java
parenta9d012c6e67b644498e07c63f36617c1b8915e02 (diff)
downloadandroid_packages_apps_Trebuchet-2e6da1539bc7286336b3c24d96ab76434939ce4d.tar.gz
android_packages_apps_Trebuchet-2e6da1539bc7286336b3c24d96ab76434939ce4d.tar.bz2
android_packages_apps_Trebuchet-2e6da1539bc7286336b3c24d96ab76434939ce4d.zip
Refactoring DeviceProfile
-> Pulling out the parts of device profile which can (and need to be) initialized and accessed without access to an Activity context, ie. the invariant bits. -> The invariant bits are stored in InvariantDeviceProfile which is initialized statically from LauncherAppState. -> The DeviceProfile contains the Activity context-dependent bits, and we will create one of these for each Activity instance, and this instance is accessed through the Launcher activity. -> It's possible that we can continue to refactor this such that all appropriate dimensions can be computed without an Activity context (by only specifying orientation). This would be an extension of this CL and allow us to know exactly how launcher will look in both orientations from any context. Sets the stage for some improvements around b/19514688 Change-Id: Ia7daccf14d8ca2b9cb340b8780b684769e9f1892
Diffstat (limited to 'src/com/android/launcher3/LauncherBackupHelper.java')
-rw-r--r--src/com/android/launcher3/LauncherBackupHelper.java104
1 files changed, 40 insertions, 64 deletions
diff --git a/src/com/android/launcher3/LauncherBackupHelper.java b/src/com/android/launcher3/LauncherBackupHelper.java
index 92bbb4019..af4101221 100644
--- a/src/com/android/launcher3/LauncherBackupHelper.java
+++ b/src/com/android/launcher3/LauncherBackupHelper.java
@@ -141,13 +141,15 @@ public class LauncherBackupHelper implements BackupHelper {
private final ItemTypeMatcher[] mItemTypeMatchers;
private final long mUserSerial;
- private IconCache mIconCache;
private BackupManager mBackupManager;
private byte[] mBuffer = new byte[512];
private long mLastBackupRestoreTime;
private boolean mBackupDataWasUpdated;
- private DeviceProfieData mCurrentProfile;
+ private LauncherAppState mLauncherAppState;
+ private IconCache mIconCache;
+ private DeviceProfieData mDeviceProfileData;
+
boolean restoreSuccessful;
int restoredBackupVersion = 1;
@@ -197,10 +199,14 @@ public class LauncherBackupHelper implements BackupHelper {
Journal in = readJournal(oldState);
if (!launcherIsReady()) {
+ dataChanged();
// Perform backup later.
writeJournal(newState, in);
return;
}
+
+ lazyInitAppState(true /* noCreate */);
+
Log.v(TAG, "lastBackupTime = " + in.t);
mKeys.clear();
applyJournal(in);
@@ -296,12 +302,7 @@ public class LauncherBackupHelper implements BackupHelper {
if (!restoreSuccessful) {
return;
}
- if (!initializeIconCache()) {
- // During restore we do not need an initialized instance of IconCache. We can create
- // a temporary icon cache here, as the process will be rebooted after restore
- // is complete.
- mIconCache = new IconCache(mContext);
- }
+ lazyInitAppState(false /* noCreate */);
int dataSize = data.size();
if (mBuffer.length < dataSize) {
@@ -395,19 +396,34 @@ public class LauncherBackupHelper implements BackupHelper {
* @return the current device profile information.
*/
private DeviceProfieData getDeviceProfieData() {
- if (mCurrentProfile != null) {
- return mCurrentProfile;
+ return mDeviceProfileData;
+ }
+
+ private void lazyInitAppState(boolean noCreate) {
+ if (mLauncherAppState != null) {
+ return;
+ }
+
+ if (noCreate) {
+ mLauncherAppState = LauncherAppState.getInstanceNoCreate();
+ } else {
+ LauncherAppState.setApplicationContext(mContext);
+ mLauncherAppState = LauncherAppState.getInstance();
}
- final Context applicationContext = mContext.getApplicationContext();
- DeviceProfile profile = LauncherAppState.createDynamicGrid(applicationContext, null)
- .getDeviceProfile();
-
- mCurrentProfile = new DeviceProfieData();
- mCurrentProfile.desktopRows = profile.numRows;
- mCurrentProfile.desktopCols = profile.numColumns;
- mCurrentProfile.hotseatCount = profile.numHotseatIcons;
- mCurrentProfile.allappsRank = profile.hotseatAllAppsRank;
- return mCurrentProfile;
+
+ mIconCache = mLauncherAppState.getIconCache();
+ InvariantDeviceProfile profile = mLauncherAppState.getInvariantDeviceProfile();
+
+ mDeviceProfileData = initDeviceProfileData(profile);
+ }
+
+ private DeviceProfieData initDeviceProfileData(InvariantDeviceProfile profile) {
+ DeviceProfieData data = new DeviceProfieData();
+ data.desktopRows = profile.numRows;
+ data.desktopCols = profile.numColumns;
+ data.hotseatCount = profile.numHotseatIcons;
+ data.allappsRank = profile.hotseatAllAppsRank;
+ return data;
}
/**
@@ -518,11 +534,6 @@ public class LauncherBackupHelper implements BackupHelper {
*/
private void backupIcons(BackupDataOutput data) throws IOException {
// persist icons that haven't been persisted yet
- if (!initializeIconCache()) {
- dataChanged(); // try again later
- if (DEBUG) Log.d(TAG, "Launcher is not initialized, delaying icon backup");
- return;
- }
final ContentResolver cr = mContext.getContentResolver();
final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
final UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle();
@@ -619,16 +630,9 @@ public class LauncherBackupHelper implements BackupHelper {
*/
private void backupWidgets(BackupDataOutput data) throws IOException {
// persist static widget info that hasn't been persisted yet
- final LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
- if (appState == null || !initializeIconCache()) {
- Log.w(TAG, "Failed to get icon cache during restore");
- return;
- }
final ContentResolver cr = mContext.getContentResolver();
- final WidgetPreviewLoader previewLoader = appState.getWidgetCache();
+ final WidgetPreviewLoader previewLoader = mLauncherAppState.getWidgetCache();
final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
- final DeviceProfile profile = appState.getDynamicGrid().getDeviceProfile();
- if (DEBUG) Log.d(TAG, "cellWidthPx: " + profile.cellWidthPx);
int backupWidgetCount = 0;
String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET + " AND "
@@ -661,8 +665,7 @@ public class LauncherBackupHelper implements BackupHelper {
if (DEBUG) Log.d(TAG, "saving widget " + backupKey);
UserHandleCompat user = UserHandleCompat.myUserHandle();
writeRowToBackup(key,
- packWidget(dpi, previewLoader,spanX * profile.cellWidthPx,
- mIconCache, provider, user),
+ packWidget(dpi, previewLoader, mIconCache, provider, user),
data);
mKeys.add(key);
backupWidgetCount ++;
@@ -969,8 +972,7 @@ public class LauncherBackupHelper implements BackupHelper {
}
/** Serialize a widget for persistence, including a checksum wrapper. */
- private Widget packWidget(int dpi, WidgetPreviewLoader previewLoader,
- int previewWidth, IconCache iconCache,
+ private Widget packWidget(int dpi, WidgetPreviewLoader previewLoader, IconCache iconCache,
ComponentName provider, UserHandleCompat user) {
final LauncherAppWidgetProviderInfo info =
LauncherModel.getProviderInfo(mContext, provider, user);
@@ -985,12 +987,6 @@ public class LauncherBackupHelper implements BackupHelper {
widget.icon.data = Utilities.flattenBitmap(icon);
widget.icon.dpi = dpi;
}
- if (info.previewImage != 0) {
- widget.preview = new Resource();
- Bitmap preview = previewLoader.generateWidgetPreview(info, previewWidth, null);
- widget.preview.data = Utilities.flattenBitmap(preview);
- widget.preview.dpi = dpi;
- }
return widget;
}
@@ -1136,25 +1132,6 @@ public class LauncherBackupHelper implements BackupHelper {
return wrapper.payload;
}
- private boolean initializeIconCache() {
- if (mIconCache != null) {
- return true;
- }
-
- final LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
- if (appState == null) {
- if (DEBUG) {
- Throwable stackTrace = new Throwable();
- stackTrace.fillInStackTrace();
- Log.w(TAG, "Failed to get app state during backup/restore", stackTrace);
- }
- return false;
- }
- mIconCache = appState.getIconCache();
- return mIconCache != null;
- }
-
-
/**
* @return true if the launcher is in a state to support backup
*/
@@ -1167,9 +1144,8 @@ public class LauncherBackupHelper implements BackupHelper {
}
cursor.close();
- if (!initializeIconCache()) {
+ if (LauncherAppState.getInstanceNoCreate() == null) {
// launcher services are unavailable, try again later
- dataChanged();
return false;
}