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

Commit de96a050 authored by Szu-An Lu's avatar Szu-An Lu
Browse files

[MQ] Align sound profile permission control with picture profile

A system app with MANAGE_GLOBAL_SOUND_QUALITY_SERVICE permission
- can update system sound profiles
- can get callbacks regarding to system sound profiles

Flag: EXEMPT bug fix
Bug: 410776219
Test: update default sound profile from another system app
Test: get callback when sound profile changed by settings UI
Change-Id: I06a42c02c5ce8ccf84ed985707f8eca5b4b3df4c
parent 59e1eee0
Loading
Loading
Loading
Loading
+20 −14
Original line number Diff line number Diff line
@@ -1006,7 +1006,7 @@ public class MediaQualityService extends SystemService {
            int callingPid = Binder.getCallingPid();
            mHandler.post(() -> {
                Long dbId = mSoundProfileTempIdMap.getKey(id);
                if (!hasPermissionToUpdateSoundProfile(dbId, sp, callingUid)) {
                if (!hasPermissionToUpdateSoundProfile(dbId, sp, callingUid, callingPid)) {
                    mMqManagerNotifier.notifyOnSoundProfileError(
                            id, SoundProfile.ERROR_NO_PERMISSION, callingUid, callingPid);
                    Slog.e(TAG, "updateSoundProfile: no permission to update sound profile");
@@ -1027,12 +1027,16 @@ public class MediaQualityService extends SystemService {
            });
        }

        private boolean hasPermissionToUpdateSoundProfile(Long dbId, SoundProfile sp, int uid) {
        private boolean hasPermissionToUpdateSoundProfile(
                Long dbId, SoundProfile toUpdate, int uid, int pid) {
            SoundProfile fromDb = mMqDatabaseUtils.getSoundProfile(dbId);
            return fromDb.getProfileType() == sp.getProfileType()
                    && fromDb.getPackageName().equals(sp.getPackageName())
                    && fromDb.getName().equals(sp.getName())
                    && fromDb.getPackageName().equals(getPackageOfUid(uid));
            boolean isPackageOwner = fromDb.getPackageName().equals(getPackageOfUid(uid));
            boolean isSystemAppWithPermission = hasGlobalSoundQualityServicePermission(uid, pid)
                    && fromDb.getProfileType() == PictureProfile.TYPE_SYSTEM;
            return fromDb.getProfileType() == toUpdate.getProfileType()
                    && fromDb.getName().equals(toUpdate.getName())
                    && fromDb.getPackageName().equals(toUpdate.getPackageName())
                    && (isPackageOwner || isSystemAppWithPermission);
        }

        @GuardedBy("mSoundProfileLock")
@@ -1257,13 +1261,6 @@ public class MediaQualityService extends SystemService {
            return incomingPackage.equalsIgnoreCase(getPackageOfUid(uid));
        }

        private boolean hasGlobalSoundQualityServicePermission(int uid, int pid) {
            return mContext.checkPermission(
                           android.Manifest.permission.MANAGE_GLOBAL_SOUND_QUALITY_SERVICE, pid,
                           uid)
                    == PackageManager.PERMISSION_GRANTED;
        }

        private boolean hasReadColorZonesPermission(int uid, int pid) {
            return mContext.checkPermission(android.Manifest.permission.READ_COLOR_ZONES, pid, uid)
                    == PackageManager.PERMISSION_GRANTED;
@@ -2128,7 +2125,10 @@ public class MediaQualityService extends SystemService {
                    Pair<Integer, Integer> pidUid = userState.mSoundProfileCallbackPidUidMap
                            .get(callback);

                    if (pidUid.first == pid && pidUid.second == uid) {
                    if ((pidUid.first == pid && pidUid.second == uid)
                            || (hasGlobalSoundQualityServicePermission(pidUid.second, pidUid.first)
                                    && profile != null
                                    && profile.getProfileType() == PictureProfile.TYPE_SYSTEM)) {
                        if (mode == ProfileModes.ADD) {
                            userState.mSoundProfileCallbacks.getBroadcastItem(i)
                                    .onSoundProfileAdded(profileId, profile);
@@ -2882,6 +2882,12 @@ public class MediaQualityService extends SystemService {
                == PackageManager.PERMISSION_GRANTED;
    }

    private boolean hasGlobalSoundQualityServicePermission(int uid, int pid) {
        return mContext.checkPermission(
                       android.Manifest.permission.MANAGE_GLOBAL_SOUND_QUALITY_SERVICE, pid, uid)
                == PackageManager.PERMISSION_GRANTED;
    }

    private PictureProfile getSdrPictureProfile(String profileName, PictureProfile previous) {
        Log.d(TAG, "getSdrPictureProfile: profileName = " + profileName
                + " previous profile name = " + previous.getName());