1 /* 2 * Copyright (C) 2016 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.app.ActivityManager; 20 import android.app.ActivityManagerNative; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.media.IMediaResourceMonitor; 24 import android.os.Binder; 25 import android.os.RemoteException; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 import android.util.Log; 29 import android.util.Slog; 30 import com.android.server.SystemService; 31 32 import java.util.List; 33 34 /** This class provides a system service that monitors media resource usage. */ 35 public class MediaResourceMonitorService extends SystemService { 36 private static final String TAG = "MediaResourceMonitor"; 37 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 38 39 private static final String SERVICE_NAME = "media_resource_monitor"; 40 41 private final MediaResourceMonitorImpl mMediaResourceMonitorImpl; 42 43 public MediaResourceMonitorService(Context context) { 44 super(context); 45 mMediaResourceMonitorImpl = new MediaResourceMonitorImpl(); 46 } 47 48 @Override 49 public void onStart() { 50 publishBinderService(SERVICE_NAME, mMediaResourceMonitorImpl); 51 } 52 53 class MediaResourceMonitorImpl extends IMediaResourceMonitor.Stub { 54 @Override 55 public void notifyResourceGranted(int pid, int type) 56 throws RemoteException { 57 if (DEBUG) { 58 Slog.d(TAG, "notifyResourceGranted(pid=" + pid + ", type=" + type + ")"); 59 } 60 final long identity = Binder.clearCallingIdentity(); 61 try { 62 String pkgNames[] = getPackageNamesFromPid(pid); 63 if (pkgNames == null) { 64 return; 65 } 66 UserManager manager = (UserManager) getContext().getSystemService( 67 Context.USER_SERVICE); 68 int[] userIds = manager.getEnabledProfileIds(ActivityManager.getCurrentUser()); 69 if (userIds == null || userIds.length == 0) { 70 return; 71 } 72 Intent intent = new Intent(Intent.ACTION_MEDIA_RESOURCE_GRANTED); 73 intent.putExtra(Intent.EXTRA_PACKAGES, pkgNames); 74 intent.putExtra(Intent.EXTRA_MEDIA_RESOURCE_TYPE, type); 75 for (int userId : userIds) { 76 getContext().sendBroadcastAsUser(intent, UserHandle.of(userId), 77 android.Manifest.permission.RECEIVE_MEDIA_RESOURCE_USAGE); 78 } 79 } finally { 80 Binder.restoreCallingIdentity(identity); 81 } 82 } 83 84 private String[] getPackageNamesFromPid(int pid) { 85 try { 86 for (ActivityManager.RunningAppProcessInfo proc : 87 ActivityManagerNative.getDefault().getRunningAppProcesses()) { 88 if (proc.pid == pid) { 89 return proc.pkgList; 90 } 91 } 92 } catch (RemoteException e) { 93 Slog.w(TAG, "ActivityManager.getRunningAppProcesses() failed"); 94 } 95 return null; 96 } 97 } 98 } 99