OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.media.ui; | 5 package org.chromium.chrome.browser.media.ui; |
6 | 6 |
7 import android.content.BroadcastReceiver; | 7 import android.content.BroadcastReceiver; |
8 import android.content.ComponentName; | |
9 import android.content.Context; | 8 import android.content.Context; |
10 import android.content.Intent; | 9 import android.content.Intent; |
11 import android.content.pm.PackageManager; | |
12 import android.content.pm.ResolveInfo; | |
13 | |
14 import java.util.List; | |
15 | 10 |
16 /** | 11 /** |
17 * MediaButtonReceiver is a basic BroadcastReceiver class that receives | 12 * MediaButtonReceiver is a basic BroadcastReceiver class that receives |
18 * ACTION_MEDIA_BUTTON from a MediaSessionCompat. It then forward these intents | 13 * ACTION_MEDIA_BUTTON from a MediaSessionCompat. It then forward these intents |
19 * to the service listening to them. | 14 * to the service listening to them. |
20 * This is there for backward compatibility with JB_MR0 and JB_MR1. | 15 * This is there for backward compatibility with JB_MR0 and JB_MR1. |
21 */ | 16 */ |
22 public abstract class MediaButtonReceiver extends BroadcastReceiver { | 17 public abstract class MediaButtonReceiver extends BroadcastReceiver { |
23 private static final String LISTENER_SERVICE_CLASS_NAME = | 18 public abstract String getServiceClassName(); |
24 "org.chromium.chrome.browser.media.ui" | |
25 + "MediaNotificationManager$ListenerService"; | |
26 public static final String EXTRA_NOTIFICATION_ID = | |
27 "MediaNotificationManager.ListenerService.NOTIFICATION_ID"; | |
28 | |
29 public abstract int getNotificationId(); | |
30 | 19 |
31 @Override | 20 @Override |
32 public void onReceive(Context context, Intent intent) { | 21 public void onReceive(Context context, Intent intent) { |
33 Intent queryIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); | 22 intent.setClassName(context, getServiceClassName()); |
34 queryIntent.setPackage(context.getPackageName()); | |
35 | |
36 PackageManager pm = context.getPackageManager(); | |
37 List<ResolveInfo> infos = pm.queryIntentServices(queryIntent, 0); | |
38 assert infos.size() == 1; | |
39 | |
40 ResolveInfo info = infos.get(0); | |
41 ComponentName component = new ComponentName(info.serviceInfo.packageName , | |
42 info.serviceInfo.name); | |
43 assert LISTENER_SERVICE_CLASS_NAME.equals(component.getClassName()); | |
44 | |
45 intent.setComponent(component); | |
46 intent.putExtra(EXTRA_NOTIFICATION_ID, getNotificationId()); | |
47 context.startService(intent); | 23 context.startService(intent); |
whywhat
2015/12/09 16:59:27
Why is setClassName() equivalent to setComponent()
Zhiqiang Zhang (Slow)
2015/12/09 18:02:55
I checked the Android reference. setClassName() is
| |
48 } | 24 } |
49 } | 25 } |
OLD | NEW |