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

Commit ce36d870 authored by Richard MacGregor's avatar Richard MacGregor
Browse files

Apply sounds on theme update

Add content observer so themeservice knows whether ringtone was
changed outside of themes.

Change-Id: I83a5dff8e9574019f4d1a5bd58368b0ec3c09c88
parent d6e42283
Loading
Loading
Loading
Loading
+63 −0
Original line number Diff line number Diff line
@@ -41,9 +41,11 @@ import android.content.res.ThemeChangeRequest;
import android.content.res.ThemeConfig;
import android.content.res.IThemeChangeListener;
import android.content.res.IThemeService;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Binder;
import android.os.Environment;
import android.os.FileUtils;
@@ -123,6 +125,8 @@ public class ThemeService extends IThemeService.Stub {

    final private ArrayList<String> mThemesToProcessQueue = new ArrayList<String>(0);

    private final SettingsObserver mSettingsObserver = new SettingsObserver();

    private class ThemeWorkerHandler extends Handler {
        private static final int MESSAGE_CHANGE_THEME = 1;
        private static final int MESSAGE_APPLY_DEFAULT_THEME = 2;
@@ -243,6 +247,9 @@ public class ThemeService extends IThemeService.Stub {
        filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
        mContext.registerReceiver(mUserChangeReceiver, filter);

        // listen for alarm/notifications/ringtone changes
        mSettingsObserver.register(true);

        mPM = mContext.getPackageManager();

        processInstalledThemes();
@@ -377,6 +384,9 @@ public class ThemeService extends IThemeService.Stub {
        mIsThemeApplying = true;
        long updateTime = System.currentTimeMillis();

        // Stop listening for settings changes while applying theme
        mSettingsObserver.register(false);

        incrementProgress(5);

        // TODO: provide progress updates that reflect the time needed for each component
@@ -441,6 +451,9 @@ public class ThemeService extends IThemeService.Stub {

        postFinish(true, request, updateTime);
        mIsThemeApplying = false;

        // Start listening for settings changes while applying theme
        mSettingsObserver.register(true);
    }

    private void doApplyDefaultTheme() {
@@ -1225,4 +1238,54 @@ public class ThemeService extends IThemeService.Stub {

        return null;
    }

    private final class SettingsObserver extends ContentObserver {
        private final Uri ALARM_ALERT_URI =
                Settings.System.DEFAULT_ALARM_ALERT_URI;
        private final Uri NOTIFICATION_URI =
                Settings.System.DEFAULT_NOTIFICATION_URI;
        private final Uri RINGTONE_URI =
                Settings.System.DEFAULT_RINGTONE_URI;

        public SettingsObserver() {
            super(null);
        }

        public void register(boolean register) {
            final ContentResolver cr = mContext.getContentResolver();
            if (register) {
                cr.registerContentObserver(ALARM_ALERT_URI, false, this);
                cr.registerContentObserver(NOTIFICATION_URI, false, this);
                cr.registerContentObserver(RINGTONE_URI, false, this);
            } else {
                cr.unregisterContentObserver(this);
            }
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            super.onChange(selfChange, uri);
            boolean changed = false;
            ThemeChangeRequest.Builder builder = new ThemeChangeRequest.Builder();
            if (ALARM_ALERT_URI.equals(uri)) {
                // In case the mixnmatch table has a mods_alarms entry, we'll clear it
                builder.setAlarm("");
                changed = true;
            }
            if (NOTIFICATION_URI.equals(uri)) {
                // In case the mixnmatch table has a mods_notifications entry, we'll clear it
                builder.setNotification("");
                changed = true;
            }
            if (RINGTONE_URI.equals(uri)) {
                // In case the mixnmatch table has a mods_ringtones entry, we'll clear it
                builder.setRingtone("");
                changed = true;
            }

            if (changed) {
                updateProvider(builder.build(), System.currentTimeMillis());
            }
        }
    }
}