Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 17 | package com.android.defcontainer; |
| 18 | |
| 19 | import com.android.internal.app.IMediaContainerService; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 20 | import com.android.internal.content.NativeLibraryHelper; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 21 | import com.android.internal.content.PackageHelper; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 22 | |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 23 | import android.app.IntentService; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 24 | import android.content.Intent; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 25 | import android.content.pm.IPackageManager; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 26 | import android.content.pm.PackageInfo; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 27 | import android.content.pm.PackageInfoLite; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 29 | import android.content.pm.PackageParser; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 30 | import android.content.res.ObbInfo; |
| 31 | import android.content.res.ObbScanner; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 32 | import android.net.Uri; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 33 | import android.os.Environment; |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 34 | import android.os.FileUtils; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 35 | import android.os.IBinder; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 36 | import android.os.ParcelFileDescriptor; |
| 37 | import android.os.Process; |
| 38 | import android.os.RemoteException; |
| 39 | import android.os.ServiceManager; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 40 | import android.os.StatFs; |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 41 | import android.provider.Settings; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 42 | import android.util.DisplayMetrics; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 43 | import android.util.Slog; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 44 | |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 45 | import java.io.BufferedInputStream; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 46 | import java.io.File; |
| 47 | import java.io.FileInputStream; |
| 48 | import java.io.FileNotFoundException; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 49 | import java.io.IOException; |
| 50 | import java.io.InputStream; |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 51 | import java.io.OutputStream; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 52 | |
Jeff Sharkey | 9cbe986a | 2012-04-22 18:56:43 -0700 | [diff] [blame] | 53 | import libcore.io.ErrnoException; |
| 54 | import libcore.io.Libcore; |
| 55 | import libcore.io.StructStatFs; |
| 56 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 57 | /* |
| 58 | * This service copies a downloaded apk to a file passed in as |
| 59 | * a ParcelFileDescriptor or to a newly created container specified |
| 60 | * by parameters. The DownloadManager gives access to this process |
| 61 | * based on its uid. This process also needs the ACCESS_DOWNLOAD_MANAGER |
| 62 | * permission to access apks downloaded via the download manager. |
| 63 | */ |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 64 | public class DefaultContainerService extends IntentService { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 65 | private static final String TAG = "DefContainer"; |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 66 | private static final boolean localLOGV = true; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 67 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 68 | private static final String LIB_DIR_NAME = "lib"; |
| 69 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 70 | private IMediaContainerService.Stub mBinder = new IMediaContainerService.Stub() { |
| 71 | /* |
| 72 | * Creates a new container and copies resource there. |
| 73 | * @param paackageURI the uri of resource to be copied. Can be either |
| 74 | * a content uri or a file uri |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 75 | * @param cid the id of the secure container that should |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 76 | * be used for creating a secure container into which the resource |
| 77 | * will be copied. |
| 78 | * @param key Refers to key used for encrypting the secure container |
| 79 | * @param resFileName Name of the target resource file(relative to newly |
| 80 | * created secure container) |
| 81 | * @return Returns the new cache path where the resource has been copied into |
| 82 | * |
| 83 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 84 | public String copyResourceToContainer(final Uri packageURI, final String cid, |
| 85 | final String key, final String resFileName, final String publicResFileName, |
| 86 | boolean isExternal, boolean isForwardLocked) { |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 87 | if (packageURI == null || cid == null) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 88 | return null; |
| 89 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 90 | |
| 91 | return copyResourceInner(packageURI, cid, key, resFileName, publicResFileName, |
| 92 | isExternal, isForwardLocked); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Copy specified resource to output stream |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 97 | * @param packageURI the uri of resource to be copied. Should be a file |
| 98 | * uri |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 99 | * @param outStream Remote file descriptor to be used for copying |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 100 | * @return returns status code according to those in {@link |
| 101 | * PackageManager} |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 102 | */ |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 103 | public int copyResource(final Uri packageURI, ParcelFileDescriptor outStream) { |
| 104 | if (packageURI == null || outStream == null) { |
| 105 | return PackageManager.INSTALL_FAILED_INVALID_URI; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 106 | } |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 107 | |
| 108 | ParcelFileDescriptor.AutoCloseOutputStream autoOut |
| 109 | = new ParcelFileDescriptor.AutoCloseOutputStream(outStream); |
| 110 | |
| 111 | try { |
| 112 | copyFile(packageURI, autoOut); |
| 113 | return PackageManager.INSTALL_SUCCEEDED; |
| 114 | } catch (FileNotFoundException e) { |
| 115 | Slog.e(TAG, "Could not copy URI " + packageURI.toString() + " FNF: " |
| 116 | + e.getMessage()); |
| 117 | return PackageManager.INSTALL_FAILED_INVALID_URI; |
| 118 | } catch (IOException e) { |
| 119 | Slog.e(TAG, "Could not copy URI " + packageURI.toString() + " IO: " |
| 120 | + e.getMessage()); |
| 121 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 122 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 123 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * Determine the recommended install location for package |
| 127 | * specified by file uri location. |
| 128 | * @param fileUri the uri of resource to be copied. Should be a |
| 129 | * file uri |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 130 | * @return Returns PackageInfoLite object containing |
| 131 | * the package info and recommended app location. |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 132 | */ |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 133 | public PackageInfoLite getMinimalPackageInfo(final Uri fileUri, int flags, long threshold) { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 134 | PackageInfoLite ret = new PackageInfoLite(); |
Suchi Amalapurapu | 3602f76 | 2010-03-03 17:29:33 -0800 | [diff] [blame] | 135 | if (fileUri == null) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 136 | Slog.i(TAG, "Invalid package uri " + fileUri); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 137 | ret.recommendedInstallLocation = PackageHelper.RECOMMEND_FAILED_INVALID_APK; |
| 138 | return ret; |
Suchi Amalapurapu | 3602f76 | 2010-03-03 17:29:33 -0800 | [diff] [blame] | 139 | } |
| 140 | String scheme = fileUri.getScheme(); |
| 141 | if (scheme != null && !scheme.equals("file")) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 142 | Slog.w(TAG, "Falling back to installing on internal storage only"); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 143 | ret.recommendedInstallLocation = PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 144 | return ret; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 145 | } |
Suchi Amalapurapu | 3602f76 | 2010-03-03 17:29:33 -0800 | [diff] [blame] | 146 | String archiveFilePath = fileUri.getPath(); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 147 | DisplayMetrics metrics = new DisplayMetrics(); |
| 148 | metrics.setToDefaults(); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 149 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 150 | PackageParser.PackageLite pkg = PackageParser.parsePackageLite(archiveFilePath, 0); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 151 | if (pkg == null) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 152 | Slog.w(TAG, "Failed to parse package"); |
| 153 | |
| 154 | final File apkFile = new File(archiveFilePath); |
| 155 | if (!apkFile.exists()) { |
| 156 | ret.recommendedInstallLocation = PackageHelper.RECOMMEND_FAILED_INVALID_URI; |
| 157 | } else { |
| 158 | ret.recommendedInstallLocation = PackageHelper.RECOMMEND_FAILED_INVALID_APK; |
| 159 | } |
| 160 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 161 | return ret; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 162 | } |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 163 | ret.packageName = pkg.packageName; |
Kenny Root | 930d3af | 2010-07-30 16:52:29 -0700 | [diff] [blame] | 164 | ret.installLocation = pkg.installLocation; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 165 | ret.verifiers = pkg.verifiers; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 166 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 167 | ret.recommendedInstallLocation = recommendAppInstallLocation(pkg.installLocation, |
| 168 | archiveFilePath, flags, threshold); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 169 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 170 | return ret; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 171 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 172 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 173 | @Override |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 174 | public boolean checkInternalFreeStorage(Uri packageUri, boolean isForwardLocked, |
| 175 | long threshold) throws RemoteException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 176 | final File apkFile = new File(packageUri.getPath()); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 177 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 178 | return isUnderInternalThreshold(apkFile, isForwardLocked, threshold); |
| 179 | } catch (IOException e) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 180 | return true; |
| 181 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 185 | public boolean checkExternalFreeStorage(Uri packageUri, boolean isForwardLocked) |
| 186 | throws RemoteException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 187 | final File apkFile = new File(packageUri.getPath()); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 188 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 189 | return isUnderExternalThreshold(apkFile, isForwardLocked); |
| 190 | } catch (IOException e) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 191 | return true; |
| 192 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 193 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 194 | |
| 195 | public ObbInfo getObbInfo(String filename) { |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 196 | try { |
| 197 | return ObbScanner.getObbInfo(filename); |
| 198 | } catch (IOException e) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 199 | Slog.d(TAG, "Couldn't get OBB info for " + filename); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 200 | return null; |
| 201 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 202 | } |
Kenny Root | aa183e2 | 2010-12-02 18:00:38 -0800 | [diff] [blame] | 203 | |
| 204 | @Override |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 205 | public long calculateDirectorySize(String path) throws RemoteException { |
| 206 | final File directory = new File(path); |
| 207 | if (directory.exists() && directory.isDirectory()) { |
| 208 | return MeasurementUtils.measureDirectory(path); |
| 209 | } else { |
| 210 | return 0L; |
| 211 | } |
Kenny Root | aa183e2 | 2010-12-02 18:00:38 -0800 | [diff] [blame] | 212 | } |
Jeff Sharkey | 9cbe986a | 2012-04-22 18:56:43 -0700 | [diff] [blame] | 213 | |
| 214 | @Override |
| 215 | public long[] getFileSystemStats(String path) { |
| 216 | try { |
| 217 | final StructStatFs stat = Libcore.os.statfs(path); |
| 218 | final long totalSize = stat.f_blocks * stat.f_bsize; |
| 219 | final long availSize = stat.f_bavail * stat.f_bsize; |
| 220 | return new long[] { totalSize, availSize }; |
| 221 | } catch (ErrnoException e) { |
| 222 | throw new IllegalStateException(e); |
| 223 | } |
| 224 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 227 | public DefaultContainerService() { |
| 228 | super("DefaultContainerService"); |
| 229 | setIntentRedelivery(true); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | protected void onHandleIntent(Intent intent) { |
| 234 | if (PackageManager.ACTION_CLEAN_EXTERNAL_STORAGE.equals(intent.getAction())) { |
| 235 | IPackageManager pm = IPackageManager.Stub.asInterface( |
| 236 | ServiceManager.getService("package")); |
| 237 | String pkg = null; |
| 238 | try { |
| 239 | while ((pkg=pm.nextPackageToClean(pkg)) != null) { |
| 240 | eraseFiles(Environment.getExternalStorageAppDataDirectory(pkg)); |
| 241 | eraseFiles(Environment.getExternalStorageAppMediaDirectory(pkg)); |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 242 | eraseFiles(Environment.getExternalStorageAppObbDirectory(pkg)); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 243 | } |
| 244 | } catch (RemoteException e) { |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void eraseFiles(File path) { |
| 250 | if (path.isDirectory()) { |
| 251 | String[] files = path.list(); |
| 252 | if (files != null) { |
| 253 | for (String file : files) { |
| 254 | eraseFiles(new File(path, file)); |
| 255 | } |
| 256 | } |
| 257 | } |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 258 | path.delete(); |
| 259 | } |
| 260 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 261 | public IBinder onBind(Intent intent) { |
| 262 | return mBinder; |
| 263 | } |
| 264 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 265 | private String copyResourceInner(Uri packageURI, String newCid, String key, String resFileName, |
| 266 | String publicResFileName, boolean isExternal, boolean isForwardLocked) { |
| 267 | |
| 268 | if (isExternal) { |
| 269 | // Make sure the sdcard is mounted. |
| 270 | String status = Environment.getExternalStorageState(); |
| 271 | if (!status.equals(Environment.MEDIA_MOUNTED)) { |
| 272 | Slog.w(TAG, "Make sure sdcard is mounted."); |
| 273 | return null; |
| 274 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 275 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 276 | |
| 277 | // The .apk file |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 278 | String codePath = packageURI.getPath(); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 279 | File codeFile = new File(codePath); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 280 | |
| 281 | // Calculate size of container needed to hold base APK. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 282 | final int sizeMb; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 283 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 284 | sizeMb = calculateContainerSize(codeFile, isForwardLocked); |
| 285 | } catch (IOException e) { |
| 286 | Slog.w(TAG, "Problem when trying to copy " + codeFile.getPath()); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 287 | return null; |
| 288 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 289 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 290 | // Create new container |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 291 | final String newCachePath = PackageHelper.createSdDir(sizeMb, newCid, key, Process.myUid(), |
| 292 | isExternal); |
| 293 | if (newCachePath == null) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 294 | Slog.e(TAG, "Failed to create container " + newCid); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 295 | return null; |
| 296 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 297 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 298 | if (localLOGV) { |
| 299 | Slog.i(TAG, "Created container for " + newCid + " at path : " + newCachePath); |
| 300 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 301 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 302 | final File resFile = new File(newCachePath, resFileName); |
| 303 | if (FileUtils.copyFile(new File(codePath), resFile)) { |
| 304 | if (localLOGV) { |
| 305 | Slog.i(TAG, "Copied " + codePath + " to " + resFile); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 306 | } |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 307 | } else { |
| 308 | Slog.e(TAG, "Failed to copy " + codePath + " to " + resFile); |
| 309 | // Clean up container |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 310 | PackageHelper.destroySdDir(newCid); |
| 311 | return null; |
| 312 | } |
| 313 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 314 | try { |
| 315 | Libcore.os.chmod(resFile.getAbsolutePath(), 0640); |
| 316 | } catch (ErrnoException e) { |
| 317 | Slog.e(TAG, "Could not chown APK: " + e.getMessage()); |
| 318 | PackageHelper.destroySdDir(newCid); |
| 319 | return null; |
| 320 | } |
| 321 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 322 | if (isForwardLocked) { |
| 323 | File publicZipFile = new File(newCachePath, publicResFileName); |
| 324 | try { |
| 325 | PackageHelper.extractPublicFiles(resFile.getAbsolutePath(), publicZipFile); |
| 326 | if (localLOGV) { |
| 327 | Slog.i(TAG, "Copied resources to " + publicZipFile); |
| 328 | } |
| 329 | } catch (IOException e) { |
| 330 | Slog.e(TAG, "Could not chown public APK " + publicZipFile.getAbsolutePath() + ": " |
| 331 | + e.getMessage()); |
| 332 | PackageHelper.destroySdDir(newCid); |
| 333 | return null; |
| 334 | } |
| 335 | |
| 336 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 337 | Libcore.os.chmod(publicZipFile.getAbsolutePath(), 0644); |
| 338 | } catch (ErrnoException e) { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 339 | Slog.e(TAG, "Could not chown public resource file: " + e.getMessage()); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 340 | PackageHelper.destroySdDir(newCid); |
| 341 | return null; |
| 342 | } |
| 343 | } |
| 344 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 345 | final File sharedLibraryDir = new File(newCachePath, LIB_DIR_NAME); |
| 346 | if (sharedLibraryDir.mkdir()) { |
| 347 | int ret = NativeLibraryHelper.copyNativeBinariesIfNeededLI(codeFile, sharedLibraryDir); |
| 348 | if (ret != PackageManager.INSTALL_SUCCEEDED) { |
| 349 | Slog.e(TAG, "Could not copy native libraries to " + sharedLibraryDir.getPath()); |
| 350 | PackageHelper.destroySdDir(newCid); |
| 351 | return null; |
| 352 | } |
| 353 | } else { |
| 354 | Slog.e(TAG, "Could not create native lib directory: " + sharedLibraryDir.getPath()); |
| 355 | PackageHelper.destroySdDir(newCid); |
| 356 | return null; |
| 357 | } |
| 358 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 359 | if (!PackageHelper.finalizeSdDir(newCid)) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 360 | Slog.e(TAG, "Failed to finalize " + newCid + " at path " + newCachePath); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 361 | // Clean up container |
| 362 | PackageHelper.destroySdDir(newCid); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 363 | return null; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 364 | } |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 365 | |
| 366 | if (localLOGV) { |
| 367 | Slog.i(TAG, "Finalized container " + newCid); |
| 368 | } |
| 369 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 370 | if (PackageHelper.isContainerMounted(newCid)) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 371 | if (localLOGV) { |
| 372 | Slog.i(TAG, "Unmounting " + newCid + " at path " + newCachePath); |
| 373 | } |
| 374 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 375 | // Force a gc to avoid being killed. |
| 376 | Runtime.getRuntime().gc(); |
| 377 | PackageHelper.unMountSdDir(newCid); |
| 378 | } else { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 379 | if (localLOGV) { |
| 380 | Slog.i(TAG, "Container " + newCid + " not mounted"); |
| 381 | } |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 382 | } |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 383 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 384 | return newCachePath; |
| 385 | } |
| 386 | |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 387 | private static void copyToFile(InputStream inputStream, OutputStream out) throws IOException { |
| 388 | byte[] buffer = new byte[16384]; |
| 389 | int bytesRead; |
| 390 | while ((bytesRead = inputStream.read(buffer)) >= 0) { |
| 391 | out.write(buffer, 0, bytesRead); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 395 | private static void copyToFile(File srcFile, OutputStream out) |
| 396 | throws FileNotFoundException, IOException { |
| 397 | InputStream inputStream = new BufferedInputStream(new FileInputStream(srcFile)); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 398 | try { |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 399 | copyToFile(inputStream, out); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 400 | } finally { |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 401 | try { inputStream.close(); } catch (IOException e) {} |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 405 | private void copyFile(Uri pPackageURI, OutputStream outStream) throws FileNotFoundException, |
| 406 | IOException { |
Suchi Amalapurapu | 3602f76 | 2010-03-03 17:29:33 -0800 | [diff] [blame] | 407 | String scheme = pPackageURI.getScheme(); |
| 408 | if (scheme == null || scheme.equals("file")) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 409 | final File srcPackageFile = new File(pPackageURI.getPath()); |
| 410 | // We copy the source package file to a temp file and then rename it to the |
| 411 | // destination file in order to eliminate a window where the package directory |
| 412 | // scanner notices the new package file but it's not completely copied yet. |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 413 | copyToFile(srcPackageFile, outStream); |
Suchi Amalapurapu | 3602f76 | 2010-03-03 17:29:33 -0800 | [diff] [blame] | 414 | } else if (scheme.equals("content")) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 415 | ParcelFileDescriptor fd = null; |
| 416 | try { |
| 417 | fd = getContentResolver().openFileDescriptor(pPackageURI, "r"); |
| 418 | } catch (FileNotFoundException e) { |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 419 | Slog.e(TAG, "Couldn't open file descriptor from download service. " |
| 420 | + "Failed with exception " + e); |
| 421 | throw e; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 422 | } |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 423 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 424 | if (fd == null) { |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 425 | Slog.e(TAG, "Provider returned no file descriptor for " + pPackageURI.toString()); |
| 426 | throw new FileNotFoundException("provider returned no file descriptor"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 427 | } else { |
| 428 | if (localLOGV) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 429 | Slog.i(TAG, "Opened file descriptor from download service."); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 430 | } |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 431 | ParcelFileDescriptor.AutoCloseInputStream dlStream |
| 432 | = new ParcelFileDescriptor.AutoCloseInputStream(fd); |
| 433 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 434 | // We copy the source package file to a temp file and then rename it to the |
| 435 | // destination file in order to eliminate a window where the package directory |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 436 | // scanner notices the new package file but it's not completely |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 437 | // copied |
| 438 | copyToFile(dlStream, outStream); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 439 | } |
| 440 | } else { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 441 | Slog.e(TAG, "Package URI is not 'file:' or 'content:' - " + pPackageURI); |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 442 | throw new FileNotFoundException("Package URI is not 'file:' or 'content:'"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 443 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 444 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 445 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 446 | private static final int PREFER_INTERNAL = 1; |
| 447 | private static final int PREFER_EXTERNAL = 2; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 448 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 449 | private int recommendAppInstallLocation(int installLocation, String archiveFilePath, int flags, |
| 450 | long threshold) { |
| 451 | int prefer; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 452 | boolean checkBoth = false; |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 453 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 454 | final boolean isForwardLocked = (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 455 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 456 | check_inner : { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 457 | /* |
| 458 | * Explicit install flags should override the manifest settings. |
| 459 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 460 | if ((flags & PackageManager.INSTALL_INTERNAL) != 0) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 461 | prefer = PREFER_INTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 462 | break check_inner; |
| 463 | } else if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 464 | prefer = PREFER_EXTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 465 | break check_inner; |
| 466 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 467 | |
| 468 | /* No install flags. Check for manifest option. */ |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 469 | if (installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 470 | prefer = PREFER_INTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 471 | break check_inner; |
| 472 | } else if (installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 473 | prefer = PREFER_EXTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 474 | checkBoth = true; |
| 475 | break check_inner; |
| 476 | } else if (installLocation == PackageInfo.INSTALL_LOCATION_AUTO) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 477 | // We default to preferring internal storage. |
| 478 | prefer = PREFER_INTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 479 | checkBoth = true; |
| 480 | break check_inner; |
| 481 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 482 | |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 483 | // Pick user preference |
| 484 | int installPreference = Settings.System.getInt(getApplicationContext() |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 485 | .getContentResolver(), |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 486 | Settings.Secure.DEFAULT_INSTALL_LOCATION, |
| 487 | PackageHelper.APP_INSTALL_AUTO); |
| 488 | if (installPreference == PackageHelper.APP_INSTALL_INTERNAL) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 489 | prefer = PREFER_INTERNAL; |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 490 | break check_inner; |
| 491 | } else if (installPreference == PackageHelper.APP_INSTALL_EXTERNAL) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 492 | prefer = PREFER_EXTERNAL; |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 493 | break check_inner; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 494 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 495 | |
| 496 | /* |
| 497 | * Fall back to default policy of internal-only if nothing else is |
| 498 | * specified. |
| 499 | */ |
| 500 | prefer = PREFER_INTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 503 | final boolean emulated = Environment.isExternalStorageEmulated(); |
| 504 | |
| 505 | final File apkFile = new File(archiveFilePath); |
| 506 | |
| 507 | boolean fitsOnInternal = false; |
| 508 | if (checkBoth || prefer == PREFER_INTERNAL) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 509 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 510 | fitsOnInternal = isUnderInternalThreshold(apkFile, isForwardLocked, threshold); |
| 511 | } catch (IOException e) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 512 | return PackageHelper.RECOMMEND_FAILED_INVALID_URI; |
| 513 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 516 | boolean fitsOnSd = false; |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 517 | if (!emulated && (checkBoth || prefer == PREFER_EXTERNAL)) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 518 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 519 | fitsOnSd = isUnderExternalThreshold(apkFile, isForwardLocked); |
| 520 | } catch (IOException e) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 521 | return PackageHelper.RECOMMEND_FAILED_INVALID_URI; |
| 522 | } |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 523 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 524 | |
| 525 | if (prefer == PREFER_INTERNAL) { |
| 526 | if (fitsOnInternal) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 527 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 528 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 529 | } else if (!emulated && prefer == PREFER_EXTERNAL) { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 530 | if (fitsOnSd) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 531 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 532 | } |
| 533 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 534 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 535 | if (checkBoth) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 536 | if (fitsOnInternal) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 537 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 538 | } else if (!emulated && fitsOnSd) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 539 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 540 | } |
| 541 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 542 | |
| 543 | /* |
| 544 | * If they requested to be on the external media by default, return that |
| 545 | * the media was unavailable. Otherwise, indicate there was insufficient |
| 546 | * storage space available. |
| 547 | */ |
| 548 | if (!emulated && (checkBoth || prefer == PREFER_EXTERNAL) |
| 549 | && !Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 550 | return PackageHelper.RECOMMEND_MEDIA_UNAVAILABLE; |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 551 | } else { |
| 552 | return PackageHelper.RECOMMEND_FAILED_INSUFFICIENT_STORAGE; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 553 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 554 | } |
| 555 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 556 | /** |
| 557 | * Measure a file to see if it fits within the free space threshold. |
| 558 | * |
| 559 | * @param apkFile file to check |
| 560 | * @param threshold byte threshold to compare against |
| 561 | * @return true if file fits under threshold |
| 562 | * @throws FileNotFoundException when APK does not exist |
| 563 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 564 | private boolean isUnderInternalThreshold(File apkFile, boolean isForwardLocked, long threshold) |
| 565 | throws IOException { |
| 566 | long size = apkFile.length(); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 567 | if (size == 0 && !apkFile.exists()) { |
| 568 | throw new FileNotFoundException(); |
| 569 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 570 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 571 | if (isForwardLocked) { |
| 572 | size += PackageHelper.extractPublicFiles(apkFile.getAbsolutePath(), null); |
| 573 | } |
| 574 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 575 | final StatFs internalStats = new StatFs(Environment.getDataDirectory().getPath()); |
| 576 | final long availInternalSize = (long) internalStats.getAvailableBlocks() |
| 577 | * (long) internalStats.getBlockSize(); |
| 578 | |
| 579 | return (availInternalSize - size) > threshold; |
| 580 | } |
| 581 | |
| 582 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 583 | /** |
| 584 | * Measure a file to see if it fits in the external free space. |
| 585 | * |
| 586 | * @param apkFile file to check |
| 587 | * @return true if file fits |
| 588 | * @throws IOException when file does not exist |
| 589 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 590 | private boolean isUnderExternalThreshold(File apkFile, boolean isForwardLocked) |
| 591 | throws IOException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 592 | if (Environment.isExternalStorageEmulated()) { |
| 593 | return false; |
| 594 | } |
| 595 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 596 | final int sizeMb = calculateContainerSize(apkFile, isForwardLocked); |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 597 | |
| 598 | final int availSdMb; |
| 599 | if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { |
Kenny Root | 61942c5 | 2011-08-15 12:46:04 -0700 | [diff] [blame] | 600 | final StatFs sdStats = new StatFs(Environment.getExternalStorageDirectory().getPath()); |
| 601 | final int blocksToMb = (1 << 20) / sdStats.getBlockSize(); |
| 602 | availSdMb = sdStats.getAvailableBlocks() * blocksToMb; |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 603 | } else { |
| 604 | availSdMb = -1; |
| 605 | } |
| 606 | |
| 607 | return availSdMb > sizeMb; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Calculate the container size for an APK. Takes into account the |
| 612 | * |
| 613 | * @param apkFile file from which to calculate size |
| 614 | * @return size in megabytes (2^20 bytes) |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 615 | * @throws IOException when there is a problem reading the file |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 616 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 617 | private int calculateContainerSize(File apkFile, boolean forwardLocked) throws IOException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 618 | // Calculate size of container needed to hold base APK. |
| 619 | long sizeBytes = apkFile.length(); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 620 | if (sizeBytes == 0 && !apkFile.exists()) { |
| 621 | throw new FileNotFoundException(); |
| 622 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 623 | |
| 624 | // Check all the native files that need to be copied and add that to the |
| 625 | // container size. |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 626 | sizeBytes += NativeLibraryHelper.sumNativeBinariesLI(apkFile); |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 627 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 628 | if (forwardLocked) { |
| 629 | sizeBytes += PackageHelper.extractPublicFiles(apkFile.getPath(), null); |
| 630 | } |
| 631 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 632 | int sizeMb = (int) (sizeBytes >> 20); |
| 633 | if ((sizeBytes - (sizeMb * 1024 * 1024)) > 0) { |
| 634 | sizeMb++; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Add buffer size because we don't have a good way to determine the |
| 639 | * real FAT size. Your FAT size varies with how many directory entries |
| 640 | * you need, how big the whole filesystem is, and other such headaches. |
| 641 | */ |
| 642 | sizeMb++; |
| 643 | |
| 644 | return sizeMb; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 645 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 646 | } |