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