1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.gallery3d.onetimeinitializer;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.os.Build;
22 import android.os.Environment;
23 import android.preference.PreferenceManager;
24 import android.util.Log;
25 
26 import com.android.gallery3d.app.GalleryApp;
27 import com.android.gallery3d.common.ApiHelper;
28 import com.android.gallery3d.data.DataManager;
29 import com.android.gallery3d.data.LocalAlbum;
30 import com.android.gallery3d.data.MediaSet;
31 import com.android.gallery3d.data.Path;
32 import com.android.gallery3d.gadget.WidgetDatabaseHelper;
33 import com.android.gallery3d.gadget.WidgetDatabaseHelper.Entry;
34 import com.android.gallery3d.util.GalleryUtils;
35 
36 import java.io.File;
37 import java.util.HashMap;
38 import java.util.List;
39 
40 /**
41  * This one-timer migrates local-album gallery app widgets from old paths from prior releases
42  * to updated paths in the current build version. This migration is needed because of
43  * bucket ID (i.e., directory hash) change in JB and JB MR1 (The external storage path has changed
44  * from /mnt/sdcard in pre-JB releases, to /storage/sdcard0 in JB, then again
45  * to /external/storage/sdcard/0 in JB MR1).
46  */
47 public class GalleryWidgetMigrator {
48     private static final String TAG = "GalleryWidgetMigrator";
49     private static final String PRE_JB_EXT_PATH = "/mnt/sdcard";
50     private static final String JB_EXT_PATH = "/storage/sdcard0";
51     private static final String NEW_EXT_PATH =
52             Environment.getExternalStorageDirectory().getAbsolutePath();
53     private static final int RELATIVE_PATH_START = NEW_EXT_PATH.length();
54     private static final String KEY_EXT_PATH = "external_storage_path";
55 
56     /**
57      * Migrates local-album gallery widgets from prior releases to current release
58      * due to bucket ID (i.e., directory hash) change.
59      */
migrateGalleryWidgets(Context context)60     public static void migrateGalleryWidgets(Context context) {
61         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
62         // Migration is only needed when external storage path has changed
63         String extPath = prefs.getString(KEY_EXT_PATH, null);
64         boolean isDone = NEW_EXT_PATH.equals(extPath);
65         if (isDone) return;
66 
67         try {
68             migrateGalleryWidgetsInternal(context);
69             prefs.edit().putString(KEY_EXT_PATH, NEW_EXT_PATH).commit();
70         } catch (Throwable t) {
71             // exception may be thrown if external storage is not available(?)
72             Log.w(TAG, "migrateGalleryWidgets", t);
73         }
74     }
75 
migrateGalleryWidgetsInternal(Context context)76     private static void migrateGalleryWidgetsInternal(Context context) {
77         GalleryApp galleryApp = (GalleryApp) context.getApplicationContext();
78         DataManager manager = galleryApp.getDataManager();
79         WidgetDatabaseHelper dbHelper = new WidgetDatabaseHelper(context);
80 
81         // only need to migrate local-album entries of type TYPE_ALBUM
82         List<Entry> entries = dbHelper.getEntries(WidgetDatabaseHelper.TYPE_ALBUM);
83         if (entries == null) return;
84 
85         // Check each entry's relativePath. If exists, update bucket id using relative
86         // path combined with external storage path. Otherwise, iterate through old external
87         // storage paths to find the relative path that matches the old bucket id, and then update
88         // bucket id and relative path
89         HashMap<Integer, Entry> localEntries = new HashMap<Integer, Entry>(entries.size());
90         for (Entry entry : entries) {
91             Path path = Path.fromString(entry.albumPath);
92             MediaSet mediaSet = (MediaSet) manager.getMediaObject(path);
93             if (mediaSet instanceof LocalAlbum) {
94                 if (entry.relativePath != null && entry.relativePath.length() > 0) {
95                     // update entry using relative path + external storage path
96                     updateEntryUsingRelativePath(entry, dbHelper);
97                 } else {
98                     int bucketId = Integer.parseInt(path.getSuffix());
99                     localEntries.put(bucketId, entry);
100                 }
101             }
102         }
103         if (!localEntries.isEmpty()) migrateLocalEntries(context, localEntries, dbHelper);
104     }
105 
migrateLocalEntries(Context context, HashMap<Integer, Entry> entries, WidgetDatabaseHelper dbHelper)106     private static void migrateLocalEntries(Context context,
107             HashMap<Integer, Entry> entries, WidgetDatabaseHelper dbHelper) {
108         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
109         String oldExtPath = prefs.getString(KEY_EXT_PATH, null);
110         if (oldExtPath != null) {
111             migrateLocalEntries(entries, dbHelper, oldExtPath);
112             return;
113         }
114         // If old external storage path is unknown, it could be either Pre-JB or JB version
115         // we need to try both.
116         migrateLocalEntries(entries, dbHelper, PRE_JB_EXT_PATH);
117         if (!entries.isEmpty() &&
118                 Build.VERSION.SDK_INT > ApiHelper.VERSION_CODES.JELLY_BEAN) {
119             migrateLocalEntries(entries, dbHelper, JB_EXT_PATH);
120         }
121     }
122 
migrateLocalEntries(HashMap<Integer, Entry> entries, WidgetDatabaseHelper dbHelper, String oldExtPath)123     private static void migrateLocalEntries(HashMap<Integer, Entry> entries,
124              WidgetDatabaseHelper dbHelper, String oldExtPath) {
125         File root = Environment.getExternalStorageDirectory();
126         // check the DCIM directory first; this should take care of 99% use cases
127         updatePath(new File(root, "DCIM"), entries, dbHelper, oldExtPath);
128         // check other directories if DCIM doesn't cut it
129         if (!entries.isEmpty()) updatePath(root, entries, dbHelper, oldExtPath);
130     }
updatePath(File root, HashMap<Integer, Entry> entries, WidgetDatabaseHelper dbHelper, String oldExtStorage)131     private static void updatePath(File root, HashMap<Integer, Entry> entries,
132             WidgetDatabaseHelper dbHelper, String oldExtStorage) {
133         File[] files = root.listFiles();
134         if (files != null) {
135             for (File file : files) {
136                 if (file.isDirectory() && !entries.isEmpty()) {
137                     String path = file.getAbsolutePath();
138                     String oldPath = oldExtStorage + path.substring(RELATIVE_PATH_START);
139                     int oldBucketId = GalleryUtils.getBucketId(oldPath);
140                     Entry entry = entries.remove(oldBucketId);
141                     if (entry != null) {
142                         int newBucketId = GalleryUtils.getBucketId(path);
143                         String newAlbumPath = Path.fromString(entry.albumPath)
144                                 .getParent()
145                                 .getChild(newBucketId)
146                                 .toString();
147                         Log.d(TAG, "migrate from " + entry.albumPath + " to " + newAlbumPath);
148                         entry.albumPath = newAlbumPath;
149                         // update entry's relative path
150                         entry.relativePath = path.substring(RELATIVE_PATH_START);
151                         dbHelper.updateEntry(entry);
152                     }
153                     updatePath(file, entries, dbHelper, oldExtStorage); // recursion
154                 }
155             }
156         }
157     }
158 
updateEntryUsingRelativePath(Entry entry, WidgetDatabaseHelper dbHelper)159     private static void updateEntryUsingRelativePath(Entry entry, WidgetDatabaseHelper dbHelper) {
160         String newPath = NEW_EXT_PATH + entry.relativePath;
161         int newBucketId = GalleryUtils.getBucketId(newPath);
162         String newAlbumPath = Path.fromString(entry.albumPath)
163                 .getParent()
164                 .getChild(newBucketId)
165                 .toString();
166         entry.albumPath = newAlbumPath;
167         dbHelper.updateEntry(entry);
168     }
169 }
170