Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
| 17 | package com.android.server.media; |
| 18 | |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
Dongwon Kang | ed18772 | 2018-02-27 17:25:37 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
| 23 | import android.content.pm.ApplicationInfo; |
| 24 | import android.content.pm.PackageManager; |
Dongwon Kang | ed18772 | 2018-02-27 17:25:37 -0800 | [diff] [blame] | 25 | import android.media.IMediaExtractorUpdateService; |
Dongwon Kang | 4a64936 | 2018-03-09 17:24:08 -0800 | [diff] [blame] | 26 | import android.os.Build; |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 27 | import android.os.IBinder; |
Dongwon Kang | ed18772 | 2018-02-27 17:25:37 -0800 | [diff] [blame] | 28 | import android.os.Handler; |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 29 | import android.os.PatternMatcher; |
| 30 | import android.os.ServiceManager; |
Dongwon Kang | 4a64936 | 2018-03-09 17:24:08 -0800 | [diff] [blame] | 31 | import android.os.SystemProperties; |
Dongwon Kang | ed18772 | 2018-02-27 17:25:37 -0800 | [diff] [blame] | 32 | import android.os.UserHandle; |
Dongwon Kang | 4a64936 | 2018-03-09 17:24:08 -0800 | [diff] [blame] | 33 | import android.text.TextUtils; |
Dongwon Kang | ed18772 | 2018-02-27 17:25:37 -0800 | [diff] [blame] | 34 | import android.util.Log; |
| 35 | import android.util.Slog; |
| 36 | import com.android.server.SystemService; |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 37 | |
| 38 | /** This class provides a system service that manages media framework updates. */ |
| 39 | public class MediaUpdateService extends SystemService { |
| 40 | private static final String TAG = "MediaUpdateService"; |
| 41 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Dongwon Kang | 4a64936 | 2018-03-09 17:24:08 -0800 | [diff] [blame] | 42 | private static final String MEDIA_UPDATE_PACKAGE_NAME = |
| 43 | SystemProperties.get("ro.mediacomponents.package"); |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 44 | private static final String EXTRACTOR_UPDATE_SERVICE_NAME = "media.extractor.update"; |
| 45 | |
| 46 | private IMediaExtractorUpdateService mMediaExtractorUpdateService; |
Dongwon Kang | ed18772 | 2018-02-27 17:25:37 -0800 | [diff] [blame] | 47 | final Handler mHandler; |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 48 | |
| 49 | public MediaUpdateService(Context context) { |
| 50 | super(context); |
Dongwon Kang | ed18772 | 2018-02-27 17:25:37 -0800 | [diff] [blame] | 51 | mHandler = new Handler(); |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void onStart() { |
Dongwon Kang | 4a64936 | 2018-03-09 17:24:08 -0800 | [diff] [blame] | 56 | if (("userdebug".equals(android.os.Build.TYPE) || "eng".equals(android.os.Build.TYPE)) |
| 57 | && !TextUtils.isEmpty(MEDIA_UPDATE_PACKAGE_NAME)) { |
Dongwon Kang | e6c5613 | 2018-01-16 18:14:37 -0800 | [diff] [blame] | 58 | connect(); |
| 59 | registerBroadcastReceiver(); |
| 60 | } |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private void connect() { |
| 64 | IBinder binder = ServiceManager.getService(EXTRACTOR_UPDATE_SERVICE_NAME); |
| 65 | if (binder != null) { |
| 66 | try { |
| 67 | binder.linkToDeath(new IBinder.DeathRecipient() { |
| 68 | @Override |
| 69 | public void binderDied() { |
| 70 | Slog.w(TAG, "mediaextractor died; reconnecting"); |
| 71 | mMediaExtractorUpdateService = null; |
| 72 | connect(); |
| 73 | } |
| 74 | }, 0); |
| 75 | } catch (Exception e) { |
| 76 | binder = null; |
| 77 | } |
| 78 | } |
| 79 | if (binder != null) { |
| 80 | mMediaExtractorUpdateService = IMediaExtractorUpdateService.Stub.asInterface(binder); |
Dongwon Kang | ed18772 | 2018-02-27 17:25:37 -0800 | [diff] [blame] | 81 | mHandler.post(new Runnable() { |
| 82 | @Override |
| 83 | public void run() { |
| 84 | packageStateChanged(); |
| 85 | } |
| 86 | }); |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 87 | } else { |
| 88 | Slog.w(TAG, EXTRACTOR_UPDATE_SERVICE_NAME + " not found."); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private void registerBroadcastReceiver() { |
| 93 | BroadcastReceiver updateReceiver = new BroadcastReceiver() { |
| 94 | @Override |
| 95 | public void onReceive(Context context, Intent intent) { |
| 96 | if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_SYSTEM) |
| 97 | != UserHandle.USER_SYSTEM) { |
| 98 | // Ignore broadcast for non system users. We don't want to update system |
| 99 | // service multiple times. |
| 100 | return; |
| 101 | } |
| 102 | switch (intent.getAction()) { |
| 103 | case Intent.ACTION_PACKAGE_REMOVED: |
| 104 | if (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)) { |
| 105 | // The existing package is updated. Will be handled with the |
| 106 | // following ACTION_PACKAGE_ADDED case. |
| 107 | return; |
| 108 | } |
| 109 | packageStateChanged(); |
| 110 | break; |
| 111 | case Intent.ACTION_PACKAGE_CHANGED: |
| 112 | packageStateChanged(); |
| 113 | break; |
| 114 | case Intent.ACTION_PACKAGE_ADDED: |
| 115 | packageStateChanged(); |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | }; |
| 120 | IntentFilter filter = new IntentFilter(); |
| 121 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 122 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 123 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 124 | filter.addDataScheme("package"); |
| 125 | filter.addDataSchemeSpecificPart(MEDIA_UPDATE_PACKAGE_NAME, PatternMatcher.PATTERN_LITERAL); |
| 126 | |
| 127 | getContext().registerReceiverAsUser(updateReceiver, UserHandle.ALL, filter, |
| 128 | null /* broadcast permission */, null /* handler */); |
| 129 | } |
| 130 | |
| 131 | private void packageStateChanged() { |
| 132 | ApplicationInfo packageInfo = null; |
| 133 | boolean pluginsAvailable = false; |
| 134 | try { |
| 135 | packageInfo = getContext().getPackageManager().getApplicationInfo( |
| 136 | MEDIA_UPDATE_PACKAGE_NAME, PackageManager.MATCH_SYSTEM_ONLY); |
| 137 | pluginsAvailable = packageInfo.enabled; |
| 138 | } catch (Exception e) { |
| 139 | Slog.v(TAG, "package '" + MEDIA_UPDATE_PACKAGE_NAME + "' not installed"); |
| 140 | } |
Dongwon Kang | 4a64936 | 2018-03-09 17:24:08 -0800 | [diff] [blame] | 141 | if (packageInfo != null && Build.VERSION.SDK_INT != packageInfo.targetSdkVersion) { |
| 142 | Slog.w(TAG, "This update package is not for this platform version. Ignoring. " |
| 143 | + "platform:" + Build.VERSION.SDK_INT |
| 144 | + " targetSdk:" + packageInfo.targetSdkVersion); |
| 145 | pluginsAvailable = false; |
| 146 | } |
Dongwon Kang | 569da9f | 2017-11-06 09:53:52 -0800 | [diff] [blame] | 147 | loadExtractorPlugins( |
| 148 | (packageInfo != null && pluginsAvailable) ? packageInfo.sourceDir : ""); |
| 149 | } |
| 150 | |
| 151 | private void loadExtractorPlugins(String apkPath) { |
| 152 | try { |
| 153 | if (mMediaExtractorUpdateService != null) { |
| 154 | mMediaExtractorUpdateService.loadPlugins(apkPath); |
| 155 | } |
| 156 | } catch (Exception e) { |
| 157 | Slog.w(TAG, "Error in loadPlugins", e); |
| 158 | } |
| 159 | } |
| 160 | } |