Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame^] | 19 | import android.annotation.NonNull; |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.ResolveInfo; |
| 27 | import android.content.pm.ServiceInfo; |
| 28 | import android.media.MediaRoute2ProviderService; |
| 29 | import android.os.Handler; |
| 30 | import android.os.UserHandle; |
| 31 | import android.util.Log; |
| 32 | import android.util.Slog; |
| 33 | |
| 34 | import java.io.PrintWriter; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.Collections; |
| 37 | |
| 38 | /** |
Hyundo Moon | 7d3ab33 | 2019-10-16 11:09:56 +0900 | [diff] [blame] | 39 | * Watches changes of packages, or scan them for finding media route providers. |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 40 | */ |
| 41 | final class MediaRoute2ProviderWatcher { |
Hyundo Moon | 7d3ab33 | 2019-10-16 11:09:56 +0900 | [diff] [blame] | 42 | private static final String TAG = "MR2ProviderWatcher"; |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 43 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 44 | |
| 45 | private final Context mContext; |
| 46 | private final Callback mCallback; |
| 47 | private final Handler mHandler; |
| 48 | private final int mUserId; |
| 49 | private final PackageManager mPackageManager; |
| 50 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 51 | private final ArrayList<MediaRoute2ProviderServiceProxy> mProxies = new ArrayList<>(); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 52 | private boolean mRunning; |
| 53 | |
| 54 | MediaRoute2ProviderWatcher(Context context, |
| 55 | Callback callback, Handler handler, int userId) { |
| 56 | mContext = context; |
| 57 | mCallback = callback; |
| 58 | mHandler = handler; |
| 59 | mUserId = userId; |
| 60 | mPackageManager = context.getPackageManager(); |
| 61 | } |
| 62 | |
| 63 | public void dump(PrintWriter pw, String prefix) { |
| 64 | pw.println(prefix + "Watcher"); |
| 65 | pw.println(prefix + " mUserId=" + mUserId); |
| 66 | pw.println(prefix + " mRunning=" + mRunning); |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 67 | pw.println(prefix + " mProxies.size()=" + mProxies.size()); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | public void start() { |
| 71 | if (!mRunning) { |
| 72 | mRunning = true; |
| 73 | |
| 74 | IntentFilter filter = new IntentFilter(); |
| 75 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 76 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 77 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 78 | filter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 79 | filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 80 | filter.addDataScheme("package"); |
| 81 | mContext.registerReceiverAsUser(mScanPackagesReceiver, |
| 82 | new UserHandle(mUserId), filter, null, mHandler); |
| 83 | |
| 84 | // Scan packages. |
| 85 | // Also has the side-effect of restarting providers if needed. |
| 86 | mHandler.post(mScanPackagesRunnable); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public void stop() { |
| 91 | if (mRunning) { |
| 92 | mRunning = false; |
| 93 | |
| 94 | mContext.unregisterReceiver(mScanPackagesReceiver); |
| 95 | mHandler.removeCallbacks(mScanPackagesRunnable); |
| 96 | |
| 97 | // Stop all providers. |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 98 | for (int i = mProxies.size() - 1; i >= 0; i--) { |
| 99 | mProxies.get(i).stop(); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private void scanPackages() { |
| 105 | if (!mRunning) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // Add providers for all new services. |
| 110 | // Reorder the list so that providers left at the end will be the ones to remove. |
| 111 | int targetIndex = 0; |
| 112 | Intent intent = new Intent(MediaRoute2ProviderService.SERVICE_INTERFACE); |
| 113 | for (ResolveInfo resolveInfo : mPackageManager.queryIntentServicesAsUser( |
| 114 | intent, 0, mUserId)) { |
| 115 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
| 116 | if (serviceInfo != null) { |
| 117 | int sourceIndex = findProvider(serviceInfo.packageName, serviceInfo.name); |
| 118 | if (sourceIndex < 0) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 119 | MediaRoute2ProviderServiceProxy proxy = |
| 120 | new MediaRoute2ProviderServiceProxy(mContext, |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 121 | new ComponentName(serviceInfo.packageName, serviceInfo.name), |
| 122 | mUserId); |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 123 | proxy.start(); |
| 124 | mProxies.add(targetIndex++, proxy); |
| 125 | mCallback.onAddProviderService(proxy); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 126 | } else if (sourceIndex >= targetIndex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 127 | MediaRoute2ProviderServiceProxy proxy = mProxies.get(sourceIndex); |
| 128 | proxy.start(); // restart the proxy if needed |
| 129 | proxy.rebindIfDisconnected(); |
| 130 | Collections.swap(mProxies, sourceIndex, targetIndex++); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Remove providers for missing services. |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 136 | if (targetIndex < mProxies.size()) { |
| 137 | for (int i = mProxies.size() - 1; i >= targetIndex; i--) { |
| 138 | MediaRoute2ProviderServiceProxy proxy = mProxies.get(i); |
| 139 | mCallback.onRemoveProviderService(proxy); |
| 140 | mProxies.remove(proxy); |
| 141 | proxy.stop(); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private int findProvider(String packageName, String className) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 147 | int count = mProxies.size(); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 148 | for (int i = 0; i < count; i++) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 149 | MediaRoute2ProviderServiceProxy proxy = mProxies.get(i); |
| 150 | if (proxy.hasComponentName(packageName, className)) { |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 151 | return i; |
| 152 | } |
| 153 | } |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | private final BroadcastReceiver mScanPackagesReceiver = new BroadcastReceiver() { |
| 158 | @Override |
| 159 | public void onReceive(Context context, Intent intent) { |
| 160 | if (DEBUG) { |
| 161 | Slog.d(TAG, "Received package manager broadcast: " + intent); |
| 162 | } |
| 163 | scanPackages(); |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | private final Runnable mScanPackagesRunnable = new Runnable() { |
| 168 | @Override |
| 169 | public void run() { |
| 170 | scanPackages(); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | public interface Callback { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame^] | 175 | void onAddProviderService(@NonNull MediaRoute2ProviderServiceProxy proxy); |
| 176 | void onRemoveProviderService(@NonNull MediaRoute2ProviderServiceProxy proxy); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 177 | } |
| 178 | } |