• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.example.android.musicplayer;
18 
19 import android.media.AudioManager;
20 import android.util.Log;
21 
22 import java.lang.reflect.Method;
23 
24 /**
25  * Contains methods to handle registering/unregistering remote control clients.  These methods only
26  * run on ICS devices.  On previous devices, all methods are no-ops.
27  */
28 @SuppressWarnings({"unchecked", "rawtypes"})
29 public class RemoteControlHelper {
30     private static final String TAG = "RemoteControlHelper";
31 
32     private static boolean sHasRemoteControlAPIs = false;
33 
34     private static Method sRegisterRemoteControlClientMethod;
35     private static Method sUnregisterRemoteControlClientMethod;
36 
37     static {
38         try {
39             ClassLoader classLoader = RemoteControlHelper.class.getClassLoader();
40             Class sRemoteControlClientClass =
41                     RemoteControlClientCompat.getActualRemoteControlClientClass(classLoader);
42             sRegisterRemoteControlClientMethod = AudioManager.class.getMethod(
43                     "registerRemoteControlClient", new Class[]{sRemoteControlClientClass});
44             sUnregisterRemoteControlClientMethod = AudioManager.class.getMethod(
45                     "unregisterRemoteControlClient", new Class[]{sRemoteControlClientClass});
46             sHasRemoteControlAPIs = true;
47         } catch (ClassNotFoundException e) {
48             // Silently fail when running on an OS before ICS.
49         } catch (NoSuchMethodException e) {
50             // Silently fail when running on an OS before ICS.
51         } catch (IllegalArgumentException e) {
52             // Silently fail when running on an OS before ICS.
53         } catch (SecurityException e) {
54             // Silently fail when running on an OS before ICS.
55         }
56     }
57 
registerRemoteControlClient(AudioManager audioManager, RemoteControlClientCompat remoteControlClient)58     public static void registerRemoteControlClient(AudioManager audioManager,
59             RemoteControlClientCompat remoteControlClient) {
60         if (!sHasRemoteControlAPIs) {
61             return;
62         }
63 
64         try {
65             sRegisterRemoteControlClientMethod.invoke(audioManager,
66                     remoteControlClient.getActualRemoteControlClientObject());
67         } catch (Exception e) {
68             Log.e(TAG, e.getMessage(), e);
69         }
70     }
71 
72 
unregisterRemoteControlClient(AudioManager audioManager, RemoteControlClientCompat remoteControlClient)73     public static void unregisterRemoteControlClient(AudioManager audioManager,
74             RemoteControlClientCompat remoteControlClient) {
75         if (!sHasRemoteControlAPIs) {
76             return;
77         }
78 
79         try {
80             sUnregisterRemoteControlClientMethod.invoke(audioManager,
81                     remoteControlClient.getActualRemoteControlClientObject());
82         } catch (Exception e) {
83             Log.e(TAG, e.getMessage(), e);
84         }
85     }
86 }
87 
88