Merge "Import translations. DO NOT MERGE" into jb-ub-now-kermit
diff --git a/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java b/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java
index d3c779f..c54e477 100644
--- a/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java
+++ b/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java
@@ -85,6 +85,7 @@
     public static final int PICK_WALLPAPER_THIRD_PARTY_ACTIVITY = 6;
     public static final int PICK_LIVE_WALLPAPER = 7;
     private static final String TEMP_WALLPAPER_TILES = "TEMP_WALLPAPER_TILES";
+    private static final String SELECTED_INDEX = "SELECTED_INDEX";
     private static final String OLD_DEFAULT_WALLPAPER_THUMBNAIL_FILENAME = "default_thumb.jpg";
     private static final String DEFAULT_WALLPAPER_THUMBNAIL_FILENAME = "default_thumb2.jpg";
 
@@ -103,6 +104,7 @@
     ArrayList<Uri> mTempWallpaperTiles = new ArrayList<Uri>();
     private SavedWallpaperImages mSavedImages;
     private WallpaperInfo mLiveWallpaperInfoOnPickerLaunch;
+    private int mSelectedIndex;
 
     public static abstract class WallpaperTileInfo {
         protected View mView;
@@ -148,7 +150,6 @@
                     public void run() {
                         if (mBitmapSource != null &&
                                 mBitmapSource.getLoadingState() == BitmapSource.State.LOADED) {
-                            mView.setVisibility(View.VISIBLE);
                             a.selectTile(mView);
                         } else {
                             ViewGroup parent = (ViewGroup) mView.getParent();
@@ -430,8 +431,9 @@
             public void onLayoutChange(View v, int left, int top, int right, int bottom,
                     int oldLeft, int oldTop, int oldRight, int oldBottom) {
                 if ((right - left) > 0 && (bottom - top) > 0) {
-                    if (mWallpapersView.getChildCount() > 0) {
-                        mThumbnailOnClickListener.onClick(mWallpapersView.getChildAt(0));
+                    if (mSelectedIndex >= 0 && mSelectedIndex < mWallpapersView.getChildCount()) {
+                        mThumbnailOnClickListener.onClick(
+                                mWallpapersView.getChildAt(mSelectedIndex));
                     }
                     v.removeOnLayoutChangeListener(this);
                 }
@@ -551,6 +553,7 @@
         }
         mSelectedTile = v;
         v.setSelected(true);
+        mSelectedIndex = mWallpapersView.indexOfChild(v);
         // TODO: Remove this once the accessibility framework and
         // services have better support for selection state.
         v.announceForAccessibility(
@@ -601,13 +604,15 @@
 
     protected void onSaveInstanceState(Bundle outState) {
         outState.putParcelableArrayList(TEMP_WALLPAPER_TILES, mTempWallpaperTiles);
+        outState.putInt(SELECTED_INDEX, mSelectedIndex);
     }
 
     protected void onRestoreInstanceState(Bundle savedInstanceState) {
         ArrayList<Uri> uris = savedInstanceState.getParcelableArrayList(TEMP_WALLPAPER_TILES);
         for (Uri uri : uris) {
-            addTemporaryWallpaperTile(uri);
+            addTemporaryWallpaperTile(uri, true);
         }
+        mSelectedIndex = savedInstanceState.getInt(SELECTED_INDEX, 0);
     }
 
     private void populateWallpapersFromAdapter(ViewGroup parent, BaseAdapter adapter,
@@ -711,7 +716,7 @@
         }
     }
 
-    private void addTemporaryWallpaperTile(final Uri uri) {
+    private void addTemporaryWallpaperTile(final Uri uri, boolean fromRestore) {
         mTempWallpaperTiles.add(uri);
         // Add a tile for the image picked from Gallery
         final FrameLayout pickedImageThumbnail = (FrameLayout) getLayoutInflater().
@@ -735,6 +740,7 @@
                     image.setImageBitmap(thumb);
                     Drawable thumbDrawable = image.getDrawable();
                     thumbDrawable.setDither(true);
+                    pickedImageThumbnail.setVisibility(View.VISIBLE);
                 } else {
                     Log.e(TAG, "Error loading thumbnail for uri=" + uri);
                 }
@@ -747,14 +753,16 @@
         addLongPressHandler(pickedImageThumbnail);
         updateTileIndices();
         pickedImageThumbnail.setOnClickListener(mThumbnailOnClickListener);
-        mThumbnailOnClickListener.onClick(pickedImageThumbnail);
+        if (!fromRestore) {
+            mThumbnailOnClickListener.onClick(pickedImageThumbnail);
+        }
     }
 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == IMAGE_PICK && resultCode == RESULT_OK) {
             if (data != null && data.getData() != null) {
                 Uri uri = data.getData();
-                addTemporaryWallpaperTile(uri);
+                addTemporaryWallpaperTile(uri, false);
             }
         } else if (requestCode == PICK_WALLPAPER_THIRD_PARTY_ACTIVITY) {
             setResult(RESULT_OK);
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 6e3032f..c119999 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -41,6 +41,7 @@
 import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.ResolveInfo;
@@ -478,11 +479,18 @@
         // On large interfaces, we want the screen to auto-rotate based on the current orientation
         unlockScreenOrientation(true);
 
+        // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
+        // on the device, then we always show the first run cling experience (or if there is no
+        // launcher2). Otherwise, we prompt the user upon started for migration
         showFirstRunActivity();
-        if (mModel.canMigrateFromOldLauncherDb()) {
-            mLauncherClings.showMigrationCling();
+        if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
+            if (mModel.canMigrateFromOldLauncherDb(this)) {
+                mLauncherClings.showMigrationCling();
+            } else {
+                mLauncherClings.showFirstRunCling();
+            }
         } else {
-            mLauncherClings.showFirstRunCling();
+            mLauncherClings.removeFirstRunAndMigrationClings();
         }
     }
 
@@ -4280,6 +4288,21 @@
         mLauncherClings.updateSearchBarHint(hint);
     }
 
+    protected boolean isLauncherPreinstalled() {
+        PackageManager pm = getPackageManager();
+        try {
+            ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
+            if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
+                return true;
+            } else {
+                return false;
+            }
+        } catch (NameNotFoundException e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+
     protected String getFirstRunClingSearchBarHint() {
         return "";
     }
diff --git a/src/com/android/launcher3/LauncherClings.java b/src/com/android/launcher3/LauncherClings.java
index 85937aa..42a134f 100644
--- a/src/com/android/launcher3/LauncherClings.java
+++ b/src/com/android/launcher3/LauncherClings.java
@@ -200,14 +200,27 @@
         }
     }
 
-    /** Shows the first run cling */
-    public void showFirstRunCling() {
+    public boolean shouldShowFirstRunOrMigrationClings() {
         SharedPreferences sharedPrefs = mLauncher.getSharedPrefs();
-        if (isClingsEnabled() &&
-                !sharedPrefs.getBoolean(FIRST_RUN_CLING_DISMISSED_KEY, false) &&
-                !skipCustomClingIfNoAccounts() ) {
+        return isClingsEnabled() &&
+            !sharedPrefs.getBoolean(FIRST_RUN_CLING_DISMISSED_KEY, false) &&
+            !sharedPrefs.getBoolean(MIGRATION_CLING_DISMISSED_KEY, false);
+    }
 
+    public void removeFirstRunAndMigrationClings() {
+        removeCling(R.id.first_run_cling);
+        removeCling(R.id.migration_cling);
+    }
 
+    /**
+     * Shows the first run cling.
+     *
+     * This flow is mutually exclusive with showMigrationCling, and only runs if this Launcher
+     * package was preinstalled or there is no db to migrate from.
+     */
+    public void showFirstRunCling() {
+        if (!skipCustomClingIfNoAccounts()) {
+            SharedPreferences sharedPrefs = mLauncher.getSharedPrefs();
             // If we're not using the default workspace layout, replace workspace cling
             // with a custom workspace cling (usually specified in an overlay)
             // For now, only do this on tablets
@@ -238,22 +251,22 @@
             }
             initCling(R.id.first_run_cling, 0, false, true);
         } else {
-            removeCling(R.id.first_run_cling);
+            removeFirstRunAndMigrationClings();
         }
     }
 
+    /**
+     * Shows the migration cling.
+     *
+     * This flow is mutually exclusive with showFirstRunCling, and only runs if this Launcher
+     * package was not preinstalled and there exists a db to migrate from.
+     */
     public void showMigrationCling() {
-        // Enable the clings only if they have not been dismissed before
-        if (isClingsEnabled() && !mLauncher.getSharedPrefs().getBoolean(
-                MIGRATION_CLING_DISMISSED_KEY, false)) {
-            mLauncher.hideWorkspaceSearchAndHotseat();
+        mLauncher.hideWorkspaceSearchAndHotseat();
 
-            Cling c = initCling(R.id.migration_cling, 0, false, true);
-            c.bringScrimToFront();
-            c.bringToFront();
-        } else {
-            removeCling(R.id.migration_cling);
-        }
+        Cling c = initCling(R.id.migration_cling, 0, false, true);
+        c.bringScrimToFront();
+        c.bringToFront();
     }
 
     public void showMigrationWorkspaceCling() {
@@ -300,7 +313,6 @@
         }
     }
 
-
     /** Removes the cling outright from the DragLayer */
     private void removeCling(int id) {
         final View cling = mLauncher.findViewById(id);
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index d271976..4478e9b 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -227,8 +227,8 @@
         }
     }
 
-    boolean canMigrateFromOldLauncherDb() {
-        return mOldContentProviderExists;
+    boolean canMigrateFromOldLauncherDb(Launcher launcher) {
+        return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
     }
 
     static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,