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.healthreport;
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: import org.mozilla.gecko.background.healthreport.prune.HealthReportPruneService;
michael@0: import org.mozilla.gecko.background.healthreport.upload.HealthReportUploadService;
michael@0: import org.mozilla.gecko.background.healthreport.upload.ObsoleteDocumentTracker;
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 background health report services with the
michael@0: * {@link AlarmManager}.
michael@0: */
michael@0: public class HealthReportBroadcastService extends BackgroundService {
michael@0: public static final String LOG_TAG = HealthReportBroadcastService.class.getSimpleName();
michael@0: public static final String WORKER_THREAD_NAME = LOG_TAG + "Worker";
michael@0:
michael@0: public HealthReportBroadcastService() {
michael@0: super(WORKER_THREAD_NAME);
michael@0: }
michael@0:
michael@0: protected SharedPreferences getSharedPreferences() {
michael@0: return this.getSharedPreferences(HealthReportConstants.PREFS_BRANCH, GlobalConstants.SHARED_PREFERENCES_MODE);
michael@0: }
michael@0:
michael@0: public long getSubmissionPollInterval() {
michael@0: return getSharedPreferences().getLong(HealthReportConstants.PREF_SUBMISSION_INTENT_INTERVAL_MSEC, HealthReportConstants.DEFAULT_SUBMISSION_INTENT_INTERVAL_MSEC);
michael@0: }
michael@0:
michael@0: public void setSubmissionPollInterval(final long interval) {
michael@0: getSharedPreferences().edit().putLong(HealthReportConstants.PREF_SUBMISSION_INTENT_INTERVAL_MSEC, interval).commit();
michael@0: }
michael@0:
michael@0: public long getPrunePollInterval() {
michael@0: return getSharedPreferences().getLong(HealthReportConstants.PREF_PRUNE_INTENT_INTERVAL_MSEC,
michael@0: HealthReportConstants.DEFAULT_PRUNE_INTENT_INTERVAL_MSEC);
michael@0: }
michael@0:
michael@0: public void setPrunePollInterval(final long interval) {
michael@0: getSharedPreferences().edit().putLong(HealthReportConstants.PREF_PRUNE_INTENT_INTERVAL_MSEC,
michael@0: interval).commit();
michael@0: }
michael@0:
michael@0: /**
michael@0: * Set or cancel an alarm to submit data for a profile.
michael@0: *
michael@0: * @param context
michael@0: * Android context.
michael@0: * @param profileName
michael@0: * to submit data for.
michael@0: * @param profilePath
michael@0: * to submit data for.
michael@0: * @param enabled
michael@0: * whether the user has enabled submitting health report data for
michael@0: * this profile.
michael@0: * @param serviceEnabled
michael@0: * whether submitting should be scheduled. If the user turns off
michael@0: * submitting, enabled
could be false but we could need
michael@0: * to delete so serviceEnabled
could be true.
michael@0: */
michael@0: protected void toggleSubmissionAlarm(final Context context, String profileName, String profilePath,
michael@0: boolean enabled, boolean serviceEnabled) {
michael@0: final Class> serviceClass = HealthReportUploadService.class;
michael@0: Logger.info(LOG_TAG, (serviceEnabled ? "R" : "Unr") + "egistering " +
michael@0: serviceClass.getSimpleName() + ".");
michael@0:
michael@0: // PendingIntents are compared without reference to their extras. Therefore
michael@0: // even though we pass the profile details to the action, different
michael@0: // profiles will share the *same* pending intent. In a multi-profile future,
michael@0: // this will need to be addressed. See Bug 882182.
michael@0: final Intent service = new Intent(context, serviceClass);
michael@0: service.setAction("upload"); // PendingIntents "lose" their extras if no action is set.
michael@0: service.putExtra("uploadEnabled", enabled);
michael@0: service.putExtra("profileName", profileName);
michael@0: service.putExtra("profilePath", profilePath);
michael@0: final PendingIntent pending = PendingIntent.getService(context, 0, service, PendingIntent.FLAG_CANCEL_CURRENT);
michael@0:
michael@0: if (!serviceEnabled) {
michael@0: cancelAlarm(pending);
michael@0: return;
michael@0: }
michael@0:
michael@0: final long pollInterval = getSubmissionPollInterval();
michael@0: scheduleAlarm(pollInterval, pending);
michael@0: }
michael@0:
michael@0: @Override
michael@0: protected void onHandleIntent(Intent intent) {
michael@0: Logger.setThreadLogTag(HealthReportConstants.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: // The same intent can be handled by multiple methods so do not short-circuit evaluate.
michael@0: boolean handled = attemptHandleIntentForUpload(intent);
michael@0: handled = attemptHandleIntentForPrune(intent) ? true : handled;
michael@0:
michael@0: if (!handled) {
michael@0: Logger.warn(LOG_TAG, "Unhandled intent with action " + intent.getAction() + ".");
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * Attempts to handle the given intent for FHR document upload. If it cannot, false is returned.
michael@0: *
michael@0: * @param intent must be non-null.
michael@0: */
michael@0: private boolean attemptHandleIntentForUpload(final Intent intent) {
michael@0: if (HealthReportConstants.UPLOAD_FEATURE_DISABLED) {
michael@0: Logger.debug(LOG_TAG, "Health report upload feature is compile-time disabled; not handling intent.");
michael@0: return false;
michael@0: }
michael@0:
michael@0: final String action = intent.getAction();
michael@0: Logger.debug(LOG_TAG, "Health report upload feature is compile-time enabled; attempting to " +
michael@0: "handle intent with action " + action + ".");
michael@0:
michael@0: if (HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF.equals(action)) {
michael@0: handleUploadPrefIntent(intent);
michael@0: return true;
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_HEALTHREPORT_UPLOAD_PREF_METHOD);
michael@0: return true;
michael@0: }
michael@0:
michael@0: return false;
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 handleUploadPrefIntent(Intent intent) {
michael@0: if (!intent.hasExtra("enabled")) {
michael@0: Logger.warn(LOG_TAG, "Got " + HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_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: String profileName = intent.getStringExtra("profileName");
michael@0: String profilePath = intent.getStringExtra("profilePath");
michael@0:
michael@0: if (profileName == null || profilePath == null) {
michael@0: Logger.warn(LOG_TAG, "Got " + HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF + " intent without profilePath or profileName. Ignoring.");
michael@0: return;
michael@0: }
michael@0:
michael@0: Logger.pii(LOG_TAG, "Updating health report upload alarm for profile " + profileName + " at " +
michael@0: profilePath + ".");
michael@0:
michael@0: final SharedPreferences sharedPrefs = getSharedPreferences();
michael@0: final ObsoleteDocumentTracker tracker = new ObsoleteDocumentTracker(sharedPrefs);
michael@0: final boolean hasObsoleteIds = tracker.hasObsoleteIds();
michael@0:
michael@0: if (!enabled) {
michael@0: final Editor editor = sharedPrefs.edit();
michael@0: editor.remove(HealthReportConstants.PREF_LAST_UPLOAD_DOCUMENT_ID);
michael@0:
michael@0: if (hasObsoleteIds) {
michael@0: Logger.debug(LOG_TAG, "Health report upload disabled; scheduling deletion of " + tracker.numberOfObsoleteIds() + " documents.");
michael@0: tracker.limitObsoleteIds();
michael@0: } else {
michael@0: // Primarily intended for debugging and testing.
michael@0: Logger.debug(LOG_TAG, "Health report upload disabled and no deletes to schedule: clearing prefs.");
michael@0: editor.remove(HealthReportConstants.PREF_FIRST_RUN);
michael@0: editor.remove(HealthReportConstants.PREF_NEXT_SUBMISSION);
michael@0: }
michael@0:
michael@0: editor.commit();
michael@0: }
michael@0:
michael@0: // The user can toggle us off or on, or we can have obsolete documents to
michael@0: // remove.
michael@0: final boolean serviceEnabled = hasObsoleteIds || enabled;
michael@0: toggleSubmissionAlarm(this, profileName, profilePath, enabled, serviceEnabled);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Attempts to handle the given intent for FHR data pruning. If it cannot, false is returned.
michael@0: *
michael@0: * @param intent must be non-null.
michael@0: */
michael@0: private boolean attemptHandleIntentForPrune(final Intent intent) {
michael@0: final String action = intent.getAction();
michael@0: Logger.debug(LOG_TAG, "Prune: Attempting to handle intent with action, " + action + ".");
michael@0:
michael@0: if (HealthReportConstants.ACTION_HEALTHREPORT_PRUNE.equals(action)) {
michael@0: handlePruneIntent(intent);
michael@0: return true;
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_HEALTHREPORT_PRUNE_METHOD);
michael@0: return true;
michael@0: }
michael@0:
michael@0: return false;
michael@0: }
michael@0:
michael@0: /**
michael@0: * @param intent must be non-null.
michael@0: */
michael@0: private void handlePruneIntent(final Intent intent) {
michael@0: final String profileName = intent.getStringExtra("profileName");
michael@0: final String profilePath = intent.getStringExtra("profilePath");
michael@0:
michael@0: if (profileName == null || profilePath == null) {
michael@0: Logger.warn(LOG_TAG, "Got " + HealthReportConstants.ACTION_HEALTHREPORT_PRUNE + " intent " +
michael@0: "without profilePath or profileName. Ignoring.");
michael@0: return;
michael@0: }
michael@0:
michael@0: final Class> serviceClass = HealthReportPruneService.class;
michael@0: final Intent service = new Intent(this, serviceClass);
michael@0: service.setAction("prune"); // Intents without actions have their extras removed.
michael@0: service.putExtra("profileName", profileName);
michael@0: service.putExtra("profilePath", profilePath);
michael@0: final PendingIntent pending = PendingIntent.getService(this, 0, service,
michael@0: PendingIntent.FLAG_CANCEL_CURRENT);
michael@0:
michael@0: // Set a regular alarm to start PruneService. Since the various actions that PruneService can
michael@0: // take occur on irregular intervals, we can be more efficient by only starting the Service
michael@0: // when one of these time limits runs out. However, subsequent Service invocations must then
michael@0: // be registered by the PruneService itself, which would fail if the PruneService crashes.
michael@0: // Thus, we set this regular (and slightly inefficient) alarm.
michael@0: Logger.info(LOG_TAG, "Registering " + serviceClass.getSimpleName() + ".");
michael@0: final long pollInterval = getPrunePollInterval();
michael@0: scheduleAlarm(pollInterval, pending);
michael@0: }
michael@0: }