michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: package org.mozilla.gecko.background.announcements;
michael@0:
michael@0: import org.mozilla.gecko.background.BackgroundService;
michael@0: import org.mozilla.gecko.background.common.GlobalConstants;
michael@0: import org.mozilla.gecko.background.common.log.Logger;
michael@0:
michael@0: import android.app.AlarmManager;
michael@0: import android.app.PendingIntent;
michael@0: import android.content.Context;
michael@0: import android.content.Intent;
michael@0: import android.content.SharedPreferences;
michael@0: import android.content.SharedPreferences.Editor;
michael@0:
michael@0: /**
michael@0: * A service which listens to broadcast intents from the system and from the
michael@0: * browser, registering or unregistering the main
michael@0: * {@link AnnouncementsService} with the {@link AlarmManager}.
michael@0: */
michael@0: public class AnnouncementsBroadcastService extends BackgroundService {
michael@0: private static final String WORKER_THREAD_NAME = "AnnouncementsBroadcastServiceWorker";
michael@0: private static final String LOG_TAG = "AnnounceBrSvc";
michael@0:
michael@0: public AnnouncementsBroadcastService() {
michael@0: super(WORKER_THREAD_NAME);
michael@0: }
michael@0:
michael@0: protected static SharedPreferences getSharedPreferences(Context context) {
michael@0: return context.getSharedPreferences(AnnouncementsConstants.PREFS_BRANCH,
michael@0: GlobalConstants.SHARED_PREFERENCES_MODE);
michael@0: }
michael@0:
michael@0: protected SharedPreferences getSharedPreferences() {
michael@0: return this.getSharedPreferences(AnnouncementsConstants.PREFS_BRANCH,
michael@0: GlobalConstants.SHARED_PREFERENCES_MODE);
michael@0: }
michael@0:
michael@0: private void toggleAlarm(final Context context, boolean enabled) {
michael@0: final Class> serviceClass = AnnouncementsService.class;
michael@0: Logger.info(LOG_TAG, (enabled ? "R" : "Unr") + "egistering " + serviceClass.getSimpleName() +
michael@0: ".");
michael@0:
michael@0: final Intent service = new Intent(context, serviceClass);
michael@0: final PendingIntent pending = PendingIntent.getService(context, 0, service,
michael@0: PendingIntent.FLAG_CANCEL_CURRENT);
michael@0:
michael@0: if (!enabled) {
michael@0: cancelAlarm(pending);
michael@0: return;
michael@0: }
michael@0:
michael@0: final long pollInterval = getPollInterval(context);
michael@0: scheduleAlarm(pollInterval, pending);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Record the last launch time of our version of Fennec.
michael@0: *
michael@0: * @param context
michael@0: * the Context
to use to gain access to
michael@0: * SharedPreferences
.
michael@0: */
michael@0: public static void recordLastLaunch(final Context context) {
michael@0: final long now = System.currentTimeMillis();
michael@0: final SharedPreferences preferences = getSharedPreferences(context);
michael@0:
michael@0: // One of several things might be true, according to our logs:
michael@0: //
michael@0: // * The new current time is older than the last
michael@0: // * … or way in the future
michael@0: // * … or way in the distant past
michael@0: // * … or it's reasonable.
michael@0: //
michael@0: // Furthermore, when we come to calculate idle we might find that the clock
michael@0: // is dramatically different — that the current time is thirteen years older
michael@0: // than our saved timestamp (system clock resets to 2000 on battery change),
michael@0: // or it's thirty years in the future (previous timestamp was saved as 0).
michael@0: //
michael@0: // We should try to do something vaguely sane in these situations.
michael@0: long previous = preferences.getLong(AnnouncementsConstants.PREF_LAST_LAUNCH, -1);
michael@0: if (previous == -1) {
michael@0: Logger.debug(LOG_TAG, "No previous launch recorded.");
michael@0: }
michael@0:
michael@0: if (now < GlobalConstants.BUILD_TIMESTAMP_MSEC) {
michael@0: Logger.warn(LOG_TAG, "Current time " + now + " is older than build date " +
michael@0: GlobalConstants.BUILD_TIMESTAMP_MSEC + ". Ignoring until clock is corrected.");
michael@0: return;
michael@0: }
michael@0:
michael@0: if (now > AnnouncementsConstants.LATEST_ACCEPTED_LAUNCH_TIMESTAMP_MSEC) {
michael@0: Logger.warn(LOG_TAG, "Launch time " + now + " is later than max sane launch timestamp " +
michael@0: AnnouncementsConstants.LATEST_ACCEPTED_LAUNCH_TIMESTAMP_MSEC +
michael@0: ". Ignoring until clock is corrected.");
michael@0: return;
michael@0: }
michael@0:
michael@0: if (previous > now) {
michael@0: Logger.debug(LOG_TAG, "Previous launch " + previous + " later than current time " +
michael@0: now + ", but new time is sane. Accepting new time.");
michael@0: }
michael@0:
michael@0: preferences.edit().putLong(AnnouncementsConstants.PREF_LAST_LAUNCH, now).commit();
michael@0: }
michael@0:
michael@0: public static long getPollInterval(final Context context) {
michael@0: final SharedPreferences preferences = getSharedPreferences(context);
michael@0: return preferences.getLong(AnnouncementsConstants.PREF_ANNOUNCE_FETCH_INTERVAL_MSEC, AnnouncementsConstants.DEFAULT_ANNOUNCE_FETCH_INTERVAL_MSEC);
michael@0: }
michael@0:
michael@0: public static void setPollInterval(final Context context, long interval) {
michael@0: final SharedPreferences preferences = getSharedPreferences(context);
michael@0: preferences.edit().putLong(AnnouncementsConstants.PREF_ANNOUNCE_FETCH_INTERVAL_MSEC, interval).commit();
michael@0: }
michael@0:
michael@0: @Override
michael@0: protected void onHandleIntent(Intent intent) {
michael@0: Logger.setThreadLogTag(AnnouncementsConstants.GLOBAL_LOG_TAG);
michael@0:
michael@0: // Intent can be null. Bug 1025937.
michael@0: if (intent == null) {
michael@0: Logger.debug(LOG_TAG, "Short-circuiting on null intent.");
michael@0: return;
michael@0: }
michael@0:
michael@0: final String action = intent.getAction();
michael@0: Logger.debug(LOG_TAG, "Broadcast onReceive. Intent is " + action);
michael@0:
michael@0: if (AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF.equals(action)) {
michael@0: handlePrefIntent(intent);
michael@0: return;
michael@0: }
michael@0:
michael@0: if (Intent.ACTION_BOOT_COMPLETED.equals(action) ||
michael@0: Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
michael@0: BackgroundService.reflectContextToFennec(this,
michael@0: GlobalConstants.GECKO_PREFERENCES_CLASS,
michael@0: GlobalConstants.GECKO_BROADCAST_ANNOUNCEMENTS_PREF_METHOD);
michael@0: return;
michael@0: }
michael@0:
michael@0: // Failure case.
michael@0: Logger.warn(LOG_TAG, "Unknown intent " + action);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Handle the intent sent by the browser when it wishes to notify us
michael@0: * of the value of the user preference. Look at the value and toggle the
michael@0: * alarm service accordingly.
michael@0: *
michael@0: * @param intent must be non-null.
michael@0: */
michael@0: private void handlePrefIntent(Intent intent) {
michael@0: if (!intent.hasExtra("enabled")) {
michael@0: Logger.warn(LOG_TAG, "Got ANNOUNCEMENTS_PREF intent without enabled. Ignoring.");
michael@0: return;
michael@0: }
michael@0:
michael@0: final boolean enabled = intent.getBooleanExtra("enabled", true);
michael@0: Logger.debug(LOG_TAG, intent.getStringExtra("branch") + "/" +
michael@0: intent.getStringExtra("pref") + " = " +
michael@0: (intent.hasExtra("enabled") ? enabled : ""));
michael@0:
michael@0: toggleAlarm(this, enabled);
michael@0:
michael@0: // Primarily intended for debugging and testing, but this doesn't do any harm.
michael@0: if (!enabled) {
michael@0: Logger.info(LOG_TAG, "!enabled: clearing last fetch.");
michael@0: final SharedPreferences sharedPreferences = getSharedPreferences();
michael@0: final Editor editor = sharedPreferences.edit();
michael@0: editor.remove(AnnouncementsConstants.PREF_LAST_FETCH_LOCAL_TIME);
michael@0: editor.remove(AnnouncementsConstants.PREF_EARLIEST_NEXT_ANNOUNCE_FETCH);
michael@0: editor.commit();
michael@0: }
michael@0: }
michael@0: }