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

Commit 7bdc6d80 authored by Andre Lago's avatar Andre Lago Committed by Robin Lee
Browse files

[SettingsProvider] Auto disable ringtone sync

Disable the SYNC_PARENT_SOUNDS setting if a work profile ringtone is set

Test: cts-tradefed --test com.android.cts.devicepolicy.ManagedProfileTest
Bug: 30658854
Change-Id: I172f5396b47f03ac8afa365db0ec90f3a2dd0e29
parent 3e604155
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -192,5 +192,7 @@ interface IAudioService {

    oneway void releasePlayer(in int piid);

    void disableRingtoneSync();

    // WARNING: read warning at top of file, it is recommended to add new methods at the end
}
+15 −0
Original line number Diff line number Diff line
@@ -33,8 +33,11 @@ import android.database.Cursor;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.MediaStore;
@@ -850,6 +853,18 @@ public class RingtoneManager {
    public static void setActualDefaultRingtoneUri(Context context, int type, Uri ringtoneUri) {
        final ContentResolver resolver = context.getContentResolver();

        if (Settings.Secure.getString(resolver, Settings.Secure.SYNC_PARENT_SOUNDS).equals("1")) {
            // Sync is enabled, so we need to disable it
            IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
            IAudioService audioService = IAudioService.Stub.asInterface(b);
            try {
                audioService.disableRingtoneSync();
            } catch (RemoteException e) {
                Log.e(TAG, "Unable to disable ringtone sync.");
                return;
            }
        }

        String setting = getSettingForType(type);
        if (setting == null) return;
        if(!isInternalRingtoneUri(ringtoneUri)) {
+29 −0
Original line number Diff line number Diff line
@@ -6479,6 +6479,35 @@ public class AudioService extends IAudioService.Stub
        return mRecordMonitor.getActiveRecordingConfigurations();
    }

    public void disableRingtoneSync() {
        final int callingUserId = UserHandle.getCallingUserId();
        final long token = Binder.clearCallingIdentity();
        try {
            UserManager userManager = UserManager.get(mContext);

            // Disable the sync setting
            Settings.Secure.putIntForUser(mContentResolver,
                    Settings.Secure.SYNC_PARENT_SOUNDS, 0 /* false */, callingUserId);

            UserInfo parentInfo = userManager.getProfileParent(callingUserId);
            if (parentInfo != null && parentInfo.id != callingUserId) {
                // This is a managed profile, so we clone the ringtones from the parent profile
                cloneRingtoneSetting(callingUserId, parentInfo.id, Settings.System.RINGTONE);
                cloneRingtoneSetting(callingUserId, parentInfo.id,
                        Settings.System.NOTIFICATION_SOUND);
                cloneRingtoneSetting(callingUserId, parentInfo.id, Settings.System.ALARM_ALERT);
            }
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }

    private void cloneRingtoneSetting(int userId, int parentId, String ringtoneSetting) {
        String parentSetting = Settings.System.getStringForUser(mContentResolver, ringtoneSetting,
                parentId);
        Settings.System.putStringForUser(mContentResolver, ringtoneSetting, parentSetting, userId);
    }

    //======================
    // Audio playback notification
    //======================