Back to project page battery-monitor-for-android.
The source code is released under:
Copyright (c) 2012 Hisato SHOJI Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softw...
If you think the Android project battery-monitor-for-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package jp.f.dev.android.batterymonitor; /*from w w w. j a va 2s . c om*/ import android.app.AlarmManager; import android.app.Application; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.util.Log; public final class BatteryMonitorApplication extends Application { private static final String TAG = "BatteryMonitorApplication"; private static final String PREFERENCE_NAME = "setting"; private static final String PREFERENCE_KEY_MONITORING = "monitoring"; private static final String PREFERENCE_KEY_PERIOD = "period"; private static Boolean sMonitoring = null; private static Long sMonitoringPeriod = null; private OnSharedPreferenceChangeListener mLister; /* package */final boolean getMonitorStatus() { // lock for getter - setter synchronized (this) { // singleton for performance. if (sMonitoring == null) { SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE); sMonitoring = pref.getBoolean(PREFERENCE_KEY_MONITORING, false); } } return sMonitoring; } /* package */final void setMonitorStatus(final boolean monitoring) { // lock for getter - setter synchronized (this) { // An attribute to store status of recording. // Service and process can be killed by system during recording. // Need to store status attribute so that to handle state properly. SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE); Editor edit = pref.edit(); edit.putBoolean(PREFERENCE_KEY_MONITORING, monitoring); edit.commit(); sMonitoring = monitoring; } } /* package */final long getMonitorPeriod() { synchronized (this) { // singleton for performance. if (sMonitoringPeriod == null) { SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE); sMonitoringPeriod = pref.getLong(PREFERENCE_KEY_PERIOD, Util.DEFAULT_PERIOD); } } return sMonitoringPeriod; } /* package */final void setMonitoringPeriod(final long period) { // lock for getter - setter synchronized (this) { // An attribute to store period of recording. // Service and process can be killed by system during recording. // Need to store status attribute so that to handle state properly. SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE); Editor edit = pref.edit(); edit.putLong(PREFERENCE_KEY_PERIOD, period); edit.commit(); sMonitoringPeriod = period; } } /* package */final void setOnStateChangeListener( final OnStateChangeListener listener) { // unset if already registered. unsetOnStateChangeLister(); // bypass preference state change lister to original listener for // encapsulating SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE); //TODO should be synchronized? mLister = new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences pref, String key) { Log.d(TAG, "onSharedPreferenceChanged called"); if (PREFERENCE_KEY_MONITORING.equals(key) || PREFERENCE_KEY_PERIOD.equals(key)) { listener.onStateChanged(pref.getBoolean( PREFERENCE_KEY_MONITORING, false), pref.getLong( PREFERENCE_KEY_PERIOD, Util.DEFAULT_PERIOD)); } } }; pref.registerOnSharedPreferenceChangeListener(mLister); } /* package */final void unsetOnStateChangeLister() { //TODO should be synchronized? if (mLister != null) { SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE); pref.unregisterOnSharedPreferenceChangeListener(mLister); mLister = null; } } }