1/*
2 * Copyright (C) 2014 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
17package android.media;
18
19import android.os.Handler;
20import android.os.Looper;
21import android.os.Message;
22import android.util.Log;
23
24import java.util.ArrayList;
25import java.lang.ref.WeakReference;
26
27/**
28 * The AudioPortEventHandler handles AudioManager.OnAudioPortUpdateListener callbacks
29 * posted from JNI
30 * @hide
31 */
32
33class AudioPortEventHandler {
34    private final Handler mHandler;
35    private ArrayList<AudioManager.OnAudioPortUpdateListener> mListeners;
36    private AudioManager mAudioManager;
37
38    private static String TAG = "AudioPortEventHandler";
39
40    private static final int AUDIOPORT_EVENT_PORT_LIST_UPDATED = 1;
41    private static final int AUDIOPORT_EVENT_PATCH_LIST_UPDATED = 2;
42    private static final int AUDIOPORT_EVENT_SERVICE_DIED = 3;
43    private static final int AUDIOPORT_EVENT_NEW_LISTENER = 4;
44
45    AudioPortEventHandler(AudioManager audioManager) {
46        mAudioManager = audioManager;
47        mListeners = new ArrayList<AudioManager.OnAudioPortUpdateListener>();
48
49        // find the looper for our new event handler
50        Looper looper = Looper.getMainLooper();
51
52        if (looper != null) {
53            mHandler = new Handler(looper) {
54                @Override
55                public void handleMessage(Message msg) {
56                    ArrayList<AudioManager.OnAudioPortUpdateListener> listeners;
57                    synchronized (this) {
58                        if (msg.what == AUDIOPORT_EVENT_NEW_LISTENER) {
59                            listeners = new ArrayList<AudioManager.OnAudioPortUpdateListener>();
60                            if (mListeners.contains(msg.obj)) {
61                                listeners.add((AudioManager.OnAudioPortUpdateListener)msg.obj);
62                            }
63                        } else {
64                            listeners = mListeners;
65                        }
66                    }
67                    if (listeners.isEmpty()) {
68                        return;
69                    }
70                    // reset audio port cache if the event corresponds to a change coming
71                    // from audio policy service or if mediaserver process died.
72                    if (msg.what == AUDIOPORT_EVENT_PORT_LIST_UPDATED ||
73                            msg.what == AUDIOPORT_EVENT_PATCH_LIST_UPDATED ||
74                            msg.what == AUDIOPORT_EVENT_SERVICE_DIED) {
75                        mAudioManager.resetAudioPortGeneration();
76                    }
77                    ArrayList<AudioPort> ports = new ArrayList<AudioPort>();
78                    ArrayList<AudioPatch> patches = new ArrayList<AudioPatch>();
79                    if (msg.what != AUDIOPORT_EVENT_SERVICE_DIED) {
80                        int status = mAudioManager.updateAudioPortCache(ports, patches);
81                        if (status != AudioManager.SUCCESS) {
82                            return;
83                        }
84                    }
85
86                    switch (msg.what) {
87                    case AUDIOPORT_EVENT_NEW_LISTENER:
88                    case AUDIOPORT_EVENT_PORT_LIST_UPDATED:
89                        AudioPort[] portList = ports.toArray(new AudioPort[0]);
90                        for (int i = 0; i < listeners.size(); i++) {
91                            listeners.get(i).onAudioPortListUpdate(portList);
92                        }
93                        if (msg.what == AUDIOPORT_EVENT_PORT_LIST_UPDATED) {
94                            break;
95                        }
96                        // FALL THROUGH
97
98                    case AUDIOPORT_EVENT_PATCH_LIST_UPDATED:
99                        AudioPatch[] patchList = patches.toArray(new AudioPatch[0]);
100                        for (int i = 0; i < listeners.size(); i++) {
101                            listeners.get(i).onAudioPatchListUpdate(patchList);
102                        }
103                        break;
104
105                    case AUDIOPORT_EVENT_SERVICE_DIED:
106                        for (int i = 0; i < listeners.size(); i++) {
107                            listeners.get(i).onServiceDied();
108                        }
109                        break;
110
111                    default:
112                        break;
113                    }
114                }
115            };
116        } else {
117            mHandler = null;
118        }
119
120        native_setup(new WeakReference<AudioPortEventHandler>(this));
121    }
122    private native void native_setup(Object module_this);
123
124    @Override
125    protected void finalize() {
126        native_finalize();
127    }
128    private native void native_finalize();
129
130    void registerListener(AudioManager.OnAudioPortUpdateListener l) {
131        synchronized (this) {
132            mListeners.add(l);
133        }
134        if (mHandler != null) {
135            Message m = mHandler.obtainMessage(AUDIOPORT_EVENT_NEW_LISTENER, 0, 0, l);
136            mHandler.sendMessage(m);
137        }
138    }
139
140    void unregisterListener(AudioManager.OnAudioPortUpdateListener l) {
141        synchronized (this) {
142            mListeners.remove(l);
143        }
144    }
145
146    Handler handler() {
147        return mHandler;
148    }
149
150    @SuppressWarnings("unused")
151    private static void postEventFromNative(Object module_ref,
152                                            int what, int arg1, int arg2, Object obj) {
153        AudioPortEventHandler eventHandler =
154                (AudioPortEventHandler)((WeakReference)module_ref).get();
155        if (eventHandler == null) {
156            return;
157        }
158
159        if (eventHandler != null) {
160            Handler handler = eventHandler.handler();
161            if (handler != null) {
162                Message m = handler.obtainMessage(what, arg1, arg2, obj);
163                handler.sendMessage(m);
164            }
165        }
166    }
167
168}
169