Android Open Source - smsproxy Battery Level Observer






From Project

Back to project page smsproxy.

License

The source code is released under:

Copyright (c) 2012 Kei Nakazawa 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 smsproxy listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jp.muo.smsproxy;
//from w  ww .j a v  a  2  s  . co  m
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.BatteryManager;
import android.util.Log;

public class BatteryLevelObserver extends BroadcastReceiver {
  private static final String BATTERY_PREFS_KEY = "bat_level";
  private static final String PREFS_IS_OKAY = "is_okay";
  private static final int BAT_LOW = 15;
  private static final int BAT_OKAY = 20;

  @Override
  public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    if (action.equals(Intent.ACTION_BATTERY_LOW)) {
      BatteryLevelObserver.updateStatus(context);
    } else if (action.equals(Intent.ACTION_BATTERY_OKAY)) {
      BatteryLevelObserver.updateStatus(context);
    }
  }

  public static void updateStatus(Context context) {
    BroadcastReceiver batReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
        context.getApplicationContext().unregisterReceiver(this);
        SharedPreferences prefs = context.getSharedPreferences(BATTERY_PREFS_KEY, Context.MODE_PRIVATE);
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        boolean isLowerTrigger = level <= BAT_LOW;
        boolean isOkayTrigger = level >= BAT_OKAY;
        intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
        boolean isPlugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
        boolean isOkayInPrefs = prefs.getBoolean(PREFS_IS_OKAY, true);
        boolean isBatteryLevelOkay = isOkayInPrefs;
        SharedPreferences.Editor editor = prefs.edit();
        if (isOkayInPrefs) {
          if (isLowerTrigger && !isPlugged) {
            SmsProxyManager mgr = new SmsProxyManager(context);
            Log.d(SmsProxyManager.TAG, String.format(
                "sending battery level notification(level: %d%%, isPlugged: %s", level,
                isPlugged ? "true" : "false"));
            mgr.send(SmsProxyManager.Mode.BATTERY, context.getString(R.string.sms_bat));
            isBatteryLevelOkay = false;
          }
        } else {
          if (isOkayTrigger) {
            isBatteryLevelOkay = true;
          }
        }
        Log.d(SmsProxyManager.TAG,
            String.format("isBatteryLevelOkay: %s", isBatteryLevelOkay ? "true" : "false"));
        if (isOkayInPrefs != isBatteryLevelOkay) {
          editor.putBoolean(PREFS_IS_OKAY, isBatteryLevelOkay);
          editor.commit();
        }
      }
    };
    context.getApplicationContext().registerReceiver(batReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  }
}




Java Source Code List

jp.muo.smsproxy.BatteryLevelObserver.java
jp.muo.smsproxy.RingingReceiver.java
jp.muo.smsproxy.SmsPreferenceActivity.java
jp.muo.smsproxy.SmsProxyManager.java
jp.muo.smsproxy.SmsReceiver.java