Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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.internal.content; |
| 18 | |
Jeff Sharkey | 4233f03 | 2017-07-15 12:58:38 -0600 | [diff] [blame] | 19 | import static android.net.TrafficStats.MB_IN_BYTES; |
| 20 | import static android.os.storage.VolumeInfo.ID_PRIVATE_INTERNAL; |
| 21 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 23 | import android.content.pm.ApplicationInfo; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageInfo; |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 25 | import android.content.pm.PackageInstaller.SessionParams; |
Jeff Sharkey | 4233f03 | 2017-07-15 12:58:38 -0600 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageParser.PackageLite; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 29 | import android.os.Environment; |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 30 | import android.os.FileUtils; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 31 | import android.os.IBinder; |
| 32 | import android.os.RemoteException; |
| 33 | import android.os.ServiceManager; |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 34 | import android.os.storage.IStorageManager; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 35 | import android.os.storage.StorageManager; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 36 | import android.os.storage.StorageResultCode; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 37 | import android.os.storage.StorageVolume; |
| 38 | import android.os.storage.VolumeInfo; |
Todd Kennedy | f39ca8f | 2015-08-07 14:15:07 -0700 | [diff] [blame] | 39 | import android.provider.Settings; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 40 | import android.util.ArraySet; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 41 | import android.util.Log; |
| 42 | |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 43 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 44 | |
Jeff Sharkey | 4233f03 | 2017-07-15 12:58:38 -0600 | [diff] [blame] | 45 | import libcore.io.IoUtils; |
| 46 | |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 47 | import java.io.File; |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 48 | import java.io.FileOutputStream; |
| 49 | import java.io.IOException; |
| 50 | import java.io.InputStream; |
| 51 | import java.util.Collections; |
Jeff Sharkey | 2d37092 | 2016-05-10 17:01:49 -0600 | [diff] [blame] | 52 | import java.util.Objects; |
Jeff Sharkey | 4233f03 | 2017-07-15 12:58:38 -0600 | [diff] [blame] | 53 | import java.util.UUID; |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 54 | import java.util.zip.ZipEntry; |
| 55 | import java.util.zip.ZipFile; |
| 56 | import java.util.zip.ZipOutputStream; |
| 57 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Constants used internally between the PackageManager |
| 60 | * and media container service transports. |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 61 | * Some utility methods to invoke StorageManagerService api. |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 62 | */ |
| 63 | public class PackageHelper { |
| 64 | public static final int RECOMMEND_INSTALL_INTERNAL = 1; |
| 65 | public static final int RECOMMEND_INSTALL_EXTERNAL = 2; |
Todd Kennedy | 2699f06 | 2015-11-20 13:07:17 -0800 | [diff] [blame] | 66 | public static final int RECOMMEND_INSTALL_EPHEMERAL = 3; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 67 | public static final int RECOMMEND_FAILED_INSUFFICIENT_STORAGE = -1; |
| 68 | public static final int RECOMMEND_FAILED_INVALID_APK = -2; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 69 | public static final int RECOMMEND_FAILED_INVALID_LOCATION = -3; |
| 70 | public static final int RECOMMEND_FAILED_ALREADY_EXISTS = -4; |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 71 | public static final int RECOMMEND_MEDIA_UNAVAILABLE = -5; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 72 | public static final int RECOMMEND_FAILED_INVALID_URI = -6; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 73 | public static final int RECOMMEND_FAILED_VERSION_DOWNGRADE = -7; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 74 | |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 75 | private static final boolean localLOGV = false; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 76 | private static final String TAG = "PackageHelper"; |
Suchi Amalapurapu | 089262d | 2010-03-10 14:19:21 -0800 | [diff] [blame] | 77 | // App installation location settings values |
| 78 | public static final int APP_INSTALL_AUTO = 0; |
| 79 | public static final int APP_INSTALL_INTERNAL = 1; |
| 80 | public static final int APP_INSTALL_EXTERNAL = 2; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 81 | |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 82 | private static TestableInterface sDefaultTestableInterface = null; |
| 83 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 84 | public static IStorageManager getStorageManager() throws RemoteException { |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 85 | IBinder service = ServiceManager.getService("mount"); |
| 86 | if (service != null) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 87 | return IStorageManager.Stub.asInterface(service); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 88 | } else { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 89 | Log.e(TAG, "Can't get storagemanager service"); |
| 90 | throw new RemoteException("Could not contact storagemanager service"); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 91 | } |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 94 | public static String createSdDir(long sizeBytes, String cid, String sdEncKey, int uid, |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 95 | boolean isExternal) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 96 | // Round up to nearest MB, plus another MB for filesystem overhead |
| 97 | final int sizeMb = (int) ((sizeBytes + MB_IN_BYTES) / MB_IN_BYTES) + 1; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 98 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 99 | IStorageManager storageManager = getStorageManager(); |
Kenny Root | 07ba2ae | 2012-05-09 09:14:28 -0700 | [diff] [blame] | 100 | |
| 101 | if (localLOGV) |
| 102 | Log.i(TAG, "Size of container " + sizeMb + " MB"); |
| 103 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 104 | int rc = storageManager.createSecureContainer(cid, sizeMb, "ext4", sdEncKey, uid, |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 105 | isExternal); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 106 | if (rc != StorageResultCode.OperationSucceeded) { |
| 107 | Log.e(TAG, "Failed to create secure container " + cid); |
| 108 | return null; |
| 109 | } |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 110 | String cachePath = storageManager.getSecureContainerPath(cid); |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 111 | if (localLOGV) Log.i(TAG, "Created secure container " + cid + |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 112 | " at " + cachePath); |
| 113 | return cachePath; |
| 114 | } catch (RemoteException e) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 115 | Log.e(TAG, "StorageManagerService running?"); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 116 | } |
| 117 | return null; |
| 118 | } |
| 119 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 120 | public static boolean resizeSdDir(long sizeBytes, String cid, String sdEncKey) { |
| 121 | // Round up to nearest MB, plus another MB for filesystem overhead |
| 122 | final int sizeMb = (int) ((sizeBytes + MB_IN_BYTES) / MB_IN_BYTES) + 1; |
| 123 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 124 | IStorageManager storageManager = getStorageManager(); |
| 125 | int rc = storageManager.resizeSecureContainer(cid, sizeMb, sdEncKey); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 126 | if (rc == StorageResultCode.OperationSucceeded) { |
| 127 | return true; |
| 128 | } |
| 129 | } catch (RemoteException e) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 130 | Log.e(TAG, "StorageManagerService running?"); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 131 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 132 | Log.e(TAG, "Failed to create secure container " + cid); |
| 133 | return false; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 134 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 135 | |
| 136 | public static String mountSdDir(String cid, String key, int ownerUid) { |
| 137 | return mountSdDir(cid, key, ownerUid, true); |
| 138 | } |
| 139 | |
| 140 | public static String mountSdDir(String cid, String key, int ownerUid, boolean readOnly) { |
| 141 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 142 | int rc = getStorageManager().mountSecureContainer(cid, key, ownerUid, readOnly); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 143 | if (rc != StorageResultCode.OperationSucceeded) { |
| 144 | Log.i(TAG, "Failed to mount container " + cid + " rc : " + rc); |
| 145 | return null; |
| 146 | } |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 147 | return getStorageManager().getSecureContainerPath(cid); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 148 | } catch (RemoteException e) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 149 | Log.e(TAG, "StorageManagerService running?"); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 150 | } |
| 151 | return null; |
| 152 | } |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 153 | |
| 154 | public static boolean unMountSdDir(String cid) { |
| 155 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 156 | int rc = getStorageManager().unmountSecureContainer(cid, true); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 157 | if (rc != StorageResultCode.OperationSucceeded) { |
| 158 | Log.e(TAG, "Failed to unmount " + cid + " with rc " + rc); |
| 159 | return false; |
| 160 | } |
| 161 | return true; |
| 162 | } catch (RemoteException e) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 163 | Log.e(TAG, "StorageManagerService running?"); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | public static boolean renameSdDir(String oldId, String newId) { |
| 169 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 170 | int rc = getStorageManager().renameSecureContainer(oldId, newId); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 171 | if (rc != StorageResultCode.OperationSucceeded) { |
| 172 | Log.e(TAG, "Failed to rename " + oldId + " to " + |
| 173 | newId + "with rc " + rc); |
| 174 | return false; |
| 175 | } |
| 176 | return true; |
| 177 | } catch (RemoteException e) { |
| 178 | Log.i(TAG, "Failed ot rename " + oldId + " to " + newId + |
| 179 | " with exception : " + e); |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | public static String getSdDir(String cid) { |
| 185 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 186 | return getStorageManager().getSecureContainerPath(cid); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 187 | } catch (RemoteException e) { |
| 188 | Log.e(TAG, "Failed to get container path for " + cid + |
| 189 | " with exception " + e); |
| 190 | } |
| 191 | return null; |
| 192 | } |
| 193 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 194 | public static String getSdFilesystem(String cid) { |
| 195 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 196 | return getStorageManager().getSecureContainerFilesystemPath(cid); |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 197 | } catch (RemoteException e) { |
| 198 | Log.e(TAG, "Failed to get container path for " + cid + |
| 199 | " with exception " + e); |
| 200 | } |
| 201 | return null; |
| 202 | } |
| 203 | |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 204 | public static boolean finalizeSdDir(String cid) { |
| 205 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 206 | int rc = getStorageManager().finalizeSecureContainer(cid); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 207 | if (rc != StorageResultCode.OperationSucceeded) { |
| 208 | Log.i(TAG, "Failed to finalize container " + cid); |
| 209 | return false; |
| 210 | } |
| 211 | return true; |
| 212 | } catch (RemoteException e) { |
| 213 | Log.e(TAG, "Failed to finalize container " + cid + |
| 214 | " with exception " + e); |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | public static boolean destroySdDir(String cid) { |
| 220 | try { |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 221 | if (localLOGV) Log.i(TAG, "Forcibly destroying container " + cid); |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 222 | int rc = getStorageManager().destroySecureContainer(cid, true); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 223 | if (rc != StorageResultCode.OperationSucceeded) { |
| 224 | Log.i(TAG, "Failed to destroy container " + cid); |
| 225 | return false; |
| 226 | } |
| 227 | return true; |
| 228 | } catch (RemoteException e) { |
| 229 | Log.e(TAG, "Failed to destroy container " + cid + |
| 230 | " with exception " + e); |
| 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | public static String[] getSecureContainerList() { |
| 236 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 237 | return getStorageManager().getSecureContainerList(); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 238 | } catch (RemoteException e) { |
| 239 | Log.e(TAG, "Failed to get secure container list with exception" + |
| 240 | e); |
| 241 | } |
| 242 | return null; |
| 243 | } |
| 244 | |
| 245 | public static boolean isContainerMounted(String cid) { |
| 246 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 247 | return getStorageManager().isSecureContainerMounted(cid); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 248 | } catch (RemoteException e) { |
| 249 | Log.e(TAG, "Failed to find out if container " + cid + " mounted"); |
| 250 | } |
| 251 | return false; |
| 252 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 253 | |
Jeff Sharkey | be520fb | 2014-07-04 18:23:17 -0700 | [diff] [blame] | 254 | /** |
| 255 | * Extract public files for the single given APK. |
| 256 | */ |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 257 | public static long extractPublicFiles(File apkFile, File publicZipFile) |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 258 | throws IOException { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 259 | final FileOutputStream fstr; |
| 260 | final ZipOutputStream publicZipOutStream; |
| 261 | |
| 262 | if (publicZipFile == null) { |
| 263 | fstr = null; |
| 264 | publicZipOutStream = null; |
| 265 | } else { |
| 266 | fstr = new FileOutputStream(publicZipFile); |
| 267 | publicZipOutStream = new ZipOutputStream(fstr); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 268 | Log.d(TAG, "Extracting " + apkFile + " to " + publicZipFile); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 271 | long size = 0L; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 272 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 273 | try { |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 274 | final ZipFile privateZip = new ZipFile(apkFile.getAbsolutePath()); |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 275 | try { |
| 276 | // Copy manifest, resources.arsc and res directory to public zip |
| 277 | for (final ZipEntry zipEntry : Collections.list(privateZip.entries())) { |
| 278 | final String zipEntryName = zipEntry.getName(); |
| 279 | if ("AndroidManifest.xml".equals(zipEntryName) |
| 280 | || "resources.arsc".equals(zipEntryName) |
| 281 | || zipEntryName.startsWith("res/")) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 282 | size += zipEntry.getSize(); |
| 283 | if (publicZipFile != null) { |
| 284 | copyZipEntry(zipEntry, privateZip, publicZipOutStream); |
| 285 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } finally { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 289 | try { privateZip.close(); } catch (IOException e) {} |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 292 | if (publicZipFile != null) { |
| 293 | publicZipOutStream.finish(); |
| 294 | publicZipOutStream.flush(); |
| 295 | FileUtils.sync(fstr); |
| 296 | publicZipOutStream.close(); |
| 297 | FileUtils.setPermissions(publicZipFile.getAbsolutePath(), FileUtils.S_IRUSR |
| 298 | | FileUtils.S_IWUSR | FileUtils.S_IRGRP | FileUtils.S_IROTH, -1, -1); |
| 299 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 300 | } finally { |
| 301 | IoUtils.closeQuietly(publicZipOutStream); |
| 302 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 303 | |
| 304 | return size; |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | private static void copyZipEntry(ZipEntry zipEntry, ZipFile inZipFile, |
| 308 | ZipOutputStream outZipStream) throws IOException { |
| 309 | byte[] buffer = new byte[4096]; |
| 310 | int num; |
| 311 | |
| 312 | ZipEntry newEntry; |
| 313 | if (zipEntry.getMethod() == ZipEntry.STORED) { |
| 314 | // Preserve the STORED method of the input entry. |
| 315 | newEntry = new ZipEntry(zipEntry); |
| 316 | } else { |
| 317 | // Create a new entry so that the compressed len is recomputed. |
| 318 | newEntry = new ZipEntry(zipEntry.getName()); |
| 319 | } |
| 320 | outZipStream.putNextEntry(newEntry); |
| 321 | |
| 322 | final InputStream data = inZipFile.getInputStream(zipEntry); |
| 323 | try { |
| 324 | while ((num = data.read(buffer)) > 0) { |
| 325 | outZipStream.write(buffer, 0, num); |
| 326 | } |
| 327 | outZipStream.flush(); |
| 328 | } finally { |
| 329 | IoUtils.closeQuietly(data); |
| 330 | } |
| 331 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 332 | |
| 333 | public static boolean fixSdPermissions(String cid, int gid, String filename) { |
| 334 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 335 | int rc = getStorageManager().fixPermissionsSecureContainer(cid, gid, filename); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 336 | if (rc != StorageResultCode.OperationSucceeded) { |
| 337 | Log.i(TAG, "Failed to fixperms container " + cid); |
| 338 | return false; |
| 339 | } |
| 340 | return true; |
| 341 | } catch (RemoteException e) { |
| 342 | Log.e(TAG, "Failed to fixperms container " + cid + " with exception " + e); |
| 343 | } |
| 344 | return false; |
| 345 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 346 | |
| 347 | /** |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 348 | * A group of external dependencies used in |
| 349 | * {@link #resolveInstallVolume(Context, String, int, long)}. It can be backed by real values |
| 350 | * from the system or mocked ones for testing purposes. |
| 351 | */ |
| 352 | public static abstract class TestableInterface { |
| 353 | abstract public StorageManager getStorageManager(Context context); |
| 354 | abstract public boolean getForceAllowOnExternalSetting(Context context); |
| 355 | abstract public boolean getAllow3rdPartyOnInternalConfig(Context context); |
| 356 | abstract public ApplicationInfo getExistingAppInfo(Context context, String packageName); |
| 357 | abstract public File getDataDirectory(); |
| 358 | |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 359 | public boolean fitsOnInternalStorage(Context context, SessionParams params) |
| 360 | throws IOException { |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 361 | StorageManager storage = getStorageManager(context); |
Jeff Sharkey | 4233f03 | 2017-07-15 12:58:38 -0600 | [diff] [blame] | 362 | final UUID target = storage.getUuidForPath(getDataDirectory()); |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 363 | return (params.sizeBytes <= storage.getAllocatableBytes(target, |
| 364 | translateAllocateFlags(params.installFlags))); |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | private synchronized static TestableInterface getDefaultTestableInterface() { |
| 369 | if (sDefaultTestableInterface == null) { |
| 370 | sDefaultTestableInterface = new TestableInterface() { |
| 371 | @Override |
| 372 | public StorageManager getStorageManager(Context context) { |
| 373 | return context.getSystemService(StorageManager.class); |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public boolean getForceAllowOnExternalSetting(Context context) { |
| 378 | return Settings.Global.getInt(context.getContentResolver(), |
| 379 | Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0; |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public boolean getAllow3rdPartyOnInternalConfig(Context context) { |
| 384 | return context.getResources().getBoolean( |
| 385 | com.android.internal.R.bool.config_allow3rdPartyAppOnInternal); |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public ApplicationInfo getExistingAppInfo(Context context, String packageName) { |
| 390 | ApplicationInfo existingInfo = null; |
| 391 | try { |
| 392 | existingInfo = context.getPackageManager().getApplicationInfo(packageName, |
| 393 | PackageManager.MATCH_ANY_USER); |
| 394 | } catch (NameNotFoundException ignored) { |
| 395 | } |
| 396 | return existingInfo; |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public File getDataDirectory() { |
| 401 | return Environment.getDataDirectory(); |
| 402 | } |
| 403 | }; |
| 404 | } |
| 405 | return sDefaultTestableInterface; |
| 406 | } |
| 407 | |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 408 | @VisibleForTesting |
| 409 | @Deprecated |
| 410 | public static String resolveInstallVolume(Context context, String packageName, |
| 411 | int installLocation, long sizeBytes, TestableInterface testInterface) |
| 412 | throws IOException { |
| 413 | final SessionParams params = new SessionParams(SessionParams.MODE_INVALID); |
| 414 | params.appPackageName = packageName; |
| 415 | params.installLocation = installLocation; |
| 416 | params.sizeBytes = sizeBytes; |
| 417 | return resolveInstallVolume(context, params, testInterface); |
| 418 | } |
| 419 | |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 420 | /** |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 421 | * Given a requested {@link PackageInfo#installLocation} and calculated |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 422 | * install size, pick the actual volume to install the app. Only considers |
| 423 | * internal and private volumes, and prefers to keep an existing package on |
| 424 | * its current volume. |
| 425 | * |
| 426 | * @return the {@link VolumeInfo#fsUuid} to install onto, or {@code null} |
| 427 | * for internal storage. |
| 428 | */ |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 429 | public static String resolveInstallVolume(Context context, SessionParams params) |
| 430 | throws IOException { |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 431 | TestableInterface testableInterface = getDefaultTestableInterface(); |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 432 | return resolveInstallVolume(context, params.appPackageName, params.installLocation, |
| 433 | params.sizeBytes, testableInterface); |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | @VisibleForTesting |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 437 | public static String resolveInstallVolume(Context context, SessionParams params, |
| 438 | TestableInterface testInterface) throws IOException { |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 439 | final boolean forceAllowOnExternal = testInterface.getForceAllowOnExternalSetting(context); |
| 440 | final boolean allow3rdPartyOnInternal = |
| 441 | testInterface.getAllow3rdPartyOnInternalConfig(context); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 442 | // TODO: handle existing apps installed in ASEC; currently assumes |
| 443 | // they'll end up back on internal storage |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 444 | ApplicationInfo existingInfo = testInterface.getExistingAppInfo(context, |
| 445 | params.appPackageName); |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 446 | |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 447 | final boolean fitsOnInternal = testInterface.fitsOnInternalStorage(context, params); |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 448 | final StorageManager storageManager = |
| 449 | testInterface.getStorageManager(context); |
| 450 | |
| 451 | // System apps always forced to internal storage |
| 452 | if (existingInfo != null && existingInfo.isSystemApp()) { |
| 453 | if (fitsOnInternal) { |
| 454 | return StorageManager.UUID_PRIVATE_INTERNAL; |
| 455 | } else { |
| 456 | throw new IOException("Not enough space on existing volume " |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 457 | + existingInfo.volumeUuid + " for system app " + params.appPackageName |
| 458 | + " upgrade"); |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 459 | } |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 462 | // Now deal with non-system apps. |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 463 | final ArraySet<String> allCandidates = new ArraySet<>(); |
| 464 | VolumeInfo bestCandidate = null; |
| 465 | long bestCandidateAvailBytes = Long.MIN_VALUE; |
| 466 | for (VolumeInfo vol : storageManager.getVolumes()) { |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 467 | boolean isInternalStorage = ID_PRIVATE_INTERNAL.equals(vol.id); |
| 468 | if (vol.type == VolumeInfo.TYPE_PRIVATE && vol.isMountedWritable() |
| 469 | && (!isInternalStorage || allow3rdPartyOnInternal)) { |
Jeff Sharkey | 4233f03 | 2017-07-15 12:58:38 -0600 | [diff] [blame] | 470 | final UUID target = storageManager.getUuidForPath(new File(vol.path)); |
| 471 | final long availBytes = storageManager.getAllocatableBytes(target, |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 472 | translateAllocateFlags(params.installFlags)); |
| 473 | if (availBytes >= params.sizeBytes) { |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 474 | allCandidates.add(vol.fsUuid); |
| 475 | } |
| 476 | if (availBytes >= bestCandidateAvailBytes) { |
| 477 | bestCandidate = vol; |
| 478 | bestCandidateAvailBytes = availBytes; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
Jeff Sharkey | 2d37092 | 2016-05-10 17:01:49 -0600 | [diff] [blame] | 483 | // If app expresses strong desire for internal storage, honor it |
Todd Kennedy | f39ca8f | 2015-08-07 14:15:07 -0700 | [diff] [blame] | 484 | if (!forceAllowOnExternal |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 485 | && params.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { |
Jeff Sharkey | 2d37092 | 2016-05-10 17:01:49 -0600 | [diff] [blame] | 486 | if (existingInfo != null && !Objects.equals(existingInfo.volumeUuid, |
| 487 | StorageManager.UUID_PRIVATE_INTERNAL)) { |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 488 | throw new IOException("Cannot automatically move " + params.appPackageName |
| 489 | + " from " + existingInfo.volumeUuid + " to internal storage"); |
Jeff Sharkey | 2d37092 | 2016-05-10 17:01:49 -0600 | [diff] [blame] | 490 | } |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 491 | |
| 492 | if (!allow3rdPartyOnInternal) { |
| 493 | throw new IOException("Not allowed to install non-system apps on internal storage"); |
| 494 | } |
| 495 | |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 496 | if (fitsOnInternal) { |
Jeff Sharkey | 2d37092 | 2016-05-10 17:01:49 -0600 | [diff] [blame] | 497 | return StorageManager.UUID_PRIVATE_INTERNAL; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 498 | } else { |
| 499 | throw new IOException("Requested internal only, but not enough space"); |
| 500 | } |
| 501 | } |
| 502 | |
Jeff Sharkey | 2d37092 | 2016-05-10 17:01:49 -0600 | [diff] [blame] | 503 | // If app already exists somewhere, we must stay on that volume |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 504 | if (existingInfo != null) { |
Jeff Sharkey | 2d37092 | 2016-05-10 17:01:49 -0600 | [diff] [blame] | 505 | if (Objects.equals(existingInfo.volumeUuid, StorageManager.UUID_PRIVATE_INTERNAL) |
| 506 | && fitsOnInternal) { |
| 507 | return StorageManager.UUID_PRIVATE_INTERNAL; |
| 508 | } else if (allCandidates.contains(existingInfo.volumeUuid)) { |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 509 | return existingInfo.volumeUuid; |
Jeff Sharkey | 2d37092 | 2016-05-10 17:01:49 -0600 | [diff] [blame] | 510 | } else { |
| 511 | throw new IOException("Not enough space on existing volume " |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 512 | + existingInfo.volumeUuid + " for " + params.appPackageName + " upgrade"); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 516 | // We're left with new installations with either preferring external or auto, so just pick |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 517 | // volume with most space |
| 518 | if (bestCandidate != null) { |
| 519 | return bestCandidate.fsUuid; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 520 | } else { |
Yao Chen | 199b5d6 | 2016-11-16 13:45:45 -0800 | [diff] [blame] | 521 | throw new IOException("No special requests, but no room on allowed volumes. " |
| 522 | + " allow3rdPartyOnInternal? " + allow3rdPartyOnInternal); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 526 | public static boolean fitsOnInternal(Context context, SessionParams params) throws IOException { |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 527 | final StorageManager storage = context.getSystemService(StorageManager.class); |
Jeff Sharkey | 4233f03 | 2017-07-15 12:58:38 -0600 | [diff] [blame] | 528 | final UUID target = storage.getUuidForPath(Environment.getDataDirectory()); |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 529 | return (params.sizeBytes <= storage.getAllocatableBytes(target, |
| 530 | translateAllocateFlags(params.installFlags))); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 533 | public static boolean fitsOnExternal(Context context, SessionParams params) { |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 534 | final StorageManager storage = context.getSystemService(StorageManager.class); |
| 535 | final StorageVolume primary = storage.getPrimaryVolume(); |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 536 | return (params.sizeBytes > 0) && !primary.isEmulated() |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 537 | && Environment.MEDIA_MOUNTED.equals(primary.getState()) |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 538 | && params.sizeBytes <= storage.getStorageBytesUntilLow(primary.getPathFile()); |
| 539 | } |
| 540 | |
| 541 | @Deprecated |
| 542 | public static int resolveInstallLocation(Context context, String packageName, |
| 543 | int installLocation, long sizeBytes, int installFlags) { |
| 544 | final SessionParams params = new SessionParams(SessionParams.MODE_INVALID); |
| 545 | params.appPackageName = packageName; |
| 546 | params.installLocation = installLocation; |
| 547 | params.sizeBytes = sizeBytes; |
| 548 | params.installFlags = installFlags; |
| 549 | try { |
| 550 | return resolveInstallLocation(context, params); |
| 551 | } catch (IOException e) { |
| 552 | throw new IllegalStateException(e); |
| 553 | } |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Given a requested {@link PackageInfo#installLocation} and calculated |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 558 | * install size, pick the actual location to install the app. |
| 559 | */ |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 560 | public static int resolveInstallLocation(Context context, SessionParams params) |
| 561 | throws IOException { |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 562 | ApplicationInfo existingInfo = null; |
| 563 | try { |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 564 | existingInfo = context.getPackageManager().getApplicationInfo(params.appPackageName, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 565 | PackageManager.MATCH_ANY_USER); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 566 | } catch (NameNotFoundException ignored) { |
| 567 | } |
| 568 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 569 | final int prefer; |
| 570 | final boolean checkBoth; |
Todd Kennedy | 2699f06 | 2015-11-20 13:07:17 -0800 | [diff] [blame] | 571 | boolean ephemeral = false; |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 572 | if ((params.installFlags & PackageManager.INSTALL_INSTANT_APP) != 0) { |
Todd Kennedy | 2699f06 | 2015-11-20 13:07:17 -0800 | [diff] [blame] | 573 | prefer = RECOMMEND_INSTALL_INTERNAL; |
| 574 | ephemeral = true; |
| 575 | checkBoth = false; |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 576 | } else if ((params.installFlags & PackageManager.INSTALL_INTERNAL) != 0) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 577 | prefer = RECOMMEND_INSTALL_INTERNAL; |
| 578 | checkBoth = false; |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 579 | } else if ((params.installFlags & PackageManager.INSTALL_EXTERNAL) != 0) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 580 | prefer = RECOMMEND_INSTALL_EXTERNAL; |
| 581 | checkBoth = false; |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 582 | } else if (params.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 583 | prefer = RECOMMEND_INSTALL_INTERNAL; |
| 584 | checkBoth = false; |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 585 | } else if (params.installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 586 | prefer = RECOMMEND_INSTALL_EXTERNAL; |
| 587 | checkBoth = true; |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 588 | } else if (params.installLocation == PackageInfo.INSTALL_LOCATION_AUTO) { |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 589 | // When app is already installed, prefer same medium |
| 590 | if (existingInfo != null) { |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 591 | // TODO: distinguish if this is external ASEC |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 592 | if ((existingInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 593 | prefer = RECOMMEND_INSTALL_EXTERNAL; |
| 594 | } else { |
| 595 | prefer = RECOMMEND_INSTALL_INTERNAL; |
| 596 | } |
| 597 | } else { |
| 598 | prefer = RECOMMEND_INSTALL_INTERNAL; |
| 599 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 600 | checkBoth = true; |
| 601 | } else { |
| 602 | prefer = RECOMMEND_INSTALL_INTERNAL; |
| 603 | checkBoth = false; |
| 604 | } |
| 605 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 606 | boolean fitsOnInternal = false; |
| 607 | if (checkBoth || prefer == RECOMMEND_INSTALL_INTERNAL) { |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 608 | fitsOnInternal = fitsOnInternal(context, params); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | boolean fitsOnExternal = false; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 612 | if (checkBoth || prefer == RECOMMEND_INSTALL_EXTERNAL) { |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 613 | fitsOnExternal = fitsOnExternal(context, params); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | if (prefer == RECOMMEND_INSTALL_INTERNAL) { |
Todd Kennedy | 2699f06 | 2015-11-20 13:07:17 -0800 | [diff] [blame] | 617 | // The ephemeral case will either fit and return EPHEMERAL, or will not fit |
| 618 | // and will fall through to return INSUFFICIENT_STORAGE |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 619 | if (fitsOnInternal) { |
Todd Kennedy | 2699f06 | 2015-11-20 13:07:17 -0800 | [diff] [blame] | 620 | return (ephemeral) |
| 621 | ? PackageHelper.RECOMMEND_INSTALL_EPHEMERAL |
| 622 | : PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 623 | } |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 624 | } else if (prefer == RECOMMEND_INSTALL_EXTERNAL) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 625 | if (fitsOnExternal) { |
| 626 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if (checkBoth) { |
| 631 | if (fitsOnInternal) { |
| 632 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 633 | } else if (fitsOnExternal) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 634 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 635 | } |
| 636 | } |
| 637 | |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 638 | return PackageHelper.RECOMMEND_FAILED_INSUFFICIENT_STORAGE; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 639 | } |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 640 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 641 | public static long calculateInstalledSize(PackageLite pkg, boolean isForwardLocked, |
| 642 | String abiOverride) throws IOException { |
| 643 | NativeLibraryHelper.Handle handle = null; |
| 644 | try { |
| 645 | handle = NativeLibraryHelper.Handle.create(pkg); |
| 646 | return calculateInstalledSize(pkg, handle, isForwardLocked, abiOverride); |
| 647 | } finally { |
| 648 | IoUtils.closeQuietly(handle); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | public static long calculateInstalledSize(PackageLite pkg, NativeLibraryHelper.Handle handle, |
| 653 | boolean isForwardLocked, String abiOverride) throws IOException { |
| 654 | long sizeBytes = 0; |
| 655 | |
| 656 | // Include raw APKs, and possibly unpacked resources |
| 657 | for (String codePath : pkg.getAllCodePaths()) { |
| 658 | final File codeFile = new File(codePath); |
| 659 | sizeBytes += codeFile.length(); |
| 660 | |
| 661 | if (isForwardLocked) { |
| 662 | sizeBytes += PackageHelper.extractPublicFiles(codeFile, null); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // Include all relevant native code |
| 667 | sizeBytes += NativeLibraryHelper.sumNativeBinariesWithOverride(handle, abiOverride); |
| 668 | |
| 669 | return sizeBytes; |
| 670 | } |
| 671 | |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 672 | public static String replaceEnd(String str, String before, String after) { |
| 673 | if (!str.endsWith(before)) { |
| 674 | throw new IllegalArgumentException( |
| 675 | "Expected " + str + " to end with " + before); |
| 676 | } |
| 677 | return str.substring(0, str.length() - before.length()) + after; |
| 678 | } |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 679 | |
| 680 | public static int translateAllocateFlags(int installFlags) { |
| 681 | if ((installFlags & PackageManager.INSTALL_ALLOCATE_AGGRESSIVE) != 0) { |
| 682 | return StorageManager.FLAG_ALLOCATE_AGGRESSIVE; |
| 683 | } else { |
| 684 | return 0; |
| 685 | } |
| 686 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 687 | } |