ApiLoader.java revision 6bd3ab6dc6ad4001c387d7f4a4a2ce954be6ca16
1/* 2 * Copyright (C) 2017 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 android.media.update; 18 19import android.app.ActivityManager; 20import android.app.AppGlobals; 21import android.content.Context; 22import android.content.pm.ApplicationInfo; 23import android.content.pm.PackageManager; 24import android.content.pm.PackageManager.NameNotFoundException; 25import android.os.Build; 26import android.os.RemoteException; 27import android.os.UserHandle; 28 29import com.android.internal.annotations.GuardedBy; 30 31import dalvik.system.PathClassLoader; 32 33import java.io.File; 34 35/** 36 * @hide 37 */ 38public final class ApiLoader { 39 @GuardedBy("this") 40 private static StaticProvider sMediaUpdatable; 41 42 private static final String UPDATE_PACKAGE = "com.android.media.update"; 43 private static final String UPDATE_CLASS = "com.android.media.update.ApiFactory"; 44 private static final String UPDATE_METHOD = "initialize"; 45 private static final boolean REGISTER_UPDATE_DEPENDENCY = true; 46 47 private ApiLoader() { } 48 49 public static StaticProvider getProvider() { 50 if (sMediaUpdatable != null) return sMediaUpdatable; 51 52 try { 53 return getMediaUpdatable(); 54 } catch (RemoteException e) { 55 throw e.rethrowFromSystemServer(); 56 } catch (NameNotFoundException | ReflectiveOperationException e) { 57 throw new RuntimeException(e); 58 } 59 } 60 61 // TODO This method may do I/O; Ensure it does not violate (emit warnings in) strict mode. 62 private static synchronized StaticProvider getMediaUpdatable() 63 throws NameNotFoundException, ReflectiveOperationException, RemoteException { 64 if (sMediaUpdatable != null) return sMediaUpdatable; 65 66 // TODO Figure out when to use which package (query media update service) 67 int flags = Build.IS_DEBUGGABLE ? 0 : PackageManager.MATCH_FACTORY_ONLY; 68 flags |= PackageManager.GET_SHARED_LIBRARY_FILES; 69 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo( 70 UPDATE_PACKAGE, flags, UserHandle.myUserId()); 71 72 if (REGISTER_UPDATE_DEPENDENCY) { 73 // Register a dependency to the updatable in order to be killed during updates 74 ActivityManager.getService().addPackageDependency(ai.packageName); 75 } 76 77 ClassLoader classLoader = new PathClassLoader(ai.sourceDir, 78 ai.nativeLibraryDir + File.pathSeparator + System.getProperty("java.library.path"), 79 ClassLoader.getSystemClassLoader().getParent()); 80 return sMediaUpdatable = (StaticProvider) classLoader.loadClass(UPDATE_CLASS) 81 .getMethod(UPDATE_METHOD, ApplicationInfo.class).invoke(null, ai); 82 } 83} 84