Skip to content

Commit

Permalink
Add temperature alerts limit rate, notifications context and many min…
Browse files Browse the repository at this point in the history
…or bug fixes
  • Loading branch information
Hugo Matalonga committed Oct 14, 2017
1 parent 49b05de commit 99bf6d6
Show file tree
Hide file tree
Showing 22 changed files with 438 additions and 72 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ proguard/

# Android Studio .gitignore default file
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
/.idea/copyright
.DS_Store
/build
/captures

# Assets folders
Expand Down
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# GreenHub BatteryHub

[![Build Status](https://travis-ci.org/hmatalonga/greenhub.svg?branch=master)](https://travis-ci.org/hmatalonga/greenhub)
[![Build Status](https://travis-ci.org/greenhub-project/batteryhub.svg?branch=master)](https://travis-ci.org/hmatalonga/greenhub)

GreenHub is a collaborative approach to power consumption analysis of Android devices.

Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/hmatalonga/greenhub/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
public final class Config {
// Whether to output debug messages.
public static final boolean DEBUG = true;
public static final boolean PRODUCTION = false;

public static final String SERVER_STATUS_URL = "http://greenhub.hmatalonga.com/";
public static final String SERVER_URL_DEFAULT = "none";
Expand Down Expand Up @@ -90,7 +89,11 @@ public final class Config {
public static final int REFRESH_STATUS_BAR_INTERVAL = REFRESH_CURRENT_INTERVAL * 6;
public static final int REFRESH_STATUS_ERROR = REFRESH_CURRENT_INTERVAL * 2;

public static final String NOTIFICATION_DEFAULT_TEMPERATURE_RATE = "5";
public static final double BATTERY_LOW_LEVEL = 0.2;
public static final int BATTERY_TEMPERATURE_MEDIUM = 35;
public static final int BATTERY_TEMPERATURE_HIGH = 45;


public static final String STATUS_IDLE = "Idle";

Expand Down
12 changes: 9 additions & 3 deletions app/src/main/java/com/hmatalonga/greenhub/GreenHubApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.realm.RealmMigration;
import io.realm.RealmSchema;

import static com.hmatalonga.greenhub.util.LogUtils.LOGE;
import static com.hmatalonga.greenhub.util.LogUtils.LOGI;
import static com.hmatalonga.greenhub.util.LogUtils.makeLogTag;

Expand Down Expand Up @@ -131,9 +132,14 @@ public void run() {
}

public void stopGreenHubService() {
if (estimator != null) {
unregisterReceiver(estimator);
isServiceRunning = false;
try {
if (estimator != null) {
unregisterReceiver(estimator);
isServiceRunning = false;
}
} catch (IllegalArgumentException e) {
LOGE(TAG, "Estimator receiver is not registered!");
e.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ReceiverCallNotAllowedException;
import android.os.BatteryManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import com.hmatalonga.greenhub.Config;
import com.hmatalonga.greenhub.events.BatteryLevelEvent;
import com.hmatalonga.greenhub.models.Battery;
import com.hmatalonga.greenhub.util.Notifier;
import com.hmatalonga.greenhub.util.SettingsUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

import static com.hmatalonga.greenhub.util.LogUtils.LOGE;
import static com.hmatalonga.greenhub.util.LogUtils.LOGI;
import static com.hmatalonga.greenhub.util.LogUtils.makeLogTag;
Expand Down Expand Up @@ -86,12 +90,43 @@ public void onReceive(Context context, Intent intent) {
e.printStackTrace();
}

if (SettingsUtils.isBatteryAlertsOn(context)) {
// Notify for temperature alerts...
if (temperature > 45) {
Notifier.batteryHighTemperature(context);
} else if (temperature <= 45 && temperature > 35) {
Notifier.batteryWarningTemperature(context);
// We don't send battery level alerts here because we need to check if the level changed
// So we verify that inside the DataEstimator Service

if (temperature > Config.BATTERY_TEMPERATURE_MEDIUM) {
if (SettingsUtils.isBatteryAlertsOn(context) &&
SettingsUtils.isTemperatureAlertsOn(context)) {

// Check temperature limit rate
Calendar lastAlert = Calendar.getInstance();
long lastSavedTime = SettingsUtils.fetchLastTemperatureAlertDate(context);

// Set last alert time with saved preferences
if (lastSavedTime != 0) {
lastAlert.setTimeInMillis(lastSavedTime);
}
int minutes = SettingsUtils.fetchTemperatureAlertsRate(context);

lastAlert.add(Calendar.MINUTE, minutes);

// If last saved time isn't default and now is after limit rate then notify
if (lastSavedTime == 0 || Calendar.getInstance().after(lastAlert)) {
// Notify for temperature alerts...
if (temperature > Config.BATTERY_TEMPERATURE_HIGH) {
Notifier.batteryHighTemperature(context);
SettingsUtils.saveLastTemperatureAlertDate(
context,
System.currentTimeMillis()
);
} else if (temperature <= Config.BATTERY_TEMPERATURE_HIGH &&
temperature > Config.BATTERY_TEMPERATURE_MEDIUM) {
Notifier.batteryWarningTemperature(context);
SettingsUtils.saveLastTemperatureAlertDate(
context,
System.currentTimeMillis()
);
}
}
}
}
}
Expand Down Expand Up @@ -135,18 +170,25 @@ null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)
// Getters & Setters
public void getCurrentStatus(final Context context) {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
assert batteryStatus != null;

level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
health = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
plugged = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
present = batteryStatus.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
technology = batteryStatus.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
temperature = (float) (batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10);
voltage = (float) (batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0) / 1000);

try {
Intent batteryStatus = context.registerReceiver(null, ifilter);

if (batteryStatus != null) {
level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
health = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
plugged = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
present = batteryStatus.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
technology = batteryStatus.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
temperature = (float) (batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10);
voltage = (float) (batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0) / 1000);
}
} catch (ReceiverCallNotAllowedException e) {
LOGE(TAG, "ReceiverCallNotAllowedException from Notification Receiver?");
e.printStackTrace();
}
}

public String getHealthStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ private void takeSampleIfBatteryLevelChanged(Intent intent, final Context contex

boolean isPlugged = 0 != intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

if (SettingsUtils.isBatteryAlertsOn(context)) {
if (SettingsUtils.isBatteryAlertsOn(context) &&
SettingsUtils.isChargeAlertsOn(context)) {
if (Inspector.getCurrentBatteryLevel() == 1 && isPlugged) {
Notifier.batteryFullAlert(context);
} else if (Inspector.getCurrentBatteryLevel() == Config.BATTERY_LOW_LEVEL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@
import android.preference.PreferenceManager;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hmatalonga.greenhub.BuildConfig;
import com.hmatalonga.greenhub.Config;
import com.hmatalonga.greenhub.events.StatusEvent;
Expand Down Expand Up @@ -99,6 +90,17 @@
import com.hmatalonga.greenhub.models.data.Sample;
import com.hmatalonga.greenhub.models.data.Settings;
import com.hmatalonga.greenhub.util.SettingsUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.realm.RealmList;

import static com.hmatalonga.greenhub.util.LogUtils.LOGI;
Expand All @@ -124,6 +126,7 @@ public final class Inspector {

private static double sCurrentBatteryLevel = 0;


// we might not be able to read the current battery level at the first run
// of GreenHub.
// so it might be zero until we get the non-zero value from the intent
Expand All @@ -134,10 +137,6 @@ public final class Inspector {
*/
private Inspector() {}

public static double readLastBatteryLevel() {
return sLastBatteryLevel;
}

public static void setLastBatteryLevel(double level) {
sLastBatteryLevel = level;
}
Expand All @@ -150,10 +149,6 @@ public static double getCurrentBatteryLevel() {
return sCurrentBatteryLevel;
}

public static void setCurrentBatteryLevel(double level) {
sCurrentBatteryLevel = level;
}

/**
* Take in currentLevel and scale as doubles to avoid loss of precision issues.
* Note that GreenHub stores battery level as a value between 0 and 1, e.g. 0.45 for 45%.
Expand Down
48 changes: 48 additions & 0 deletions app/src/main/java/com/hmatalonga/greenhub/models/Battery.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.lang.reflect.Method;

import com.hmatalonga.greenhub.Config;
import com.hmatalonga.greenhub.util.SettingsUtils;

import static com.hmatalonga.greenhub.util.LogUtils.LOGI;
import static com.hmatalonga.greenhub.util.LogUtils.makeLogTag;
Expand Down Expand Up @@ -146,6 +147,53 @@ public static long getBatteryEnergyCounter(final Context context) {
return (value != Long.MIN_VALUE) ? value : -1;
}

/**
* Calculate Average Power
* Average Power = (Average Voltage * Average Current) / 1e9
*
* @param context Context of application
* @return Average power in integer
*/
public static int getBatteryAveragePower(final Context context) {
int voltage;
int current = 0;

Intent receiver =
context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

if (receiver == null) return -1;

voltage = receiver.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
BatteryManager manager = (BatteryManager)
context.getSystemService(Context.BATTERY_SERVICE);
current = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
}

return (voltage * current) / 1000000000;
}

/**
* Calculate Battery Capacity Consumed
* Battery Capacity Consumed = (Average Current * Workload Duration) / 1e3
*
* @param workload Workload duration (in hours)
* @param context Context of application
* @return Average power in integer
*/
public static double getBatteryCapacityConsumed(final double workload, final Context context) {
int current = 0;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
BatteryManager manager = (BatteryManager)
context.getSystemService(Context.BATTERY_SERVICE);
current = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
}

return (current * workload) / 1000;
}

private static int getBatteryCurrentNowLegacy() {
int value = -1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class BatteryDetails extends RealmObject {
// Average battery current in microAmperes
public int currentAverage;

// Instantaneous battery current in microAmperes
// Instantaneous battery current in milliAmperes
public int currentNow;

// Battery remaining energy in nanoWatt-hours
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void onReceive(Context context, Intent intent) {
new CheckNewMessagesTask().execute(context);
}

if (CommunicationManager.isQueued) {
if (CommunicationManager.isQueued && SettingsUtils.isServerUrlPresent(context)) {
CommunicationManager manager = new CommunicationManager(context, true);
manager.sendSamples();
CommunicationManager.isQueued = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.ReceiverCallNotAllowedException;

import com.hmatalonga.greenhub.util.LogUtils;
import com.hmatalonga.greenhub.util.Notifier;
Expand All @@ -16,6 +17,10 @@ public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
LOGI(TAG, "onReceive called!");
Notifier.updateStatusBar(context);
try {
Notifier.updateStatusBar(context);
} catch (ReceiverCallNotAllowedException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit 99bf6d6

Please sign in to comment.