mobile/android/base/background/healthreport/prune/HealthReportPruneService.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.healthreport.prune;
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 import org.mozilla.gecko.background.healthreport.HealthReportConstants;
michael@0 11
michael@0 12 import android.content.Intent;
michael@0 13 import android.content.SharedPreferences;
michael@0 14 import android.os.IBinder;
michael@0 15
michael@0 16 /**
michael@0 17 * A <code>Service</code> to prune unnecessary or excessive health report data.
michael@0 18 *
michael@0 19 * We extend <code>IntentService</code>, rather than just <code>Service</code>,
michael@0 20 * because this gives us a worker thread to avoid excessive main-thread disk access.
michael@0 21 */
michael@0 22 public class HealthReportPruneService extends BackgroundService {
michael@0 23 public static final String LOG_TAG = HealthReportPruneService.class.getSimpleName();
michael@0 24 public static final String WORKER_THREAD_NAME = LOG_TAG + "Worker";
michael@0 25
michael@0 26 public HealthReportPruneService() {
michael@0 27 super(WORKER_THREAD_NAME);
michael@0 28 }
michael@0 29
michael@0 30 @Override
michael@0 31 public IBinder onBind(Intent intent) {
michael@0 32 return null;
michael@0 33 }
michael@0 34
michael@0 35 protected SharedPreferences getSharedPreferences() {
michael@0 36 return this.getSharedPreferences(HealthReportConstants.PREFS_BRANCH, GlobalConstants.SHARED_PREFERENCES_MODE);
michael@0 37 }
michael@0 38
michael@0 39 @Override
michael@0 40 public void onHandleIntent(Intent intent) {
michael@0 41 Logger.setThreadLogTag(HealthReportConstants.GLOBAL_LOG_TAG);
michael@0 42
michael@0 43 // Intent can be null. Bug 1025937.
michael@0 44 if (intent == null) {
michael@0 45 Logger.debug(LOG_TAG, "Short-circuiting on null intent.");
michael@0 46 return;
michael@0 47 }
michael@0 48
michael@0 49 Logger.debug(LOG_TAG, "Handling prune intent.");
michael@0 50
michael@0 51 if (!isIntentValid(intent)) {
michael@0 52 Logger.warn(LOG_TAG, "Intent not valid - returning.");
michael@0 53 return;
michael@0 54 }
michael@0 55
michael@0 56 final String profileName = intent.getStringExtra("profileName");
michael@0 57 final String profilePath = intent.getStringExtra("profilePath");
michael@0 58 Logger.debug(LOG_TAG, "Ticking for profile " + profileName + " at " + profilePath + ".");
michael@0 59 final PrunePolicy policy = getPrunePolicy(profilePath);
michael@0 60 policy.tick(System.currentTimeMillis());
michael@0 61 }
michael@0 62
michael@0 63 // Generator function wraps constructor for testing purposes.
michael@0 64 protected PrunePolicy getPrunePolicy(final String profilePath) {
michael@0 65 final PrunePolicyStorage storage = new PrunePolicyDatabaseStorage(this, profilePath);
michael@0 66 return new PrunePolicy(storage, getSharedPreferences());
michael@0 67 }
michael@0 68
michael@0 69 /**
michael@0 70 * @param intent must be non-null.
michael@0 71 * @return true if the supplied intent contains both profileName and profilePath.
michael@0 72 */
michael@0 73 private static boolean isIntentValid(final Intent intent) {
michael@0 74 boolean isValid = true;
michael@0 75
michael@0 76 final String profileName = intent.getStringExtra("profileName");
michael@0 77 if (profileName == null) {
michael@0 78 Logger.warn(LOG_TAG, "Got intent without profileName.");
michael@0 79 isValid = false;
michael@0 80 }
michael@0 81
michael@0 82 final String profilePath = intent.getStringExtra("profilePath");
michael@0 83 if (profilePath == null) {
michael@0 84 Logger.warn(LOG_TAG, "Got intent without profilePath.");
michael@0 85 isValid = false;
michael@0 86 }
michael@0 87
michael@0 88 return isValid;
michael@0 89 }
michael@0 90 }

mercurial