PackageHelper.java revision a2b6c3775ed6b8924232d6a01bae4a19740a15f8
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 private static final boolean localLOGV = true; 42 private static final String TAG = "PackageHelper"; 43 44 public static IMountService getMountService() { 45 IBinder service = ServiceManager.getService("mount"); 46 if (service != null) { 47 return IMountService.Stub.asInterface(service); 48 } else { 49 Log.e(TAG, "Can't get mount service"); 50 } 51 return null; 52 } 53 54 public static String createSdDir(File tmpPackageFile, String cid, 55 String sdEncKey, int uid) { 56 // Create mount point via MountService 57 IMountService mountService = getMountService(); 58 long len = tmpPackageFile.length(); 59 int mbLen = (int) (len >> 20); 60 if ((len - (mbLen * 1024 * 1024)) > 0) { 61 mbLen++; 62 } 63 // Add buffer size 64 mbLen++; 65 if (localLOGV) Log.i(TAG, "Size of container " + mbLen + " MB " + len + " bytes"); 66 67 try { 68 int rc = mountService.createSecureContainer( 69 cid, mbLen, "vfat", 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 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 { 155 if (localLOGV) Log.i(TAG, "Forcibly destroying container " + cid); 156 int rc = getMountService().destroySecureContainer(cid, true); 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 } 187} 188