blob: b57046c39b2c53be2db9e8976f2c40e5768ea29a [file] [log] [blame]
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001/*
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
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080019import 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
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -080029/**
30 * Constants used internally between the PackageManager
31 * and media container service transports.
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080032 * Some utility methods to invoke MountService api.
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -080033 */
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;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -080039 public static final int RECOMMEND_FAILED_INVALID_LOCATION = -3;
40 public static final int RECOMMEND_FAILED_ALREADY_EXISTS = -4;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080041 public static final int RECOMMEND_MEDIA_UNAVAILABLE = -5;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -080042 private static final boolean localLOGV = true;
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080043 private static final String TAG = "PackageHelper";
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080044 // 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 Amalapurapu679bba32010-02-16 11:52:44 -080048
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 Root62e1b4e2011-03-14 17:13:39 -070059 public static String createSdDir(int sizeMb, String cid,
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080060 String sdEncKey, int uid) {
61 // Create mount point via MountService
62 IMountService mountService = getMountService();
Kenny Root62e1b4e2011-03-14 17:13:39 -070063
Kenny Root85387d72010-08-26 10:13:11 -070064 if (localLOGV)
Kenny Root62e1b4e2011-03-14 17:13:39 -070065 Log.i(TAG, "Size of container " + sizeMb + " MB");
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080066
67 try {
68 int rc = mountService.createSecureContainer(
Kenny Root85387d72010-08-26 10:13:11 -070069 cid, sizeMb, "fat", sdEncKey, uid);
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080070 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 Amalapurapucf6eaea2010-02-23 19:37:45 -080075 if (localLOGV) Log.i(TAG, "Created secure container " + cid +
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080076 " 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 Amalapurapucf6eaea2010-02-23 19:37:45 -0800100 int rc = getMountService().unmountSecureContainer(cid, true);
Suchi Amalapurapu679bba32010-02-16 11:52:44 -0800101 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 Amalapurapucf6eaea2010-02-23 19:37:45 -0800155 if (localLOGV) Log.i(TAG, "Forcibly destroying container " + cid);
156 int rc = getMountService().destroySecureContainer(cid, true);
Suchi Amalapurapu679bba32010-02-16 11:52:44 -0800157 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 Amalapurapu5b993ce2010-02-12 09:43:29 -0800187}