mobile/android/base/background/announcements/AnnouncementsBroadcastService.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.background.announcements;
michael@0 6
michael@0 7 import org.mozilla.gecko.background.BackgroundService;
michael@0 8 import org.mozilla.gecko.background.common.GlobalConstants;
michael@0 9 import org.mozilla.gecko.background.common.log.Logger;
michael@0 10
michael@0 11 import android.app.AlarmManager;
michael@0 12 import android.app.PendingIntent;
michael@0 13 import android.content.Context;
michael@0 14 import android.content.Intent;
michael@0 15 import android.content.SharedPreferences;
michael@0 16 import android.content.SharedPreferences.Editor;
michael@0 17
michael@0 18 /**
michael@0 19 * A service which listens to broadcast intents from the system and from the
michael@0 20 * browser, registering or unregistering the main
michael@0 21 * {@link AnnouncementsService} with the {@link AlarmManager}.
michael@0 22 */
michael@0 23 public class AnnouncementsBroadcastService extends BackgroundService {
michael@0 24 private static final String WORKER_THREAD_NAME = "AnnouncementsBroadcastServiceWorker";
michael@0 25 private static final String LOG_TAG = "AnnounceBrSvc";
michael@0 26
michael@0 27 public AnnouncementsBroadcastService() {
michael@0 28 super(WORKER_THREAD_NAME);
michael@0 29 }
michael@0 30
michael@0 31 protected static SharedPreferences getSharedPreferences(Context context) {
michael@0 32 return context.getSharedPreferences(AnnouncementsConstants.PREFS_BRANCH,
michael@0 33 GlobalConstants.SHARED_PREFERENCES_MODE);
michael@0 34 }
michael@0 35
michael@0 36 protected SharedPreferences getSharedPreferences() {
michael@0 37 return this.getSharedPreferences(AnnouncementsConstants.PREFS_BRANCH,
michael@0 38 GlobalConstants.SHARED_PREFERENCES_MODE);
michael@0 39 }
michael@0 40
michael@0 41 private void toggleAlarm(final Context context, boolean enabled) {
michael@0 42 final Class<?> serviceClass = AnnouncementsService.class;
michael@0 43 Logger.info(LOG_TAG, (enabled ? "R" : "Unr") + "egistering " + serviceClass.getSimpleName() +
michael@0 44 ".");
michael@0 45
michael@0 46 final Intent service = new Intent(context, serviceClass);
michael@0 47 final PendingIntent pending = PendingIntent.getService(context, 0, service,
michael@0 48 PendingIntent.FLAG_CANCEL_CURRENT);
michael@0 49
michael@0 50 if (!enabled) {
michael@0 51 cancelAlarm(pending);
michael@0 52 return;
michael@0 53 }
michael@0 54
michael@0 55 final long pollInterval = getPollInterval(context);
michael@0 56 scheduleAlarm(pollInterval, pending);
michael@0 57 }
michael@0 58
michael@0 59 /**
michael@0 60 * Record the last launch time of our version of Fennec.
michael@0 61 *
michael@0 62 * @param context
michael@0 63 * the <code>Context</code> to use to gain access to
michael@0 64 * <code>SharedPreferences</code>.
michael@0 65 */
michael@0 66 public static void recordLastLaunch(final Context context) {
michael@0 67 final long now = System.currentTimeMillis();
michael@0 68 final SharedPreferences preferences = getSharedPreferences(context);
michael@0 69
michael@0 70 // One of several things might be true, according to our logs:
michael@0 71 //
michael@0 72 // * The new current time is older than the last
michael@0 73 // * … or way in the future
michael@0 74 // * … or way in the distant past
michael@0 75 // * … or it's reasonable.
michael@0 76 //
michael@0 77 // Furthermore, when we come to calculate idle we might find that the clock
michael@0 78 // is dramatically different — that the current time is thirteen years older
michael@0 79 // than our saved timestamp (system clock resets to 2000 on battery change),
michael@0 80 // or it's thirty years in the future (previous timestamp was saved as 0).
michael@0 81 //
michael@0 82 // We should try to do something vaguely sane in these situations.
michael@0 83 long previous = preferences.getLong(AnnouncementsConstants.PREF_LAST_LAUNCH, -1);
michael@0 84 if (previous == -1) {
michael@0 85 Logger.debug(LOG_TAG, "No previous launch recorded.");
michael@0 86 }
michael@0 87
michael@0 88 if (now < GlobalConstants.BUILD_TIMESTAMP_MSEC) {
michael@0 89 Logger.warn(LOG_TAG, "Current time " + now + " is older than build date " +
michael@0 90 GlobalConstants.BUILD_TIMESTAMP_MSEC + ". Ignoring until clock is corrected.");
michael@0 91 return;
michael@0 92 }
michael@0 93
michael@0 94 if (now > AnnouncementsConstants.LATEST_ACCEPTED_LAUNCH_TIMESTAMP_MSEC) {
michael@0 95 Logger.warn(LOG_TAG, "Launch time " + now + " is later than max sane launch timestamp " +
michael@0 96 AnnouncementsConstants.LATEST_ACCEPTED_LAUNCH_TIMESTAMP_MSEC +
michael@0 97 ". Ignoring until clock is corrected.");
michael@0 98 return;
michael@0 99 }
michael@0 100
michael@0 101 if (previous > now) {
michael@0 102 Logger.debug(LOG_TAG, "Previous launch " + previous + " later than current time " +
michael@0 103 now + ", but new time is sane. Accepting new time.");
michael@0 104 }
michael@0 105
michael@0 106 preferences.edit().putLong(AnnouncementsConstants.PREF_LAST_LAUNCH, now).commit();
michael@0 107 }
michael@0 108
michael@0 109 public static long getPollInterval(final Context context) {
michael@0 110 final SharedPreferences preferences = getSharedPreferences(context);
michael@0 111 return preferences.getLong(AnnouncementsConstants.PREF_ANNOUNCE_FETCH_INTERVAL_MSEC, AnnouncementsConstants.DEFAULT_ANNOUNCE_FETCH_INTERVAL_MSEC);
michael@0 112 }
michael@0 113
michael@0 114 public static void setPollInterval(final Context context, long interval) {
michael@0 115 final SharedPreferences preferences = getSharedPreferences(context);
michael@0 116 preferences.edit().putLong(AnnouncementsConstants.PREF_ANNOUNCE_FETCH_INTERVAL_MSEC, interval).commit();
michael@0 117 }
michael@0 118
michael@0 119 @Override
michael@0 120 protected void onHandleIntent(Intent intent) {
michael@0 121 Logger.setThreadLogTag(AnnouncementsConstants.GLOBAL_LOG_TAG);
michael@0 122
michael@0 123 // Intent can be null. Bug 1025937.
michael@0 124 if (intent == null) {
michael@0 125 Logger.debug(LOG_TAG, "Short-circuiting on null intent.");
michael@0 126 return;
michael@0 127 }
michael@0 128
michael@0 129 final String action = intent.getAction();
michael@0 130 Logger.debug(LOG_TAG, "Broadcast onReceive. Intent is " + action);
michael@0 131
michael@0 132 if (AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF.equals(action)) {
michael@0 133 handlePrefIntent(intent);
michael@0 134 return;
michael@0 135 }
michael@0 136
michael@0 137 if (Intent.ACTION_BOOT_COMPLETED.equals(action) ||
michael@0 138 Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
michael@0 139 BackgroundService.reflectContextToFennec(this,
michael@0 140 GlobalConstants.GECKO_PREFERENCES_CLASS,
michael@0 141 GlobalConstants.GECKO_BROADCAST_ANNOUNCEMENTS_PREF_METHOD);
michael@0 142 return;
michael@0 143 }
michael@0 144
michael@0 145 // Failure case.
michael@0 146 Logger.warn(LOG_TAG, "Unknown intent " + action);
michael@0 147 }
michael@0 148
michael@0 149 /**
michael@0 150 * Handle the intent sent by the browser when it wishes to notify us
michael@0 151 * of the value of the user preference. Look at the value and toggle the
michael@0 152 * alarm service accordingly.
michael@0 153 *
michael@0 154 * @param intent must be non-null.
michael@0 155 */
michael@0 156 private void handlePrefIntent(Intent intent) {
michael@0 157 if (!intent.hasExtra("enabled")) {
michael@0 158 Logger.warn(LOG_TAG, "Got ANNOUNCEMENTS_PREF intent without enabled. Ignoring.");
michael@0 159 return;
michael@0 160 }
michael@0 161
michael@0 162 final boolean enabled = intent.getBooleanExtra("enabled", true);
michael@0 163 Logger.debug(LOG_TAG, intent.getStringExtra("branch") + "/" +
michael@0 164 intent.getStringExtra("pref") + " = " +
michael@0 165 (intent.hasExtra("enabled") ? enabled : ""));
michael@0 166
michael@0 167 toggleAlarm(this, enabled);
michael@0 168
michael@0 169 // Primarily intended for debugging and testing, but this doesn't do any harm.
michael@0 170 if (!enabled) {
michael@0 171 Logger.info(LOG_TAG, "!enabled: clearing last fetch.");
michael@0 172 final SharedPreferences sharedPreferences = getSharedPreferences();
michael@0 173 final Editor editor = sharedPreferences.edit();
michael@0 174 editor.remove(AnnouncementsConstants.PREF_LAST_FETCH_LOCAL_TIME);
michael@0 175 editor.remove(AnnouncementsConstants.PREF_EARLIEST_NEXT_ANNOUNCE_FETCH);
michael@0 176 editor.commit();
michael@0 177 }
michael@0 178 }
michael@0 179 }

mercurial