mobile/android/base/background/healthreport/prune/HealthReportPruneService.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/background/healthreport/prune/HealthReportPruneService.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.background.healthreport.prune;
     1.9 +
    1.10 +import org.mozilla.gecko.background.BackgroundService;
    1.11 +import org.mozilla.gecko.background.common.GlobalConstants;
    1.12 +import org.mozilla.gecko.background.common.log.Logger;
    1.13 +import org.mozilla.gecko.background.healthreport.HealthReportConstants;
    1.14 +
    1.15 +import android.content.Intent;
    1.16 +import android.content.SharedPreferences;
    1.17 +import android.os.IBinder;
    1.18 +
    1.19 +/**
    1.20 + * A <code>Service</code> to prune unnecessary or excessive health report data.
    1.21 + *
    1.22 + * We extend <code>IntentService</code>, rather than just <code>Service</code>,
    1.23 + * because this gives us a worker thread to avoid excessive main-thread disk access.
    1.24 + */
    1.25 +public class HealthReportPruneService extends BackgroundService {
    1.26 +  public static final String LOG_TAG = HealthReportPruneService.class.getSimpleName();
    1.27 +  public static final String WORKER_THREAD_NAME = LOG_TAG + "Worker";
    1.28 +
    1.29 +  public HealthReportPruneService() {
    1.30 +    super(WORKER_THREAD_NAME);
    1.31 +  }
    1.32 +
    1.33 +  @Override
    1.34 +  public IBinder onBind(Intent intent) {
    1.35 +    return null;
    1.36 +  }
    1.37 +
    1.38 +  protected SharedPreferences getSharedPreferences() {
    1.39 +    return this.getSharedPreferences(HealthReportConstants.PREFS_BRANCH, GlobalConstants.SHARED_PREFERENCES_MODE);
    1.40 +  }
    1.41 +
    1.42 +  @Override
    1.43 +  public void onHandleIntent(Intent intent) {
    1.44 +    Logger.setThreadLogTag(HealthReportConstants.GLOBAL_LOG_TAG);
    1.45 +
    1.46 +    // Intent can be null. Bug 1025937.
    1.47 +    if (intent == null) {
    1.48 +      Logger.debug(LOG_TAG, "Short-circuiting on null intent.");
    1.49 +      return;
    1.50 +    }
    1.51 +
    1.52 +    Logger.debug(LOG_TAG, "Handling prune intent.");
    1.53 +
    1.54 +    if (!isIntentValid(intent)) {
    1.55 +      Logger.warn(LOG_TAG, "Intent not valid - returning.");
    1.56 +      return;
    1.57 +    }
    1.58 +
    1.59 +    final String profileName = intent.getStringExtra("profileName");
    1.60 +    final String profilePath = intent.getStringExtra("profilePath");
    1.61 +    Logger.debug(LOG_TAG, "Ticking for profile " + profileName + " at " + profilePath + ".");
    1.62 +    final PrunePolicy policy = getPrunePolicy(profilePath);
    1.63 +    policy.tick(System.currentTimeMillis());
    1.64 +  }
    1.65 +
    1.66 +  // Generator function wraps constructor for testing purposes.
    1.67 +  protected PrunePolicy getPrunePolicy(final String profilePath) {
    1.68 +    final PrunePolicyStorage storage = new PrunePolicyDatabaseStorage(this, profilePath);
    1.69 +    return new PrunePolicy(storage, getSharedPreferences());
    1.70 +  }
    1.71 +
    1.72 +  /**
    1.73 +   * @param intent must be non-null.
    1.74 +   * @return true if the supplied intent contains both profileName and profilePath.
    1.75 +   */
    1.76 +  private static boolean isIntentValid(final Intent intent) {
    1.77 +    boolean isValid = true;
    1.78 +
    1.79 +    final String profileName = intent.getStringExtra("profileName");
    1.80 +    if (profileName == null) {
    1.81 +      Logger.warn(LOG_TAG, "Got intent without profileName.");
    1.82 +      isValid = false;
    1.83 +    }
    1.84 +
    1.85 +    final String profilePath = intent.getStringExtra("profilePath");
    1.86 +    if (profilePath == null) {
    1.87 +      Logger.warn(LOG_TAG, "Got intent without profilePath.");
    1.88 +      isValid = false;
    1.89 +    }
    1.90 +
    1.91 +    return isValid;
    1.92 +  }
    1.93 +}

mercurial