Dongwon Kang | 2034a4c | 2015-12-14 21:57:34 +0900 | [diff] [blame] | 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; |
Jaewan Kim | efd92a3 | 2016-06-23 15:58:09 +0900 | [diff] [blame] | 27 | import android.os.UserManager; |
Dongwon Kang | 2034a4c | 2015-12-14 21:57:34 +0900 | [diff] [blame] | 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 { |
Dongwon Kang | ee3d811 | 2016-01-13 11:39:38 -0800 | [diff] [blame] | 36 | private static final String TAG = "MediaResourceMonitor"; |
Dongwon Kang | 2034a4c | 2015-12-14 21:57:34 +0900 | [diff] [blame] | 37 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 38 | |
| 39 | private static final String SERVICE_NAME = "media_resource_monitor"; |
| 40 | |
Dongwon Kang | 2034a4c | 2015-12-14 21:57:34 +0900 | [diff] [blame] | 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 |
Dongwon Kang | 6d8587a | 2016-03-22 15:20:40 -0700 | [diff] [blame] | 55 | public void notifyResourceGranted(int pid, int type) |
Dongwon Kang | 2034a4c | 2015-12-14 21:57:34 +0900 | [diff] [blame] | 56 | throws RemoteException { |
| 57 | if (DEBUG) { |
Dongwon Kang | 6d8587a | 2016-03-22 15:20:40 -0700 | [diff] [blame] | 58 | Slog.d(TAG, "notifyResourceGranted(pid=" + pid + ", type=" + type + ")"); |
Dongwon Kang | 2034a4c | 2015-12-14 21:57:34 +0900 | [diff] [blame] | 59 | } |
| 60 | final long identity = Binder.clearCallingIdentity(); |
| 61 | try { |
| 62 | String pkgNames[] = getPackageNamesFromPid(pid); |
Jaewan Kim | efd92a3 | 2016-06-23 15:58:09 +0900 | [diff] [blame] | 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), |
Dongwon Kang | 2034a4c | 2015-12-14 21:57:34 +0900 | [diff] [blame] | 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 | } |