PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/actionslib/src/com/google/android/marvin/commands/impls/BatteryLevelCommand.java

http://eyes-free.googlecode.com/
Java | 38 lines | 25 code | 6 blank | 7 comment | 4 complexity | 74f738c63c3c9ac41b1a92e7fbf22a8d MD5 | raw file
 1// Copyright 2010 Google Inc. All Rights Reserved.
 2
 3package com.google.android.marvin.commands.impls;
 4
 5import com.google.android.marvin.actionslib.R;
 6import com.google.android.marvin.commands.CommandExecutor;
 7
 8import android.content.Context;
 9import android.content.Intent;
10import android.content.IntentFilter;
11import android.os.BatteryManager;
12
13/**
14 * A command to speak the current battery level.
15 * 
16 * @author clsimon@google.com (Cheryl Simon)
17 *
18 */
19public class BatteryLevelCommand implements CommandExecutor {
20    
21    public String executeCommand(Context context) {
22        IntentFilter battFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
23        Intent intent = context.getApplicationContext().registerReceiver(null, battFilter);
24
25        int rawlevel = intent.getIntExtra("level", -1);
26        int scale = intent.getIntExtra("scale", -1);
27        int status = intent.getIntExtra("status", -1);
28        String message = "";
29        if (rawlevel >= 0 && scale > 0) {
30            int batteryLevel = (rawlevel * 100) / scale;
31            message = Integer.toString(batteryLevel) + "%";
32        }
33        if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
34            message = message + " " + context.getString(R.string.charging);
35        }
36        return message;
37    }
38}