Installer.java revision e4697136ed8d3e2486738b5798b22f2226b7de75
1/*
2 * Copyright (C) 2008 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.server.pm;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.content.pm.PackageStats;
22import android.os.Build;
23import android.os.storage.StorageManager;
24import android.util.Slog;
25
26import com.android.internal.os.InstallerConnection;
27import com.android.internal.os.InstallerConnection.InstallerException;
28import com.android.server.SystemService;
29
30import dalvik.system.VMRuntime;
31
32public final class Installer extends SystemService {
33    private static final String TAG = "Installer";
34
35    /* ***************************************************************************
36     * IMPORTANT: These values are passed to native code. Keep them in sync with
37     * frameworks/native/cmds/installd/installd.h
38     * **************************************************************************/
39    /** Application should be visible to everyone */
40    public static final int DEXOPT_PUBLIC       = 1 << 1;
41    /** Application wants to run in VM safe mode */
42    public static final int DEXOPT_SAFEMODE     = 1 << 2;
43    /** Application wants to allow debugging of its code */
44    public static final int DEXOPT_DEBUGGABLE   = 1 << 3;
45    /** The system boot has finished */
46    public static final int DEXOPT_BOOTCOMPLETE = 1 << 4;
47    /** Do not compile, only extract bytecode into an OAT file */
48    public static final int DEXOPT_EXTRACTONLY  = 1 << 5;
49    /** This is an OTA update dexopt */
50    public static final int DEXOPT_OTA          = 1 << 6;
51
52    // NOTE: keep in sync with installd
53    public static final int FLAG_CLEAR_CACHE_ONLY = 1 << 8;
54    public static final int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9;
55
56    private final InstallerConnection mInstaller;
57
58    public Installer(Context context) {
59        super(context);
60        mInstaller = new InstallerConnection();
61    }
62
63    /**
64     * Yell loudly if someone tries making future calls while holding a lock on
65     * the given object.
66     */
67    public void setWarnIfHeld(Object warnIfHeld) {
68        mInstaller.setWarnIfHeld(warnIfHeld);
69    }
70
71    @Override
72    public void onStart() {
73        Slog.i(TAG, "Waiting for installd to be ready.");
74        mInstaller.waitForConnection();
75    }
76
77    public void createAppData(String uuid, String pkgname, int userid, int flags, int appid,
78            String seinfo, int targetSdkVersion) throws InstallerException {
79        mInstaller.execute("create_app_data", uuid, pkgname, userid, flags, appid, seinfo,
80            targetSdkVersion);
81    }
82
83    public void restoreconAppData(String uuid, String pkgname, int userid, int flags, int appid,
84            String seinfo) throws InstallerException {
85        mInstaller.execute("restorecon_app_data", uuid, pkgname, userid, flags, appid,
86                seinfo);
87    }
88
89    public void migrateAppData(String uuid, String pkgname, int userid, int flags)
90            throws InstallerException {
91        mInstaller.execute("migrate_app_data", uuid, pkgname, userid, flags);
92    }
93
94    public void clearAppData(String uuid, String pkgname, int userid, int flags)
95            throws InstallerException {
96        mInstaller.execute("clear_app_data", uuid, pkgname, userid, flags);
97    }
98
99    public void destroyAppData(String uuid, String pkgname, int userid, int flags)
100            throws InstallerException {
101        mInstaller.execute("destroy_app_data", uuid, pkgname, userid, flags);
102    }
103
104    public void moveCompleteApp(String from_uuid, String to_uuid, String package_name,
105            String data_app_name, int appid, String seinfo, int targetSdkVersion)
106            throws InstallerException {
107        mInstaller.execute("move_complete_app", from_uuid, to_uuid, package_name,
108                data_app_name, appid, seinfo, targetSdkVersion);
109    }
110
111    public void getAppSize(String uuid, String pkgname, int userid, int flags, String apkPath,
112            String libDirPath, String fwdLockApkPath, String asecPath, String[] instructionSets,
113            PackageStats pStats) throws InstallerException {
114        for (String instructionSet : instructionSets) {
115            assertValidInstructionSet(instructionSet);
116        }
117
118        // TODO: Extend getSizeInfo to look at the full subdirectory tree,
119        // not just the first level.
120        // TODO: Extend getSizeInfo to look at *all* instrution sets, not
121        // just the primary.
122        final String rawRes = mInstaller.executeForResult("get_app_size", uuid, pkgname, userid,
123                flags, apkPath, libDirPath, fwdLockApkPath, asecPath, instructionSets[0]);
124        final String res[] = rawRes.split(" ");
125
126        if ((res == null) || (res.length != 5)) {
127            throw new InstallerException("Invalid size result: " + rawRes);
128        }
129        try {
130            pStats.codeSize = Long.parseLong(res[1]);
131            pStats.dataSize = Long.parseLong(res[2]);
132            pStats.cacheSize = Long.parseLong(res[3]);
133            pStats.externalCodeSize = Long.parseLong(res[4]);
134        } catch (NumberFormatException e) {
135            throw new InstallerException("Invalid size result: " + rawRes);
136        }
137    }
138
139    public void dexopt(String apkPath, int uid, String instructionSet, int dexoptNeeded,
140            int dexFlags, String volumeUuid, boolean useProfiles) throws InstallerException {
141        assertValidInstructionSet(instructionSet);
142        mInstaller.dexopt(apkPath, uid, instructionSet, dexoptNeeded, dexFlags,
143                volumeUuid, useProfiles);
144    }
145
146    public void dexopt(String apkPath, int uid, String pkgName, String instructionSet,
147            int dexoptNeeded, @Nullable String outputPath, int dexFlags,
148            String volumeUuid, boolean useProfiles)
149                    throws InstallerException {
150        assertValidInstructionSet(instructionSet);
151        mInstaller.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded,
152                outputPath, dexFlags, volumeUuid, useProfiles);
153    }
154
155    public void idmap(String targetApkPath, String overlayApkPath, int uid)
156            throws InstallerException {
157        mInstaller.execute("idmap", targetApkPath, overlayApkPath, uid);
158    }
159
160    public void rmdex(String codePath, String instructionSet) throws InstallerException {
161        assertValidInstructionSet(instructionSet);
162        mInstaller.execute("rmdex", codePath, instructionSet);
163    }
164
165    public void rmPackageDir(String packageDir) throws InstallerException {
166        mInstaller.execute("rmpackagedir", packageDir);
167    }
168
169    public void createUserConfig(int userid) throws InstallerException {
170        mInstaller.execute("mkuserconfig", userid);
171    }
172
173    public void removeUserDataDirs(String uuid, int userid) throws InstallerException {
174        mInstaller.execute("rmuser", uuid, userid);
175    }
176
177    public void markBootComplete(String instructionSet) throws InstallerException {
178        assertValidInstructionSet(instructionSet);
179        mInstaller.execute("markbootcomplete", instructionSet);
180    }
181
182    public void freeCache(String uuid, long freeStorageSize) throws InstallerException {
183        mInstaller.execute("freecache", uuid, freeStorageSize);
184    }
185
186    /**
187     * Links the 32 bit native library directory in an application's data
188     * directory to the real location for backward compatibility. Note that no
189     * such symlink is created for 64 bit shared libraries.
190     */
191    public void linkNativeLibraryDirectory(String uuid, String dataPath, String nativeLibPath32,
192            int userId) throws InstallerException {
193        mInstaller.execute("linklib", uuid, dataPath, nativeLibPath32, userId);
194    }
195
196    public void createOatDir(String oatDir, String dexInstructionSet)
197            throws InstallerException {
198        mInstaller.execute("createoatdir", oatDir, dexInstructionSet);
199    }
200
201    public void linkFile(String relativePath, String fromBase, String toBase)
202            throws InstallerException {
203        mInstaller.execute("linkfile", relativePath, fromBase, toBase);
204    }
205
206    private static void assertValidInstructionSet(String instructionSet)
207            throws InstallerException {
208        for (String abi : Build.SUPPORTED_ABIS) {
209            if (VMRuntime.getInstructionSet(abi).equals(instructionSet)) {
210                return;
211            }
212        }
213        throw new InstallerException("Invalid instruction set: " + instructionSet);
214    }
215}
216