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 = (ArrayList<AudioManager.VolumeGroupCallback>) 84 mListeners.clone(); 85 } 86 } 87 if (listeners.isEmpty()) { 88 return; 89 } 90 91 switch (msg.what) { 92 case AUDIOVOLUMEGROUP_EVENT_VOLUME_CHANGED: 93 for (int i = 0; i < listeners.size(); i++) { 94 listeners.get(i).onAudioVolumeGroupChanged((int) msg.arg1, 95 (int) msg.arg2); 96 } 97 break; 98 99 default: 100 break; 101 } 102 } 103 }; 104 native_setup(new WeakReference<AudioVolumeGroupChangeHandler>(this)); 105 } 106 } 107 native_setup(Object moduleThis)108 private native void native_setup(Object moduleThis); 109 110 @Override finalize()111 protected void finalize() { 112 native_finalize(); 113 if (mHandlerThread.isAlive()) { 114 mHandlerThread.quit(); 115 } 116 } native_finalize()117 private native void native_finalize(); 118 119 /** 120 * @param cb the {@link AudioManager.VolumeGroupCallback} to register 121 */ registerListener(@onNull AudioManager.VolumeGroupCallback cb)122 public void registerListener(@NonNull AudioManager.VolumeGroupCallback cb) { 123 Preconditions.checkNotNull(cb, "volume group callback shall not be null"); 124 synchronized (this) { 125 mListeners.add(cb); 126 } 127 if (mHandler != null) { 128 Message m = mHandler.obtainMessage( 129 AUDIOVOLUMEGROUP_EVENT_NEW_LISTENER, 0, 0, cb); 130 mHandler.sendMessage(m); 131 } 132 } 133 134 /** 135 * @param cb the {@link AudioManager.VolumeGroupCallback} to unregister 136 */ unregisterListener(@onNull AudioManager.VolumeGroupCallback cb)137 public void unregisterListener(@NonNull AudioManager.VolumeGroupCallback cb) { 138 Preconditions.checkNotNull(cb, "volume group callback shall not be null"); 139 synchronized (this) { 140 mListeners.remove(cb); 141 } 142 } 143 handler()144 Handler handler() { 145 return mHandler; 146 } 147 148 @SuppressWarnings("unused") postEventFromNative(Object moduleRef, int what, int arg1, int arg2, Object obj)149 private static void postEventFromNative(Object moduleRef, 150 int what, int arg1, int arg2, Object obj) { 151 AudioVolumeGroupChangeHandler eventHandler = 152 (AudioVolumeGroupChangeHandler) ((WeakReference) moduleRef).get(); 153 if (eventHandler == null) { 154 return; 155 } 156 157 if (eventHandler != null) { 158 Handler handler = eventHandler.handler(); 159 if (handler != null) { 160 Message m = handler.obtainMessage(what, arg1, arg2, obj); 161 // Do not remove previous messages, as we would lose notification of group changes 162 handler.sendMessage(m); 163 } 164 } 165 } 166 } 167