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

Commit 70e0c58c authored by Kenny Guy's avatar Kenny Guy
Browse files

Mute correct user from device policy manager.

Add per user versions of mute methods so
device policy manager can mute the correct
user.
Just persist change if the calling user
isn't the current user.
Treat calls to audio manager coming from uid
1000 as if they were coming from current user
rather than user 0 so that the correct user's
user restriction is checked.

Bug: 21782066
Bug: 21778905
Change-Id: I51469b741096d8a2ffdc520eaf5b3fd754f2c819
parent 7fe86c47
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.os.Process;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.ArrayMap;
import android.util.Log;
@@ -872,7 +873,8 @@ public class AudioManager {
    public void setMasterMute(boolean mute, int flags) {
        IAudioService service = getService();
        try {
            service.setMasterMute(mute, flags, getContext().getOpPackageName());
            service.setMasterMute(mute, flags, getContext().getOpPackageName(),
                    UserHandle.getCallingUserId());
        } catch (RemoteException e) {
            Log.e(TAG, "Dead object in setMasterMute", e);
        }
@@ -1554,7 +1556,8 @@ public class AudioManager {
    public void setMicrophoneMute(boolean on) {
        IAudioService service = getService();
        try {
            service.setMicrophoneMute(on, getContext().getOpPackageName());
            service.setMicrophoneMute(on, getContext().getOpPackageName(),
                    UserHandle.getCallingUserId());
        } catch (RemoteException e) {
            Log.e(TAG, "Dead object in setMicrophoneMute", e);
        }
+2 −2
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ interface IAudioService {

    boolean isMasterMute();

    void setMasterMute(boolean mute, int flags, String callingPackage);
    void setMasterMute(boolean mute, int flags, String callingPackage, int userId);

    int getStreamVolume(int streamType);

@@ -65,7 +65,7 @@ interface IAudioService {

    int getLastAudibleStreamVolume(int streamType);

    void setMicrophoneMute(boolean on, String callingPackage);
    void setMicrophoneMute(boolean on, String callingPackage, int userId);

    void setRingerModeExternal(int ringerMode, String caller);

+78 −20
Original line number Diff line number Diff line
@@ -1166,6 +1166,11 @@ public class AudioService extends IAudioService.Stub {
            return;
        }

        // If we are being called by the system (e.g. hardware keys) check for current user
        // so we handle user restrictions correctly.
        if (uid == android.os.Process.SYSTEM_UID) {
            uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid));
        }
        if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage)
                != AppOpsManager.MODE_ALLOWED) {
            return;
@@ -1412,7 +1417,11 @@ public class AudioService extends IAudioService.Stub {
            (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) {
            return;
        }

        // If we are being called by the system (e.g. hardware keys) check for current user
        // so we handle user restrictions correctly.
        if (uid == android.os.Process.SYSTEM_UID) {
            uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid));
        }
        if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage)
                != AppOpsManager.MODE_ALLOWED) {
            return;
@@ -1540,6 +1549,19 @@ public class AudioService extends IAudioService.Stub {
        }
    }

    private int getCurrentUserId() {
        final long ident = Binder.clearCallingIdentity();
        try {
            UserInfo currentUser = ActivityManagerNative.getDefault().getCurrentUser();
            return currentUser.id;
        } catch (RemoteException e) {
            // Activity manager not running, nothing we can do assume user 0.
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
        return UserHandle.USER_OWNER;
    }

    // UI update and Broadcast Intent
    private void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) {
        streamType = mStreamVolumeAlias[streamType];
@@ -1733,23 +1755,42 @@ public class AudioService extends IAudioService.Stub {
        }
    }

    private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid) {
    private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid,
            int userId) {
        // If we are being called by the system check for user we are going to change
        // so we handle user restrictions correctly.
        if (uid == android.os.Process.SYSTEM_UID) {
            uid = UserHandle.getUid(userId, UserHandle.getAppId(uid));
        }
        if (mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage)
                != AppOpsManager.MODE_ALLOWED) {
            return;
        }
        if (userId != UserHandle.getCallingUserId() &&
                mContext.checkCallingOrSelfPermission(
                        android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
                != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if (getCurrentUserId() == userId) {
            if (mute != AudioSystem.getMasterMute()) {
                setSystemAudioMute(mute);
                AudioSystem.setMasterMute(mute);
                // Post a persist master volume msg
                sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME_MUTE, SENDMSG_REPLACE, mute ? 1
                    : 0, UserHandle.getCallingUserId(), null, PERSIST_DELAY);
                        : 0, userId, null, PERSIST_DELAY);
                sendMasterMuteUpdate(mute, flags);

                Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION);
                intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, mute);
                sendBroadcastToAll(intent);
            }
        } else {
            // If not the current user just persist the setting which will be loaded
            // on user switch.
            sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME_MUTE, SENDMSG_REPLACE, mute ? 1
                    : 0, userId, null, PERSIST_DELAY);
        }
    }

    /** get master mute state. */
@@ -1757,8 +1798,9 @@ public class AudioService extends IAudioService.Stub {
        return AudioSystem.getMasterMute();
    }

    public void setMasterMute(boolean mute, int flags, String callingPackage) {
        setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid());
    public void setMasterMute(boolean mute, int flags, String callingPackage, int userId) {
        setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid(),
                userId);
    }

    /** @see AudioManager#getStreamVolume(int) */
@@ -1804,20 +1846,36 @@ public class AudioService extends IAudioService.Stub {
        return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM];
    }

    /** @see AudioManager#setMicrophoneMute(boolean) */
    public void setMicrophoneMute(boolean on, String callingPackage) {
        if (mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, Binder.getCallingUid(),
                callingPackage) != AppOpsManager.MODE_ALLOWED) {
    /** @see AudioManager#setMicrophoneMute(boolean, int) */
    public void setMicrophoneMute(boolean on, String callingPackage, int userId) {
        // If we are being called by the system check for user we are going to change
        // so we handle user restrictions correctly.
        int uid = Binder.getCallingUid();
        if (uid == android.os.Process.SYSTEM_UID) {
            uid = UserHandle.getUid(userId, UserHandle.getAppId(uid));
        }
        if (mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, uid, callingPackage)
                != AppOpsManager.MODE_ALLOWED) {
            return;
        }
        if (!checkAudioSettingsPermission("setMicrophoneMute()")) {
            return;
        }
        if (userId != UserHandle.getCallingUserId() &&
                mContext.checkCallingOrSelfPermission(
                android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
                != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        // If mute is for current user actually mute, else just persist the setting
        // which will be loaded on user switch.
        if (getCurrentUserId() == userId) {
            AudioSystem.muteMicrophone(on);
        }
        // Post a persist microphone msg.
        sendMsg(mAudioHandler, MSG_PERSIST_MICROPHONE_MUTE, SENDMSG_REPLACE, on ? 1
                : 0, UserHandle.getCallingUserId(), null, PERSIST_DELAY);
                : 0, userId, null, PERSIST_DELAY);
    }

    @Override
+29 −12
Original line number Diff line number Diff line
@@ -4407,15 +4407,25 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {


    private void clearUserRestrictions(UserHandle userHandle) {
        AudioManager audioManager =
                (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        Bundle userRestrictions = mUserManager.getUserRestrictions();
        mUserManager.setUserRestrictions(new Bundle(), userHandle);
        IAudioService iAudioService = IAudioService.Stub.asInterface(
                ServiceManager.getService(Context.AUDIO_SERVICE));
        if (userRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME)) {
            audioManager.setMasterMute(false, 0);
            try {
                iAudioService.setMasterMute(true, 0, mContext.getPackageName(),
                        userHandle.getIdentifier());
            } catch (RemoteException e) {
                // Not much we can do here.
            }
        }
        if (userRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE)) {
            audioManager.setMicrophoneMute(false);
            try {
                iAudioService.setMicrophoneMute(true, mContext.getPackageName(),
                        userHandle.getIdentifier());
            } catch (RemoteException e) {
                // Not much we can do here.
            }
        }
    }

@@ -5426,9 +5436,11 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
            try {
                if (enabled && !alreadyRestricted) {
                    if (UserManager.DISALLOW_UNMUTE_MICROPHONE.equals(key)) {
                        iAudioService.setMicrophoneMute(true, mContext.getPackageName());
                        iAudioService.setMicrophoneMute(true, mContext.getPackageName(),
                                userHandle);
                    } else if (UserManager.DISALLOW_ADJUST_VOLUME.equals(key)) {
                        iAudioService.setMasterMute(true, 0, mContext.getPackageName());
                        iAudioService.setMasterMute(true, 0, mContext.getPackageName(),
                                userHandle);
                    }
                    if (UserManager.DISALLOW_CONFIG_WIFI.equals(key)) {
                        Settings.Secure.putIntForUser(mContext.getContentResolver(),
@@ -5480,9 +5492,11 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                }
                if (!enabled && alreadyRestricted) {
                    if (UserManager.DISALLOW_UNMUTE_MICROPHONE.equals(key)) {
                        iAudioService.setMicrophoneMute(false, mContext.getPackageName());
                        iAudioService.setMicrophoneMute(false, mContext.getPackageName(),
                                userHandle);
                    } else if (UserManager.DISALLOW_ADJUST_VOLUME.equals(key)) {
                        iAudioService.setMasterMute(false, 0, mContext.getPackageName());
                        iAudioService.setMasterMute(false, 0, mContext.getPackageName(),
                                userHandle);
                    }
                }
            } catch (RemoteException re) {
@@ -6031,13 +6045,16 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        Preconditions.checkNotNull(who, "ComponentName is null");
        synchronized (this) {
            getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER);

            int userId = UserHandle.getCallingUserId();
            long identity = Binder.clearCallingIdentity();
            try {
                IAudioService iAudioService = IAudioService.Stub.asInterface(
                        ServiceManager.getService(Context.AUDIO_SERVICE));
            try {
                iAudioService.setMasterMute(on, 0, who.getPackageName());
                iAudioService.setMasterMute(on, 0, mContext.getPackageName(), userId);
            } catch (RemoteException re) {
                Slog.e(LOG_TAG, "Failed to setMasterMute", re);
            } finally {
                Binder.restoreCallingIdentity(identity);
            }
        }
    }