summaryrefslogtreecommitdiff
path: root/android/media/update/ApiLoader.java
blob: 6f82f68304c9d12686179f1d8e1c9138709ec25c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.media.update;

import android.app.ActivityManager;
import android.app.AppGlobals;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.RemoteException;
import android.os.UserHandle;

import com.android.internal.annotations.GuardedBy;

import dalvik.system.PathClassLoader;

import java.io.File;

/**
 * @hide
 */
public final class ApiLoader {
    @GuardedBy("this")
    private static StaticProvider sMediaUpdatable;

    private static final String UPDATE_PACKAGE = "com.android.media.update";
    private static final String UPDATE_CLASS = "com.android.media.update.ApiFactory";
    private static final String UPDATE_METHOD = "initialize";
    private static final boolean REGISTER_UPDATE_DEPENDENCY = true;

    private ApiLoader() { }

    public static StaticProvider getProvider() {
        if (sMediaUpdatable != null) return sMediaUpdatable;

        try {
            return getMediaUpdatable();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        } catch (NameNotFoundException | ReflectiveOperationException e) {
            throw new RuntimeException(e);
        }
    }

    // TODO This method may do I/O; Ensure it does not violate (emit warnings in) strict mode.
    private static synchronized StaticProvider getMediaUpdatable()
            throws NameNotFoundException, ReflectiveOperationException, RemoteException {
        if (sMediaUpdatable != null) return sMediaUpdatable;

        // TODO Figure out when to use which package (query media update service)
        int flags = Build.IS_DEBUGGABLE ? 0 : PackageManager.MATCH_FACTORY_ONLY;
        flags |= PackageManager.GET_SHARED_LIBRARY_FILES;
        ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
                UPDATE_PACKAGE, flags, UserHandle.myUserId());

        if (REGISTER_UPDATE_DEPENDENCY) {
            // Register a dependency to the updatable in order to be killed during updates
            ActivityManager.getService().addPackageDependency(ai.packageName);
        }

        ClassLoader classLoader = new PathClassLoader(ai.sourceDir,
                ai.nativeLibraryDir + File.pathSeparator + System.getProperty("java.library.path"),
                ClassLoader.getSystemClassLoader().getParent());
        return sMediaUpdatable = (StaticProvider) classLoader.loadClass(UPDATE_CLASS)
                .getMethod(UPDATE_METHOD, ApplicationInfo.class).invoke(null, ai);
    }
}