Skip to content

Commit

Permalink
Android: Integrates native and java SystemMonitor.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bulach@chromium.org committed Oct 16, 2012
1 parent 8a429d0 commit a614f15
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 3 deletions.
4 changes: 3 additions & 1 deletion base/android/base_jni_registrar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#include "base/android/base_jni_registrar.h"

#include "base/basictypes.h"
#include "base/message_pump_android.h"
#include "base/android/build_info.h"
#include "base/android/jni_android.h"
#include "base/android/jni_registrar.h"
#include "base/android/locale_utils.h"
#include "base/android/path_service_android.h"
#include "base/android/path_utils.h"
#include "base/message_pump_android.h"
#include "base/system_monitor/system_monitor_android.h"

namespace base {
namespace android {
Expand All @@ -22,6 +23,7 @@ static RegistrationMethod kBaseRegisteredMethods[] = {
{ "PathService", base::android::RegisterPathService },
{ "PathUtils", base::android::RegisterPathUtils },
{ "SystemMessageHandler", base::MessagePumpForUI::RegisterBindings },
{ "SystemMonitor", base::RegisterSystemMonitor },
};

bool RegisterJni(JNIEnv* env) {
Expand Down
23 changes: 23 additions & 0 deletions base/android/java/src/org/chromium/base/PowerStatusReceiver.java
Original file line number Diff line number Diff line change
@@ -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 changes: 64 additions & 0 deletions base/android/java/src/org/chromium/base/SystemMonitor.java
Original file line number Diff line number Diff line change
@@ -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();

}
1 change: 1 addition & 0 deletions base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,7 @@
'android/java/src/org/chromium/base/PathService.java',
'android/java/src/org/chromium/base/PathUtils.java',
'android/java/src/org/chromium/base/SystemMessageHandler.java',
'android/java/src/org/chromium/base/SystemMonitor.java',
],
'variables': {
'jni_gen_dir': 'base',
Expand Down
1 change: 1 addition & 0 deletions base/base.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@
'system_monitor/system_monitor.cc',
'system_monitor/system_monitor.h',
'system_monitor/system_monitor_android.cc',
'system_monitor/system_monitor_android.h',
'system_monitor/system_monitor_ios.mm',
'system_monitor/system_monitor_mac.mm',
'system_monitor/system_monitor_posix.cc',
Expand Down
19 changes: 17 additions & 2 deletions base/system_monitor/system_monitor_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@
// found in the LICENSE file.

#include "base/system_monitor/system_monitor.h"
#include "jni/SystemMonitor_jni.h"

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() {
NOTIMPLEMENTED();
return true;
JNIEnv* env = base::android::AttachCurrentThread();
return base::android::Java_SystemMonitor_isBatteryPower(env);
}

bool RegisterSystemMonitor(JNIEnv* env) {
return base::android::RegisterNativesImpl(env);
}

} // namespace base
17 changes: 17 additions & 0 deletions base/system_monitor/system_monitor_android.h
Original file line number Diff line number Diff line change
@@ -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_
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.util.Log;

import org.chromium.base.PathUtils;
import org.chromium.base.SystemMonitor;

import java.io.File;

Expand Down Expand Up @@ -40,6 +41,9 @@ public void onCreate(Bundle savedInstanceState) {
// Needed by path_utils_unittest.cc
PathUtils.setPrivateDataDirectorySuffix("chrome");

// Needed by system_monitor_unittest.cc
SystemMonitor.createForTests(this);

try {
loadLibrary();
Bundle extras = this.getIntent().getExtras();
Expand Down

0 comments on commit a614f15

Please sign in to comment.