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

Commit 0b0c8673 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Fix NPE in MediaConrolProfile"

parents f09826e4 9f1c3446
Loading
Loading
Loading
Loading
+58 −18
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.util.Log;

import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.ProfileService;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import java.util.HashMap;
@@ -41,8 +42,11 @@ public class McpService extends ProfileService {
    private static final String TAG = "BluetoothMcpService";

    private static McpService sMcpService;
    private static MediaControlProfile sGmcsForTesting;

    private static MediaControlProfile sGmcs;
    private Object mLock = new Object();
    @GuardedBy("mLock")
    private MediaControlProfile mGmcs;
    private Map<BluetoothDevice, Integer> mDeviceAuthorizations = new HashMap<>();
    private Handler mHandler = new Handler(Looper.getMainLooper());

@@ -68,11 +72,12 @@ public class McpService extends ProfileService {

    @VisibleForTesting
    public static MediaControlProfile getMediaControlProfile() {
        return sGmcs;
        return sGmcsForTesting;
    }

    @VisibleForTesting
    public static void setMediaControlProfileForTesting(MediaControlProfile mediaControlProfile) {
        sGmcs = mediaControlProfile;
        sGmcsForTesting = mediaControlProfile;
    }

    @Override
@@ -100,11 +105,19 @@ public class McpService extends ProfileService {
        // Mark service as started
        setMcpService(this);

        if (sGmcs == null) {
        synchronized (mLock) {
            if (getGmcsLocked() == null) {
                // Initialize the Media Control Service Server
            sGmcs = new MediaControlProfile(this);
                mGmcs = new MediaControlProfile(this);
                // Requires this service to be already started thus we have to make it an async call
            mHandler.post(() -> sGmcs.init());
                mHandler.post(() -> {
                    synchronized (mLock) {
                        if (mGmcs != null) {
                            mGmcs.init();
                        }
                    }
                });
            }
        }

        return true;
@@ -121,9 +134,17 @@ public class McpService extends ProfileService {
            return true;
        }

        if (sGmcs != null) {
            sGmcs.cleanup();
            sGmcs = null;
        synchronized (mLock) {
            // A runnable for calling mGmcs.init() could be pending on mHandler
            mHandler.removeCallbacksAndMessages(null);
            if (mGmcs != null) {
                mGmcs.cleanup();
                mGmcs = null;
            }
            if (sGmcsForTesting != null) {
                sGmcsForTesting.cleanup();
                sGmcsForTesting = null;
            }
        }

        // Mark service as stopped
@@ -138,6 +159,17 @@ public class McpService extends ProfileService {
        }
    }

    @Override
    public void dump(StringBuilder sb) {
        super.dump(sb);
        synchronized (mLock) {
            MediaControlProfile gmcs = getGmcsLocked();
            if (gmcs != null) {
                gmcs.dump(sb);
            }
        }
    }

    public void onDeviceUnauthorized(BluetoothDevice device) {
        Log.w(TAG, "onDeviceUnauthorized - authorization notification not implemented yet ");
    }
@@ -148,7 +180,12 @@ public class McpService extends ProfileService {
                : BluetoothDevice.ACCESS_REJECTED;
        mDeviceAuthorizations.put(device, authorization);

        sGmcs.onDeviceAuthorizationSet(device);
        synchronized (mLock) {
            MediaControlProfile gmcs = getGmcsLocked();
            if (gmcs != null) {
                gmcs.onDeviceAuthorizationSet(device);
            }
        }
    }

    public int getDeviceAuthorization(BluetoothDevice device) {
@@ -157,6 +194,15 @@ public class McpService extends ProfileService {
        return mDeviceAuthorizations.getOrDefault(device, BluetoothDevice.ACCESS_UNKNOWN);
    }

    @GuardedBy("mLock")
    private MediaControlProfile getGmcsLocked() {
        if (sGmcsForTesting != null) {
            return sGmcsForTesting;
        } else {
            return mGmcs;
        }
    }

    /**
     * Binder object: must be a static class or memory leak may occur
     */
@@ -195,10 +241,4 @@ public class McpService extends ProfileService {
            mService = null;
        }
    }

    @Override
    public void dump(StringBuilder sb) {
        super.dump(sb);
        sGmcs.dump(sb);
    }
}