ADROID 2.1 架构解析 5 电池_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > ADROID 2.1 架构解析 5 电池

ADROID 2.1 架构解析 5 电池

 2010/12/5 13:03:23  liutianze  http://liutianze.javaeye.com  我要评论(0)
  • 摘要:5电池电池涉及的功能有:BatteryStatus、BatteryHealth、BatteryPresent、BatteryLevel、PlugType、BatteryVoltage、BatteryTemperature、BatteryTechnology等,它是种从下而上的表现方式,即底层电池变化后通过uevent事件向上传递电池的当前状态。5.1硬件调用文件:frameworks/base/services/jni/com_android_server_BatteryService
  • 标签:ADROID架构解析
5 电池
电池涉及的功能有:BatteryStatus、BatteryHealth、BatteryPresent、BatteryLevel、PlugType、BatteryVoltage、BatteryTemperature、BatteryTechnology等,它是种从下而上的表现方式,即底层电池变化后通过uevent事件向上传递电池的当前状态。

5.1 硬件调用
文件:frameworks/base/services/jni/com_android_server_BatteryService.cpp

#define POWER_SUPPLY_PATH "/sys/class/power_supply"

int register_android_server_BatteryService(JNIEnv* env)

{

  ...

  DIR* dir = opendir(POWER_SUPPLY_PATH);

    if (dir == NULL) {

        LOGE("Could not open %s\n", POWER_SUPPLY_PATH);

        return -1;

    }

    while ((entry = readdir(dir))) {

        const char* name = entry->d_name;



        // ignore "." and ".."

        if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {

            continue;

        }



        char buf[20];

        // Look for "type" file in each subdirectory

        snprintf(path, sizeof(path), "%s/%s/type", POWER_SUPPLY_PATH, name);

        int length = readFromFile(path, buf, sizeof(buf));

        if (length > 0) {

            if (buf[length - 1] == '\n')

                buf[length - 1] = 0;



            if (strcmp(buf, "Mains") == 0) {

                snprintf(path, sizeof(path), "%s/%s/online", POWER_SUPPLY_PATH, name);

                if (access(path, R_OK) == 0)

                    gPaths.acOnlinePath = strdup(path);

            }

            else if (strcmp(buf, "USB") == 0) {

                snprintf(path, sizeof(path), "%s/%s/online", POWER_SUPPLY_PATH, name);

                if (access(path, R_OK) == 0)

                    gPaths.usbOnlinePath = strdup(path);

            }

            else if (strcmp(buf, "Battery") == 0) {

                snprintf(path, sizeof(path), "%s/%s/status", POWER_SUPPLY_PATH, name);

                if (access(path, R_OK) == 0)

                    gPaths.batteryStatusPath = strdup(path);

                snprintf(path, sizeof(path), "%s/%s/health", POWER_SUPPLY_PATH, name);

                if (access(path, R_OK) == 0)

                    gPaths.batteryHealthPath = strdup(path);

                snprintf(path, sizeof(path), "%s/%s/present", POWER_SUPPLY_PATH, name);

                if (access(path, R_OK) == 0)

                    gPaths.batteryPresentPath = strdup(path);

                snprintf(path, sizeof(path), "%s/%s/capacity", POWER_SUPPLY_PATH, name);

                if (access(path, R_OK) == 0)

                    gPaths.batteryCapacityPath = strdup(path);



                snprintf(path, sizeof(path), "%s/%s/voltage_now", POWER_SUPPLY_PATH, name);

                if (access(path, R_OK) == 0) {

                    gPaths.batteryVoltagePath = strdup(path);

                    // voltage_now is in microvolts, not millivolts

                    gVoltageDivisor = 1000;

                } else {

                    snprintf(path, sizeof(path), "%s/%s/batt_vol", POWER_SUPPLY_PATH, name);

                    if (access(path, R_OK) == 0)

                        gPaths.batteryVoltagePath = strdup(path);

                }



                snprintf(path, sizeof(path), "%s/%s/temp", POWER_SUPPLY_PATH, name);

                if (access(path, R_OK) == 0) {

                    gPaths.batteryTemperaturePath = strdup(path);

                } else {

                    snprintf(path, sizeof(path), "%s/%s/batt_temp", POWER_SUPPLY_PATH, name);

                    if (access(path, R_OK) == 0)

                        gPaths.batteryTemperaturePath = strdup(path);

                }



                snprintf(path, sizeof(path), "%s/%s/technology", POWER_SUPPLY_PATH, name);

                if (access(path, R_OK) == 0)

                    gPaths.batteryTechnologyPath = strdup(path);

            }

        }

    }

closedir(dir);

扫描/sys/class/power_supply目录下的文件夹,根据其type文件里的字符串来判断是哪一种类型:Mains、USB还是Battery,然后不同的参数对应着不同的文件



jclass clazz = env->FindClass("com/android/server/BatteryService");



    if (clazz == NULL) {

        LOGE("Can't find com/android/server/BatteryService");

        return -1;

    }

   

    gFieldIds.mAcOnline = env->GetFieldID(clazz, "mAcOnline", "Z");

    gFieldIds.mUsbOnline = env->GetFieldID(clazz, "mUsbOnline", "Z");

    gFieldIds.mBatteryStatus = env->GetFieldID(clazz, "mBatteryStatus", "I");

    gFieldIds.mBatteryHealth = env->GetFieldID(clazz, "mBatteryHealth", "I");

    gFieldIds.mBatteryPresent = env->GetFieldID(clazz, "mBatteryPresent", "Z");

    gFieldIds.mBatteryLevel = env->GetFieldID(clazz, "mBatteryLevel", "I");

    gFieldIds.mBatteryTechnology = env->GetFieldID(clazz, "mBatteryTechnology", "Ljava/lang/String;");

    gFieldIds.mBatteryVoltage = env->GetFieldID(clazz, "mBatteryVoltage", "I");

gFieldIds.mBatteryTemperature = env->GetFieldID(clazz, "mBatteryTemperature", "I");

接口说明:

env->FindClass("com/android/server/BatteryService");  获取指定文件的类。

env->GetFieldID(clazz, "mAcOnline", "Z"); 获取指定类的mAcOnline变量值。

以上代码的功能是:将frameworks/base/services/java/com/android/server/BatteryService.java里的各变量值存到全局变量里。



clazz = env->FindClass("android/os/BatteryManager");

    if (clazz == NULL) {

        LOGE("Can't find android/os/BatteryManager");

        return -1;

    }

    gConstants.statusUnknown = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_STATUS_UNKNOWN", "I"));

    gConstants.statusCharging = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_STATUS_CHARGING", "I"));

    gConstants.statusDischarging = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_STATUS_DISCHARGING", "I"));

    gConstants.statusNotCharging = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_STATUS_NOT_CHARGING", "I"));

    gConstants.statusFull = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_STATUS_FULL", "I"));

    gConstants.healthUnknown = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_HEALTH_UNKNOWN", "I"));

    gConstants.healthGood = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_HEALTH_GOOD", "I"));

    gConstants.healthOverheat = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_HEALTH_OVERHEAT", "I"));

    gConstants.healthDead = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_HEALTH_DEAD", "I"));

    gConstants.healthOverVoltage = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_HEALTH_OVER_VOLTAGE", "I"));

    gConstants.healthUnspecifiedFailure = env->GetStaticIntField(clazz,

            env->GetStaticFieldID(clazz, "BATTERY_HEALTH_UNSPECIFIED_FAILURE", "I"));

以上代码的功能是:将frameworks/base/core/java/android/os/BatteryManager.java里类的变量值存到全局变量里。



...

}

static void setBooleanField(JNIEnv* env, jobject obj, const char* path, jfieldID fieldID)

{

    const int SIZE = 16;

    char buf[SIZE];

   

    jboolean value = false;

    if (readFromFile(path, buf, SIZE) > 0) {

        if (buf[0] == '1') {

            value = true;

        }

    }

    env->SetBooleanField(obj, fieldID, value);

}



static void setIntField(JNIEnv* env, jobject obj, const char* path, jfieldID fieldID)

{

    const int SIZE = 128;

    char buf[SIZE];

   

    jint value = 0;

    if (readFromFile(path, buf, SIZE) > 0) {

        value = atoi(buf);

    }

    env->SetIntField(obj, fieldID, value);

}



static void setVoltageField(JNIEnv* env, jobject obj, const char* path, jfieldID fieldID)

{

    const int SIZE = 128;

    char buf[SIZE];



    jint value = 0;

    if (readFromFile(path, buf, SIZE) > 0) {

        value = atoi(buf);

        value /= gVoltageDivisor;

    }

    env->SetIntField(obj, fieldID, value);

}

static void android_server_BatteryService_update(JNIEnv* env, jobject obj)

{

    setBooleanField(env, obj, gPaths.acOnlinePath, gFieldIds.mAcOnline);

    setBooleanField(env, obj, gPaths.usbOnlinePath, gFieldIds.mUsbOnline);

    setBooleanField(env, obj, gPaths.batteryPresentPath, gFieldIds.mBatteryPresent);

   

    setIntField(env, obj, gPaths.batteryCapacityPath, gFieldIds.mBatteryLevel);

    setVoltageField(env, obj, gPaths.batteryVoltagePath, gFieldIds.mBatteryVoltage);

    setIntField(env, obj, gPaths.batteryTemperaturePath, gFieldIds.mBatteryTemperature);

   

    const int SIZE = 128;

    char buf[SIZE];

   

    if (readFromFile(gPaths.batteryStatusPath, buf, SIZE) > 0)

        env->SetIntField(obj, gFieldIds.mBatteryStatus, getBatteryStatus(buf));

    else

        env->SetIntField(obj, gFieldIds.mBatteryStatus,

                         gConstants.statusUnknown);

   

    if (readFromFile(gPaths.batteryHealthPath, buf, SIZE) > 0)

        env->SetIntField(obj, gFieldIds.mBatteryHealth, getBatteryHealth(buf));



    if (readFromFile(gPaths.batteryTechnologyPath, buf, SIZE) > 0)

        env->SetObjectField(obj, gFieldIds.mBatteryTechnology, env->NewStringUTF(buf));

}



static JNINativeMethod sMethods[] = {

     /* name, signature, funcPtr */

       {"native_update", "()V", (void*)android_server_BatteryService_update},

};

接口说明:

env->SetIntField(obj, fieldID, value); 将指定类的变量以整型付值。

env->SetBooleanField(obj, fieldID, value); 将指定类的变量以布尔付值。

以上代码的功能是:

将全局变量的值付给frameworks/base/core/java/android/os/BatteryManager.java里类的变量;并且将ative_update与android_server_BatteryService_update绑定。

5.2 电池服务
文件:frameworks/base/services/java/com/android/server/BatteryService.java

mUEventObserver.startObserving("SUBSYSTEM=power_supply");

private UEventObserver mUEventObserver = new UEventObserver() {

        @Override

        public void onUEvent(UEventObserver.UEvent event) {

            update();

        }

    };

监控uevent事件,响应SUBSYSTEM=power_supply的事件。

private synchronized final void update() {

        native_update();

            ...

        if (mAcOnline) {

            mPlugType = BatteryManager.BATTERY_PLUGGED_AC;

        } else if (mUsbOnline) {

            mPlugType = BatteryManager.BATTERY_PLUGGED_USB;

        } else {

            mPlugType = BATTERY_PLUGGED_NONE;

        }

        ...

        sendIntent();

              ...

    }



    private final void sendIntent() {

        //  Pack up the values and broadcast them to everyone

        Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);

        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);

        try {

            mBatteryStats.setOnBattery(mPlugType == BATTERY_PLUGGED_NONE, mBatteryLevel);

        } catch (RemoteException e) {

            // Should never happen.

        }

       

        int icon = getIcon(mBatteryLevel);



        intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);

        intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);

        intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);

        intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);

        intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);

        intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);

        intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);

        intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);

        intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);

        intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);



        if (false) {

            Log.d(TAG, "updateBattery level:" + mBatteryLevel +

                    " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +

                    " health:" + mBatteryHealth +  " present:" + mBatteryPresent +

                    " voltage: " + mBatteryVoltage +

                    " temperature: " + mBatteryTemperature +

                    " technology: " + mBatteryTechnology +

                    " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline +

                    " icon:" + icon );

        }



        ActivityManagerNative.broadcastStickyIntent(intent, null);

    }

以上代码的功能是:

通过native_update();更新各状态变量的值,如:

private boolean mAcOnline;

    private boolean mUsbOnline;

    private int mBatteryStatus;

    private int mBatteryHealth;

    private boolean mBatteryPresent;

    private int mBatteryLevel;

    private int mBatteryVoltage;

    private int mBatteryTemperature;

    private String mBatteryTechnology;

    private boolean mBatteryLevelCritical;

然后通过:

intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);

        intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);

        intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);

        intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);

        intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);

        intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);

        intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);

        intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);

        intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);

        intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);

              ActivityManagerNative.broadcastStickyIntent(intent, null);

发布给各实体窗口取用,EXTRA_STATUS等对应的字符是:

文件:frameworks/base/core/java/android/os/BatteryManager.java

public static final String EXTRA_STATUS = "status";

    public static final String EXTRA_HEALTH = "health";

    public static final String EXTRA_PRESENT = "present";

    public static final String EXTRA_LEVEL = "level";

    public static final String EXTRA_SCALE = "scale";

    public static final String EXTRA_ICON_SMALL = "icon-small";

    public static final String EXTRA_PLUGGED = "plugged";

    public static final String EXTRA_VOLTAGE = "voltage";

    public static final String EXTRA_TEMPERATURE = "temperature";

    public static final String EXTRA_TECHNOLOGY = "technology";

5.3 显示电池状态
文件:packages/apps/DeskClock/src/com/android/deskclock/DeskClock.java

private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {

        @Override

        public void onReceive(Context context, Intent intent) {

            final String action = intent.getAction();

            if (Intent.ACTION_DATE_CHANGED.equals(action)) {

                refreshDate();

            } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {

                handleBatteryUpdate(

                    intent.getIntExtra("status", BATTERY_STATUS_UNKNOWN),

                    intent.getIntExtra("level", 0));

            } else if (Intent.ACTION_DOCK_EVENT.equals(action)) {

                int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, -1);

                if (DEBUG) Log.d(LOG_TAG, "ACTION_DOCK_EVENT, state=" + state);

                if (state == Intent.EXTRA_DOCK_STATE_UNDOCKED) {

                    if (mLaunchedFromDock) {

                        // moveTaskToBack(false);

                        finish();

                    }

                    mLaunchedFromDock = false;

                }

            }

        }

    };

// Adapted from KeyguardUpdateMonitor.java

    private void handleBatteryUpdate(int plugStatus, int batteryLevel) {

        final boolean pluggedIn = (plugStatus == BATTERY_STATUS_CHARGING || plugStatus == BATTERY_STATUS_FULL);

        if (pluggedIn != mPluggedIn) {

            setWakeLock(pluggedIn);



            if (pluggedIn) {

                // policy: update weather info when attaching to power

                requestWeatherDataFetch();

            }

        }

        if (pluggedIn != mPluggedIn || batteryLevel != mBatteryLevel) {

            mBatteryLevel = batteryLevel;

            mPluggedIn = pluggedIn;

            refreshBattery();

        }

    }



    private void refreshBattery() {

        if (mBatteryDisplay == null) return;



        if (mPluggedIn /* || mBatteryLevel < LOW_BATTERY_THRESHOLD */) {

            mBatteryDisplay.setCompoundDrawablesWithIntrinsicBounds(

                0, 0, android.R.drawable.ic_lock_idle_charging, 0);

            mBatteryDisplay.setText(

                getString(R.string.battery_charging_level, mBatteryLevel));

            mBatteryDisplay.setVisibility(View.VISIBLE);

        } else {

            mBatteryDisplay.setVisibility(View.INVISIBLE);

        }

    }

由以上代码可知,程序通过接收广播信息,获取电池的当前节数:intent.getIntExtra("level", 0));,然后再将其显示。
  • 相关文章
发表评论
用户名: 匿名