diff options
| -rw-r--r-- | services/core/java/com/android/server/media/quality/MediaQualityService.java | 482 |
1 files changed, 286 insertions, 196 deletions
diff --git a/services/core/java/com/android/server/media/quality/MediaQualityService.java b/services/core/java/com/android/server/media/quality/MediaQualityService.java index f6c94a7d9a5a..d23a8638803b 100644 --- a/services/core/java/com/android/server/media/quality/MediaQualityService.java +++ b/services/core/java/com/android/server/media/quality/MediaQualityService.java @@ -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; @@ -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; @@ -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() @@ -263,24 +272,27 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } - SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); + synchronized (mPictureProfileLock) { + SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); - ContentValues values = getContentValues(null, - pp.getProfileType(), - pp.getName(), - pp.getPackageName() == null || pp.getPackageName().isEmpty() - ? getPackageOfCallingUid() : pp.getPackageName(), - pp.getInputId(), - pp.getParameters()); + ContentValues values = getContentValues(null, + pp.getProfileType(), + pp.getName(), + pp.getPackageName() == null || pp.getPackageName().isEmpty() + ? getPackageOfCallingUid() : pp.getPackageName(), + pp.getInputId(), + pp.getParameters()); - // id is auto-generated by SQLite upon successful insertion of row - Long id = db.insert(mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, - null, values); - populateTempIdMap(mPictureProfileTempIdMap, id); - String value = mPictureProfileTempIdMap.getValue(id); - pp.setProfileId(value); - notifyOnPictureProfileAdded(value, pp, Binder.getCallingUid(), Binder.getCallingPid()); - return pp; + // id is auto-generated by SQLite upon successful insertion of row + Long id = db.insert(mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, + null, values); + populateTempIdMap(mPictureProfileTempIdMap, id); + String value = mPictureProfileTempIdMap.getValue(id); + pp.setProfileId(value); + notifyOnPictureProfileAdded(value, pp, Binder.getCallingUid(), + Binder.getCallingPid()); + return pp; + } } private void notifyHalOnPictureProfileChange(Long dbId, PersistableBundle params) { @@ -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); @@ -315,12 +328,13 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } - ContentValues values = getContentValues(dbId, - pp.getProfileType(), - pp.getName(), - pp.getPackageName(), - pp.getInputId(), - pp.getParameters()); + synchronized (mPictureProfileLock) { + ContentValues values = getContentValues(dbId, + pp.getProfileType(), + pp.getName(), + pp.getPackageName(), + pp.getInputId(), + pp.getParameters()); SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); db.replace(mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, @@ -328,6 +342,7 @@ public class MediaQualityService extends SystemService { notifyOnPictureProfileUpdated(mPictureProfileTempIdMap.getValue(dbId), getPictureProfile(dbId), Binder.getCallingUid(), Binder.getCallingPid()); notifyHalOnPictureProfileChange(dbId, pp.getParameters()); + } } private boolean hasPermissionToUpdatePictureProfile(Long dbId, PictureProfile toUpdate) { @@ -338,30 +353,33 @@ public class MediaQualityService extends SystemService { && fromDb.getName().equals(getPackageOfCallingUid()); } + @GuardedBy("mPictureProfileLock") @Override public void removePictureProfile(String id, UserHandle user) { - Long dbId = mPictureProfileTempIdMap.getKey(id); + synchronized (mPictureProfileLock) { + Long dbId = mPictureProfileTempIdMap.getKey(id); - PictureProfile toDelete = getPictureProfile(dbId); - if (!hasPermissionToRemovePictureProfile(toDelete)) { - notifyOnPictureProfileError(id, PictureProfile.ERROR_NO_PERMISSION, - Binder.getCallingUid(), Binder.getCallingPid()); - } + PictureProfile toDelete = getPictureProfile(dbId); + if (!hasPermissionToRemovePictureProfile(toDelete)) { + notifyOnPictureProfileError(id, PictureProfile.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.PICTURE_QUALITY_TABLE_NAME, selection, - selectionArgs); - if (result == 0) { - notifyOnPictureProfileError(id, PictureProfile.ERROR_INVALID_ARGUMENT, + if (dbId != null) { + 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); + if (result == 0) { + notifyOnPictureProfileError(id, PictureProfile.ERROR_INVALID_ARGUMENT, + Binder.getCallingUid(), Binder.getCallingPid()); + } + notifyOnPictureProfileRemoved(mPictureProfileTempIdMap.getValue(dbId), toDelete, Binder.getCallingUid(), Binder.getCallingPid()); + mPictureProfileTempIdMap.remove(dbId); + notifyHalOnPictureProfileChange(dbId, null); } - notifyOnPictureProfileRemoved(mPictureProfileTempIdMap.getValue(dbId), toDelete, - Binder.getCallingUid(), Binder.getCallingPid()); - mPictureProfileTempIdMap.remove(dbId); - notifyHalOnPictureProfileChange(dbId, null); } } @@ -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) { @@ -382,23 +401,27 @@ public class MediaQualityService extends SystemService { + BaseParameters.PARAMETER_PACKAGE + " = ?"; String[] selectionArguments = {Integer.toString(type), name, getPackageOfCallingUid()}; - try ( - Cursor cursor = getCursorAfterQuerying( - mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, - 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, - mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME)); - return null; + synchronized (mPictureProfileLock) { + try ( + Cursor cursor = getCursorAfterQuerying( + mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, + getMediaProfileColumns(includeParams), selection, + selectionArguments) + ) { + int count = cursor.getCount(); + if (count == 0) { + return null; + } + if (count > 1) { + 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; + } + cursor.moveToFirst(); + return convertCursorToPictureProfileWithTempId(cursor); } - cursor.moveToFirst(); - return convertCursorToPictureProfileWithTempId(cursor); } } @@ -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(); @@ -426,6 +449,7 @@ public class MediaQualityService extends SystemService { } } + @GuardedBy("mPictureProfileLock") @Override public List<PictureProfile> getPictureProfilesByPackage( String packageName, Bundle options, UserHandle user) { @@ -434,14 +458,17 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } - boolean includeParams = - options.getBoolean(MediaQualityManager.OPTION_INCLUDE_PARAMETERS, false); - String selection = BaseParameters.PARAMETER_PACKAGE + " = ?"; - String[] selectionArguments = {packageName}; - return getPictureProfilesBasedOnConditions(getMediaProfileColumns(includeParams), - selection, selectionArguments); + synchronized (mPictureProfileLock) { + boolean includeParams = + options.getBoolean(MediaQualityManager.OPTION_INCLUDE_PARAMETERS, false); + String selection = BaseParameters.PARAMETER_PACKAGE + " = ?"; + String[] selectionArguments = {packageName}; + return getPictureProfilesBasedOnConditions(getMediaProfileColumns(includeParams), + selection, selectionArguments); + } } + @GuardedBy("mPictureProfileLock") @Override public List<PictureProfile> getAvailablePictureProfiles(Bundle options, UserHandle user) { String packageName = getPackageOfCallingUid(); @@ -451,6 +478,7 @@ public class MediaQualityService extends SystemService { return new ArrayList<>(); } + @GuardedBy("mPictureProfileLock") @Override public boolean setDefaultPictureProfile(String profileId, UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { @@ -810,6 +838,7 @@ public class MediaQualityService extends SystemService { return (PictureParameter[]) pictureParams.toArray(); } + @GuardedBy("mPictureProfileLock") @Override public List<String> getPictureProfilePackageNames(UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { @@ -817,42 +846,51 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } String [] column = {BaseParameters.PARAMETER_PACKAGE}; - List<PictureProfile> pictureProfiles = getPictureProfilesBasedOnConditions(column, - null, null); - return pictureProfiles.stream() - .map(PictureProfile::getPackageName) - .distinct() - .collect(Collectors.toList()); + synchronized (mPictureProfileLock) { + List<PictureProfile> pictureProfiles = getPictureProfilesBasedOnConditions(column, + null, null); + return pictureProfiles.stream() + .map(PictureProfile::getPackageName) + .distinct() + .collect(Collectors.toList()); + } } + @GuardedBy("mPictureProfileLock") @Override public List<PictureProfileHandle> getPictureProfileHandle(String[] ids, UserHandle user) { List<PictureProfileHandle> toReturn = new ArrayList<>(); - for (String id : ids) { - Long key = mPictureProfileTempIdMap.getKey(id); - if (key != null) { - toReturn.add(new PictureProfileHandle(key)); - } else { - toReturn.add(null); + synchronized (mPictureProfileLock) { + for (String id : ids) { + Long key = mPictureProfileTempIdMap.getKey(id); + if (key != null) { + toReturn.add(new PictureProfileHandle(key)); + } else { + toReturn.add(null); + } } } return toReturn; } + @GuardedBy("mSoundProfileLock") @Override public List<SoundProfileHandle> getSoundProfileHandle(String[] ids, UserHandle user) { List<SoundProfileHandle> toReturn = new ArrayList<>(); - for (String id : ids) { - Long key = mSoundProfileTempIdMap.getKey(id); - if (key != null) { - toReturn.add(new SoundProfileHandle(key)); - } else { - toReturn.add(null); + synchronized (mSoundProfileLock) { + for (String id : ids) { + Long key = mSoundProfileTempIdMap.getKey(id); + if (key != null) { + toReturn.add(new SoundProfileHandle(key)); + } else { + toReturn.add(null); + } } } return toReturn; } + @GuardedBy("mSoundProfileLock") @Override public SoundProfile createSoundProfile(SoundProfile sp, UserHandle user) { if ((sp.getPackageName() != null && !sp.getPackageName().isEmpty() @@ -861,24 +899,28 @@ public class MediaQualityService extends SystemService { notifyOnSoundProfileError(null, SoundProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } - SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); - ContentValues values = getContentValues(null, - sp.getProfileType(), - sp.getName(), - sp.getPackageName() == null || sp.getPackageName().isEmpty() - ? getPackageOfCallingUid() : sp.getPackageName(), - sp.getInputId(), - sp.getParameters()); + synchronized (mSoundProfileLock) { + SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); - // id is auto-generated by SQLite upon successful insertion of row - Long id = db.insert(mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, - null, values); - populateTempIdMap(mSoundProfileTempIdMap, id); - String value = mSoundProfileTempIdMap.getValue(id); - sp.setProfileId(value); - notifyOnSoundProfileAdded(value, sp, Binder.getCallingUid(), Binder.getCallingPid()); - return sp; + ContentValues values = getContentValues(null, + sp.getProfileType(), + sp.getName(), + sp.getPackageName() == null || sp.getPackageName().isEmpty() + ? getPackageOfCallingUid() : sp.getPackageName(), + sp.getInputId(), + sp.getParameters()); + + // id is auto-generated by SQLite upon successful insertion of row + Long id = db.insert(mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, + null, values); + populateTempIdMap(mSoundProfileTempIdMap, id); + String value = mSoundProfileTempIdMap.getValue(id); + sp.setProfileId(value); + notifyOnSoundProfileAdded(value, sp, Binder.getCallingUid(), + Binder.getCallingPid()); + return sp; + } } private void notifyHalOnSoundProfileChange(Long dbId, PersistableBundle params) { @@ -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); @@ -911,18 +954,20 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } - ContentValues values = getContentValues(dbId, - sp.getProfileType(), - sp.getName(), - sp.getPackageName(), - sp.getInputId(), - sp.getParameters()); + synchronized (mSoundProfileLock) { + ContentValues values = getContentValues(dbId, + sp.getProfileType(), + sp.getName(), + sp.getPackageName(), + sp.getInputId(), + sp.getParameters()); SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); db.replace(mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, null, values); notifyOnSoundProfileUpdated(mSoundProfileTempIdMap.getValue(dbId), getSoundProfile(dbId), Binder.getCallingUid(), Binder.getCallingPid()); notifyHalOnSoundProfileChange(dbId, sp.getParameters()); + } } private boolean hasPermissionToUpdateSoundProfile(Long dbId, SoundProfile sp) { @@ -933,29 +978,32 @@ public class MediaQualityService extends SystemService { && fromDb.getName().equals(getPackageOfCallingUid()); } + @GuardedBy("mSoundProfileLock") @Override public void removeSoundProfile(String id, UserHandle user) { - 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, - selectionArgs); - if (result == 0) { - notifyOnSoundProfileError(id, SoundProfile.ERROR_INVALID_ARGUMENT, + synchronized (mSoundProfileLock) { + Long dbId = mSoundProfileTempIdMap.getKey(id); + SoundProfile toDelete = getSoundProfile(dbId); + if (!hasPermissionToRemoveSoundProfile(toDelete)) { + notifyOnSoundProfileError(id, SoundProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } - notifyOnSoundProfileRemoved(mSoundProfileTempIdMap.getValue(dbId), toDelete, - Binder.getCallingUid(), Binder.getCallingPid()); - mSoundProfileTempIdMap.remove(dbId); - notifyHalOnSoundProfileChange(dbId, null); + 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, + selectionArgs); + if (result == 0) { + notifyOnSoundProfileError(id, SoundProfile.ERROR_INVALID_ARGUMENT, + Binder.getCallingUid(), Binder.getCallingPid()); + } + notifyOnSoundProfileRemoved(mSoundProfileTempIdMap.getValue(dbId), toDelete, + Binder.getCallingUid(), Binder.getCallingPid()); + mSoundProfileTempIdMap.remove(dbId); + notifyHalOnSoundProfileChange(dbId, null); + } } } @@ -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) { @@ -976,23 +1025,27 @@ public class MediaQualityService extends SystemService { + BaseParameters.PARAMETER_PACKAGE + " = ?"; String[] selectionArguments = {String.valueOf(type), name, getPackageOfCallingUid()}; - try ( - Cursor cursor = getCursorAfterQuerying( - mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, - 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, - mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME)); - return null; + synchronized (mSoundProfileLock) { + try ( + Cursor cursor = getCursorAfterQuerying( + mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, + getMediaProfileColumns(includeParams), selection, + selectionArguments) + ) { + int count = cursor.getCount(); + if (count == 0) { + return null; + } + if (count > 1) { + 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; + } + cursor.moveToFirst(); + return convertCursorToSoundProfileWithTempId(cursor); } - cursor.moveToFirst(); - return convertCursorToSoundProfileWithTempId(cursor); } } @@ -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(); @@ -1020,6 +1073,7 @@ public class MediaQualityService extends SystemService { } } + @GuardedBy("mSoundProfileLock") @Override public List<SoundProfile> getSoundProfilesByPackage( String packageName, Bundle options, UserHandle user) { @@ -1028,14 +1082,17 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } - boolean includeParams = - options.getBoolean(MediaQualityManager.OPTION_INCLUDE_PARAMETERS, false); - String selection = BaseParameters.PARAMETER_PACKAGE + " = ?"; - String[] selectionArguments = {packageName}; - return getSoundProfilesBasedOnConditions(getMediaProfileColumns(includeParams), - selection, selectionArguments); + synchronized (mSoundProfileLock) { + boolean includeParams = + options.getBoolean(MediaQualityManager.OPTION_INCLUDE_PARAMETERS, false); + String selection = BaseParameters.PARAMETER_PACKAGE + " = ?"; + String[] selectionArguments = {packageName}; + return getSoundProfilesBasedOnConditions(getMediaProfileColumns(includeParams), + selection, selectionArguments); + } } + @GuardedBy("mSoundProfileLock") @Override public List<SoundProfile> getAvailableSoundProfiles(Bundle options, UserHandle user) { String packageName = getPackageOfCallingUid(); @@ -1045,6 +1102,7 @@ public class MediaQualityService extends SystemService { return new ArrayList<>(); } + @GuardedBy("mSoundProfileLock") @Override public boolean setDefaultSoundProfile(String profileId, UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { @@ -1161,6 +1219,7 @@ public class MediaQualityService extends SystemService { return (SoundParameter[]) soundParams.toArray(); } + @GuardedBy("mSoundProfileLock") @Override public List<String> getSoundProfilePackageNames(UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { @@ -1168,12 +1227,15 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } String [] column = {BaseParameters.PARAMETER_NAME}; - List<SoundProfile> soundProfiles = getSoundProfilesBasedOnConditions(column, - null, null); - return soundProfiles.stream() - .map(SoundProfile::getPackageName) - .distinct() - .collect(Collectors.toList()); + + synchronized (mSoundProfileLock) { + List<SoundProfile> soundProfiles = getSoundProfilesBasedOnConditions(column, + null, null); + return soundProfiles.stream() + .map(SoundProfile::getPackageName) + .distinct() + .collect(Collectors.toList()); + } } private String getPackageOfCallingUid() { @@ -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(); @@ -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(); @@ -1586,6 +1650,7 @@ public class MediaQualityService extends SystemService { } } + @GuardedBy("mAmbientBacklightLock") @Override public void setAmbientBacklightSettings( AmbientBacklightSettings settings, UserHandle user) { @@ -1624,6 +1689,7 @@ public class MediaQualityService extends SystemService { } } + @GuardedBy("mAmbientBacklightLock") @Override public void setAmbientBacklightEnabled(boolean enabled, UserHandle user) { if (DEBUG) { @@ -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()) { @@ -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()) { @@ -1674,6 +1743,7 @@ public class MediaQualityService extends SystemService { editor.commit(); } + @GuardedBy("mSoundProfileLock") @Override public List<String> getSoundProfileAllowList(UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { @@ -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()) { @@ -1704,70 +1775,81 @@ 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()); } - - try { - if (mMediaQuality != null) { - if (mMediaQuality.isAutoPqSupported()) { - mMediaQuality.setAutoPqEnabled(enabled); + synchronized (mPictureProfileLock) { + try { + if (mMediaQuality != null) { + if (mMediaQuality.isAutoPqSupported()) { + mMediaQuality.setAutoPqEnabled(enabled); + } } + } catch (RemoteException e) { + Slog.e(TAG, "Failed to set auto picture quality", e); } - } catch (RemoteException e) { - Slog.e(TAG, "Failed to set auto picture quality", e); } } + @GuardedBy("mPictureProfileLock") @Override public boolean isAutoPictureQualityEnabled(UserHandle user) { - try { - if (mMediaQuality != null) { - if (mMediaQuality.isAutoPqSupported()) { - return mMediaQuality.getAutoPqEnabled(); + synchronized (mPictureProfileLock) { + try { + if (mMediaQuality != null) { + if (mMediaQuality.isAutoPqSupported()) { + return mMediaQuality.getAutoPqEnabled(); + } } + } catch (RemoteException e) { + Slog.e(TAG, "Failed to get auto picture quality", e); } - } catch (RemoteException e) { - Slog.e(TAG, "Failed to get auto picture quality", e); + return false; } - return false; } + @GuardedBy("mPictureProfileLock") @Override public void setSuperResolutionEnabled(boolean enabled, UserHandle user) { if (!hasGlobalPictureQualityServicePermission()) { notifyOnPictureProfileError(null, PictureProfile.ERROR_NO_PERMISSION, Binder.getCallingUid(), Binder.getCallingPid()); } - - try { - if (mMediaQuality != null) { - if (mMediaQuality.isAutoSrSupported()) { - mMediaQuality.setAutoSrEnabled(enabled); + synchronized (mPictureProfileLock) { + try { + if (mMediaQuality != null) { + if (mMediaQuality.isAutoSrSupported()) { + mMediaQuality.setAutoSrEnabled(enabled); + } } + } catch (RemoteException e) { + Slog.e(TAG, "Failed to set super resolution", e); } - } catch (RemoteException e) { - Slog.e(TAG, "Failed to set super resolution", e); } } + @GuardedBy("mPictureProfileLock") @Override public boolean isSuperResolutionEnabled(UserHandle user) { - try { - if (mMediaQuality != null) { - if (mMediaQuality.isAutoSrSupported()) { - return mMediaQuality.getAutoSrEnabled(); + synchronized (mPictureProfileLock) { + try { + if (mMediaQuality != null) { + if (mMediaQuality.isAutoSrSupported()) { + return mMediaQuality.getAutoSrEnabled(); + } } + } catch (RemoteException e) { + Slog.e(TAG, "Failed to get super resolution", e); } - } catch (RemoteException e) { - Slog.e(TAG, "Failed to get super resolution", e); + return false; } - return false; } + @GuardedBy("mSoundProfileLock") @Override public void setAutoSoundQualityEnabled(boolean enabled, UserHandle user) { if (!hasGlobalSoundQualityServicePermission()) { @@ -1775,31 +1857,37 @@ public class MediaQualityService extends SystemService { Binder.getCallingUid(), Binder.getCallingPid()); } - try { - if (mMediaQuality != null) { - if (mMediaQuality.isAutoAqSupported()) { - mMediaQuality.setAutoAqEnabled(enabled); + synchronized (mSoundProfileLock) { + try { + if (mMediaQuality != null) { + if (mMediaQuality.isAutoAqSupported()) { + mMediaQuality.setAutoAqEnabled(enabled); + } } + } catch (RemoteException e) { + Slog.e(TAG, "Failed to set auto sound quality", e); } - } catch (RemoteException e) { - Slog.e(TAG, "Failed to set auto sound quality", e); } } + @GuardedBy("mSoundProfileLock") @Override public boolean isAutoSoundQualityEnabled(UserHandle user) { - try { - if (mMediaQuality != null) { - if (mMediaQuality.isAutoAqSupported()) { - return mMediaQuality.getAutoAqEnabled(); + synchronized (mSoundProfileLock) { + try { + if (mMediaQuality != null) { + if (mMediaQuality.isAutoAqSupported()) { + return mMediaQuality.getAutoAqEnabled(); + } } + } catch (RemoteException e) { + Slog.e(TAG, "Failed to get auto sound quality", e); } - } catch (RemoteException e) { - Slog.e(TAG, "Failed to get auto sound quality", e); + return false; } - return false; } + @GuardedBy("mAmbientBacklightLock") @Override public boolean isAmbientBacklightEnabled(UserHandle user) { return false; @@ -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) { @@ -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); } |