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

Commit 82a1c80a authored by Iván Budnik's avatar Iván Budnik Committed by Android (Google) Code Review
Browse files

Merge "Remove MediaManager and push down callbacks to InfoMediaManager" into main

parents f5f650fe 597d5709
Loading
Loading
Loading
Loading
+80 −17
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ import static android.media.MediaRoute2Info.TYPE_WIRED_HEADSET;
import static com.android.settingslib.media.LocalMediaManager.MediaDeviceState.STATE_SELECTED;

import android.annotation.TargetApi;
import android.app.Notification;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
@@ -67,6 +66,7 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.media.flags.Flags;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
@@ -81,11 +81,43 @@ import java.util.stream.Stream;

/** InfoMediaManager provide interface to get InfoMediaDevice list. */
@RequiresApi(Build.VERSION_CODES.R)
public abstract class InfoMediaManager extends MediaManager {
public abstract class InfoMediaManager {
    /** Callback for notifying device is added, removed and attributes changed. */
    public interface MediaDeviceCallback {

    private static final String TAG = "InfoMediaManager";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
    protected final List<MediaDevice> mMediaDevices = new CopyOnWriteArrayList<>();
        /**
         * Callback for notifying MediaDevice list is added.
         *
         * @param devices the MediaDevice list
         */
        void onDeviceListAdded(@NonNull List<MediaDevice> devices);

        /**
         * Callback for notifying MediaDevice list is removed.
         *
         * @param devices the MediaDevice list
         */
        void onDeviceListRemoved(@NonNull List<MediaDevice> devices);

        /**
         * Callback for notifying connected MediaDevice is changed.
         *
         * @param id the id of MediaDevice
         */
        void onConnectedDeviceChanged(@Nullable String id);

        /**
         * Callback for notifying that transferring is failed.
         *
         * @param reason the reason that the request has failed. Can be one of followings: {@link
         *     android.media.MediaRoute2ProviderService#REASON_UNKNOWN_ERROR}, {@link
         *     android.media.MediaRoute2ProviderService#REASON_REJECTED}, {@link
         *     android.media.MediaRoute2ProviderService#REASON_NETWORK_ERROR}, {@link
         *     android.media.MediaRoute2ProviderService#REASON_ROUTE_NOT_AVAILABLE}, {@link
         *     android.media.MediaRoute2ProviderService#REASON_INVALID_COMMAND},
         */
        void onRequestFailed(int reason);
    }

    /** Checked exception that signals the specified package is not present in the system. */
    public static class PackageNotAvailableException extends Exception {
@@ -94,19 +126,22 @@ public abstract class InfoMediaManager extends MediaManager {
        }
    }

    private static final String TAG = "InfoMediaManager";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
    protected final List<MediaDevice> mMediaDevices = new CopyOnWriteArrayList<>();
    @NonNull protected final Context mContext;
    @NonNull protected final String mPackageName;
    private final Collection<MediaDeviceCallback> mCallbacks = new CopyOnWriteArrayList<>();
    private MediaDevice mCurrentConnectedDevice;
    private final LocalBluetoothManager mBluetoothManager;
    private final Map<String, RouteListingPreference.Item> mPreferenceItemMap =
            new ConcurrentHashMap<>();

    /* package */ InfoMediaManager(
            Context context,
            @NonNull Context context,
            @NonNull String packageName,
            Notification notification,
            LocalBluetoothManager localBluetoothManager) {
        super(context, notification);

            @NonNull LocalBluetoothManager localBluetoothManager) {
        mContext = context;
        mBluetoothManager = localBluetoothManager;
        mPackageName = packageName;
    }
@@ -115,7 +150,6 @@ public abstract class InfoMediaManager extends MediaManager {
    public static InfoMediaManager createInstance(
            Context context,
            @Nullable String packageName,
            Notification notification,
            LocalBluetoothManager localBluetoothManager) {

        // The caller is only interested in system routes (headsets, built-in speakers, etc), and is
@@ -127,17 +161,14 @@ public abstract class InfoMediaManager extends MediaManager {

        if (Flags.useMediaRouter2ForInfoMediaManager()) {
            try {
                return new RouterInfoMediaManager(
                        context, packageName, notification, localBluetoothManager);
                return new RouterInfoMediaManager(context, packageName, localBluetoothManager);
            } catch (PackageNotAvailableException ex) {
                // TODO: b/293578081 - Propagate this exception to callers for proper handling.
                Log.w(TAG, "Returning a no-op InfoMediaManager for package " + packageName);
                return new NoOpInfoMediaManager(
                        context, packageName, notification, localBluetoothManager);
                return new NoOpInfoMediaManager(context, packageName, localBluetoothManager);
            }
        } else {
            return new ManagerInfoMediaManager(
                    context, packageName, notification, localBluetoothManager);
            return new ManagerInfoMediaManager(context, packageName, localBluetoothManager);
        }
    }

@@ -239,6 +270,38 @@ public abstract class InfoMediaManager extends MediaManager {
        return null;
    }

    protected final void registerCallback(MediaDeviceCallback callback) {
        if (!mCallbacks.contains(callback)) {
            mCallbacks.add(callback);
        }
    }

    protected final void unregisterCallback(MediaDeviceCallback callback) {
        mCallbacks.remove(callback);
    }

    private void dispatchDeviceListAdded(@NonNull List<MediaDevice> devices) {
        for (MediaDeviceCallback callback : getCallbacks()) {
            callback.onDeviceListAdded(new ArrayList<>(devices));
        }
    }

    private void dispatchConnectedDeviceChanged(String id) {
        for (MediaDeviceCallback callback : getCallbacks()) {
            callback.onConnectedDeviceChanged(id);
        }
    }

    protected void dispatchOnRequestFailed(int reason) {
        for (MediaDeviceCallback callback : getCallbacks()) {
            callback.onRequestFailed(reason);
        }
    }

    private Collection<MediaDeviceCallback> getCallbacks() {
        return new CopyOnWriteArrayList<>(mCallbacks);
    }

    /**
     * Get current device that played media.
     * @return MediaDevice
+4 −5
Original line number Diff line number Diff line
@@ -138,8 +138,7 @@ public class LocalMediaManager implements BluetoothCallback {
        }

        mInfoMediaManager =
                InfoMediaManager.createInstance(
                        context, packageName, notification, mLocalBluetoothManager);
                InfoMediaManager.createInstance(context, packageName, mLocalBluetoothManager);
    }

    /**
@@ -505,9 +504,9 @@ public class LocalMediaManager implements BluetoothCallback {
        return new CopyOnWriteArrayList<>(mCallbacks);
    }

    class MediaDeviceCallback implements MediaManager.MediaDeviceCallback {
    class MediaDeviceCallback implements InfoMediaManager.MediaDeviceCallback {
        @Override
        public void onDeviceListAdded(List<MediaDevice> devices) {
        public void onDeviceListAdded(@NonNull List<MediaDevice> devices) {
            synchronized (mMediaDevicesLock) {
                mMediaDevices.clear();
                mMediaDevices.addAll(devices);
@@ -637,7 +636,7 @@ public class LocalMediaManager implements BluetoothCallback {
        }

        @Override
        public void onDeviceListRemoved(List<MediaDevice> devices) {
        public void onDeviceListRemoved(@NonNull List<MediaDevice> devices) {
            synchronized (mMediaDevicesLock) {
                mMediaDevices.removeAll(devices);
            }
+1 −3
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.settingslib.media;

import android.app.Notification;
import android.content.Context;
import android.media.MediaRoute2Info;
import android.media.MediaRouter2Manager;
@@ -54,9 +53,8 @@ public class ManagerInfoMediaManager extends InfoMediaManager {
    /* package */ ManagerInfoMediaManager(
            Context context,
            @NonNull String packageName,
            Notification notification,
            LocalBluetoothManager localBluetoothManager) {
        super(context, packageName, notification, localBluetoothManager);
        super(context, packageName, localBluetoothManager);

        mRouterManager = MediaRouter2Manager.getInstance(context);
    }
+0 −120
Original line number Diff line number Diff line
/*
 * Copyright 2018 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 com.android.settingslib.media;

import android.annotation.NonNull;
import android.app.Notification;
import android.content.Context;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * MediaManager provide interface to get MediaDevice list.
 */
public abstract class MediaManager {

    protected final Collection<MediaDeviceCallback> mCallbacks = new CopyOnWriteArrayList<>();

    protected Context mContext;
    protected Notification mNotification;

    MediaManager(Context context, Notification notification) {
        mContext = context;
        mNotification = notification;
    }

    protected void registerCallback(MediaDeviceCallback callback) {
        if (!mCallbacks.contains(callback)) {
            mCallbacks.add(callback);
        }
    }

    protected void unregisterCallback(MediaDeviceCallback callback) {
        if (mCallbacks.contains(callback)) {
            mCallbacks.remove(callback);
        }
    }

    protected void dispatchDeviceListAdded(@NonNull List<MediaDevice> devices) {
        for (MediaDeviceCallback callback : getCallbacks()) {
            callback.onDeviceListAdded(new ArrayList<>(devices));
        }
    }

    protected void dispatchDeviceListRemoved(List<MediaDevice> devices) {
        for (MediaDeviceCallback callback : getCallbacks()) {
            callback.onDeviceListRemoved(devices);
        }
    }

    protected void dispatchConnectedDeviceChanged(String id) {
        for (MediaDeviceCallback callback : getCallbacks()) {
            callback.onConnectedDeviceChanged(id);
        }
    }

    protected void dispatchOnRequestFailed(int reason) {
        for (MediaDeviceCallback callback : getCallbacks()) {
            callback.onRequestFailed(reason);
        }
    }

    private Collection<MediaDeviceCallback> getCallbacks() {
        return new CopyOnWriteArrayList<>(mCallbacks);
    }

    /**
     * Callback for notifying device is added, removed and attributes changed.
     */
    public interface MediaDeviceCallback {

        /**
         * Callback for notifying MediaDevice list is added.
         *
         * @param devices the MediaDevice list
         */
        void onDeviceListAdded(List<MediaDevice> devices);

        /**
         * Callback for notifying MediaDevice list is removed.
         *
         * @param devices the MediaDevice list
         */
        void onDeviceListRemoved(List<MediaDevice> devices);

        /**
         * Callback for notifying connected MediaDevice is changed.
         *
         * @param id the id of MediaDevice
         */
        void onConnectedDeviceChanged(String id);

        /**
         * Callback for notifying that transferring is failed.
         *
         * @param reason the reason that the request has failed. Can be one of followings:
         * {@link android.media.MediaRoute2ProviderService#REASON_UNKNOWN_ERROR},
         * {@link android.media.MediaRoute2ProviderService#REASON_REJECTED},
         * {@link android.media.MediaRoute2ProviderService#REASON_NETWORK_ERROR},
         * {@link android.media.MediaRoute2ProviderService#REASON_ROUTE_NOT_AVAILABLE},
         * {@link android.media.MediaRoute2ProviderService#REASON_INVALID_COMMAND},
         */
        void onRequestFailed(int reason);
    }
}
+1 −3
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.settingslib.media;

import android.app.Notification;
import android.content.Context;
import android.media.MediaRoute2Info;
import android.media.RouteListingPreference;
@@ -42,9 +41,8 @@ import java.util.List;
    NoOpInfoMediaManager(
            Context context,
            @NonNull String packageName,
            Notification notification,
            LocalBluetoothManager localBluetoothManager) {
        super(context, packageName, notification, localBluetoothManager);
        super(context, packageName, localBluetoothManager);
    }

    @Override
Loading