Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 54c43f46 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Make native AudioRouting Handler in a seperate class."

parents cf6e63b4 9070e295
Loading
Loading
Loading
Loading
+1 −54
Original line number Diff line number Diff line
@@ -1515,56 +1515,6 @@ public class AudioRecord implements AudioRouting
        removeOnRoutingChangedListener((AudioRouting.OnRoutingChangedListener) listener);
    }

    /**
     * Helper class to handle the forwarding of native events to the appropriate listener
     * (potentially) handled in a different thread
     */
    private class NativeRoutingEventHandlerDelegate {
        private final Handler mHandler;

        NativeRoutingEventHandlerDelegate(final AudioRecord record,
                                   final AudioRouting.OnRoutingChangedListener listener,
                                   Handler handler) {
            // find the looper for our new event handler
            Looper looper;
            if (handler != null) {
                looper = handler.getLooper();
            } else {
                // no given handler, use the looper the AudioRecord was created in
                looper = mInitializationLooper;
            }

            // construct the event handler with this looper
            if (looper != null) {
                // implement the event handler delegate
                mHandler = new Handler(looper) {
                    @Override
                    public void handleMessage(Message msg) {
                        if (record == null) {
                            return;
                        }
                        switch(msg.what) {
                        case AudioSystem.NATIVE_EVENT_ROUTING_CHANGE:
                            if (listener != null) {
                                listener.onRoutingChanged(record);
                            }
                            break;
                        default:
                            loge("Unknown native event type: " + msg.what);
                            break;
                        }
                    }
                };
            } else {
                mHandler = null;
            }
        }

        Handler getHandler() {
            return mHandler;
        }
    }

    /**
     * Sends device list change notification to all listeners.
     */
@@ -1572,10 +1522,7 @@ public class AudioRecord implements AudioRouting
        AudioManager.resetAudioPortGeneration();
        synchronized (mRoutingChangeListeners) {
            for (NativeRoutingEventHandlerDelegate delegate : mRoutingChangeListeners.values()) {
                Handler handler = delegate.getHandler();
                if (handler != null) {
                    handler.sendEmptyMessage(AudioSystem.NATIVE_EVENT_ROUTING_CHANGE);
                }
                delegate.notifyClient();
            }
        }
    }
+1 −54
Original line number Diff line number Diff line
@@ -2856,10 +2856,7 @@ public class AudioTrack extends PlayerBase
        AudioManager.resetAudioPortGeneration();
        synchronized (mRoutingChangeListeners) {
            for (NativeRoutingEventHandlerDelegate delegate : mRoutingChangeListeners.values()) {
                Handler handler = delegate.getHandler();
                if (handler != null) {
                    handler.sendEmptyMessage(AudioSystem.NATIVE_EVENT_ROUTING_CHANGE);
                }
                delegate.notifyClient();
            }
        }
    }
@@ -2943,56 +2940,6 @@ public class AudioTrack extends PlayerBase
        }
    }

    /**
     * Helper class to handle the forwarding of native events to the appropriate listener
     * (potentially) handled in a different thread
     */
    private class NativeRoutingEventHandlerDelegate {
        private final Handler mHandler;

        NativeRoutingEventHandlerDelegate(final AudioTrack track,
                                   final AudioRouting.OnRoutingChangedListener listener,
                                   Handler handler) {
            // find the looper for our new event handler
            Looper looper;
            if (handler != null) {
                looper = handler.getLooper();
            } else {
                // no given handler, use the looper the AudioTrack was created in
                looper = mInitializationLooper;
            }

            // construct the event handler with this looper
            if (looper != null) {
                // implement the event handler delegate
                mHandler = new Handler(looper) {
                    @Override
                    public void handleMessage(Message msg) {
                        if (track == null) {
                            return;
                        }
                        switch(msg.what) {
                        case AudioSystem.NATIVE_EVENT_ROUTING_CHANGE:
                            if (listener != null) {
                                listener.onRoutingChanged(track);
                            }
                            break;
                        default:
                            loge("Unknown native event type: " + msg.what);
                            break;
                        }
                    }
                };
            } else {
                mHandler = null;
            }
        }

        Handler getHandler() {
            return mHandler;
        }
    }

    //---------------------------------------------------------
    // Methods for IPlayer interface
    //--------------------
+2 −31
Original line number Diff line number Diff line
@@ -1514,7 +1514,8 @@ public class MediaPlayer extends PlayerBase
            if (listener != null && !mRoutingChangeListeners.containsKey(listener)) {
                enableNativeRoutingCallbacksLocked(true);
                mRoutingChangeListeners.put(
                        listener, new NativeRoutingEventHandlerDelegate(this, listener, handler));
                        listener, new NativeRoutingEventHandlerDelegate(this, listener,
                                handler != null ? handler : mEventHandler));
            }
        }
    }
@@ -1535,36 +1536,6 @@ public class MediaPlayer extends PlayerBase
        }
    }

    /**
     * Helper class to handle the forwarding of native events to the appropriate listener
     * (potentially) handled in a different thread
     */
    private class NativeRoutingEventHandlerDelegate {
        private MediaPlayer mMediaPlayer;
        private AudioRouting.OnRoutingChangedListener mOnRoutingChangedListener;
        private Handler mHandler;

        NativeRoutingEventHandlerDelegate(final MediaPlayer mediaPlayer,
                final AudioRouting.OnRoutingChangedListener listener, Handler handler) {
            mMediaPlayer = mediaPlayer;
            mOnRoutingChangedListener = listener;
            mHandler = handler != null ? handler : mEventHandler;
        }

        void notifyClient() {
            if (mHandler != null) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (mOnRoutingChangedListener != null) {
                            mOnRoutingChangedListener.onRoutingChanged(mMediaPlayer);
                        }
                    }
                });
            }
        }
    }

    private native final boolean native_setOutputDevice(int deviceId);
    private native final int native_getRoutedDeviceId();
    private native final void native_enableDeviceCallback(boolean enabled);
+51 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.media;

import android.os.Handler;

/**
 * Helper class {@link AudioTrack}, {@link AudioRecord}, {@link MediaPlayer} and {@link MediaRecorder}
 * to handle the forwarding of native events to the appropriate listener
 * (potentially) handled in a different thread.
 * @hide
 */
class NativeRoutingEventHandlerDelegate {
    private AudioRouting mAudioRouting;
    private AudioRouting.OnRoutingChangedListener mOnRoutingChangedListener;
    private Handler mHandler;

    NativeRoutingEventHandlerDelegate(final AudioRouting audioRouting,
            final AudioRouting.OnRoutingChangedListener listener, Handler handler) {
        mAudioRouting = audioRouting;
        mOnRoutingChangedListener = listener;
        mHandler = handler;
    }

    void notifyClient() {
        if (mHandler != null) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (mOnRoutingChangedListener != null) {
                        mOnRoutingChangedListener.onRoutingChanged(mAudioRouting);
                    }
                }
            });
        }
    }
}