improved power state receiver

This commit is contained in:
Mariotaku Lee 2015-09-30 21:42:26 +08:00
parent 970877fdd5
commit 5abcdaab5f
4 changed files with 38 additions and 10 deletions

View File

@ -544,6 +544,8 @@
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
<action android:name="android.intent.action.BATTERY_OKAY" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>

View File

@ -22,6 +22,7 @@ package edu.tsinghua.hotmobi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.BatteryManager;
@ -146,7 +147,12 @@ public class HotMobiLogger {
return prefs.getLong(LAST_UPLOAD_TIME, -1);
}
public static void logPowerBroadcast(Context context) {
logPowerBroadcast(context, context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)));
}
public static void logPowerBroadcast(Context context, Intent intent) {
if (intent == null) return;
if (!intent.hasExtra(BatteryManager.EXTRA_LEVEL) || !intent.hasExtra(BatteryManager.EXTRA_SCALE) ||
!intent.hasExtra(BatteryManager.EXTRA_STATUS)) return;
final BatteryRecord record = new BatteryRecord();

View File

@ -22,6 +22,7 @@ package org.mariotaku.twidere.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import edu.tsinghua.hotmobi.HotMobiLogger;
@ -29,8 +30,15 @@ import edu.tsinghua.hotmobi.HotMobiLogger;
* Created by mariotaku on 15/9/29.
*/
public class PowerStateReceiver extends BroadcastReceiver {
private static boolean serviceReceiverStarted;
public static void setServiceReceiverStarted(boolean started) {
PowerStateReceiver.serviceReceiverStarted = started;
}
@Override
public void onReceive(Context context, Intent intent) {
HotMobiLogger.logPowerBroadcast(context, intent);
if (serviceReceiverStarted) return;
HotMobiLogger.logPowerBroadcast(context);
}
}

View File

@ -36,6 +36,7 @@ import org.mariotaku.twidere.model.AccountPreferences;
import org.mariotaku.twidere.provider.TwidereDataStore.DirectMessages;
import org.mariotaku.twidere.provider.TwidereDataStore.Mentions;
import org.mariotaku.twidere.provider.TwidereDataStore.Statuses;
import org.mariotaku.twidere.receiver.PowerStateReceiver;
import org.mariotaku.twidere.util.AsyncTwitterWrapper;
import org.mariotaku.twidere.util.SharedPreferencesWrapper;
@ -123,10 +124,19 @@ public class RefreshService extends Service implements Constants {
};
private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
private final BroadcastReceiver mPowerStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
HotMobiLogger.logPowerBroadcast(context, intent);
switch (intent.getAction()) {
case Intent.ACTION_BATTERY_CHANGED: {
HotMobiLogger.logPowerBroadcast(context, intent);
break;
}
default: {
HotMobiLogger.logPowerBroadcast(context);
break;
}
}
}
};
@ -157,18 +167,20 @@ public class RefreshService extends Service implements Constants {
refreshFilter.addAction(BROADCAST_RESCHEDULE_DIRECT_MESSAGES_REFRESHING);
registerReceiver(mStateReceiver, refreshFilter);
final IntentFilter batteryFilter = new IntentFilter();
refreshFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
refreshFilter.addAction(Intent.ACTION_BATTERY_OKAY);
refreshFilter.addAction(Intent.ACTION_BATTERY_LOW);
refreshFilter.addAction(Intent.ACTION_POWER_CONNECTED);
refreshFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
registerReceiver(mBatteryReceiver, batteryFilter);
batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
batteryFilter.addAction(Intent.ACTION_BATTERY_OKAY);
batteryFilter.addAction(Intent.ACTION_BATTERY_LOW);
batteryFilter.addAction(Intent.ACTION_POWER_CONNECTED);
batteryFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
registerReceiver(mPowerStateReceiver, batteryFilter);
PowerStateReceiver.setServiceReceiverStarted(true);
startAutoRefresh();
}
@Override
public void onDestroy() {
unregisterReceiver(mBatteryReceiver);
PowerStateReceiver.setServiceReceiverStarted(false);
unregisterReceiver(mPowerStateReceiver);
unregisterReceiver(mStateReceiver);
if (hasAutoRefreshAccounts(this)) {
// Auto refresh enabled, so I will try to start service after it was