src/core/android/SDL_android.cpp
changeset 6448 64a6297a8b93
parent 6392 fa7eb111f994
child 6464 ab55284b389f
     1.1 --- a/src/core/android/SDL_android.cpp	Thu Sep 20 22:40:23 2012 -0700
     1.2 +++ b/src/core/android/SDL_android.cpp	Sun Sep 23 01:37:44 2012 -0700
     1.3 @@ -735,6 +735,96 @@
     1.4      return Android_JNI_FileClose(ctx, true);
     1.5  }
     1.6  
     1.7 +// returns 0 on success or -1 on error (others undefined then)
     1.8 +// returns truthy or falsy value in plugged, charged and battery
     1.9 +// returns the value in seconds and percent or -1 if not available
    1.10 +extern "C" int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent)
    1.11 +{
    1.12 +    LocalReferenceHolder refs;
    1.13 +    JNIEnv* env = Android_JNI_GetEnv();
    1.14 +    if (!refs.init(env)) {
    1.15 +        return -1;
    1.16 +    }
    1.17 +
    1.18 +    jmethodID mid;
    1.19 +
    1.20 +    mid = env->GetStaticMethodID(mActivityClass, "getContext", "()Landroid/content/Context;");
    1.21 +    jobject context = env->CallStaticObjectMethod(mActivityClass, mid);
    1.22 +
    1.23 +    jstring action = env->NewStringUTF("android.intent.action.BATTERY_CHANGED");
    1.24 +
    1.25 +    jclass cls = env->FindClass("android/content/IntentFilter");
    1.26 +
    1.27 +    mid = env->GetMethodID(cls, "<init>", "(Ljava/lang/String;)V");
    1.28 +    jobject filter = env->NewObject(cls, mid, action);
    1.29 +
    1.30 +    env->DeleteLocalRef(action);
    1.31 +
    1.32 +    mid = env->GetMethodID(mActivityClass, "registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;");
    1.33 +    jobject intent = env->CallObjectMethod(context, mid, NULL, filter);
    1.34 +
    1.35 +    env->DeleteLocalRef(filter);
    1.36 +
    1.37 +    cls = env->GetObjectClass(intent);
    1.38 +
    1.39 +    jstring iname;
    1.40 +    jmethodID imid = env->GetMethodID(cls, "getIntExtra", "(Ljava/lang/String;I)I");
    1.41 +
    1.42 +#define GET_INT_EXTRA(var, key) \
    1.43 +    iname = env->NewStringUTF(key); \
    1.44 +    int var = env->CallIntMethod(intent, imid, iname, -1); \
    1.45 +    env->DeleteLocalRef(iname);
    1.46 +
    1.47 +    jstring bname;
    1.48 +    jmethodID bmid = env->GetMethodID(cls, "getBooleanExtra", "(Ljava/lang/String;Z)Z");
    1.49 +
    1.50 +#define GET_BOOL_EXTRA(var, key) \
    1.51 +    bname = env->NewStringUTF(key); \
    1.52 +    int var = env->CallBooleanMethod(intent, bmid, bname, JNI_FALSE); \
    1.53 +    env->DeleteLocalRef(bname);
    1.54 +
    1.55 +    if (plugged) {
    1.56 +        GET_INT_EXTRA(plug, "plugged") // == BatteryManager.EXTRA_PLUGGED (API 5)
    1.57 +        if (plug == -1) {
    1.58 +            return -1;
    1.59 +        }
    1.60 +        // 1 == BatteryManager.BATTERY_PLUGGED_AC
    1.61 +        // 2 == BatteryManager.BATTERY_PLUGGED_USB
    1.62 +        *plugged = (0 < plug) ? 1 : 0;
    1.63 +    }
    1.64 +
    1.65 +    if (charged) {
    1.66 +        GET_INT_EXTRA(status, "status") // == BatteryManager.EXTRA_STATUS (API 5)
    1.67 +        if (status == -1) {
    1.68 +            return -1;
    1.69 +        }
    1.70 +        // 5 == BatteryManager.BATTERY_STATUS_FULL
    1.71 +        *charged = (status == 5) ? 1 : 0;
    1.72 +    }
    1.73 +
    1.74 +    if (battery) {
    1.75 +        GET_BOOL_EXTRA(present, "present") // == BatteryManager.EXTRA_PRESENT (API 5)
    1.76 +        *battery = present ? 1 : 0;
    1.77 +    }
    1.78 +
    1.79 +    if (seconds) {
    1.80 +        *seconds = -1; // not possible
    1.81 +    }
    1.82 +
    1.83 +    if (percent) {
    1.84 +        GET_INT_EXTRA(level, "level") // == BatteryManager.EXTRA_LEVEL (API 5)
    1.85 +        GET_INT_EXTRA(scale, "scale") // == BatteryManager.EXTRA_SCALE (API 5)
    1.86 +        if ((level == -1) || (scale == -1)) {
    1.87 +            return -1;
    1.88 +        }
    1.89 +        *percent = level * 100 / scale;
    1.90 +    }
    1.91 +
    1.92 +    env->DeleteLocalRef(intent);
    1.93 +
    1.94 +    return 0;
    1.95 +}
    1.96 +
    1.97  // sends message to be handled on the UI event dispatch thread
    1.98  extern "C" int Android_JNI_SendMessage(int command, int param)
    1.99  {