1 /*
2  * Copyright (C) 2018 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 android.media.audiopolicy;
18 
19 import android.annotation.NonNull;
20 import android.media.AudioManager;
21 import android.os.Handler;
22 import android.os.HandlerThread;
23 import android.os.Message;
24 
25 import com.android.internal.util.Preconditions;
26 
27 import java.lang.ref.WeakReference;
28 import java.util.ArrayList;
29 
30 /**
31  * The AudioVolumeGroupChangeHandler handles AudioManager.OnAudioVolumeGroupChangedListener
32  * callbacks posted from JNI
33  *
34  * TODO: Make use of Executor of callbacks.
35  * @hide
36  */
37 public class AudioVolumeGroupChangeHandler {
38     private Handler mHandler;
39     private HandlerThread mHandlerThread;
40     private final ArrayList<AudioManager.VolumeGroupCallback> mListeners =
41             new ArrayList<AudioManager.VolumeGroupCallback>();
42 
43     private static final String TAG = "AudioVolumeGroupChangeHandler";
44 
45     private static final int AUDIOVOLUMEGROUP_EVENT_VOLUME_CHANGED = 1000;
46     private static final int AUDIOVOLUMEGROUP_EVENT_NEW_LISTENER = 4;
47 
48     /**
49      * Accessed by native methods: JNI Callback context.
50      */
51     @SuppressWarnings("unused")
52     private long mJniCallback;
53 
54     /**
55      * Initialization
56      */
init()57     public void init() {
58         synchronized (this) {
59             if (mHandler != null) {
60                 return;
61             }
62             // create a new thread for our new event handler
63             mHandlerThread = new HandlerThread(TAG);
64             mHandlerThread.start();
65 
66             if (mHandlerThread.getLooper() == null) {
67                 mHandler = null;
68                 return;
69             }
70             mHandler = new Handler(mHandlerThread.getLooper()) {
71                 @Override
72                 public void handleMessage(Message msg) {
73                     ArrayList<AudioManager.VolumeGroupCallback> listeners;
74                     synchronized (this) {
75                         if (msg.what == AUDIOVOLUMEGROUP_EVENT_NEW_LISTENER) {
76                             listeners =
77                                     new ArrayList<AudioManager.VolumeGroupCallback>();
78                             if (mListeners.contains(msg.obj)) {
79                                 listeners.add(
80                                         (AudioManager.VolumeGroupCallback) msg.obj);
81                             }
82                         } else {
83                             listeners = mListeners;
84                         }
85                     }
86                     if (listeners.isEmpty()) {
87                         return;
88                     }
89 
90                     switch (msg.what) {
91                         case AUDIOVOLUMEGROUP_EVENT_VOLUME_CHANGED:
92                             for (int i = 0; i < listeners.size(); i++) {
93                                 listeners.get(i).onAudioVolumeGroupChanged((int) msg.arg1,
94                                                                            (int) msg.arg2);
95                             }
96                             break;
97 
98                         default:
99                             break;
100                     }
101                 }
102             };
103             native_setup(new WeakReference<AudioVolumeGroupChangeHandler>(this));
104         }
105     }
106 
native_setup(Object moduleThis)107     private native void native_setup(Object moduleThis);
108 
109     @Override
finalize()110     protected void finalize() {
111         native_finalize();
112         if (mHandlerThread.isAlive()) {
113             mHandlerThread.quit();
114         }
115     }
native_finalize()116     private native void native_finalize();
117 
118    /**
119     * @param cb the {@link AudioManager.VolumeGroupCallback} to register
120     */
registerListener(@onNull AudioManager.VolumeGroupCallback cb)121     public void registerListener(@NonNull AudioManager.VolumeGroupCallback cb) {
122         Preconditions.checkNotNull(cb, "volume group callback shall not be null");
123         synchronized (this) {
124             mListeners.add(cb);
125         }
126         if (mHandler != null) {
127             Message m = mHandler.obtainMessage(
128                     AUDIOVOLUMEGROUP_EVENT_NEW_LISTENER, 0, 0, cb);
129             mHandler.sendMessage(m);
130         }
131     }
132 
133    /**
134     * @param cb the {@link AudioManager.VolumeGroupCallback} to unregister
135     */
unregisterListener(@onNull AudioManager.VolumeGroupCallback cb)136     public void unregisterListener(@NonNull AudioManager.VolumeGroupCallback cb) {
137         Preconditions.checkNotNull(cb, "volume group callback shall not be null");
138         synchronized (this) {
139             mListeners.remove(cb);
140         }
141     }
142 
handler()143     Handler handler() {
144         return mHandler;
145     }
146 
147     @SuppressWarnings("unused")
postEventFromNative(Object moduleRef, int what, int arg1, int arg2, Object obj)148     private static void postEventFromNative(Object moduleRef,
149                                             int what, int arg1, int arg2, Object obj) {
150         AudioVolumeGroupChangeHandler eventHandler =
151                 (AudioVolumeGroupChangeHandler) ((WeakReference) moduleRef).get();
152         if (eventHandler == null) {
153             return;
154         }
155 
156         if (eventHandler != null) {
157             Handler handler = eventHandler.handler();
158             if (handler != null) {
159                 Message m = handler.obtainMessage(what, arg1, arg2, obj);
160                 // Do not remove previous messages, as we would lose notification of group changes
161                 handler.sendMessage(m);
162             }
163         }
164     }
165 }
166