PackageHelper.java revision d970998b0d489774ad1c5b94b47d233912f00214
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 DEBUG_SD_INSTALL = 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 (DEBUG_SD_INSTALL) 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 (DEBUG_SD_INSTALL) 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, false); 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 int rc = getMountService().destroySecureContainer(cid, false); 152 if (rc != StorageResultCode.OperationSucceeded) { 153 Log.i(TAG, "Failed to destroy container " + cid); 154 return false; 155 } 156 return true; 157 } catch (RemoteException e) { 158 Log.e(TAG, "Failed to destroy container " + cid + 159 " with exception " + e); 160 } 161 return false; 162 } 163 164 public static String[] getSecureContainerList() { 165 try { 166 return getMountService().getSecureContainerList(); 167 } catch (RemoteException e) { 168 Log.e(TAG, "Failed to get secure container list with exception" + 169 e); 170 } 171 return null; 172 } 173 174 public static boolean isContainerMounted(String cid) { 175 try { 176 return getMountService().isSecureContainerMounted(cid); 177 } catch (RemoteException e) { 178 Log.e(TAG, "Failed to find out if container " + cid + " mounted"); 179 } 180 return false; 181 } 182} 183