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

Commit 9071670e authored by James Lemieux's avatar James Lemieux
Browse files

Handle SecurityException while updating next alarm time on K

Bug: 32330397
Bug: 32272767
Bug: 32272516
Test: manual testing
Change-Id: Id49264ae4903c80f6fb48db33963a2b93459ebc5
parent 9794d446
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -61,7 +61,6 @@ import com.android.deskclock.actionbarmenu.MenuItemControllerFactory;
import com.android.deskclock.actionbarmenu.NightModeMenuItemController;
import com.android.deskclock.actionbarmenu.OptionsMenuManager;
import com.android.deskclock.actionbarmenu.SettingsMenuItemController;
import com.android.deskclock.alarms.AlarmStateManager;
import com.android.deskclock.data.DataModel;
import com.android.deskclock.events.Events;
import com.android.deskclock.provider.Alarm;
@@ -296,9 +295,6 @@ public class DeskClock extends BaseActivity
        // Honor changes to the selected tab from outside entities.
        UiDataModel.getUiDataModel().addTabListener(mTabChangeWatcher);

        // Update the next alarm time on app startup because the user might have altered the data.
        AlarmStateManager.updateNextAlarm(this);

        if (savedInstanceState == null) {
            // Set the background color to initially match the theme value so that we can
            // smoothly transition to the dynamic color.
+20 −19
Original line number Diff line number Diff line
@@ -50,6 +50,8 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static android.provider.Settings.System.NEXT_ALARM_FORMATTED;

/**
 * This class handles all the state changes for alarm instances. You need to
 * register all alarm instances with the state manager if you want them to
@@ -171,12 +173,10 @@ public final class AlarmStateManager extends BroadcastReceiver {
    }

    /**
     * Find and notify system what the next alarm that will fire. This is used
     * to update text in the system and widgets.
     *
     * @param context application context
     * Update the next alarm stored in framework. This value is also displayed in digital widgets
     * and the clock tab in this app.
     */
    public static void updateNextAlarm(Context context) {
    private static void updateNextAlarm(Context context) {
        final AlarmInstance nextAlarm = getNextFiringAlarm(context);

        if (Utils.isPreL()) {
@@ -212,23 +212,24 @@ public final class AlarmStateManager extends BroadcastReceiver {
    @SuppressWarnings("deprecation")
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private static void updateNextAlarmInSystemSettings(Context context, AlarmInstance nextAlarm) {
        // Send broadcast message so pre-L AppWidgets will recognize an update
        String timeString = "";
        boolean showStatusIcon = false;
        // Format the next alarm time if an alarm is scheduled.
        String time = "";
        if (nextAlarm != null) {
            timeString = AlarmUtils.getFormattedTime(context, nextAlarm.getAlarmTime());
            showStatusIcon = true;
            time = AlarmUtils.getFormattedTime(context, nextAlarm.getAlarmTime());
        }

        // Set and notify next alarm text to system
        LogUtils.i("Displaying next alarm time: \'" + timeString + '\'');
        try {
            // Write directly to NEXT_ALARM_FORMATTED in all pre-L versions
        Settings.System.putString(context.getContentResolver(),
                Settings.System.NEXT_ALARM_FORMATTED,
                timeString);
        Intent alarmChanged = new Intent(ACTION_ALARM_CHANGED);
        alarmChanged.putExtra("alarmSet", showStatusIcon);
        context.sendBroadcast(alarmChanged);
            Settings.System.putString(context.getContentResolver(), NEXT_ALARM_FORMATTED, time);

            LogUtils.i("Updated next alarm time to: \'" + time + '\'');

            // Send broadcast message so pre-L AppWidgets will recognize an update.
            context.sendBroadcast(new Intent(ACTION_ALARM_CHANGED));
        } catch (SecurityException se) {
            // The user has most likely revoked WRITE_SETTINGS.
            LogUtils.e("Unable to update next alarm to: \'" + time + '\'', se);
        }
    }

    /**
+1 −1
Original line number Diff line number Diff line
@@ -232,7 +232,7 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
                                                   String... selectionArgs) {
        final List<AlarmInstance> result = new LinkedList<>();
        try (Cursor cursor = cr.query(CONTENT_URI, QUERY_COLUMNS, selection, selectionArgs, null)) {
            if (cursor.moveToFirst()) {
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    result.add(new AlarmInstance(cursor, false /* joinedTable */));
                } while (cursor.moveToNext());