Home | History | Annotate | Download | only in media
      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 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.content.pm.ApplicationInfo;
     24 import android.content.pm.PackageManager;
     25 import android.media.IMediaExtractorUpdateService;
     26 import android.os.Build;
     27 import android.os.IBinder;
     28 import android.os.Handler;
     29 import android.os.PatternMatcher;
     30 import android.os.ServiceManager;
     31 import android.os.SystemProperties;
     32 import android.os.UserHandle;
     33 import android.text.TextUtils;
     34 import android.util.Log;
     35 import android.util.Slog;
     36 import com.android.server.SystemService;
     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);
     42     private static final String MEDIA_UPDATE_PACKAGE_NAME =
     43             SystemProperties.get("ro.mediacomponents.package");
     44     private static final String EXTRACTOR_UPDATE_SERVICE_NAME = "media.extractor.update";
     45 
     46     private IMediaExtractorUpdateService mMediaExtractorUpdateService;
     47     final Handler mHandler;
     48 
     49     public MediaUpdateService(Context context) {
     50         super(context);
     51         mHandler = new Handler();
     52     }
     53 
     54     @Override
     55     public void onStart() {
     56         if (("userdebug".equals(android.os.Build.TYPE) || "eng".equals(android.os.Build.TYPE))
     57                 && !TextUtils.isEmpty(MEDIA_UPDATE_PACKAGE_NAME)) {
     58             connect();
     59             registerBroadcastReceiver();
     60         }
     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);
     81             mHandler.post(new Runnable() {
     82                 @Override
     83                 public void run() {
     84                     packageStateChanged();
     85                 }
     86             });
     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         }
    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         }
    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 }
    161