How to write a DashClock Extension: Battery Extension example

With DashClock API it is very easy to write a DashClock Extension.

UPDATE 27/05/2013---------------------------------------------------.
You can find it in Google Play.

In this github repository you can find a full version.
------------------------------------------------------------------------------.


Here you can find a very simple Battery Extension.
This is just an example (bit raw) that should be improved !!.

We are going to write our service BatteryExtension that extends the DashClockExtension class.
public class BatteryExtension extends DashClockExtension {

 private static final String TAG = "BatteryExtension";

 @Override
 protected void onUpdateData(int reason) {

  IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  Intent batteryStatus = getApplicationContext().registerReceiver(null,
    ifilter);

  // Level
  int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

  //health
  //int health = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
    
  // How are we charging?
  int chargePlug = batteryStatus.getIntExtra(
    BatteryManager.EXTRA_PLUGGED, -1);
  boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
  boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
  boolean wirelessCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS;

  String charge = "";
  if (usbCharge)
   charge = getString(R.string.charge_usb);
  else if (acCharge)
   charge = getString(R.string.charge_ac);
  else if (wirelessCharge)
   charge = getString(R.string.charge_wireless);

  // Are we charging / charged?
  int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING;
  boolean isFull = status == BatteryManager.BATTERY_STATUS_FULL;
  //boolean isDischarging = status == BatteryManager.BATTERY_STATUS_DISCHARGING;

  String charging = getString(R.string.discharging);
  if (isFull)
   charging=getString(R.string.full);
  else if (isCharging)
   charging = getString(R.string.charging);
  
  
  
  //String technology = batteryStatus.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
  
  int temperature = batteryStatus.getIntExtra(
    BatteryManager.EXTRA_TEMPERATURE, 0);
  int voltage = batteryStatus
    .getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);

  String umTemp=getString(R.string.celsius);
  String umMv=getString(R.string.mv);
  
  // Publish the extension data update.
  publishUpdate(new ExtensionData()
    .visible(true)
    .icon(R.drawable.ic_extension_battery)
    .status("" + level + "%")
    .expandedTitle("" + level + "% " + charging)
    .expandedBody(
      charge +  " - " + temperature/10 + umTemp+" - "
        + voltage+umMv));

 }
}

We first retrieve information using
     IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
     Intent batteryStatus = getApplicationContext().registerReceiver(null,
    ifilter);
and then publish our ExtensionData with publishUpdate().
Add the corresponding service> tag to our AndroidManifest.xml file

    <service
            android:name=".BatteryExtension"
            android:icon="@drawable/ic_extension_battery"
            android:label="@string/extension_title"
            android:permission="com.google.android.apps.dashclock.permission.READ_EXTENSION_DATA" >
            <intent-filter>
                <action android:name="com.google.android.apps.dashclock.Extension" />
            </intent-filter>

            <meta-data
                android:name="protocolVersion"
                android:value="1" />
            <meta-data
                android:name="description"
                android:value="@string/extension_description" />
            
        </service>
And that's all....

What are the next steps?
  1. Add a settings activity for example to choose frequency update.Actually onUpdateData(int) will by default be called roughly once per hour, to select the unit of measurement for temperature..
  2. Add a listener to monitor changes in charging state
  3. Calculate remaining time
  4. ......

UPDATE: I've improved code above.
In github repository you can find updated code.

UPDATE 2: In this github repository you can find a full version.

In the next post I'll try to write a Dial Extension.

You can get code from GitHub:

Comments

  1. the two screen shots where you add the extension, where is that accesses from?

    ReplyDelete
    Replies
    1. ok got it, needed to install dashclock widget first :$

      Delete

Post a Comment

Popular posts from this blog

AntiPattern: freezing a UI with Broadcast Receiver

How to centralize the support libraries dependencies in gradle

NotificationListenerService and kitkat