Add new manifest option for install location
Change recommendAppInstallLocation api
add code to parse new attribute.
Define flags in PackageInfo
Add new settings attributes for enabling setting and value for install location
Some tests
The policy for install location: if explicitly set in manifest as internal only we try to install the app only on internal storage. if set to preferExternal, we try to install it on sdcard if possible. If not we fall back to internal.
If the user enables setting SET_INSTALL_LOCATION(which will always
be set to false in final release builds) and sets a prefered location, we try
to honour it.
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java
index 9b9cbd5..0a18fe5 100644
--- a/core/java/android/app/ContextImpl.java
+++ b/core/java/android/app/ContextImpl.java
@@ -52,6 +52,7 @@
 import android.content.pm.ProviderInfo;
 import android.content.pm.ResolveInfo;
 import android.content.pm.ServiceInfo;
+import android.content.pm.PackageParser.Package;
 import android.content.res.AssetManager;
 import android.content.res.Resources;
 import android.content.res.XmlResourceParser;
@@ -85,6 +86,7 @@
 import android.os.Vibrator;
 import android.os.FileUtils.FileStatus;
 import android.os.storage.StorageManager;
+import android.provider.Settings;
 import android.telephony.TelephonyManager;
 import android.text.ClipboardManager;
 import android.util.AndroidRuntimeException;
@@ -2646,14 +2648,13 @@
         // SD-to-internal app size threshold: currently set to 1 MB
         private static final long INSTALL_ON_SD_THRESHOLD = (1024 * 1024);
 
-        @Override
-        public int recommendAppInstallLocation(ApplicationInfo appInfo, Uri packageURI) {
+        public int recommendAppInstallLocation(Package pkg) {
             // Initial implementation:
             // Package size = code size + cache size + data size
             // If code size > 1 MB, install on SD card.
             // Else install on internal NAND flash, unless space on NAND is less than 10%
 
-            if ((packageURI == null) || (appInfo == null)) {
+            if (pkg == null) {
                 return INSTALL_PARSE_FAILED_NOT_APK;
             }
 
@@ -2669,44 +2670,71 @@
 
             double pctNandFree = (double)availInternalFlashSize / (double)totalInternalFlashSize;
 
-            final String archiveFilePath = packageURI.getPath();
+            final String archiveFilePath = pkg.mScanPath;
             File apkFile = new File(archiveFilePath);
             long pkgLen = apkFile.length();
 
+            boolean auto = true;
+            // To make final copy
+            long reqInstallSize = pkgLen;
+            // For dex files
+            long reqInternalSize = 1 * pkgLen;
+            boolean intThresholdOk = (pctNandFree >= LOW_NAND_FLASH_TRESHOLD);
+            boolean intAvailOk = ((reqInstallSize + reqInternalSize) < availInternalFlashSize);
+            boolean fitsOnSd = (reqInstallSize < availSDSize) && intThresholdOk &&
+                    (reqInternalSize < availInternalFlashSize);
+            boolean fitsOnInt = intThresholdOk && intAvailOk;
+
             // Consider application flags preferences as well...
-            boolean installOnlyOnSD = ((appInfo.flags & PackageManager.INSTALL_ON_SDCARD) != 0);
-
-            // These are not very precise measures, but I guess it is hard to estimate sizes
-            // before installing the package.
-            // As a shortcut, I am assuming that the package fits on NAND flash if the available
-            // space is three times that of the APK size. For SD, we only worry about the APK size.
-            // Since packages are downloaded into SD, this might not even be necessary.
-            boolean fitsOnSD = (pkgLen < availSDSize) && ((2 * pkgLen) < availInternalFlashSize);
-            boolean fitsOnInternalFlash = ((pkgLen * 3) < availInternalFlashSize);
-
-            // Does not fit, recommend no installation.
-            if (!fitsOnSD && !fitsOnInternalFlash) {
-                return INSTALL_FAILED_INSUFFICIENT_STORAGE;
-            }
-
-            if (pkgLen < (INSTALL_ON_SD_THRESHOLD) && fitsOnInternalFlash && !(installOnlyOnSD)) {
-                // recommend internal NAND likely
-                if (pctNandFree < LOW_NAND_FLASH_TRESHOLD) {
-                    // Low space on NAND (<10%) - install on SD
-                    return INSTALL_ON_SDCARD;
+            boolean installOnlyOnSd = (pkg.installLocation ==
+                    PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
+            boolean installOnlyInternal = (pkg.installLocation ==
+                    PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
+            if (installOnlyInternal) {
+                // If set explicitly in manifest,
+                // let that override everything else
+                auto = false;
+            } else if (installOnlyOnSd){
+                // Check if this can be accommodated on the sdcard
+                if (fitsOnSd) {
+                    auto = false;
                 }
-                return INSTALL_ON_INTERNAL_FLASH;
             } else {
-                if (fitsOnSD) {
-                    // Recommend SD card
-                    return INSTALL_ON_SDCARD;
-                } else if (fitsOnInternalFlash && (pctNandFree >= LOW_NAND_FLASH_TRESHOLD) &&
-                        !(installOnlyOnSD)) {
-                    return INSTALL_ON_INTERNAL_FLASH;
-                } else {
-                    return INSTALL_FAILED_INSUFFICIENT_STORAGE;
+                // Check if user option is enabled
+                boolean setInstallLoc = Settings.System.getInt(mContext.getContentResolver(),
+                        Settings.System.SET_INSTALL_LOCATION, 0) != 0;
+                if (setInstallLoc) {
+                    // Pick user preference
+                    int installPreference = Settings.System.getInt(mContext.getContentResolver(),
+                            Settings.System.DEFAULT_INSTALL_LOCATION,
+                            PackageInfo.INSTALL_LOCATION_AUTO);
+                    if (installPreference == 1) {
+                        installOnlyInternal = true;
+                        auto = false;
+                    } else if (installPreference == 2) {
+                        installOnlyOnSd = true;
+                        auto = false;
+                    }
                 }
             }
+            if (!auto) {
+                if (installOnlyOnSd) {
+                    return fitsOnSd ? INSTALL_ON_SDCARD : INSTALL_FAILED_INSUFFICIENT_STORAGE;
+                } else if (installOnlyInternal){
+                    // Check on internal flash
+                    return fitsOnInt ? INSTALL_ON_INTERNAL_FLASH : INSTALL_FAILED_INSUFFICIENT_STORAGE;
+                }
+            }
+            // Try to install internally
+            if (fitsOnInt) {
+                return INSTALL_ON_INTERNAL_FLASH;
+            }
+            // Try the sdcard now.
+            if (fitsOnSd) {
+                return INSTALL_ON_SDCARD;
+            }
+            // Return error code
+            return INSTALL_FAILED_INSUFFICIENT_STORAGE;
         }
 
         private final ContextImpl mContext;
diff --git a/core/java/android/content/pm/PackageInfo.java b/core/java/android/content/pm/PackageInfo.java
index a8ce889..c003355 100644
--- a/core/java/android/content/pm/PackageInfo.java
+++ b/core/java/android/content/pm/PackageInfo.java
@@ -131,6 +131,34 @@
      * The features that this application has said it requires.
      */
     public FeatureInfo[] reqFeatures;
+
+    /**
+     * Constant corresponding to <code>auto</code> in
+     * the {@link android.R.attr#installLocation} attribute.
+     * @hide
+     */
+    public static final int INSTALL_LOCATION_AUTO = 0;
+    /**
+     * Constant corresponding to <code>internalOnly</code> in
+     * the {@link android.R.attr#installLocation} attribute.
+     * @hide
+     */
+    public static final int INSTALL_LOCATION_INTERNAL_ONLY = 1;
+    /**
+     * Constant corresponding to <code>preferExternal</code> in
+     * the {@link android.R.attr#installLocation} attribute.
+     * @hide
+     */
+    public static final int INSTALL_LOCATION_PREFER_EXTERNAL = 2;
+    /**
+     * The launch mode style requested by the activity.  From the
+     * {@link android.R.attr#installLocation} attribute, one of
+     * {@link #INSTALL_LOCATION_AUTO},
+     * {@link #INSTALL_LOCATION_INTERNAL_ONLY},
+     * {@link #INSTALL_LOCATION_PREFER_EXTERNAL}
+     * @hide
+     */
+    public int installLocation = INSTALL_LOCATION_INTERNAL_ONLY;
     
     public PackageInfo() {
     }
@@ -168,6 +196,7 @@
         dest.writeTypedArray(signatures, parcelableFlags);
         dest.writeTypedArray(configPreferences, parcelableFlags);
         dest.writeTypedArray(reqFeatures, parcelableFlags);
+        dest.writeInt(installLocation);
     }
 
     public static final Parcelable.Creator<PackageInfo> CREATOR
@@ -202,5 +231,6 @@
         signatures = source.createTypedArray(Signature.CREATOR);
         configPreferences = source.createTypedArray(ConfigurationInfo.CREATOR);
         reqFeatures = source.createTypedArray(FeatureInfo.CREATOR);
+        installLocation = source.readInt();
     }
 }
diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java
index fca8588..a61eab9 100644
--- a/core/java/android/content/pm/PackageManager.java
+++ b/core/java/android/content/pm/PackageManager.java
@@ -630,10 +630,11 @@
     
     /**
      * Determines best place to install an application: either SD or internal FLASH.
-     * Tweak the algorithm for best results.
-     * @param appInfo ApplicationInfo object of the package to install.
+     * If applications explicitly set installLocation in their manifest, that
+     * preference takes precedence. If not a recommended location is returned
+     * based on current available storage on internal flash or sdcard.
+     * @param pkgInfo PackageParser.Package of the package that is to be installed.
      * Call utility method to obtain.
-     * @param packageURI URI identifying the package's APK file.
      * @return {@link INSTALL_ON_INTERNAL_FLASH} if it is best to install package on internal
      * storage, {@link INSTALL_ON_SDCARD} if it is best to install package on SD card,
      * and {@link INSTALL_FAILED_INSUFFICIENT_STORAGE} if insufficient space to safely install
@@ -642,7 +643,7 @@
      * This recommendation does take into account the package's own flags.
      * @hide
      */
-    public abstract int recommendAppInstallLocation(ApplicationInfo appInfo, Uri packageURI);
+    public abstract int recommendAppInstallLocation(PackageParser.Package pkg);
 
     /**
      * Retrieve overall information about an application package that is
diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java
index b31df32..0a6195f 100644
--- a/core/java/android/content/pm/PackageParser.java
+++ b/core/java/android/content/pm/PackageParser.java
@@ -177,6 +177,7 @@
         pi.sharedUserId = p.mSharedUserId;
         pi.sharedUserLabel = p.mSharedUserLabel;
         pi.applicationInfo = p.applicationInfo;
+        pi.installLocation = p.installLocation;
         if ((flags&PackageManager.GET_GIDS) != 0) {
             pi.gids = gids;
         }
@@ -709,6 +710,9 @@
                     com.android.internal.R.styleable.AndroidManifest_sharedUserLabel, 0);
         }
         sa.recycle();
+        pkg.installLocation = sa.getInteger(
+                com.android.internal.R.styleable.AndroidManifest_installLocation,
+                PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
 
         // Resource boolean are -1, so 1 means we don't know the value.
         int supportsSmallScreens = 1;
@@ -2610,6 +2614,8 @@
          */
         public ArrayList<FeatureInfo> reqFeatures = null;
 
+        public int installLocation;
+
         public Package(String _name) {
             packageName = _name;
             applicationInfo.packageName = _name;
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index bacaf43..c2cdcc0 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -1473,6 +1473,21 @@
         public static final String NOTIFICATION_LIGHT_PULSE = "notification_light_pulse";
 
         /**
+         * Let user pick default install location.
+         * @hide
+         */
+        public static final String SET_INSTALL_LOCATION = "set_install_location";
+
+        /**
+         * Default install location value.
+         * 0 = auto, let system decide
+         * 1 = internal
+         * 2 = sdcard
+         * @hide
+         */
+        public static final String DEFAULT_INSTALL_LOCATION = "default_install_location";
+
+        /**
          * Settings to backup. This is here so that it's in the same place as the settings
          * keys and easy to update.
          * @hide
diff --git a/core/res/res/values/attrs_manifest.xml b/core/res/res/values/attrs_manifest.xml
index 54781e3..2da23eb 100644
--- a/core/res/res/values/attrs_manifest.xml
+++ b/core/res/res/values/attrs_manifest.xml
@@ -600,6 +600,20 @@
          Application class to avoid interference with application logic. -->
     <attr name="restoreNeedsApplication" format="boolean" />
 
+    <!-- The default install location defined by an application. -->
+    <attr name="installLocation">
+        <!-- Let the system decide ideal install location -->
+        <enum name="auto" value="0" />
+        <!-- Explicitly request to be installed on internal phone storate
+             only. -->
+        <enum name="internalOnly" value="1" />
+        <!-- Prefer to be installed on sdcard. There is no guarantee that
+             the system will honour this request. The application might end
+             up being installed on internal storage if external media
+             is unavailable or too full. -->
+        <enum name="preferExternal" value="2" />
+    </attr>
+
     <!-- The <code>manifest</code> tag is the root of an
          <code>AndroidManifest.xml</code> file,
          describing the contents of an Android package (.apk) file.  One
@@ -624,6 +638,7 @@
         <attr name="versionName" />
         <attr name="sharedUserId" />
         <attr name="sharedUserLabel" />
+        <attr name="installLocation" />
     </declare-styleable>
     
     <!-- The <code>application</code> tag describes application-level components
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index bd4a3eb..596e0b2 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -1224,9 +1224,10 @@
   <public type="attr" name="expandableListViewWhiteStyle" id="0x010102b6" />
 
 <!-- ===============================================================
-     Resources proposed for Flan.
+     Resources proposed for Froyo.
      =============================================================== -->
   <eat-comment />
   <public type="attr" name="neverEncrypt" id="0x010102b7" />
+  <public type="attr" name="installLocation" id="0x010102b8" />
     
 </resources>