Android: Integrates native and java SystemMonitor.
BUG=154293 Review URL: https://chromiumcodereview.appspot.com/11027024 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162124 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
@@ -5,13 +5,14 @@
|
|||||||
#include "base/android/base_jni_registrar.h"
|
#include "base/android/base_jni_registrar.h"
|
||||||
|
|
||||||
#include "base/basictypes.h"
|
#include "base/basictypes.h"
|
||||||
#include "base/message_pump_android.h"
|
|
||||||
#include "base/android/build_info.h"
|
#include "base/android/build_info.h"
|
||||||
#include "base/android/jni_android.h"
|
#include "base/android/jni_android.h"
|
||||||
#include "base/android/jni_registrar.h"
|
#include "base/android/jni_registrar.h"
|
||||||
#include "base/android/locale_utils.h"
|
#include "base/android/locale_utils.h"
|
||||||
#include "base/android/path_service_android.h"
|
#include "base/android/path_service_android.h"
|
||||||
#include "base/android/path_utils.h"
|
#include "base/android/path_utils.h"
|
||||||
|
#include "base/message_pump_android.h"
|
||||||
|
#include "base/system_monitor/system_monitor_android.h"
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
namespace android {
|
namespace android {
|
||||||
@@ -22,6 +23,7 @@ static RegistrationMethod kBaseRegisteredMethods[] = {
|
|||||||
{ "PathService", base::android::RegisterPathService },
|
{ "PathService", base::android::RegisterPathService },
|
||||||
{ "PathUtils", base::android::RegisterPathUtils },
|
{ "PathUtils", base::android::RegisterPathUtils },
|
||||||
{ "SystemMessageHandler", base::MessagePumpForUI::RegisterBindings },
|
{ "SystemMessageHandler", base::MessagePumpForUI::RegisterBindings },
|
||||||
|
{ "SystemMonitor", base::RegisterSystemMonitor },
|
||||||
};
|
};
|
||||||
|
|
||||||
bool RegisterJni(JNIEnv* env) {
|
bool RegisterJni(JNIEnv* env) {
|
||||||
|
@@ -0,0 +1,23 @@
|
|||||||
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
package org.chromium.base;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A BroadcastReceiver that listens to changes in power status and notifies
|
||||||
|
* SystemMonitor.
|
||||||
|
* It's instantiated by the framework via the application intent-filter
|
||||||
|
* declared in its manifest.
|
||||||
|
*/
|
||||||
|
public class PowerStatusReceiver extends BroadcastReceiver {
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
SystemMonitor.onBatteryChargingChanged(intent);
|
||||||
|
}
|
||||||
|
}
|
64
base/android/java/src/org/chromium/base/SystemMonitor.java
Normal file
64
base/android/java/src/org/chromium/base/SystemMonitor.java
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
package org.chromium.base;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.os.BatteryManager;
|
||||||
|
import android.os.Looper;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integrates native SystemMonitor with the java side.
|
||||||
|
*/
|
||||||
|
@JNINamespace("base::android")
|
||||||
|
public class SystemMonitor {
|
||||||
|
private static SystemMonitor sInstance;
|
||||||
|
|
||||||
|
private boolean mIsBatteryPower;
|
||||||
|
|
||||||
|
public static void createForTests(Context context) {
|
||||||
|
// Applications will create this once the
|
||||||
|
// JNI side has been fully wired up both sides.
|
||||||
|
// For tests, we just need native -> java, that is,
|
||||||
|
// we don't need to notify java -> native on creation.
|
||||||
|
sInstance = new SystemMonitor();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void create(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new SystemMonitor();
|
||||||
|
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
||||||
|
Intent batteryStatusIntent = context.registerReceiver(null, ifilter);
|
||||||
|
onBatteryChargingChanged(batteryStatusIntent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SystemMonitor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void onBatteryChargingChanged(Intent intent) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
// We may be called by the framework intent-filter before being
|
||||||
|
// fully initialized. This is not a problem, since our constructor
|
||||||
|
// will check for the state later on.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
||||||
|
// If we're not plugged, assume we're running on battery power.
|
||||||
|
sInstance.mIsBatteryPower = chargePlug != BatteryManager.BATTERY_PLUGGED_USB &&
|
||||||
|
chargePlug != BatteryManager.BATTERY_PLUGGED_AC;
|
||||||
|
nativeOnBatteryChargingChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
@CalledByNative
|
||||||
|
private static boolean isBatteryPower() {
|
||||||
|
return sInstance.mIsBatteryPower;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static native void nativeOnBatteryChargingChanged();
|
||||||
|
|
||||||
|
}
|
@@ -968,6 +968,7 @@
|
|||||||
'android/java/src/org/chromium/base/PathService.java',
|
'android/java/src/org/chromium/base/PathService.java',
|
||||||
'android/java/src/org/chromium/base/PathUtils.java',
|
'android/java/src/org/chromium/base/PathUtils.java',
|
||||||
'android/java/src/org/chromium/base/SystemMessageHandler.java',
|
'android/java/src/org/chromium/base/SystemMessageHandler.java',
|
||||||
|
'android/java/src/org/chromium/base/SystemMonitor.java',
|
||||||
],
|
],
|
||||||
'variables': {
|
'variables': {
|
||||||
'jni_gen_dir': 'base',
|
'jni_gen_dir': 'base',
|
||||||
|
@@ -391,6 +391,7 @@
|
|||||||
'system_monitor/system_monitor.cc',
|
'system_monitor/system_monitor.cc',
|
||||||
'system_monitor/system_monitor.h',
|
'system_monitor/system_monitor.h',
|
||||||
'system_monitor/system_monitor_android.cc',
|
'system_monitor/system_monitor_android.cc',
|
||||||
|
'system_monitor/system_monitor_android.h',
|
||||||
'system_monitor/system_monitor_ios.mm',
|
'system_monitor/system_monitor_ios.mm',
|
||||||
'system_monitor/system_monitor_mac.mm',
|
'system_monitor/system_monitor_mac.mm',
|
||||||
'system_monitor/system_monitor_posix.cc',
|
'system_monitor/system_monitor_posix.cc',
|
||||||
|
@@ -3,12 +3,27 @@
|
|||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
#include "base/system_monitor/system_monitor.h"
|
#include "base/system_monitor/system_monitor.h"
|
||||||
|
#include "jni/SystemMonitor_jni.h"
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
|
|
||||||
|
namespace android {
|
||||||
|
|
||||||
|
// Native implementation of SystemMonitor.java.
|
||||||
|
void OnBatteryChargingChanged(JNIEnv* env,
|
||||||
|
jclass clazz) {
|
||||||
|
SystemMonitor::Get()->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android
|
||||||
|
|
||||||
bool SystemMonitor::IsBatteryPower() {
|
bool SystemMonitor::IsBatteryPower() {
|
||||||
NOTIMPLEMENTED();
|
JNIEnv* env = base::android::AttachCurrentThread();
|
||||||
return true;
|
return base::android::Java_SystemMonitor_isBatteryPower(env);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RegisterSystemMonitor(JNIEnv* env) {
|
||||||
|
return base::android::RegisterNativesImpl(env);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace base
|
} // namespace base
|
||||||
|
17
base/system_monitor/system_monitor_android.h
Normal file
17
base/system_monitor/system_monitor_android.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#ifndef BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_ANDROID_H_
|
||||||
|
#define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_ANDROID_H_
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
|
||||||
|
// Registers the JNI bindings for SystemMonitor.
|
||||||
|
bool RegisterSystemMonitor(JNIEnv* env);
|
||||||
|
|
||||||
|
} // namespace base
|
||||||
|
|
||||||
|
#endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_ANDROID_H_
|
@@ -12,6 +12,7 @@ import android.os.Handler;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.chromium.base.PathUtils;
|
import org.chromium.base.PathUtils;
|
||||||
|
import org.chromium.base.SystemMonitor;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
@@ -40,6 +41,9 @@ public class ChromeNativeTestActivity extends Activity {
|
|||||||
// Needed by path_utils_unittest.cc
|
// Needed by path_utils_unittest.cc
|
||||||
PathUtils.setPrivateDataDirectorySuffix("chrome");
|
PathUtils.setPrivateDataDirectorySuffix("chrome");
|
||||||
|
|
||||||
|
// Needed by system_monitor_unittest.cc
|
||||||
|
SystemMonitor.createForTests(this);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
loadLibrary();
|
loadLibrary();
|
||||||
Bundle extras = this.getIntent().getExtras();
|
Bundle extras = this.getIntent().getExtras();
|
||||||
|
Reference in New Issue
Block a user