Loading services/core/java/com/android/server/media/quality/MediaQualityService.java +286 −196 Original line number Diff line number Diff line Loading @@ -73,6 +73,7 @@ import android.util.Pair; import android.util.Slog; import android.util.SparseArray; import com.android.internal.annotations.GuardedBy; import com.android.server.SystemService; import com.android.server.utils.Slogf; Loading Loading @@ -122,6 +123,13 @@ public class MediaQualityService extends SystemService { private SharedPreferences mPictureProfileSharedPreference; private SharedPreferences mSoundProfileSharedPreference; // A global lock for picture profile objects. private final Object mPictureProfileLock = new Object(); // A global lock for sound profile objects. private final Object mSoundProfileLock = new Object(); // A global lock for ambient backlight objects. private final Object mAmbientBacklightLock = new Object(); public MediaQualityService(Context context) { super(context); mContext = context; Loading Loading @@ -254,6 +262,7 @@ public class MediaQualityService extends SystemService { // TODO: Add additional APIs. b/373951081 private final class BinderService extends IMediaQualityManager.Stub { @GuardedBy("mPictureProfileLock") @Override public PictureProfile createPictureProfile(PictureProfile pp, UserHandle user) { if ((pp.getPackageName() != null && !pp.getPackageName().isEmpty() Loading @@ -263,6 +272,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mPictureProfileLock) { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); ContentValues values = getContentValues(null, Loading @@ -279,9 +289,11 @@ public class MediaQualityService extends SystemService { populateTempIdMap(mPictureProfileTempIdMap, id); String value = mPictureProfileTempIdMap.getValue(id); pp.setProfileId(value); notifyOnPictureProfileAdded(value, pp, Binder.getCallingUid(), Binder.getCallingPid()); notifyOnPictureProfileAdded(value, pp, Binder.getCallingUid(), Binder.getCallingPid()); return pp; } } private void notifyHalOnPictureProfileChange(Long dbId, PersistableBundle params) { // TODO: only notify HAL when the profile is active / being used Loading @@ -307,6 +319,7 @@ public class MediaQualityService extends SystemService { return toReturn; } @GuardedBy("mPictureProfileLock") @Override public void updatePictureProfile(String id, PictureProfile pp, UserHandle user) { Long dbId = mPictureProfileTempIdMap.getKey(id); Loading @@ -315,6 +328,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mPictureProfileLock) { ContentValues values = getContentValues(dbId, pp.getProfileType(), pp.getName(), Loading @@ -329,6 +343,7 @@ public class MediaQualityService extends SystemService { getPictureProfile(dbId), Binder.getCallingUid(), Binder.getCallingPid()); notifyHalOnPictureProfileChange(dbId, pp.getParameters()); } } private boolean hasPermissionToUpdatePictureProfile(Long dbId, PictureProfile toUpdate) { PictureProfile fromDb = getPictureProfile(dbId); Loading @@ -338,8 +353,10 @@ public class MediaQualityService extends SystemService { && fromDb.getName().equals(getPackageOfCallingUid()); } @GuardedBy("mPictureProfileLock") @Override public void removePictureProfile(String id, UserHandle user) { synchronized (mPictureProfileLock) { Long dbId = mPictureProfileTempIdMap.getKey(id); PictureProfile toDelete = getPictureProfile(dbId); Loading @@ -352,8 +369,8 @@ public class MediaQualityService extends SystemService { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); String selection = BaseParameters.PARAMETER_ID + " = ?"; String[] selectionArgs = {Long.toString(dbId)}; int result = db.delete(mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, selection, selectionArgs); int result = db.delete(mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, selection, selectionArgs); if (result == 0) { notifyOnPictureProfileError(id, PictureProfile.ERROR_INVALID_ARGUMENT, Binder.getCallingUid(), Binder.getCallingPid()); Loading @@ -364,6 +381,7 @@ public class MediaQualityService extends SystemService { notifyHalOnPictureProfileChange(dbId, null); } } } private boolean hasPermissionToRemovePictureProfile(PictureProfile toDelete) { if (toDelete != null) { Loading @@ -372,6 +390,7 @@ public class MediaQualityService extends SystemService { return false; } @GuardedBy("mPictureProfileLock") @Override public PictureProfile getPictureProfile(int type, String name, Bundle options, UserHandle user) { Loading @@ -382,18 +401,21 @@ public class MediaQualityService extends SystemService { + BaseParameters.PARAMETER_PACKAGE + " = ?"; String[] selectionArguments = {Integer.toString(type), name, getPackageOfCallingUid()}; synchronized (mPictureProfileLock) { try ( Cursor cursor = getCursorAfterQuerying( mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, getMediaProfileColumns(includeParams), selection, selectionArguments) getMediaProfileColumns(includeParams), selection, selectionArguments) ) { int count = cursor.getCount(); if (count == 0) { return null; } if (count > 1) { Log.wtf(TAG, String.format(Locale.US, "%d entries found for type=%d and name=%s" + " in %s. Should only ever be 0 or 1.", count, type, name, Log.wtf(TAG, TextUtils.formatSimple(String.valueOf(Locale.US), "%d " + "entries found for type=%d and name=%s in %s. Should" + " only ever be 0 or 1.", count, type, name, mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME)); return null; } Loading @@ -401,6 +423,7 @@ public class MediaQualityService extends SystemService { return convertCursorToPictureProfileWithTempId(cursor); } } } private PictureProfile getPictureProfile(Long dbId) { String selection = BaseParameters.PARAMETER_ID + " = ?"; Loading @@ -416,9 +439,9 @@ public class MediaQualityService extends SystemService { return null; } if (count > 1) { Log.wtf(TAG, String.format(Locale.US, "%d entries found for id=%d" + " in %s. Should only ever be 0 or 1.", count, dbId, mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME)); Log.wtf(TAG, TextUtils.formatSimple(String.valueOf(Locale.US), "%d entries " + "found for id=%d in %s. Should only ever be 0 or 1.", count, dbId, mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME)); return null; } cursor.moveToFirst(); Loading @@ -426,6 +449,7 @@ public class MediaQualityService extends SystemService { } } @GuardedBy("mPictureProfileLock") @Override public List<PictureProfile> getPictureProfilesByPackage( String packageName, Bundle options, UserHandle user) { Loading @@ -434,6 +458,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mPictureProfileLock) { boolean includeParams = options.getBoolean(MediaQualityManager.OPTION_INCLUDE_PARAMETERS, false); String selection = BaseParameters.PARAMETER_PACKAGE + " = ?"; Loading @@ -441,7 +466,9 @@ public class MediaQualityService extends SystemService { return getPictureProfilesBasedOnConditions(getMediaProfileColumns(includeParams), selection, selectionArguments); } } @GuardedBy("mPictureProfileLock") @Override public List<PictureProfile> getAvailablePictureProfiles(Bundle options, UserHandle user) { String packageName = getPackageOfCallingUid(); Loading @@ -451,6 +478,7 @@ public class MediaQualityService extends SystemService { return new ArrayList<>(); } @GuardedBy("mPictureProfileLock") @Override public boolean setDefaultPictureProfile(String profileId, UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { Loading Loading @@ -810,6 +838,7 @@ public class MediaQualityService extends SystemService { return (PictureParameter[]) pictureParams.toArray(); } @GuardedBy("mPictureProfileLock") @Override public List<String> getPictureProfilePackageNames(UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { Loading @@ -817,6 +846,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } String [] column = {BaseParameters.PARAMETER_PACKAGE}; synchronized (mPictureProfileLock) { List<PictureProfile> pictureProfiles = getPictureProfilesBasedOnConditions(column, null, null); return pictureProfiles.stream() Loading @@ -824,10 +854,13 @@ public class MediaQualityService extends SystemService { .distinct() .collect(Collectors.toList()); } } @GuardedBy("mPictureProfileLock") @Override public List<PictureProfileHandle> getPictureProfileHandle(String[] ids, UserHandle user) { List<PictureProfileHandle> toReturn = new ArrayList<>(); synchronized (mPictureProfileLock) { for (String id : ids) { Long key = mPictureProfileTempIdMap.getKey(id); if (key != null) { Loading @@ -836,12 +869,15 @@ public class MediaQualityService extends SystemService { toReturn.add(null); } } } return toReturn; } @GuardedBy("mSoundProfileLock") @Override public List<SoundProfileHandle> getSoundProfileHandle(String[] ids, UserHandle user) { List<SoundProfileHandle> toReturn = new ArrayList<>(); synchronized (mSoundProfileLock) { for (String id : ids) { Long key = mSoundProfileTempIdMap.getKey(id); if (key != null) { Loading @@ -850,9 +886,11 @@ public class MediaQualityService extends SystemService { toReturn.add(null); } } } return toReturn; } @GuardedBy("mSoundProfileLock") @Override public SoundProfile createSoundProfile(SoundProfile sp, UserHandle user) { if ((sp.getPackageName() != null && !sp.getPackageName().isEmpty() Loading @@ -861,6 +899,8 @@ public class MediaQualityService extends SystemService { notifyOnSoundProfileError(null, SoundProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mSoundProfileLock) { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); ContentValues values = getContentValues(null, Loading @@ -877,9 +917,11 @@ public class MediaQualityService extends SystemService { populateTempIdMap(mSoundProfileTempIdMap, id); String value = mSoundProfileTempIdMap.getValue(id); sp.setProfileId(value); notifyOnSoundProfileAdded(value, sp, Binder.getCallingUid(), Binder.getCallingPid()); notifyOnSoundProfileAdded(value, sp, Binder.getCallingUid(), Binder.getCallingPid()); return sp; } } private void notifyHalOnSoundProfileChange(Long dbId, PersistableBundle params) { // TODO: only notify HAL when the profile is active / being used Loading @@ -903,6 +945,7 @@ public class MediaQualityService extends SystemService { return toReturn; } @GuardedBy("mSoundProfileLock") @Override public void updateSoundProfile(String id, SoundProfile sp, UserHandle user) { Long dbId = mSoundProfileTempIdMap.getKey(id); Loading @@ -911,6 +954,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mSoundProfileLock) { ContentValues values = getContentValues(dbId, sp.getProfileType(), sp.getName(), Loading @@ -924,6 +968,7 @@ public class MediaQualityService extends SystemService { getSoundProfile(dbId), Binder.getCallingUid(), Binder.getCallingPid()); notifyHalOnSoundProfileChange(dbId, sp.getParameters()); } } private boolean hasPermissionToUpdateSoundProfile(Long dbId, SoundProfile sp) { SoundProfile fromDb = getSoundProfile(dbId); Loading @@ -933,20 +978,22 @@ public class MediaQualityService extends SystemService { && fromDb.getName().equals(getPackageOfCallingUid()); } @GuardedBy("mSoundProfileLock") @Override public void removeSoundProfile(String id, UserHandle user) { synchronized (mSoundProfileLock) { Long dbId = mSoundProfileTempIdMap.getKey(id); SoundProfile toDelete = getSoundProfile(dbId); if (!hasPermissionToRemoveSoundProfile(toDelete)) { notifyOnSoundProfileError(id, SoundProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } if (dbId != null) { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); String selection = BaseParameters.PARAMETER_ID + " = ?"; String[] selectionArgs = {Long.toString(dbId)}; int result = db.delete(mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, selection, int result = db.delete(mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, selection, selectionArgs); if (result == 0) { notifyOnSoundProfileError(id, SoundProfile.ERROR_INVALID_ARGUMENT, Loading @@ -958,6 +1005,7 @@ public class MediaQualityService extends SystemService { notifyHalOnSoundProfileChange(dbId, null); } } } private boolean hasPermissionToRemoveSoundProfile(SoundProfile toDelete) { if (toDelete != null) { Loading @@ -966,6 +1014,7 @@ public class MediaQualityService extends SystemService { return false; } @GuardedBy("mSoundProfileLock") @Override public SoundProfile getSoundProfile(int type, String name, Bundle options, UserHandle user) { Loading @@ -976,18 +1025,21 @@ public class MediaQualityService extends SystemService { + BaseParameters.PARAMETER_PACKAGE + " = ?"; String[] selectionArguments = {String.valueOf(type), name, getPackageOfCallingUid()}; synchronized (mSoundProfileLock) { try ( Cursor cursor = getCursorAfterQuerying( mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, getMediaProfileColumns(includeParams), selection, selectionArguments) getMediaProfileColumns(includeParams), selection, selectionArguments) ) { int count = cursor.getCount(); if (count == 0) { return null; } if (count > 1) { Log.wtf(TAG, String.format(Locale.US, "%d entries found for name=%s" + " in %s. Should only ever be 0 or 1.", count, name, Log.wtf(TAG, TextUtils.formatSimple(String.valueOf(Locale.US), "%d " + "entries found for name=%s in %s. Should only ever " + "be 0 or 1.", String.valueOf(count), name, mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME)); return null; } Loading @@ -995,6 +1047,7 @@ public class MediaQualityService extends SystemService { return convertCursorToSoundProfileWithTempId(cursor); } } } private SoundProfile getSoundProfile(Long dbId) { String selection = BaseParameters.PARAMETER_ID + " = ?"; Loading @@ -1010,9 +1063,9 @@ public class MediaQualityService extends SystemService { return null; } if (count > 1) { Log.wtf(TAG, String.format(Locale.US, "%d entries found for id=%s " + "in %s. Should only ever be 0 or 1.", count, dbId, mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME)); Log.wtf(TAG, TextUtils.formatSimple(String.valueOf(Locale.US), "%d entries " + "found for id=%s in %s. Should only ever be 0 or 1.", count, dbId, mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME)); return null; } cursor.moveToFirst(); Loading @@ -1020,6 +1073,7 @@ public class MediaQualityService extends SystemService { } } @GuardedBy("mSoundProfileLock") @Override public List<SoundProfile> getSoundProfilesByPackage( String packageName, Bundle options, UserHandle user) { Loading @@ -1028,6 +1082,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mSoundProfileLock) { boolean includeParams = options.getBoolean(MediaQualityManager.OPTION_INCLUDE_PARAMETERS, false); String selection = BaseParameters.PARAMETER_PACKAGE + " = ?"; Loading @@ -1035,7 +1090,9 @@ public class MediaQualityService extends SystemService { return getSoundProfilesBasedOnConditions(getMediaProfileColumns(includeParams), selection, selectionArguments); } } @GuardedBy("mSoundProfileLock") @Override public List<SoundProfile> getAvailableSoundProfiles(Bundle options, UserHandle user) { String packageName = getPackageOfCallingUid(); Loading @@ -1045,6 +1102,7 @@ public class MediaQualityService extends SystemService { return new ArrayList<>(); } @GuardedBy("mSoundProfileLock") @Override public boolean setDefaultSoundProfile(String profileId, UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { Loading Loading @@ -1161,6 +1219,7 @@ public class MediaQualityService extends SystemService { return (SoundParameter[]) soundParams.toArray(); } @GuardedBy("mSoundProfileLock") @Override public List<String> getSoundProfilePackageNames(UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { Loading @@ -1168,6 +1227,8 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } String [] column = {BaseParameters.PARAMETER_NAME}; synchronized (mSoundProfileLock) { List<SoundProfile> soundProfiles = getSoundProfilesBasedOnConditions(column, null, null); return soundProfiles.stream() Loading @@ -1175,6 +1236,7 @@ public class MediaQualityService extends SystemService { .distinct() .collect(Collectors.toList()); } } private String getPackageOfCallingUid() { String[] packageNames = mPackageManager.getPackagesForUid( Loading Loading @@ -1539,6 +1601,7 @@ public class MediaQualityService extends SystemService { userState.mSoundProfileCallbacks.finishBroadcast(); } //TODO: need lock here? @Override public void registerPictureProfileCallback(final IPictureProfileCallback callback) { int callingPid = Binder.getCallingPid(); Loading @@ -1549,6 +1612,7 @@ public class MediaQualityService extends SystemService { Pair.create(callingPid, callingUid)); } //TODO: need lock here? @Override public void registerSoundProfileCallback(final ISoundProfileCallback callback) { int callingPid = Binder.getCallingPid(); Loading Loading @@ -1586,6 +1650,7 @@ public class MediaQualityService extends SystemService { } } @GuardedBy("mAmbientBacklightLock") @Override public void setAmbientBacklightSettings( AmbientBacklightSettings settings, UserHandle user) { Loading Loading @@ -1624,6 +1689,7 @@ public class MediaQualityService extends SystemService { } } @GuardedBy("mAmbientBacklightLock") @Override public void setAmbientBacklightEnabled(boolean enabled, UserHandle user) { if (DEBUG) { Loading @@ -1643,12 +1709,14 @@ public class MediaQualityService extends SystemService { } } //TODO: do I need a lock here? @Override public List<ParameterCapability> getParameterCapabilities( List<String> names, UserHandle user) { return new ArrayList<>(); } @GuardedBy("mPictureProfileLock") @Override public List<String> getPictureProfileAllowList(UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { Loading @@ -1663,6 +1731,7 @@ public class MediaQualityService extends SystemService { return new ArrayList<>(); } @GuardedBy("mPictureProfileLock") @Override public void setPictureProfileAllowList(List<String> packages, UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { Loading @@ -1674,6 +1743,7 @@ public class MediaQualityService extends SystemService { editor.commit(); } @GuardedBy("mSoundProfileLock") @Override public List<String> getSoundProfileAllowList(UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { Loading @@ -1688,6 +1758,7 @@ public class MediaQualityService extends SystemService { return new ArrayList<>(); } @GuardedBy("mSoundProfileLock") @Override public void setSoundProfileAllowList(List<String> packages, UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { Loading @@ -1704,13 +1775,14 @@ public class MediaQualityService extends SystemService { return false; } @GuardedBy("mPictureProfileLock") @Override public void setAutoPictureQualityEnabled(boolean enabled, UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { notifyOnPictureProfileError(null, PictureProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mPictureProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoPqSupported()) { Loading @@ -1721,9 +1793,12 @@ public class MediaQualityService extends SystemService { Slog.e(TAG, "Failed to set auto picture quality", e); } } } @GuardedBy("mPictureProfileLock") @Override public boolean isAutoPictureQualityEnabled(UserHandle user) { synchronized (mPictureProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoPqSupported()) { Loading @@ -1735,14 +1810,16 @@ public class MediaQualityService extends SystemService { } return false; } } @GuardedBy("mPictureProfileLock") @Override public void setSuperResolutionEnabled(boolean enabled, UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { notifyOnPictureProfileError(null, PictureProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mPictureProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoSrSupported()) { Loading @@ -1753,9 +1830,12 @@ public class MediaQualityService extends SystemService { Slog.e(TAG, "Failed to set super resolution", e); } } } @GuardedBy("mPictureProfileLock") @Override public boolean isSuperResolutionEnabled(UserHandle user) { synchronized (mPictureProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoSrSupported()) { Loading @@ -1767,7 +1847,9 @@ public class MediaQualityService extends SystemService { } return false; } } @GuardedBy("mSoundProfileLock") @Override public void setAutoSoundQualityEnabled(boolean enabled, UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { Loading @@ -1775,6 +1857,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mSoundProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoAqSupported()) { Loading @@ -1785,9 +1868,12 @@ public class MediaQualityService extends SystemService { Slog.e(TAG, "Failed to set auto sound quality", e); } } } @GuardedBy("mSoundProfileLock") @Override public boolean isAutoSoundQualityEnabled(UserHandle user) { synchronized (mSoundProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoAqSupported()) { Loading @@ -1799,7 +1885,9 @@ public class MediaQualityService extends SystemService { } return false; } } @GuardedBy("mAmbientBacklightLock") @Override public boolean isAmbientBacklightEnabled(UserHandle user) { return false; Loading Loading @@ -1853,6 +1941,7 @@ public class MediaQualityService extends SystemService { } } //TODO: used by both picture and sound. can i add both locks? private UserState getOrCreateUserStateLocked(int userId) { UserState userState = getUserStateLocked(userId); if (userState == null) { Loading @@ -1862,6 +1951,7 @@ public class MediaQualityService extends SystemService { return userState; } //TODO: used by both picture and sound. can i add both locks? private UserState getUserStateLocked(int userId) { return mUserStates.get(userId); } Loading Loading
services/core/java/com/android/server/media/quality/MediaQualityService.java +286 −196 Original line number Diff line number Diff line Loading @@ -73,6 +73,7 @@ import android.util.Pair; import android.util.Slog; import android.util.SparseArray; import com.android.internal.annotations.GuardedBy; import com.android.server.SystemService; import com.android.server.utils.Slogf; Loading Loading @@ -122,6 +123,13 @@ public class MediaQualityService extends SystemService { private SharedPreferences mPictureProfileSharedPreference; private SharedPreferences mSoundProfileSharedPreference; // A global lock for picture profile objects. private final Object mPictureProfileLock = new Object(); // A global lock for sound profile objects. private final Object mSoundProfileLock = new Object(); // A global lock for ambient backlight objects. private final Object mAmbientBacklightLock = new Object(); public MediaQualityService(Context context) { super(context); mContext = context; Loading Loading @@ -254,6 +262,7 @@ public class MediaQualityService extends SystemService { // TODO: Add additional APIs. b/373951081 private final class BinderService extends IMediaQualityManager.Stub { @GuardedBy("mPictureProfileLock") @Override public PictureProfile createPictureProfile(PictureProfile pp, UserHandle user) { if ((pp.getPackageName() != null && !pp.getPackageName().isEmpty() Loading @@ -263,6 +272,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mPictureProfileLock) { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); ContentValues values = getContentValues(null, Loading @@ -279,9 +289,11 @@ public class MediaQualityService extends SystemService { populateTempIdMap(mPictureProfileTempIdMap, id); String value = mPictureProfileTempIdMap.getValue(id); pp.setProfileId(value); notifyOnPictureProfileAdded(value, pp, Binder.getCallingUid(), Binder.getCallingPid()); notifyOnPictureProfileAdded(value, pp, Binder.getCallingUid(), Binder.getCallingPid()); return pp; } } private void notifyHalOnPictureProfileChange(Long dbId, PersistableBundle params) { // TODO: only notify HAL when the profile is active / being used Loading @@ -307,6 +319,7 @@ public class MediaQualityService extends SystemService { return toReturn; } @GuardedBy("mPictureProfileLock") @Override public void updatePictureProfile(String id, PictureProfile pp, UserHandle user) { Long dbId = mPictureProfileTempIdMap.getKey(id); Loading @@ -315,6 +328,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mPictureProfileLock) { ContentValues values = getContentValues(dbId, pp.getProfileType(), pp.getName(), Loading @@ -329,6 +343,7 @@ public class MediaQualityService extends SystemService { getPictureProfile(dbId), Binder.getCallingUid(), Binder.getCallingPid()); notifyHalOnPictureProfileChange(dbId, pp.getParameters()); } } private boolean hasPermissionToUpdatePictureProfile(Long dbId, PictureProfile toUpdate) { PictureProfile fromDb = getPictureProfile(dbId); Loading @@ -338,8 +353,10 @@ public class MediaQualityService extends SystemService { && fromDb.getName().equals(getPackageOfCallingUid()); } @GuardedBy("mPictureProfileLock") @Override public void removePictureProfile(String id, UserHandle user) { synchronized (mPictureProfileLock) { Long dbId = mPictureProfileTempIdMap.getKey(id); PictureProfile toDelete = getPictureProfile(dbId); Loading @@ -352,8 +369,8 @@ public class MediaQualityService extends SystemService { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); String selection = BaseParameters.PARAMETER_ID + " = ?"; String[] selectionArgs = {Long.toString(dbId)}; int result = db.delete(mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, selection, selectionArgs); int result = db.delete(mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, selection, selectionArgs); if (result == 0) { notifyOnPictureProfileError(id, PictureProfile.ERROR_INVALID_ARGUMENT, Binder.getCallingUid(), Binder.getCallingPid()); Loading @@ -364,6 +381,7 @@ public class MediaQualityService extends SystemService { notifyHalOnPictureProfileChange(dbId, null); } } } private boolean hasPermissionToRemovePictureProfile(PictureProfile toDelete) { if (toDelete != null) { Loading @@ -372,6 +390,7 @@ public class MediaQualityService extends SystemService { return false; } @GuardedBy("mPictureProfileLock") @Override public PictureProfile getPictureProfile(int type, String name, Bundle options, UserHandle user) { Loading @@ -382,18 +401,21 @@ public class MediaQualityService extends SystemService { + BaseParameters.PARAMETER_PACKAGE + " = ?"; String[] selectionArguments = {Integer.toString(type), name, getPackageOfCallingUid()}; synchronized (mPictureProfileLock) { try ( Cursor cursor = getCursorAfterQuerying( mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, getMediaProfileColumns(includeParams), selection, selectionArguments) getMediaProfileColumns(includeParams), selection, selectionArguments) ) { int count = cursor.getCount(); if (count == 0) { return null; } if (count > 1) { Log.wtf(TAG, String.format(Locale.US, "%d entries found for type=%d and name=%s" + " in %s. Should only ever be 0 or 1.", count, type, name, Log.wtf(TAG, TextUtils.formatSimple(String.valueOf(Locale.US), "%d " + "entries found for type=%d and name=%s in %s. Should" + " only ever be 0 or 1.", count, type, name, mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME)); return null; } Loading @@ -401,6 +423,7 @@ public class MediaQualityService extends SystemService { return convertCursorToPictureProfileWithTempId(cursor); } } } private PictureProfile getPictureProfile(Long dbId) { String selection = BaseParameters.PARAMETER_ID + " = ?"; Loading @@ -416,9 +439,9 @@ public class MediaQualityService extends SystemService { return null; } if (count > 1) { Log.wtf(TAG, String.format(Locale.US, "%d entries found for id=%d" + " in %s. Should only ever be 0 or 1.", count, dbId, mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME)); Log.wtf(TAG, TextUtils.formatSimple(String.valueOf(Locale.US), "%d entries " + "found for id=%d in %s. Should only ever be 0 or 1.", count, dbId, mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME)); return null; } cursor.moveToFirst(); Loading @@ -426,6 +449,7 @@ public class MediaQualityService extends SystemService { } } @GuardedBy("mPictureProfileLock") @Override public List<PictureProfile> getPictureProfilesByPackage( String packageName, Bundle options, UserHandle user) { Loading @@ -434,6 +458,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mPictureProfileLock) { boolean includeParams = options.getBoolean(MediaQualityManager.OPTION_INCLUDE_PARAMETERS, false); String selection = BaseParameters.PARAMETER_PACKAGE + " = ?"; Loading @@ -441,7 +466,9 @@ public class MediaQualityService extends SystemService { return getPictureProfilesBasedOnConditions(getMediaProfileColumns(includeParams), selection, selectionArguments); } } @GuardedBy("mPictureProfileLock") @Override public List<PictureProfile> getAvailablePictureProfiles(Bundle options, UserHandle user) { String packageName = getPackageOfCallingUid(); Loading @@ -451,6 +478,7 @@ public class MediaQualityService extends SystemService { return new ArrayList<>(); } @GuardedBy("mPictureProfileLock") @Override public boolean setDefaultPictureProfile(String profileId, UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { Loading Loading @@ -810,6 +838,7 @@ public class MediaQualityService extends SystemService { return (PictureParameter[]) pictureParams.toArray(); } @GuardedBy("mPictureProfileLock") @Override public List<String> getPictureProfilePackageNames(UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { Loading @@ -817,6 +846,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } String [] column = {BaseParameters.PARAMETER_PACKAGE}; synchronized (mPictureProfileLock) { List<PictureProfile> pictureProfiles = getPictureProfilesBasedOnConditions(column, null, null); return pictureProfiles.stream() Loading @@ -824,10 +854,13 @@ public class MediaQualityService extends SystemService { .distinct() .collect(Collectors.toList()); } } @GuardedBy("mPictureProfileLock") @Override public List<PictureProfileHandle> getPictureProfileHandle(String[] ids, UserHandle user) { List<PictureProfileHandle> toReturn = new ArrayList<>(); synchronized (mPictureProfileLock) { for (String id : ids) { Long key = mPictureProfileTempIdMap.getKey(id); if (key != null) { Loading @@ -836,12 +869,15 @@ public class MediaQualityService extends SystemService { toReturn.add(null); } } } return toReturn; } @GuardedBy("mSoundProfileLock") @Override public List<SoundProfileHandle> getSoundProfileHandle(String[] ids, UserHandle user) { List<SoundProfileHandle> toReturn = new ArrayList<>(); synchronized (mSoundProfileLock) { for (String id : ids) { Long key = mSoundProfileTempIdMap.getKey(id); if (key != null) { Loading @@ -850,9 +886,11 @@ public class MediaQualityService extends SystemService { toReturn.add(null); } } } return toReturn; } @GuardedBy("mSoundProfileLock") @Override public SoundProfile createSoundProfile(SoundProfile sp, UserHandle user) { if ((sp.getPackageName() != null && !sp.getPackageName().isEmpty() Loading @@ -861,6 +899,8 @@ public class MediaQualityService extends SystemService { notifyOnSoundProfileError(null, SoundProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mSoundProfileLock) { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); ContentValues values = getContentValues(null, Loading @@ -877,9 +917,11 @@ public class MediaQualityService extends SystemService { populateTempIdMap(mSoundProfileTempIdMap, id); String value = mSoundProfileTempIdMap.getValue(id); sp.setProfileId(value); notifyOnSoundProfileAdded(value, sp, Binder.getCallingUid(), Binder.getCallingPid()); notifyOnSoundProfileAdded(value, sp, Binder.getCallingUid(), Binder.getCallingPid()); return sp; } } private void notifyHalOnSoundProfileChange(Long dbId, PersistableBundle params) { // TODO: only notify HAL when the profile is active / being used Loading @@ -903,6 +945,7 @@ public class MediaQualityService extends SystemService { return toReturn; } @GuardedBy("mSoundProfileLock") @Override public void updateSoundProfile(String id, SoundProfile sp, UserHandle user) { Long dbId = mSoundProfileTempIdMap.getKey(id); Loading @@ -911,6 +954,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mSoundProfileLock) { ContentValues values = getContentValues(dbId, sp.getProfileType(), sp.getName(), Loading @@ -924,6 +968,7 @@ public class MediaQualityService extends SystemService { getSoundProfile(dbId), Binder.getCallingUid(), Binder.getCallingPid()); notifyHalOnSoundProfileChange(dbId, sp.getParameters()); } } private boolean hasPermissionToUpdateSoundProfile(Long dbId, SoundProfile sp) { SoundProfile fromDb = getSoundProfile(dbId); Loading @@ -933,20 +978,22 @@ public class MediaQualityService extends SystemService { && fromDb.getName().equals(getPackageOfCallingUid()); } @GuardedBy("mSoundProfileLock") @Override public void removeSoundProfile(String id, UserHandle user) { synchronized (mSoundProfileLock) { Long dbId = mSoundProfileTempIdMap.getKey(id); SoundProfile toDelete = getSoundProfile(dbId); if (!hasPermissionToRemoveSoundProfile(toDelete)) { notifyOnSoundProfileError(id, SoundProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } if (dbId != null) { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); String selection = BaseParameters.PARAMETER_ID + " = ?"; String[] selectionArgs = {Long.toString(dbId)}; int result = db.delete(mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, selection, int result = db.delete(mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, selection, selectionArgs); if (result == 0) { notifyOnSoundProfileError(id, SoundProfile.ERROR_INVALID_ARGUMENT, Loading @@ -958,6 +1005,7 @@ public class MediaQualityService extends SystemService { notifyHalOnSoundProfileChange(dbId, null); } } } private boolean hasPermissionToRemoveSoundProfile(SoundProfile toDelete) { if (toDelete != null) { Loading @@ -966,6 +1014,7 @@ public class MediaQualityService extends SystemService { return false; } @GuardedBy("mSoundProfileLock") @Override public SoundProfile getSoundProfile(int type, String name, Bundle options, UserHandle user) { Loading @@ -976,18 +1025,21 @@ public class MediaQualityService extends SystemService { + BaseParameters.PARAMETER_PACKAGE + " = ?"; String[] selectionArguments = {String.valueOf(type), name, getPackageOfCallingUid()}; synchronized (mSoundProfileLock) { try ( Cursor cursor = getCursorAfterQuerying( mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, getMediaProfileColumns(includeParams), selection, selectionArguments) getMediaProfileColumns(includeParams), selection, selectionArguments) ) { int count = cursor.getCount(); if (count == 0) { return null; } if (count > 1) { Log.wtf(TAG, String.format(Locale.US, "%d entries found for name=%s" + " in %s. Should only ever be 0 or 1.", count, name, Log.wtf(TAG, TextUtils.formatSimple(String.valueOf(Locale.US), "%d " + "entries found for name=%s in %s. Should only ever " + "be 0 or 1.", String.valueOf(count), name, mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME)); return null; } Loading @@ -995,6 +1047,7 @@ public class MediaQualityService extends SystemService { return convertCursorToSoundProfileWithTempId(cursor); } } } private SoundProfile getSoundProfile(Long dbId) { String selection = BaseParameters.PARAMETER_ID + " = ?"; Loading @@ -1010,9 +1063,9 @@ public class MediaQualityService extends SystemService { return null; } if (count > 1) { Log.wtf(TAG, String.format(Locale.US, "%d entries found for id=%s " + "in %s. Should only ever be 0 or 1.", count, dbId, mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME)); Log.wtf(TAG, TextUtils.formatSimple(String.valueOf(Locale.US), "%d entries " + "found for id=%s in %s. Should only ever be 0 or 1.", count, dbId, mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME)); return null; } cursor.moveToFirst(); Loading @@ -1020,6 +1073,7 @@ public class MediaQualityService extends SystemService { } } @GuardedBy("mSoundProfileLock") @Override public List<SoundProfile> getSoundProfilesByPackage( String packageName, Bundle options, UserHandle user) { Loading @@ -1028,6 +1082,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mSoundProfileLock) { boolean includeParams = options.getBoolean(MediaQualityManager.OPTION_INCLUDE_PARAMETERS, false); String selection = BaseParameters.PARAMETER_PACKAGE + " = ?"; Loading @@ -1035,7 +1090,9 @@ public class MediaQualityService extends SystemService { return getSoundProfilesBasedOnConditions(getMediaProfileColumns(includeParams), selection, selectionArguments); } } @GuardedBy("mSoundProfileLock") @Override public List<SoundProfile> getAvailableSoundProfiles(Bundle options, UserHandle user) { String packageName = getPackageOfCallingUid(); Loading @@ -1045,6 +1102,7 @@ public class MediaQualityService extends SystemService { return new ArrayList<>(); } @GuardedBy("mSoundProfileLock") @Override public boolean setDefaultSoundProfile(String profileId, UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { Loading Loading @@ -1161,6 +1219,7 @@ public class MediaQualityService extends SystemService { return (SoundParameter[]) soundParams.toArray(); } @GuardedBy("mSoundProfileLock") @Override public List<String> getSoundProfilePackageNames(UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { Loading @@ -1168,6 +1227,8 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } String [] column = {BaseParameters.PARAMETER_NAME}; synchronized (mSoundProfileLock) { List<SoundProfile> soundProfiles = getSoundProfilesBasedOnConditions(column, null, null); return soundProfiles.stream() Loading @@ -1175,6 +1236,7 @@ public class MediaQualityService extends SystemService { .distinct() .collect(Collectors.toList()); } } private String getPackageOfCallingUid() { String[] packageNames = mPackageManager.getPackagesForUid( Loading Loading @@ -1539,6 +1601,7 @@ public class MediaQualityService extends SystemService { userState.mSoundProfileCallbacks.finishBroadcast(); } //TODO: need lock here? @Override public void registerPictureProfileCallback(final IPictureProfileCallback callback) { int callingPid = Binder.getCallingPid(); Loading @@ -1549,6 +1612,7 @@ public class MediaQualityService extends SystemService { Pair.create(callingPid, callingUid)); } //TODO: need lock here? @Override public void registerSoundProfileCallback(final ISoundProfileCallback callback) { int callingPid = Binder.getCallingPid(); Loading Loading @@ -1586,6 +1650,7 @@ public class MediaQualityService extends SystemService { } } @GuardedBy("mAmbientBacklightLock") @Override public void setAmbientBacklightSettings( AmbientBacklightSettings settings, UserHandle user) { Loading Loading @@ -1624,6 +1689,7 @@ public class MediaQualityService extends SystemService { } } @GuardedBy("mAmbientBacklightLock") @Override public void setAmbientBacklightEnabled(boolean enabled, UserHandle user) { if (DEBUG) { Loading @@ -1643,12 +1709,14 @@ public class MediaQualityService extends SystemService { } } //TODO: do I need a lock here? @Override public List<ParameterCapability> getParameterCapabilities( List<String> names, UserHandle user) { return new ArrayList<>(); } @GuardedBy("mPictureProfileLock") @Override public List<String> getPictureProfileAllowList(UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { Loading @@ -1663,6 +1731,7 @@ public class MediaQualityService extends SystemService { return new ArrayList<>(); } @GuardedBy("mPictureProfileLock") @Override public void setPictureProfileAllowList(List<String> packages, UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { Loading @@ -1674,6 +1743,7 @@ public class MediaQualityService extends SystemService { editor.commit(); } @GuardedBy("mSoundProfileLock") @Override public List<String> getSoundProfileAllowList(UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { Loading @@ -1688,6 +1758,7 @@ public class MediaQualityService extends SystemService { return new ArrayList<>(); } @GuardedBy("mSoundProfileLock") @Override public void setSoundProfileAllowList(List<String> packages, UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { Loading @@ -1704,13 +1775,14 @@ public class MediaQualityService extends SystemService { return false; } @GuardedBy("mPictureProfileLock") @Override public void setAutoPictureQualityEnabled(boolean enabled, UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { notifyOnPictureProfileError(null, PictureProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mPictureProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoPqSupported()) { Loading @@ -1721,9 +1793,12 @@ public class MediaQualityService extends SystemService { Slog.e(TAG, "Failed to set auto picture quality", e); } } } @GuardedBy("mPictureProfileLock") @Override public boolean isAutoPictureQualityEnabled(UserHandle user) { synchronized (mPictureProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoPqSupported()) { Loading @@ -1735,14 +1810,16 @@ public class MediaQualityService extends SystemService { } return false; } } @GuardedBy("mPictureProfileLock") @Override public void setSuperResolutionEnabled(boolean enabled, UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { notifyOnPictureProfileError(null, PictureProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mPictureProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoSrSupported()) { Loading @@ -1753,9 +1830,12 @@ public class MediaQualityService extends SystemService { Slog.e(TAG, "Failed to set super resolution", e); } } } @GuardedBy("mPictureProfileLock") @Override public boolean isSuperResolutionEnabled(UserHandle user) { synchronized (mPictureProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoSrSupported()) { Loading @@ -1767,7 +1847,9 @@ public class MediaQualityService extends SystemService { } return false; } } @GuardedBy("mSoundProfileLock") @Override public void setAutoSoundQualityEnabled(boolean enabled, UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { Loading @@ -1775,6 +1857,7 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } synchronized (mSoundProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoAqSupported()) { Loading @@ -1785,9 +1868,12 @@ public class MediaQualityService extends SystemService { Slog.e(TAG, "Failed to set auto sound quality", e); } } } @GuardedBy("mSoundProfileLock") @Override public boolean isAutoSoundQualityEnabled(UserHandle user) { synchronized (mSoundProfileLock) { try { if (mMediaQuality != null) { if (mMediaQuality.isAutoAqSupported()) { Loading @@ -1799,7 +1885,9 @@ public class MediaQualityService extends SystemService { } return false; } } @GuardedBy("mAmbientBacklightLock") @Override public boolean isAmbientBacklightEnabled(UserHandle user) { return false; Loading Loading @@ -1853,6 +1941,7 @@ public class MediaQualityService extends SystemService { } } //TODO: used by both picture and sound. can i add both locks? private UserState getOrCreateUserStateLocked(int userId) { UserState userState = getUserStateLocked(userId); if (userState == null) { Loading @@ -1862,6 +1951,7 @@ public class MediaQualityService extends SystemService { return userState; } //TODO: used by both picture and sound. can i add both locks? private UserState getUserStateLocked(int userId) { return mUserStates.get(userId); } Loading