summaryrefslogtreecommitdiff
path: root/android/media/update/ApiLoader.java
blob: b928e9319b18b5945774c1d8af6900925fd5322e (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
/*
 * 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.content.res.Resources;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

/**
 * @hide
 */
public final class ApiLoader {
    private static Object sMediaLibrary;

    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 ApiLoader() { }

    public static StaticProvider getProvider(Context context) {
        try {
            return (StaticProvider) getMediaLibraryImpl(context);
        } catch (PackageManager.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 Object getMediaLibraryImpl(Context context)
            throws PackageManager.NameNotFoundException, ReflectiveOperationException {
        if (sMediaLibrary != null) return sMediaLibrary;

        // TODO Figure out when to use which package (query media update service)
        int flags = Build.IS_DEBUGGABLE ? 0 : PackageManager.MATCH_FACTORY_ONLY;
        Context libContext = context.createApplicationContext(
                context.getPackageManager().getPackageInfo(UPDATE_PACKAGE, flags).applicationInfo,
                Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
        sMediaLibrary = libContext.getClassLoader()
                .loadClass(UPDATE_CLASS)
                .getMethod(UPDATE_METHOD, Resources.class, Resources.Theme.class)
                .invoke(null, libContext.getResources(), libContext.getTheme());
        return sMediaLibrary;
    }
}