diff options
Diffstat (limited to 'src/com/android/gallery3d/onetimeinitializer/GalleryWidgetMigrator.java')
-rw-r--r-- | src/com/android/gallery3d/onetimeinitializer/GalleryWidgetMigrator.java | 99 |
1 files changed, 69 insertions, 30 deletions
diff --git a/src/com/android/gallery3d/onetimeinitializer/GalleryWidgetMigrator.java b/src/com/android/gallery3d/onetimeinitializer/GalleryWidgetMigrator.java index 0187cba4b..ef26b1b97 100644 --- a/src/com/android/gallery3d/onetimeinitializer/GalleryWidgetMigrator.java +++ b/src/com/android/gallery3d/onetimeinitializer/GalleryWidgetMigrator.java @@ -18,11 +18,13 @@ package com.android.gallery3d.onetimeinitializer; import android.content.Context; import android.content.SharedPreferences; +import android.os.Build; import android.os.Environment; import android.preference.PreferenceManager; import android.util.Log; import com.android.gallery3d.app.GalleryApp; +import com.android.gallery3d.common.ApiHelper; import com.android.gallery3d.data.DataManager; import com.android.gallery3d.data.LocalAlbum; import com.android.gallery3d.data.MediaSet; @@ -36,34 +38,35 @@ import java.util.HashMap; import java.util.List; /** - * This one-timer migrates local-album gallery app widgets from pre-JB releases to JB (or later) - * due to bucket ID (i.e., directory hash) change in JB (as the external storage path is changed - * from /mnt/sdcard to /storage/sdcard0). + * This one-timer migrates local-album gallery app widgets from old paths from prior releases + * to updated paths in the current build version. This migration is needed because of + * bucket ID (i.e., directory hash) change in JB and JB MR1 (The external storage path has changed + * from /mnt/sdcard in pre-JB releases, to /storage/sdcard0 in JB, then again + * to /external/storage/sdcard/0 in JB MR1). */ public class GalleryWidgetMigrator { private static final String TAG = "GalleryWidgetMigrator"; - private static final String OLD_EXT_PATH = "/mnt/sdcard"; + private static final String PRE_JB_EXT_PATH = "/mnt/sdcard"; + private static final String JB_EXT_PATH = "/storage/sdcard0"; private static final String NEW_EXT_PATH = Environment.getExternalStorageDirectory().getAbsolutePath(); private static final int RELATIVE_PATH_START = NEW_EXT_PATH.length(); - private static final String KEY_MIGRATION_DONE = "gallery_widget_migration_done"; + private static final String KEY_EXT_PATH = "external_storage_path"; /** - * Migrates local-album gallery widgets from pre-JB releases to JB (or later) due to bucket ID - * (i.e., directory hash) change in JB. + * Migrates local-album gallery widgets from prior releases to current release + * due to bucket ID (i.e., directory hash) change. */ public static void migrateGalleryWidgets(Context context) { - // no migration needed if path of external storage is not changed - if (OLD_EXT_PATH.equals(NEW_EXT_PATH)) return; - - // only need to migrate once; the "done" bit is saved to SharedPreferences SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - boolean isDone = prefs.getBoolean(KEY_MIGRATION_DONE, false); + // Migration is only needed when external storage path has changed + String extPath = prefs.getString(KEY_EXT_PATH, null); + boolean isDone = NEW_EXT_PATH.equals(extPath); if (isDone) return; try { migrateGalleryWidgetsInternal(context); - prefs.edit().putBoolean(KEY_MIGRATION_DONE, true).commit(); + prefs.edit().putString(KEY_EXT_PATH, NEW_EXT_PATH).commit(); } catch (Throwable t) { // exception may be thrown if external storage is not available(?) Log.w(TAG, "migrateGalleryWidgets", t); @@ -77,39 +80,62 @@ public class GalleryWidgetMigrator { // only need to migrate local-album entries of type TYPE_ALBUM List<Entry> entries = dbHelper.getEntries(WidgetDatabaseHelper.TYPE_ALBUM); - if (entries != null) { - HashMap<Integer, Entry> localEntries = new HashMap<Integer, Entry>(entries.size()); - for (Entry entry : entries) { - Path path = Path.fromString(entry.albumPath); - MediaSet mediaSet = (MediaSet) manager.getMediaObject(path); - if (mediaSet instanceof LocalAlbum) { + if (entries == null) return; + + // Check each entry's relativePath. If exists, update bucket id using relative + // path combined with external storage path. Otherwise, iterate through old external + // storage paths to find the relative path that matches the old bucket id, and then update + // bucket id and relative path + HashMap<Integer, Entry> localEntries = new HashMap<Integer, Entry>(entries.size()); + for (Entry entry : entries) { + Path path = Path.fromString(entry.albumPath); + MediaSet mediaSet = (MediaSet) manager.getMediaObject(path); + if (mediaSet instanceof LocalAlbum) { + if (entry.relativePath != null && entry.relativePath.length() > 0) { + // update entry using relative path + external storage path + updateEntryUsingRelativePath(entry, dbHelper); + } else { int bucketId = Integer.parseInt(path.getSuffix()); localEntries.put(bucketId, entry); } } - if (!localEntries.isEmpty()) migrateLocalEntries(localEntries, dbHelper); } + if (!localEntries.isEmpty()) migrateLocalEntries(context, localEntries, dbHelper); } - private static void migrateLocalEntries( + private static void migrateLocalEntries(Context context, HashMap<Integer, Entry> entries, WidgetDatabaseHelper dbHelper) { - File root = Environment.getExternalStorageDirectory(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String oldExtPath = prefs.getString(KEY_EXT_PATH, null); + if (oldExtPath != null) { + migrateLocalEntries(entries, dbHelper, oldExtPath); + return; + } + // If old external storage path is unknown, it could be either Pre-JB or JB version + // we need to try both. + migrateLocalEntries(entries, dbHelper, PRE_JB_EXT_PATH); + if (!entries.isEmpty() && + Build.VERSION.SDK_INT > ApiHelper.VERSION_CODES.JELLY_BEAN) { + migrateLocalEntries(entries, dbHelper, JB_EXT_PATH); + } + } + private static void migrateLocalEntries(HashMap<Integer, Entry> entries, + WidgetDatabaseHelper dbHelper, String oldExtPath) { + File root = Environment.getExternalStorageDirectory(); // check the DCIM directory first; this should take care of 99% use cases - updatePath(new File(root, "DCIM"), entries, dbHelper); - + updatePath(new File(root, "DCIM"), entries, dbHelper, oldExtPath); // check other directories if DCIM doesn't cut it - if (!entries.isEmpty()) updatePath(root, entries, dbHelper); + if (!entries.isEmpty()) updatePath(root, entries, dbHelper, oldExtPath); } - - private static void updatePath( - File root, HashMap<Integer, Entry> entries, WidgetDatabaseHelper dbHelper) { + private static void updatePath(File root, HashMap<Integer, Entry> entries, + WidgetDatabaseHelper dbHelper, String oldExtStorage) { File[] files = root.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory() && !entries.isEmpty()) { String path = file.getAbsolutePath(); - String oldPath = OLD_EXT_PATH + path.substring(RELATIVE_PATH_START); + String oldPath = oldExtStorage + path.substring(RELATIVE_PATH_START); int oldBucketId = GalleryUtils.getBucketId(oldPath); Entry entry = entries.remove(oldBucketId); if (entry != null) { @@ -120,11 +146,24 @@ public class GalleryWidgetMigrator { .toString(); Log.d(TAG, "migrate from " + entry.albumPath + " to " + newAlbumPath); entry.albumPath = newAlbumPath; + // update entry's relative path + entry.relativePath = path.substring(RELATIVE_PATH_START); dbHelper.updateEntry(entry); } - updatePath(file, entries, dbHelper); // recursion + updatePath(file, entries, dbHelper, oldExtStorage); // recursion } } } } + + private static void updateEntryUsingRelativePath(Entry entry, WidgetDatabaseHelper dbHelper) { + String newPath = NEW_EXT_PATH + entry.relativePath; + int newBucketId = GalleryUtils.getBucketId(newPath); + String newAlbumPath = Path.fromString(entry.albumPath) + .getParent() + .getChild(newBucketId) + .toString(); + entry.albumPath = newAlbumPath; + dbHelper.updateEntry(entry); + } } |