blob: af06d157a52697a5223ed659ea80506fb7f96fb0 [file] [log] [blame]
Dongwon Kang569da9f2017-11-06 09:53:52 -08001/*
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
17package com.android.server.media;
18
Dongwon Kang569da9f2017-11-06 09:53:52 -080019import android.content.BroadcastReceiver;
Dongwon Kanged187722018-02-27 17:25:37 -080020import android.content.Context;
Dongwon Kang569da9f2017-11-06 09:53:52 -080021import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
Dongwon Kanged187722018-02-27 17:25:37 -080025import android.media.IMediaExtractorUpdateService;
Dongwon Kang4a649362018-03-09 17:24:08 -080026import android.os.Build;
Dongwon Kang569da9f2017-11-06 09:53:52 -080027import android.os.IBinder;
Dongwon Kanged187722018-02-27 17:25:37 -080028import android.os.Handler;
Dongwon Kang569da9f2017-11-06 09:53:52 -080029import android.os.PatternMatcher;
30import android.os.ServiceManager;
Dongwon Kang4a649362018-03-09 17:24:08 -080031import android.os.SystemProperties;
Dongwon Kanged187722018-02-27 17:25:37 -080032import android.os.UserHandle;
Dongwon Kang4a649362018-03-09 17:24:08 -080033import android.text.TextUtils;
Dongwon Kanged187722018-02-27 17:25:37 -080034import android.util.Log;
35import android.util.Slog;
36import com.android.server.SystemService;
Dongwon Kang569da9f2017-11-06 09:53:52 -080037
38/** This class provides a system service that manages media framework updates. */
39public class MediaUpdateService extends SystemService {
40 private static final String TAG = "MediaUpdateService";
41 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Dongwon Kang4a649362018-03-09 17:24:08 -080042 private static final String MEDIA_UPDATE_PACKAGE_NAME =
43 SystemProperties.get("ro.mediacomponents.package");
Dongwon Kang569da9f2017-11-06 09:53:52 -080044 private static final String EXTRACTOR_UPDATE_SERVICE_NAME = "media.extractor.update";
45
46 private IMediaExtractorUpdateService mMediaExtractorUpdateService;
Dongwon Kanged187722018-02-27 17:25:37 -080047 final Handler mHandler;
Dongwon Kang569da9f2017-11-06 09:53:52 -080048
49 public MediaUpdateService(Context context) {
50 super(context);
Dongwon Kanged187722018-02-27 17:25:37 -080051 mHandler = new Handler();
Dongwon Kang569da9f2017-11-06 09:53:52 -080052 }
53
54 @Override
55 public void onStart() {
Dongwon Kang4a649362018-03-09 17:24:08 -080056 if (("userdebug".equals(android.os.Build.TYPE) || "eng".equals(android.os.Build.TYPE))
57 && !TextUtils.isEmpty(MEDIA_UPDATE_PACKAGE_NAME)) {
Dongwon Kange6c56132018-01-16 18:14:37 -080058 connect();
59 registerBroadcastReceiver();
60 }
Dongwon Kang569da9f2017-11-06 09:53:52 -080061 }
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 Kanged187722018-02-27 17:25:37 -080081 mHandler.post(new Runnable() {
82 @Override
83 public void run() {
84 packageStateChanged();
85 }
86 });
Dongwon Kang569da9f2017-11-06 09:53:52 -080087 } 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 Kang4a649362018-03-09 17:24:08 -0800141 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 Kang569da9f2017-11-06 09:53:52 -0800147 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}