PackageHelper.java revision cf6eaeaae9e6745dd6e07540812c79821d7043c2
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 private static final boolean localLOGV = true; 40 private static final String TAG = "PackageHelper"; 41 42 public static IMountService getMountService() { 43 IBinder service = ServiceManager.getService("mount"); 44 if (service != null) { 45 return IMountService.Stub.asInterface(service); 46 } else { 47 Log.e(TAG, "Can't get mount service"); 48 } 49 return null; 50 } 51 52 public static String createSdDir(File tmpPackageFile, String cid, 53 String sdEncKey, int uid) { 54 // Create mount point via MountService 55 IMountService mountService = getMountService(); 56 long len = tmpPackageFile.length(); 57 int mbLen = (int) (len/(1024*1024)); 58 if ((len - (mbLen * 1024 * 1024)) > 0) { 59 mbLen++; 60 } 61 if (localLOGV) Log.i(TAG, "Size of resource " + mbLen); 62 63 try { 64 int rc = mountService.createSecureContainer( 65 cid, mbLen, "vfat", sdEncKey, uid); 66 if (rc != StorageResultCode.OperationSucceeded) { 67 Log.e(TAG, "Failed to create secure container " + cid); 68 return null; 69 } 70 String cachePath = mountService.getSecureContainerPath(cid); 71 if (localLOGV) Log.i(TAG, "Created secure container " + cid + 72 " at " + cachePath); 73 return cachePath; 74 } catch (RemoteException e) { 75 Log.e(TAG, "MountService running?"); 76 } 77 return null; 78 } 79 80 public static String mountSdDir(String cid, String key, int ownerUid) { 81 try { 82 int rc = getMountService().mountSecureContainer(cid, key, ownerUid); 83 if (rc != StorageResultCode.OperationSucceeded) { 84 Log.i(TAG, "Failed to mount container " + cid + " rc : " + rc); 85 return null; 86 } 87 return getMountService().getSecureContainerPath(cid); 88 } catch (RemoteException e) { 89 Log.e(TAG, "MountService running?"); 90 } 91 return null; 92 } 93 94 public static boolean unMountSdDir(String cid) { 95 try { 96 int rc = getMountService().unmountSecureContainer(cid, true); 97 if (rc != StorageResultCode.OperationSucceeded) { 98 Log.e(TAG, "Failed to unmount " + cid + " with rc " + rc); 99 return false; 100 } 101 return true; 102 } catch (RemoteException e) { 103 Log.e(TAG, "MountService running?"); 104 } 105 return false; 106 } 107 108 public static boolean renameSdDir(String oldId, String newId) { 109 try { 110 int rc = getMountService().renameSecureContainer(oldId, newId); 111 if (rc != StorageResultCode.OperationSucceeded) { 112 Log.e(TAG, "Failed to rename " + oldId + " to " + 113 newId + "with rc " + rc); 114 return false; 115 } 116 return true; 117 } catch (RemoteException e) { 118 Log.i(TAG, "Failed ot rename " + oldId + " to " + newId + 119 " with exception : " + e); 120 } 121 return false; 122 } 123 124 public static String getSdDir(String cid) { 125 try { 126 return getMountService().getSecureContainerPath(cid); 127 } catch (RemoteException e) { 128 Log.e(TAG, "Failed to get container path for " + cid + 129 " with exception " + e); 130 } 131 return null; 132 } 133 134 public static boolean finalizeSdDir(String cid) { 135 try { 136 int rc = getMountService().finalizeSecureContainer(cid); 137 if (rc != StorageResultCode.OperationSucceeded) { 138 Log.i(TAG, "Failed to finalize container " + cid); 139 return false; 140 } 141 return true; 142 } catch (RemoteException e) { 143 Log.e(TAG, "Failed to finalize container " + cid + 144 " with exception " + e); 145 } 146 return false; 147 } 148 149 public static boolean destroySdDir(String cid) { 150 try { 151 if (localLOGV) Log.i(TAG, "Forcibly destroying container " + cid); 152 int rc = getMountService().destroySecureContainer(cid, true); 153 if (rc != StorageResultCode.OperationSucceeded) { 154 Log.i(TAG, "Failed to destroy container " + cid); 155 return false; 156 } 157 return true; 158 } catch (RemoteException e) { 159 Log.e(TAG, "Failed to destroy container " + cid + 160 " with exception " + e); 161 } 162 return false; 163 } 164 165 public static String[] getSecureContainerList() { 166 try { 167 return getMountService().getSecureContainerList(); 168 } catch (RemoteException e) { 169 Log.e(TAG, "Failed to get secure container list with exception" + 170 e); 171 } 172 return null; 173 } 174 175 public static boolean isContainerMounted(String cid) { 176 try { 177 return getMountService().isSecureContainerMounted(cid); 178 } catch (RemoteException e) { 179 Log.e(TAG, "Failed to find out if container " + cid + " mounted"); 180 } 181 return false; 182 } 183} 184