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 | |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 19 | import android.os.storage.IMountService; |
| 20 | |
| 21 | import android.os.IBinder; |
| 22 | import android.os.RemoteException; |
| 23 | import android.os.ServiceManager; |
| 24 | import android.os.storage.StorageResultCode; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | import java.io.File; |
| 28 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Constants used internally between the PackageManager |
| 31 | * and media container service transports. |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 32 | * Some utility methods to invoke MountService api. |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 33 | */ |
| 34 | public class PackageHelper { |
| 35 | public static final int RECOMMEND_INSTALL_INTERNAL = 1; |
| 36 | public static final int RECOMMEND_INSTALL_EXTERNAL = 2; |
| 37 | public static final int RECOMMEND_FAILED_INSUFFICIENT_STORAGE = -1; |
| 38 | public static final int RECOMMEND_FAILED_INVALID_APK = -2; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 39 | public static final int RECOMMEND_FAILED_INVALID_LOCATION = -3; |
| 40 | public static final int RECOMMEND_FAILED_ALREADY_EXISTS = -4; |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 41 | public static final int RECOMMEND_MEDIA_UNAVAILABLE = -5; |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 42 | private static final boolean localLOGV = true; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 43 | private static final String TAG = "PackageHelper"; |
Suchi Amalapurapu | 089262d | 2010-03-10 14:19:21 -0800 | [diff] [blame] | 44 | // App installation location settings values |
| 45 | public static final int APP_INSTALL_AUTO = 0; |
| 46 | public static final int APP_INSTALL_INTERNAL = 1; |
| 47 | public static final int APP_INSTALL_EXTERNAL = 2; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 48 | |
| 49 | public static IMountService getMountService() { |
| 50 | IBinder service = ServiceManager.getService("mount"); |
| 51 | if (service != null) { |
| 52 | return IMountService.Stub.asInterface(service); |
| 53 | } else { |
| 54 | Log.e(TAG, "Can't get mount service"); |
| 55 | } |
| 56 | return null; |
| 57 | } |
| 58 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 59 | public static String createSdDir(int sizeMb, String cid, |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 60 | String sdEncKey, int uid) { |
| 61 | // Create mount point via MountService |
| 62 | IMountService mountService = getMountService(); |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 63 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 64 | if (localLOGV) |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 65 | Log.i(TAG, "Size of container " + sizeMb + " MB"); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 66 | |
| 67 | try { |
| 68 | int rc = mountService.createSecureContainer( |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 69 | cid, sizeMb, "fat", sdEncKey, uid); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 70 | if (rc != StorageResultCode.OperationSucceeded) { |
| 71 | Log.e(TAG, "Failed to create secure container " + cid); |
| 72 | return null; |
| 73 | } |
| 74 | String cachePath = mountService.getSecureContainerPath(cid); |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 75 | if (localLOGV) Log.i(TAG, "Created secure container " + cid + |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 76 | " at " + cachePath); |
| 77 | return cachePath; |
| 78 | } catch (RemoteException e) { |
| 79 | Log.e(TAG, "MountService running?"); |
| 80 | } |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | public static String mountSdDir(String cid, String key, int ownerUid) { |
| 85 | try { |
| 86 | int rc = getMountService().mountSecureContainer(cid, key, ownerUid); |
| 87 | if (rc != StorageResultCode.OperationSucceeded) { |
| 88 | Log.i(TAG, "Failed to mount container " + cid + " rc : " + rc); |
| 89 | return null; |
| 90 | } |
| 91 | return getMountService().getSecureContainerPath(cid); |
| 92 | } catch (RemoteException e) { |
| 93 | Log.e(TAG, "MountService running?"); |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | public static boolean unMountSdDir(String cid) { |
| 99 | try { |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 100 | int rc = getMountService().unmountSecureContainer(cid, true); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 101 | if (rc != StorageResultCode.OperationSucceeded) { |
| 102 | Log.e(TAG, "Failed to unmount " + cid + " with rc " + rc); |
| 103 | return false; |
| 104 | } |
| 105 | return true; |
| 106 | } catch (RemoteException e) { |
| 107 | Log.e(TAG, "MountService running?"); |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | public static boolean renameSdDir(String oldId, String newId) { |
| 113 | try { |
| 114 | int rc = getMountService().renameSecureContainer(oldId, newId); |
| 115 | if (rc != StorageResultCode.OperationSucceeded) { |
| 116 | Log.e(TAG, "Failed to rename " + oldId + " to " + |
| 117 | newId + "with rc " + rc); |
| 118 | return false; |
| 119 | } |
| 120 | return true; |
| 121 | } catch (RemoteException e) { |
| 122 | Log.i(TAG, "Failed ot rename " + oldId + " to " + newId + |
| 123 | " with exception : " + e); |
| 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | public static String getSdDir(String cid) { |
| 129 | try { |
| 130 | return getMountService().getSecureContainerPath(cid); |
| 131 | } catch (RemoteException e) { |
| 132 | Log.e(TAG, "Failed to get container path for " + cid + |
| 133 | " with exception " + e); |
| 134 | } |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | public static boolean finalizeSdDir(String cid) { |
| 139 | try { |
| 140 | int rc = getMountService().finalizeSecureContainer(cid); |
| 141 | if (rc != StorageResultCode.OperationSucceeded) { |
| 142 | Log.i(TAG, "Failed to finalize container " + cid); |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } catch (RemoteException e) { |
| 147 | Log.e(TAG, "Failed to finalize container " + cid + |
| 148 | " with exception " + e); |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | public static boolean destroySdDir(String cid) { |
| 154 | try { |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 155 | if (localLOGV) Log.i(TAG, "Forcibly destroying container " + cid); |
| 156 | int rc = getMountService().destroySecureContainer(cid, true); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 157 | if (rc != StorageResultCode.OperationSucceeded) { |
| 158 | Log.i(TAG, "Failed to destroy container " + cid); |
| 159 | return false; |
| 160 | } |
| 161 | return true; |
| 162 | } catch (RemoteException e) { |
| 163 | Log.e(TAG, "Failed to destroy container " + cid + |
| 164 | " with exception " + e); |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | public static String[] getSecureContainerList() { |
| 170 | try { |
| 171 | return getMountService().getSecureContainerList(); |
| 172 | } catch (RemoteException e) { |
| 173 | Log.e(TAG, "Failed to get secure container list with exception" + |
| 174 | e); |
| 175 | } |
| 176 | return null; |
| 177 | } |
| 178 | |
| 179 | public static boolean isContainerMounted(String cid) { |
| 180 | try { |
| 181 | return getMountService().isSecureContainerMounted(cid); |
| 182 | } catch (RemoteException e) { |
| 183 | Log.e(TAG, "Failed to find out if container " + cid + " mounted"); |
| 184 | } |
| 185 | return false; |
| 186 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 187 | } |