Fix shortcup broken issue.

fix: 5154308

Change-Id: I5cd2ef8efb84d4f356b3fe93106bddf10e0823cc
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index f568265..7d9bc1a 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -66,6 +66,17 @@
                 <data android:mimeType="application/x-mpegurl" />
              </intent-filter>
         </activity>
+
+        <!-- we add the activity-alias for shortcut backward compatibility -->
+        <activity-alias android:name="com.cooliris.media.Gallery"
+                android:targetActivity="com.android.gallery3d.app.Gallery"
+                android:configChanges="keyboardHidden|orientation|screenSize"
+                android:label="string/app_name">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+            </intent-filter>
+        </activity-alias>
+
         <activity android:name="com.android.gallery3d.app.Gallery" android:label="@string/app_name"
                 android:configChanges="keyboardHidden|orientation|screenSize">
             <intent-filter>
@@ -200,18 +211,18 @@
                 android:syncable="false"
                 android:grantUriPermissions="true"
                 android:authorities="com.android.gallery3d.provider" />
-        <activity android:name="com.android.gallery3d.widget.WidgetClickHandler" />
+        <activity android:name="com.android.gallery3d.gadget.WidgetClickHandler" />
         <activity android:name="com.android.gallery3d.app.DialogPicker"
                 android:configChanges="keyboardHidden|orientation|screenSize"
                 android:theme="@style/DialogPickerTheme"/>
         <activity android:name="com.android.gallery3d.app.AlbumPicker"
                 android:configChanges="keyboardHidden|orientation|screenSize"
                 android:theme="@style/DialogPickerTheme"/>
-        <activity android:name="com.android.gallery3d.widget.WidgetTypeChooser"
+        <activity android:name="com.android.gallery3d.gadget.WidgetTypeChooser"
                 android:configChanges="keyboardHidden|orientation|screenSize"
                 android:theme="@style/DialogPickerTheme"/>
 
-        <receiver android:name="com.android.gallery3d.widget.WidgetProvider"
+        <receiver android:name="com.android.gallery3d.gadget.PhotoAppWidgetProvider"
                 android:label="@string/appwidget_title">
             <intent-filter>
                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
@@ -226,9 +237,9 @@
                 <data android:scheme="package"/>
             </intent-filter>
         </receiver>
-        <service android:name="com.android.gallery3d.widget.WidgetService"
+        <service android:name="com.android.gallery3d.gadget.WidgetService"
                 android:permission="android.permission.BIND_REMOTEVIEWS"/>
-        <activity android:name="com.android.gallery3d.widget.WidgetConfigure"
+        <activity android:name="com.android.gallery3d.gadget.WidgetConfigure"
                 android:configChanges="keyboardHidden|orientation|screenSize"
                 android:theme="@style/android:Theme.Translucent.NoTitleBar">
             <intent-filter>
diff --git a/res/xml/widget_info.xml b/res/xml/widget_info.xml
index 5f71192..522f848 100644
--- a/res/xml/widget_info.xml
+++ b/res/xml/widget_info.xml
@@ -5,4 +5,4 @@
         android:updatePeriodMillis="86400000"
         android:previewImage="@drawable/preview"
         android:initialLayout="@layout/appwidget_main"
-        android:configure="com.android.gallery3d.widget.WidgetConfigure"/>
+        android:configure="com.android.gallery3d.gadget.WidgetConfigure"/>
diff --git a/src/com/android/gallery3d/app/GalleryAppImpl.java b/src/com/android/gallery3d/app/GalleryAppImpl.java
index a11d920..8d25ebf 100644
--- a/src/com/android/gallery3d/app/GalleryAppImpl.java
+++ b/src/com/android/gallery3d/app/GalleryAppImpl.java
@@ -19,10 +19,10 @@
 import com.android.gallery3d.data.DataManager;
 import com.android.gallery3d.data.DownloadCache;
 import com.android.gallery3d.data.ImageCacheService;
+import com.android.gallery3d.gadget.WidgetUtils;
 import com.android.gallery3d.picasasource.PicasaSource;
 import com.android.gallery3d.util.GalleryUtils;
 import com.android.gallery3d.util.ThreadPool;
-import com.android.gallery3d.widget.WidgetUtils;
 
 import android.app.Application;
 import android.content.Context;
diff --git a/src/com/android/gallery3d/widget/LocalPhotoSource.java b/src/com/android/gallery3d/gadget/LocalPhotoSource.java
similarity index 99%
rename from src/com/android/gallery3d/widget/LocalPhotoSource.java
rename to src/com/android/gallery3d/gadget/LocalPhotoSource.java
index de16a71..ad77de5 100644
--- a/src/com/android/gallery3d/widget/LocalPhotoSource.java
+++ b/src/com/android/gallery3d/gadget/LocalPhotoSource.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.gallery3d.widget;
+package com.android.gallery3d.gadget;
 
 import com.android.gallery3d.app.GalleryApp;
 import com.android.gallery3d.common.Utils;
diff --git a/src/com/android/gallery3d/widget/MediaSetSource.java b/src/com/android/gallery3d/gadget/MediaSetSource.java
similarity index 98%
rename from src/com/android/gallery3d/widget/MediaSetSource.java
rename to src/com/android/gallery3d/gadget/MediaSetSource.java
index 1677f69..c1687e0 100644
--- a/src/com/android/gallery3d/widget/MediaSetSource.java
+++ b/src/com/android/gallery3d/gadget/MediaSetSource.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.gallery3d.widget;
+package com.android.gallery3d.gadget;
 
 import com.android.gallery3d.common.Utils;
 import com.android.gallery3d.data.ContentListener;
diff --git a/src/com/android/gallery3d/widget/WidgetProvider.java b/src/com/android/gallery3d/gadget/PhotoAppWidgetProvider.java
similarity index 76%
rename from src/com/android/gallery3d/widget/WidgetProvider.java
rename to src/com/android/gallery3d/gadget/PhotoAppWidgetProvider.java
index 0a2fbfb..814ede2 100644
--- a/src/com/android/gallery3d/widget/WidgetProvider.java
+++ b/src/com/android/gallery3d/gadget/PhotoAppWidgetProvider.java
@@ -14,21 +14,23 @@
  * limitations under the License.
  */
 
-package com.android.gallery3d.widget;
+package com.android.gallery3d.gadget;
 
 import com.android.gallery3d.R;
-import com.android.gallery3d.widget.WidgetDatabaseHelper.Entry;
+import com.android.gallery3d.gadget.WidgetDatabaseHelper.Entry;
 
 import android.app.PendingIntent;
 import android.appwidget.AppWidgetManager;
 import android.appwidget.AppWidgetProvider;
 import android.content.Context;
 import android.content.Intent;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.util.Log;
 import android.widget.RemoteViews;
 
-public class WidgetProvider extends AppWidgetProvider {
+public class PhotoAppWidgetProvider extends AppWidgetProvider {
 
     private static final String TAG = "WidgetProvider";
 
@@ -88,12 +90,26 @@
     static RemoteViews buildFrameWidget(Context context, int appWidgetId, Entry entry) {
         RemoteViews views = new RemoteViews(
                 context.getPackageName(), R.layout.photo_frame);
-        views.setImageViewBitmap(R.id.photo, entry.image);
-        Intent clickIntent = new Intent(context,
-                WidgetClickHandler.class).setData(entry.imageUri);
-        PendingIntent pendingClickIntent = PendingIntent.getActivity(context, 0,
-                clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
-        views.setOnClickPendingIntent(R.id.photo, pendingClickIntent);
+        try {
+            byte[] data = entry.imageData;
+            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
+            views.setImageViewBitmap(R.id.photo, bitmap);
+        } catch (Throwable t) {
+            Log.w(TAG, "cannot load widget image: " + appWidgetId, t);
+        }
+
+        if (entry.imageUri != null) {
+            try {
+                Uri uri = Uri.parse(entry.imageUri);
+                Intent clickIntent = new Intent(context, WidgetClickHandler.class)
+                        .setData(uri);
+                PendingIntent pendingClickIntent = PendingIntent.getActivity(context, 0,
+                        clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
+                views.setOnClickPendingIntent(R.id.photo, pendingClickIntent);
+            } catch (Throwable t) {
+                Log.w(TAG, "cannot load widget uri: " + appWidgetId, t);
+            }
+        }
         return views;
     }
 
diff --git a/src/com/android/gallery3d/widget/WidgetClickHandler.java b/src/com/android/gallery3d/gadget/WidgetClickHandler.java
similarity index 97%
rename from src/com/android/gallery3d/widget/WidgetClickHandler.java
rename to src/com/android/gallery3d/gadget/WidgetClickHandler.java
index 362e4d2..075644d 100644
--- a/src/com/android/gallery3d/widget/WidgetClickHandler.java
+++ b/src/com/android/gallery3d/gadget/WidgetClickHandler.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.gallery3d.widget;
+package com.android.gallery3d.gadget;
 
 import com.android.gallery3d.R;
 import com.android.gallery3d.app.Gallery;
diff --git a/src/com/android/gallery3d/widget/WidgetConfigure.java b/src/com/android/gallery3d/gadget/WidgetConfigure.java
similarity index 97%
rename from src/com/android/gallery3d/widget/WidgetConfigure.java
rename to src/com/android/gallery3d/gadget/WidgetConfigure.java
index 3bcd9c4..747cc3a 100644
--- a/src/com/android/gallery3d/widget/WidgetConfigure.java
+++ b/src/com/android/gallery3d/gadget/WidgetConfigure.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.gallery3d.widget;
+package com.android.gallery3d.gadget;
 
 import com.android.gallery3d.R;
 import com.android.gallery3d.app.AlbumPicker;
@@ -72,7 +72,7 @@
 
     private void updateWidgetAndFinish(WidgetDatabaseHelper.Entry entry) {
         AppWidgetManager manager = AppWidgetManager.getInstance(this);
-        RemoteViews views = WidgetProvider.buildWidget(this, mAppWidgetId, entry);
+        RemoteViews views = PhotoAppWidgetProvider.buildWidget(this, mAppWidgetId, entry);
         manager.updateAppWidget(mAppWidgetId, views);
         setResult(RESULT_OK, new Intent().putExtra(
                 AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId));
diff --git a/src/com/android/gallery3d/widget/WidgetDatabaseHelper.java b/src/com/android/gallery3d/gadget/WidgetDatabaseHelper.java
similarity index 70%
rename from src/com/android/gallery3d/widget/WidgetDatabaseHelper.java
rename to src/com/android/gallery3d/gadget/WidgetDatabaseHelper.java
index d5bf22e..1d07548 100644
--- a/src/com/android/gallery3d/widget/WidgetDatabaseHelper.java
+++ b/src/com/android/gallery3d/gadget/WidgetDatabaseHelper.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.gallery3d.widget;
+package com.android.gallery3d.gadget;
 
 import com.android.gallery3d.common.Utils;
 
@@ -25,11 +25,11 @@
 import android.database.sqlite.SQLiteException;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.util.Log;
 
 import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
 
 public class WidgetDatabaseHelper extends SQLiteOpenHelper {
     private static final String TAG = "PhotoDatabaseHelper";
@@ -60,17 +60,18 @@
     public static class Entry {
         public int widgetId;
         public int type;
-        public Uri imageUri;
-        public Bitmap image;
+        public String imageUri;
+        public byte imageData[];
         public String albumPath;
 
+        private Entry() {}
+
         private Entry(int id, Cursor cursor) {
             widgetId = id;
             type = cursor.getInt(INDEX_WIDGET_TYPE);
-
             if (type == TYPE_SINGLE_PHOTO) {
-                imageUri = Uri.parse(cursor.getString(INDEX_IMAGE_URI));
-                image = loadBitmap(cursor, INDEX_PHOTO_BLOB);
+                imageUri = cursor.getString(INDEX_IMAGE_URI);
+                imageData = cursor.getBlob(INDEX_PHOTO_BLOB);
             } else if (type == TYPE_ALBUM) {
                 albumPath = cursor.getString(INDEX_ALBUM_PATH);
             }
@@ -91,16 +92,79 @@
                 + FIELD_PHOTO_BLOB + " BLOB)");
     }
 
+    private void saveData(SQLiteDatabase db, int oldVersion, ArrayList<Entry> data) {
+        if (oldVersion <= 2) {
+            Cursor cursor = db.query("photos",
+                    new String[] {FIELD_APPWIDGET_ID, FIELD_PHOTO_BLOB},
+                    null, null, null, null, null);
+            if (cursor == null) return;
+            try {
+                while (cursor.moveToNext()) {
+                    Entry entry = new Entry();
+                    entry.type = TYPE_SINGLE_PHOTO;
+                    entry.widgetId = cursor.getInt(0);
+                    entry.imageData = cursor.getBlob(1);
+                    data.add(entry);
+                }
+            } finally {
+                cursor.close();
+            }
+        } else if (oldVersion == 3) {
+            Utils.debug("saveData of version: %s", oldVersion);
+            Cursor cursor = db.query("photos",
+                    new String[] {FIELD_APPWIDGET_ID, FIELD_PHOTO_BLOB, FIELD_IMAGE_URI},
+                    null, null, null, null, null);
+            if (cursor == null) return;
+            try {
+                while (cursor.moveToNext()) {
+                    Entry entry = new Entry();
+                    entry.type = TYPE_SINGLE_PHOTO;
+                    entry.widgetId = cursor.getInt(0);
+                    entry.imageData = cursor.getBlob(1);
+                    entry.imageUri = cursor.getString(2);
+
+                    Utils.debug("store widget[%s] - %s", entry.widgetId, entry.imageUri);
+                    data.add(entry);
+                }
+            } finally {
+                cursor.close();
+            }
+        }
+    }
+
+    private void restoreData(SQLiteDatabase db, ArrayList<Entry> data) {
+        db.beginTransaction();
+        try {
+            for (Entry entry : data) {
+                ContentValues values = new ContentValues();
+                values.put(FIELD_APPWIDGET_ID, entry.widgetId);
+                values.put(FIELD_WIDGET_TYPE, entry.type);
+                values.put(FIELD_IMAGE_URI, entry.imageUri);
+                values.put(FIELD_PHOTO_BLOB, entry.imageData);
+                values.put(FIELD_ALBUM_PATH, entry.albumPath);
+                db.insert(TABLE_WIDGETS, null, values);
+            }
+            db.setTransactionSuccessful();
+        } finally {
+            db.endTransaction();
+        }
+    }
+
     @Override
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         int version = oldVersion;
 
         if (version != DATABASE_VERSION) {
+            ArrayList<Entry> data = new ArrayList<Entry>();
+            saveData(db, oldVersion, data);
+
             Log.w(TAG, "destroying all old data.");
             // Table "photos" is renamed to "widget" in version 4
             db.execSQL("DROP TABLE IF EXISTS photos");
             db.execSQL("DROP TABLE IF EXISTS " + TABLE_WIDGETS);
             onCreate(db);
+
+            restoreData(db, data);
         }
     }
 
@@ -146,12 +210,6 @@
         }
     }
 
-    private static Bitmap loadBitmap(Cursor cursor, int columnIndex) {
-        byte[] data = cursor.getBlob(columnIndex);
-        if (data == null) return null;
-        return BitmapFactory.decodeByteArray(data, 0, data.length);
-    }
-
     public Entry getEntry(int appWidgetId) {
         Cursor cursor = null;
         try {
diff --git a/src/com/android/gallery3d/widget/WidgetService.java b/src/com/android/gallery3d/gadget/WidgetService.java
similarity index 99%
rename from src/com/android/gallery3d/widget/WidgetService.java
rename to src/com/android/gallery3d/gadget/WidgetService.java
index aa167c7..a61831c 100644
--- a/src/com/android/gallery3d/widget/WidgetService.java
+++ b/src/com/android/gallery3d/gadget/WidgetService.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.gallery3d.widget;
+package com.android.gallery3d.gadget;
 
 import com.android.gallery3d.R;
 import com.android.gallery3d.app.GalleryApp;
diff --git a/src/com/android/gallery3d/widget/WidgetSource.java b/src/com/android/gallery3d/gadget/WidgetSource.java
similarity index 96%
rename from src/com/android/gallery3d/widget/WidgetSource.java
rename to src/com/android/gallery3d/gadget/WidgetSource.java
index 3c73e88..8b8eb79 100644
--- a/src/com/android/gallery3d/widget/WidgetSource.java
+++ b/src/com/android/gallery3d/gadget/WidgetSource.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.gallery3d.widget;
+package com.android.gallery3d.gadget;
 
 import com.android.gallery3d.data.ContentListener;
 
diff --git a/src/com/android/gallery3d/widget/WidgetTypeChooser.java b/src/com/android/gallery3d/gadget/WidgetTypeChooser.java
similarity index 97%
rename from src/com/android/gallery3d/widget/WidgetTypeChooser.java
rename to src/com/android/gallery3d/gadget/WidgetTypeChooser.java
index 9718e0c..c4bca60 100644
--- a/src/com/android/gallery3d/widget/WidgetTypeChooser.java
+++ b/src/com/android/gallery3d/gadget/WidgetTypeChooser.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.gallery3d.widget;
+package com.android.gallery3d.gadget;
 
 import com.android.gallery3d.R;
 
diff --git a/src/com/android/gallery3d/widget/WidgetUtils.java b/src/com/android/gallery3d/gadget/WidgetUtils.java
similarity index 98%
rename from src/com/android/gallery3d/widget/WidgetUtils.java
rename to src/com/android/gallery3d/gadget/WidgetUtils.java
index 481bbdd..b194c7d 100644
--- a/src/com/android/gallery3d/widget/WidgetUtils.java
+++ b/src/com/android/gallery3d/gadget/WidgetUtils.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.gallery3d.widget;
+package com.android.gallery3d.gadget;
 
 import com.android.gallery3d.R;
 import com.android.gallery3d.data.MediaItem;