blob: f38b35342f3a1ac261fc4192ec95cac5585ae8d2 [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 Kang569da9f2017-11-06 09:53:52 -080026import android.os.IBinder;
Dongwon Kanged187722018-02-27 17:25:37 -080027import android.os.Handler;
Dongwon Kang569da9f2017-11-06 09:53:52 -080028import android.os.PatternMatcher;
29import android.os.ServiceManager;
Dongwon Kanged187722018-02-27 17:25:37 -080030import android.os.UserHandle;
31import android.util.Log;
32import android.util.Slog;
33import com.android.server.SystemService;
Dongwon Kang569da9f2017-11-06 09:53:52 -080034
35/** This class provides a system service that manages media framework updates. */
36public 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 Kanged187722018-02-27 17:25:37 -080043 final Handler mHandler;
Dongwon Kang569da9f2017-11-06 09:53:52 -080044
45 public MediaUpdateService(Context context) {
46 super(context);
Dongwon Kanged187722018-02-27 17:25:37 -080047 mHandler = new Handler();
Dongwon Kang569da9f2017-11-06 09:53:52 -080048 }
49
50 @Override
51 public void onStart() {
Dongwon Kange6c56132018-01-16 18:14:37 -080052 if ("userdebug".equals(android.os.Build.TYPE) || "eng".equals(android.os.Build.TYPE)) {
53 connect();
54 registerBroadcastReceiver();
55 }
Dongwon Kang569da9f2017-11-06 09:53:52 -080056 }
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 Kanged187722018-02-27 17:25:37 -080076 mHandler.post(new Runnable() {
77 @Override
78 public void run() {
79 packageStateChanged();
80 }
81 });
Dongwon Kang569da9f2017-11-06 09:53:52 -080082 } 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}