Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code aptation to Android 9 #92

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ android {
lintOptions {
abortOnError false
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.hmatalonga.greenhub">

<!-- Required to start service on boot -->
Expand All @@ -39,6 +40,12 @@
<!-- Required for gps and location providers -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<permission android:name="android.permission.FOREGROUND_SERVICE"/>
<permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<!-- Required to get running apps -->
<!--<uses-permission android:name="android.permission.READ_CALL_LOG" />-->

Expand All @@ -56,7 +63,8 @@
android:label="@string/app_name"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="@style/Theme.GreenHub">
android:theme="@style/Theme.GreenHub"
android:usesCleartextTraffic="true">

<meta-data android:name="AA_DB_NAME" android:value="GreenHub.db" />

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/hmatalonga/greenhub/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public final class Config {

public static final String SERVER_STATUS_URL = "https://greenhubproject.org/";
public static final String SERVER_URL_DEFAULT = "none";
public static final String SERVER_URL_DEVELOPMENT = "http://192.168.1.95:8080";
public static final String SERVER_URL_DEVELOPMENT = "http://192.168.1.251:8000/";

public static final int DATABASE_VERSION = 5;

Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/com/hmatalonga/greenhub/GreenHubApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import android.support.annotation.RequiresApi;

import com.crashlytics.android.Crashlytics;
import com.hmatalonga.greenhub.managers.sampling.BatteryService;
Expand Down Expand Up @@ -103,9 +107,14 @@ public void startGreenHubService() {
new Thread() {

public void run() {
Intent service = new Intent(context, BatteryService.class);
try {
Intent service = new Intent(context, BatteryService.class);
context.startService(service);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(service);
} else {
context.startService(service);
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.hmatalonga.greenhub.managers.sampling;


import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;

import com.hmatalonga.greenhub.util.SettingsUtils;

Expand Down Expand Up @@ -50,6 +55,23 @@ public void onCreate() {
registerReceiver(estimator, mIntentFilter);
}

super.onCreate();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String CHANNEL_ID = "BatteryServiceChannel";

NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);

((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("")
.setContentText("").build();
startForeground(1, notification);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
import com.hmatalonga.greenhub.models.data.Sample;
import com.hmatalonga.greenhub.models.data.Settings;
import com.hmatalonga.greenhub.util.LogUtils;
import com.hmatalonga.greenhub.util.Notifier;
import com.hmatalonga.greenhub.util.SettingsUtils;

import org.greenrobot.eventbus.EventBus;
Expand Down Expand Up @@ -211,7 +210,7 @@ static Sample getSample(final Context context, Intent intent) {
newSample.database = Config.DATABASE_VERSION;

// Record first data point for CPU usage
long[] idleAndCpu1 = Cpu.readUsagePoint();
long[] idleAndCpu1 = Cpu.readUsagePoint(context);

// If the sampler is running because of the SCREEN_ON or SCREEN_OFF
// event/action,
Expand Down Expand Up @@ -437,7 +436,7 @@ static Sample getSample(final Context context, Intent intent) {
}

// Record second data point for cpu/idle time
long[] idleAndCpu2 = Cpu.readUsagePoint();
long[] idleAndCpu2 = Cpu.readUsagePoint(context);

// CPU status
long uptime = Cpu.getUptime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersio
.addField("isDynamicSensor", boolean.class)
.addField("isWakeUpSensor", boolean.class)
.addField("maxDelay", int.class)
.addField("maximumRange", float.class)
.addField("maximumRange", double.class)
.addField("minDelay", int.class)
.addField("name", String.class)
.addField("power", float.class)
.addField("power", double.class)
.addField("reportingMode", int.class)
.addField("resolution", float.class)
.addField("resolution", double.class)
.addField("stringType", String.class)
.addField("codeType", int.class)
.addField("vendor", String.class)
Expand Down
27 changes: 20 additions & 7 deletions app/src/main/java/com/hmatalonga/greenhub/models/Cpu.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.hmatalonga.greenhub.models;

import android.content.Context;
import android.os.HardwarePropertiesManager;
import android.os.SystemClock;
import android.util.Log;

import java.io.IOException;
import java.io.RandomAccessFile;
Expand Down Expand Up @@ -55,22 +58,32 @@ public class Cpu {
* splitting.:)
*/

public static synchronized long[] readUsagePoint() {
public static synchronized long[] readUsagePoint(Context context) {
long idle = 0;
long cpu = 0;

try {
RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
HardwarePropertiesManager propertiesManager = null;
RandomAccessFile reader = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
reader = new RandomAccessFile("/proc/self/stat", "r");
} else {
reader = new RandomAccessFile("/proc/stat", "r");
}

String load = reader.readLine();
String[] tokens = load.split(" ");
for (int i = 2; i <= 8; i++) {
// 5 index has idle value
if (i == 5) {
idle = Long.parseLong(tokens[i]);
continue;
try {
// 5 index has idle value
if (i == 5) {
idle = Long.parseLong(tokens[i]);
continue;
}
cpu += Long.parseLong(tokens[i]);
} catch (NumberFormatException ex) {
Log.i(TAG, "Parser: " + ex.getMessage() + " - " + tokens[i]);
}
cpu += Long.parseLong(tokens[i]);
}
reader.close();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public static void clearSensorsMap() {
sensorsMap = new HashMap<>();
}

@TargetApi(23)
private static void getAttributesNewVersion(Sensor sensor, SensorDetails details) {
if (SDK_VERSION > 23) {
details.id = sensor.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ public class SensorDetails extends RealmObject {
//This value is defined only for continuous and on-change sensors.
public int maxDelay;

public float maximumRange;
public double maximumRange;

public int minDelay;

public String name;

//the power in mA used by this sensor while in use
public float power;
public double power;

//Each sensor has exactly one reporting mode associated with it.
public int reportingMode;

public float resolution;
public double resolution;

public String stringType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.Context;
import android.os.Handler;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.hmatalonga.greenhub.BuildConfig;
Expand All @@ -43,6 +44,7 @@
import org.greenrobot.eventbus.EventBus;

import java.util.Iterator;
import java.util.Optional;

import io.realm.Realm;
import retrofit2.Call;
Expand Down Expand Up @@ -320,7 +322,19 @@ private JsonObject bundleSample(final Sample sample) {

// CpuStatus
child = new JsonObject();
child.addProperty("cpuUsage", sample.cpuStatus.cpuUsage);
String s = sample.cpuStatus.cpuUsage + "";
Double cpuUsage = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
cpuUsage = Optional.ofNullable(s).filter(v -> !"NaN".equalsIgnoreCase(v))
.map(v -> sample.cpuStatus.cpuUsage).orElse(0.0);
} else {
try {
cpuUsage = new Double(s);
} catch (Exception e) {
LogUtils.logE(TAG, "cpuUsage error", e);
}
}
child.addProperty("cpuUsage", cpuUsage);
child.addProperty("upTime", sample.cpuStatus.upTime);
child.addProperty("sleepTime", sample.cpuStatus.sleepTime);
root.add("cpuStatus", child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CpuUsageInfo;
import android.os.Handler;
import android.os.PowerManager;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
Expand Down