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