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

Commit 80abaada authored by jiabin's avatar jiabin
Browse files

Add try-finally block after clearing calling identity.

After calling Binder.clearCallingIdentity, a try-finally block is
recommended to run Binder.restoreCallingIdentity so that it is sure that
the caller's identity is restored properly.

Bug: 189818518
Test: make
Change-Id: Ia99fabbeeb847d002edab6701dadc88096ebe265
parent 00669537
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -1718,9 +1718,10 @@ import java.util.concurrent.atomic.AtomicBoolean;
                mBrokerEventWakeLock.acquire(BROKER_WAKELOCK_TIMEOUT_MS);
            } catch (Exception e) {
                Log.e(TAG, "Exception acquiring wakelock", e);
            }
            } finally {
                Binder.restoreCallingIdentity(identity);
            }
        }

        if (MESSAGES_MUTE_MUSIC.contains(msg)) {
            checkMessagesMuteMusic(msg);
+109 −63
Original line number Diff line number Diff line
@@ -2856,10 +2856,14 @@ public class AudioService extends IAudioService.Stub
        super.getPreferredDevicesForStrategy_enforcePermission();
        List<AudioDeviceAttributes> devices = new ArrayList<>();
        int status = AudioSystem.SUCCESS;
        final long identity = Binder.clearCallingIdentity();
        final int status = AudioSystem.getDevicesForRoleAndStrategy(
        try {
            status = AudioSystem.getDevicesForRoleAndStrategy(
                    strategy, AudioSystem.DEVICE_ROLE_PREFERRED, devices);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
        if (status != AudioSystem.SUCCESS) {
            Log.e(TAG, String.format("Error %d in getPreferredDeviceForStrategy(%d)",
                    status, strategy));
@@ -3045,10 +3049,14 @@ public class AudioService extends IAudioService.Stub
        super.getPreferredDevicesForCapturePreset_enforcePermission();
        List<AudioDeviceAttributes> devices = new ArrayList<>();
        int status = AudioSystem.SUCCESS;
        final long identity = Binder.clearCallingIdentity();
        final int status = AudioSystem.getDevicesForRoleAndCapturePreset(
        try {
            status = AudioSystem.getDevicesForRoleAndCapturePreset(
                    capturePreset, AudioSystem.DEVICE_ROLE_PREFERRED, devices);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
        if (status != AudioSystem.SUCCESS) {
            Log.e(TAG, String.format("Error %d in getPreferredDeviceForCapturePreset(%d)",
                    status, capturePreset));
@@ -3653,12 +3661,15 @@ public class AudioService extends IAudioService.Stub
        }
        final long identity = Binder.clearCallingIdentity();
        try {
            mHdmiAudioSystemClient.sendReportAudioStatusCecCommand(
                    isMuteAdjust, getStreamVolume(AudioSystem.STREAM_MUSIC),
                    getStreamMaxVolume(AudioSystem.STREAM_MUSIC),
                    isStreamMute(AudioSystem.STREAM_MUSIC));
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }
    private int getNewRingerMode(int stream, int index, int flags) {
        // setRingerMode does nothing if the device is single volume,so the value would be unchanged
@@ -4985,6 +4996,7 @@ public class AudioService extends IAudioService.Stub
        if (getCurrentUserId() == userId || userId == android.os.Process.SYSTEM_UID) {
            final boolean currentMute = mAudioSystem.isMicrophoneMuted();
            final long identity = Binder.clearCallingIdentity();
            try {
                final int ret = mAudioSystem.muteMicrophone(muted);
                // update cache with the real state independently from what was set
@@ -5004,7 +5016,6 @@ public class AudioService extends IAudioService.Stub
                        .set(MediaMetrics.Property.STATUS, ret)
                        .record();
            try {
                // send the intent even if there was a failure to change the actual mute state:
                // the AudioManager.setMicrophoneMute API doesn't have a return value to
                // indicate if the call failed to successfully change the mute state, and receiving
@@ -5614,9 +5625,13 @@ public class AudioService extends IAudioService.Stub
                    + mMode.get() + " requested mode: " + requestedMode);
        }
        if (mode != mMode.get() || force) {
            int status = AudioSystem.SUCCESS;
            final long identity = Binder.clearCallingIdentity();
            int status = mAudioSystem.setPhoneState(mode, uid);
            try {
                status = mAudioSystem.setPhoneState(mode, uid);
            } finally {
                Binder.restoreCallingIdentity(identity);
            }
            if (status == AudioSystem.AUDIO_STATUS_OK) {
                if (DEBUG_MODE) {
                    Log.v(TAG, "onUpdateAudioMode: mode successfully set to " + mode);
@@ -6023,17 +6038,22 @@ public class AudioService extends IAudioService.Stub
        }
        final long ident = Binder.clearCallingIdentity();
        boolean status =
                mDeviceBroker.setCommunicationDevice(cb, pid, device, eventSource);
        try {
            return mDeviceBroker.setCommunicationDevice(cb, pid, device, eventSource);
        } finally {
            Binder.restoreCallingIdentity(ident);
        return status;
        }
    }
    /** @see AudioManager#getCommunicationDevice() */
    public int getCommunicationDevice() {
        AudioDeviceInfo device = null;
        final long ident = Binder.clearCallingIdentity();
        AudioDeviceInfo device = mDeviceBroker.getCommunicationDevice();
        try {
            device = mDeviceBroker.getCommunicationDevice();
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
        if (device == null) {
            return 0;
        }
@@ -6083,9 +6103,12 @@ public class AudioService extends IAudioService.Stub
                        ? MediaMetrics.Value.ON : MediaMetrics.Value.OFF)
                .record();
        final long ident = Binder.clearCallingIdentity();
        try {
            mDeviceBroker.setSpeakerphoneOn(cb, pid, on, eventSource);
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }
    /** @see AudioManager#isSpeakerphoneOn() */
    public boolean isSpeakerphoneOn() {
@@ -6224,8 +6247,11 @@ public class AudioService extends IAudioService.Stub
            return;
        }
        final long ident = Binder.clearCallingIdentity();
        try {
            mDeviceBroker.startBluetoothScoForClient(cb, pid, scoAudioMode, eventSource);
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
        mmi.record();
    }
@@ -6241,8 +6267,11 @@ public class AudioService extends IAudioService.Stub
                .append(") from u/pid:").append(uid).append("/")
                .append(pid).toString();
        final long ident = Binder.clearCallingIdentity();
        try {
            mDeviceBroker.stopBluetoothScoForClient(cb, pid, eventSource);
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
        new MediaMetrics.Item(MediaMetrics.Name.AUDIO_BLUETOOTH)
                .setUid(uid)
                .setPid(pid)
@@ -6531,8 +6560,11 @@ public class AudioService extends IAudioService.Stub
                (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
        final long ident = Binder.clearCallingIdentity();
        try {
            IsInCall = telecomManager.isInCall();
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
        int mode = mMode.get();
        return (IsInCall
@@ -6672,10 +6704,13 @@ public class AudioService extends IAudioService.Stub
    private void queueMsgUnderWakeLock(Handler handler, int msg,
            int arg1, int arg2, Object obj, int delay) {
        final long ident = Binder.clearCallingIdentity();
        try {
            // Always acquire the wake lock as AudioService because it is released by the
            // message handler.
            mAudioEventWakeLock.acquire();
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
        sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay);
    }
@@ -11694,14 +11729,17 @@ public class AudioService extends IAudioService.Stub
        int callingUid = Binder.getCallingUid();
        int flags = AudioAttributes.capturePolicyToFlags(capturePolicy, 0x0);
        final long identity = Binder.clearCallingIdentity();
        try {
            synchronized (mPlaybackMonitor) {
                int result = mAudioSystem.setAllowedCapturePolicy(callingUid, flags);
                if (result == AudioSystem.AUDIO_STATUS_OK) {
                    mPlaybackMonitor.setAllowedCapturePolicy(callingUid, capturePolicy);
                }
            Binder.restoreCallingIdentity(identity);
                return result;
            }
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }
    /**
@@ -11711,9 +11749,11 @@ public class AudioService extends IAudioService.Stub
    public int getAllowedCapturePolicy() {
        int callingUid = Binder.getCallingUid();
        final long identity = Binder.clearCallingIdentity();
        int capturePolicy = mPlaybackMonitor.getAllowedCapturePolicy(callingUid);
        try {
            return mPlaybackMonitor.getAllowedCapturePolicy(callingUid);
        } finally {
            Binder.restoreCallingIdentity(identity);
        return capturePolicy;
        }
    }
    //======================
@@ -11855,8 +11895,11 @@ public class AudioService extends IAudioService.Stub
                }
            }
            final long identity = Binder.clearCallingIdentity();
            try {
                mAudioSystem.registerPolicyMixes(mMixes, false);
            } finally {
                Binder.restoreCallingIdentity(identity);
            }
            synchronized (mAudioPolicies) {
                mAudioPolicies.remove(mPolicyCallback.asBinder());
            }
@@ -11915,9 +11958,12 @@ public class AudioService extends IAudioService.Stub
        @AudioSystem.AudioSystemError int connectMixes() {
            final long identity = Binder.clearCallingIdentity();
            int status = mAudioSystem.registerPolicyMixes(mMixes, true);
            try {
                return mAudioSystem.registerPolicyMixes(mMixes, true);
            } finally {
                Binder.restoreCallingIdentity(identity);
            return status;
            }
        }
        int setUidDeviceAffinities(int uid, @NonNull int[] types, @NonNull String[] addresses) {