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

Commit f5abb011 authored by Robyn Coultas's avatar Robyn Coultas Committed by Robyn Coultas
Browse files

Replaced AlarmManager with postDelayed runnable

Bug: 8239532

AlarmManager.setRepeating(AlarmManager.RTC, ...) was ceasing to fire.
This replacement has proven more reliable and exact.

Change-Id: I38d9f61026c5825cde21b9d27ea5002e8861b1a1
(cherry picked from commit 9096dcec)
parent 74c7afaf
Loading
Loading
Loading
Loading
+61 −25
Original line number Diff line number Diff line
@@ -16,12 +16,12 @@

package com.android.alarmclock;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.text.format.DateFormat;
@@ -51,10 +51,56 @@ public class DigitalWidgetViewsFactory extends BroadcastReceiver implements Remo
    private boolean mReloadCitiesList = true;
    private boolean mReloadCitiesDb = true;
    private float mFontScale = 1;
    private PendingIntent mQuarterlyIntent;
    private String mLastTimeZone;
    private QuarterHourUpdater mQuarterHourUpdater;


    // Thread that runs every quarter-hour and refreshes the date.
    private class QuarterHourUpdater implements Runnable {
        private Context mUpdaterContext;
        private Handler mHandler = new Handler();
        public QuarterHourUpdater(Context context) {
            mUpdaterContext = context;
            // Chasing bug b/8239532 - log every updater creation
            Log.i(TAG, "QuarterHourUpdater.start: " + this);
            Utils.setQuarterHourUpdater(mHandler, this);
        }

        public void reset() {
            // Chasing bug b/8239532 - log every updater reset
            Log.i(TAG, "QuarterHourUpdater.reset: " + this);
            Utils.setQuarterHourUpdater(mHandler, this);
        }

        public void close() {
            Utils.cancelQuarterHourUpdater(mHandler, this);
            // Chasing bug b/8239532 - log every updater closure
            Log.i(TAG, "QuarterHourUpdater.close: " + this);
        }

        @Override
        public void run() {
            // Chasing bug b/8239532 - log every run we get to can see when run ran.
            Log.i(TAG, "QuarterHourUpdater.run: " + this);
            // Since the system may miss or not send time zone changes in all cases
            // make sure to update the world clock list if the time zone
            // changed in the last 15 minutes
            String currentTimeZone = TimeZone.getDefault().getID();
            if (!TextUtils.equals(currentTimeZone, mLastTimeZone)) {
                // refresh the list to make sure home time zone is displayed / removed
                mReloadCitiesList = true;
                mLastTimeZone = currentTimeZone;
                Log.v(TAG, "Detected time zone change,updating time zone to " + currentTimeZone);
            }

            AppWidgetManager widgetManager = AppWidgetManager.getInstance(mUpdaterContext);
            if (widgetManager != null) {
                refreshAll(mUpdaterContext, widgetManager);
            }
            Utils.setQuarterHourUpdater(mHandler, this);
        }
    }

    // An adapter to provide the view for the list of cities in the world clock.
    private class RemoteWorldClockAdapter extends WorldClockAdapter {
        private final float mFontSize;
@@ -196,14 +242,13 @@ public class DigitalWidgetViewsFactory extends BroadcastReceiver implements Remo

    @Override
    public void onCreate() {
        mQuarterlyIntent = Utils.startAlarmOnQuarterHour(mContext);
        mQuarterHourUpdater = new QuarterHourUpdater(mContext);
        // Do intent listening registration here since doing it in the manifest creates a new
        // new factory
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_DATE_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Utils.ACTION_ON_QUARTER_HOUR);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
        filter.addAction(AlarmNotifications.SYSTEM_ALARM_CHANGE_ACTION);
@@ -230,7 +275,7 @@ public class DigitalWidgetViewsFactory extends BroadcastReceiver implements Remo
    @Override
    public void onDestroy() {
        Log.i(TAG, "DigitalWidget unregister receiver");
        Utils.cancelAlarmOnQuarterHour(mContext, mQuarterlyIntent);
        mQuarterHourUpdater.close();
        mContext.unregisterReceiver(this);
    }

@@ -273,29 +318,20 @@ public class DigitalWidgetViewsFactory extends BroadcastReceiver implements Remo
            } else if (action.equals(Intent.ACTION_LOCALE_CHANGED)) {
                // reload the cities DB to pick up the cities name in the new language
                mReloadCitiesDb = true;
            } else if (action.equals(Utils.ACTION_ON_QUARTER_HOUR)) {
                // Since the system may miss or not send time zone changes in all cases
                // make sure to update the world clock list if the time zone
                // changed in the last 15 minutes
                String currentTimeZone = TimeZone.getDefault().getID();
                if (!TextUtils.equals(currentTimeZone, mLastTimeZone)) {
                    // refresh the list to make sure home time zone is displayed / removed
                    mReloadCitiesList = true;
                    mLastTimeZone = currentTimeZone;
                    Log.v(TAG,"Detected time zone change,updating time zone to " + currentTimeZone);
            }
            // For any time change or locale change, refresh all
            refreshAll(context, widgetManager);
            mQuarterHourUpdater.reset();
        }
    }

            // For any time change or locale change, refresh all
    protected void refreshAll(Context context, AppWidgetManager widgetManager) {
        widgetManager.notifyAppWidgetViewDataChanged(mId, R.id.digital_appwidget_listview);
            RemoteViews widget =
                    new RemoteViews(context.getPackageName(), R.layout.digital_appwidget);
        RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.digital_appwidget);
        float ratio = WidgetUtils.getScaleRatio(context, null, mId);
        WidgetUtils.setClockSize(context, widget, ratio);
        refreshAlarm(context, widget);
        widgetManager.partiallyUpdateAppWidget(mId, widget);
            mQuarterlyIntent = Utils.refreshAlarmOnQuarterHour(context, mQuarterlyIntent);
        }
    }

    protected static void refreshAlarm(Context context, RemoteViews widget) {
+24 −0
Original line number Diff line number Diff line
@@ -421,6 +421,30 @@ public class Utils {
        handler.removeCallbacks(runnable);
    }

    // Setup a thread that starts at the quarter-hour plus one second. The extra second is added to
    // ensure dates have changed.
    public static void setQuarterHourUpdater(Handler handler, Runnable runnable) {
        String timezone = TimeZone.getDefault().getID();
        if (handler == null || runnable == null || timezone == null) {
            return;
        }
        long runInMillis = getAlarmOnQuarterHour() - System.currentTimeMillis();
        // Ensure the delay is at least one second.
        if (runInMillis < 1000) {
            runInMillis = 1000;
        }
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, runInMillis);
    }

    // Stop the quarter-hour update thread
    public static void cancelQuarterHourUpdater(Handler handler, Runnable runnable) {
        if (handler == null || runnable == null) {
            return;
        }
        handler.removeCallbacks(runnable);
    }

    /**
     * For screensavers to set whether the digital or analog clock should be displayed.
     * Returns the view to be displayed.