• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.internal.content;
18 
19 import android.os.storage.IMountService;
20 
21 import android.os.IBinder;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 import android.os.storage.StorageResultCode;
25 import android.util.Log;
26 
27 import 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  */
34 public 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     public static final int RECOMMEND_FAILED_INVALID_URI = -6;
43 
44     private static final boolean localLOGV = true;
45     private static final String TAG = "PackageHelper";
46     // App installation location settings values
47     public static final int APP_INSTALL_AUTO = 0;
48     public static final int APP_INSTALL_INTERNAL = 1;
49     public static final int APP_INSTALL_EXTERNAL = 2;
50 
getMountService()51     public static IMountService getMountService() {
52         IBinder service = ServiceManager.getService("mount");
53         if (service != null) {
54             return IMountService.Stub.asInterface(service);
55         } else {
56             Log.e(TAG, "Can't get mount service");
57         }
58         return null;
59     }
60 
createSdDir(int sizeMb, String cid, String sdEncKey, int uid)61     public static String createSdDir(int sizeMb, String cid,
62             String sdEncKey, int uid) {
63         // Create mount point via MountService
64         IMountService mountService = getMountService();
65 
66         if (localLOGV)
67             Log.i(TAG, "Size of container " + sizeMb + " MB");
68 
69         try {
70             int rc = mountService.createSecureContainer(
71                     cid, sizeMb, "fat", sdEncKey, uid);
72             if (rc != StorageResultCode.OperationSucceeded) {
73                 Log.e(TAG, "Failed to create secure container " + cid);
74                 return null;
75             }
76             String cachePath = mountService.getSecureContainerPath(cid);
77             if (localLOGV) Log.i(TAG, "Created secure container " + cid +
78                     " at " + cachePath);
79                 return cachePath;
80         } catch (RemoteException e) {
81             Log.e(TAG, "MountService running?");
82         }
83         return null;
84     }
85 
mountSdDir(String cid, String key, int ownerUid)86    public static String mountSdDir(String cid, String key, int ownerUid) {
87     try {
88         int rc = getMountService().mountSecureContainer(cid, key, ownerUid);
89         if (rc != StorageResultCode.OperationSucceeded) {
90             Log.i(TAG, "Failed to mount container " + cid + " rc : " + rc);
91             return null;
92         }
93         return getMountService().getSecureContainerPath(cid);
94     } catch (RemoteException e) {
95         Log.e(TAG, "MountService running?");
96     }
97     return null;
98    }
99 
unMountSdDir(String cid)100    public static boolean unMountSdDir(String cid) {
101     try {
102         int rc = getMountService().unmountSecureContainer(cid, true);
103         if (rc != StorageResultCode.OperationSucceeded) {
104             Log.e(TAG, "Failed to unmount " + cid + " with rc " + rc);
105             return false;
106         }
107         return true;
108     } catch (RemoteException e) {
109         Log.e(TAG, "MountService running?");
110     }
111         return false;
112    }
113 
renameSdDir(String oldId, String newId)114    public static boolean renameSdDir(String oldId, String newId) {
115        try {
116            int rc = getMountService().renameSecureContainer(oldId, newId);
117            if (rc != StorageResultCode.OperationSucceeded) {
118                Log.e(TAG, "Failed to rename " + oldId + " to " +
119                        newId + "with rc " + rc);
120                return false;
121            }
122            return true;
123        } catch (RemoteException e) {
124            Log.i(TAG, "Failed ot rename  " + oldId + " to " + newId +
125                    " with exception : " + e);
126        }
127        return false;
128    }
129 
getSdDir(String cid)130    public static String getSdDir(String cid) {
131        try {
132             return getMountService().getSecureContainerPath(cid);
133         } catch (RemoteException e) {
134             Log.e(TAG, "Failed to get container path for " + cid +
135                 " with exception " + e);
136         }
137         return null;
138    }
139 
getSdFilesystem(String cid)140    public static String getSdFilesystem(String cid) {
141        try {
142             return getMountService().getSecureContainerFilesystemPath(cid);
143         } catch (RemoteException e) {
144             Log.e(TAG, "Failed to get container path for " + cid +
145                 " with exception " + e);
146         }
147         return null;
148    }
149 
finalizeSdDir(String cid)150     public static boolean finalizeSdDir(String cid) {
151         try {
152             int rc = getMountService().finalizeSecureContainer(cid);
153             if (rc != StorageResultCode.OperationSucceeded) {
154                 Log.i(TAG, "Failed to finalize container " + cid);
155                 return false;
156             }
157             return true;
158         } catch (RemoteException e) {
159             Log.e(TAG, "Failed to finalize container " + cid +
160                     " with exception " + e);
161         }
162         return false;
163     }
164 
destroySdDir(String cid)165     public static boolean destroySdDir(String cid) {
166         try {
167             if (localLOGV) Log.i(TAG, "Forcibly destroying container " + cid);
168             int rc = getMountService().destroySecureContainer(cid, true);
169             if (rc != StorageResultCode.OperationSucceeded) {
170                 Log.i(TAG, "Failed to destroy container " + cid);
171                 return false;
172             }
173             return true;
174         } catch (RemoteException e) {
175             Log.e(TAG, "Failed to destroy container " + cid +
176                     " with exception " + e);
177         }
178         return false;
179     }
180 
getSecureContainerList()181     public static String[] getSecureContainerList() {
182         try {
183             return getMountService().getSecureContainerList();
184         } catch (RemoteException e) {
185             Log.e(TAG, "Failed to get secure container list with exception" +
186                     e);
187         }
188         return null;
189     }
190 
isContainerMounted(String cid)191    public static boolean isContainerMounted(String cid) {
192        try {
193            return getMountService().isSecureContainerMounted(cid);
194        } catch (RemoteException e) {
195            Log.e(TAG, "Failed to find out if container " + cid + " mounted");
196        }
197        return false;
198    }
199 }
200