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