PackageHelper.java revision 292f8bc9d1b790ab975a87a842c7fabc908b97e0
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 17package com.android.internal.content; 18 19import android.os.storage.IMountService; 20 21import android.os.IBinder; 22import android.os.RemoteException; 23import android.os.ServiceManager; 24import android.os.storage.StorageResultCode; 25import android.util.Log; 26 27import java.io.File; 28 29/** 30 * Constants used internally between the PackageManager 31 * and media container service transports. 32 * Some utility methods to invoke MountService api. 33 */ 34public 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; 39 public static final int RECOMMEND_FAILED_INVALID_LOCATION = -3; 40 public static final int RECOMMEND_FAILED_ALREADY_EXISTS = -4; 41 public static final int RECOMMEND_MEDIA_UNAVAILABLE = -5; 42 private static final boolean localLOGV = true; 43 private static final String TAG = "PackageHelper"; 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; 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 59 public static String createSdDir(int sizeMb, String cid, 60 String sdEncKey, int uid) { 61 // Create mount point via MountService 62 IMountService mountService = getMountService(); 63 64 if (localLOGV) 65 Log.i(TAG, "Size of container " + sizeMb + " MB"); 66 67 try { 68 int rc = mountService.createSecureContainer( 69 cid, sizeMb, "fat", sdEncKey, uid); 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); 75 if (localLOGV) Log.i(TAG, "Created secure container " + cid + 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 { 100 int rc = getMountService().unmountSecureContainer(cid, true); 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 String getSdFilesystem(String cid) { 139 try { 140 return getMountService().getSecureContainerFilesystemPath(cid); 141 } catch (RemoteException e) { 142 Log.e(TAG, "Failed to get container path for " + cid + 143 " with exception " + e); 144 } 145 return null; 146 } 147 148 public static boolean finalizeSdDir(String cid) { 149 try { 150 int rc = getMountService().finalizeSecureContainer(cid); 151 if (rc != StorageResultCode.OperationSucceeded) { 152 Log.i(TAG, "Failed to finalize container " + cid); 153 return false; 154 } 155 return true; 156 } catch (RemoteException e) { 157 Log.e(TAG, "Failed to finalize container " + cid + 158 " with exception " + e); 159 } 160 return false; 161 } 162 163 public static boolean destroySdDir(String cid) { 164 try { 165 if (localLOGV) Log.i(TAG, "Forcibly destroying container " + cid); 166 int rc = getMountService().destroySecureContainer(cid, true); 167 if (rc != StorageResultCode.OperationSucceeded) { 168 Log.i(TAG, "Failed to destroy container " + cid); 169 return false; 170 } 171 return true; 172 } catch (RemoteException e) { 173 Log.e(TAG, "Failed to destroy container " + cid + 174 " with exception " + e); 175 } 176 return false; 177 } 178 179 public static String[] getSecureContainerList() { 180 try { 181 return getMountService().getSecureContainerList(); 182 } catch (RemoteException e) { 183 Log.e(TAG, "Failed to get secure container list with exception" + 184 e); 185 } 186 return null; 187 } 188 189 public static boolean isContainerMounted(String cid) { 190 try { 191 return getMountService().isSecureContainerMounted(cid); 192 } catch (RemoteException e) { 193 Log.e(TAG, "Failed to find out if container " + cid + " mounted"); 194 } 195 return false; 196 } 197} 198