Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
App now starts and monitors applications
* Add permission ForegroundService
* Uncomment updating the notification
* Delete .gradle files from repo
  • Loading branch information
timobaehr committed Jul 29, 2020
commit 4f1b498ea8581cedaa24dc21d57bb3f87ded468c
Binary file removed .gradle/4.8/fileChanges/last-build.bin
Binary file not shown.
Binary file removed .gradle/4.8/fileHashes/fileHashes.bin
Binary file not shown.
Binary file removed .gradle/4.8/fileHashes/fileHashes.lock
Binary file not shown.
Binary file removed .gradle/6.1.1/fileChanges/last-build.bin
Binary file not shown.
Empty file removed .gradle/6.1.1/gc.properties
Empty file.
2 changes: 0 additions & 2 deletions .gradle/buildOutputCleanup/cache.properties

This file was deleted.

Binary file removed .gradle/checksums/checksums.lock
Binary file not shown.
Empty file removed .gradle/vcs-1/gc.properties
Empty file.
Empty file.
1 change: 1 addition & 0 deletions PowerTutor2/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ui.UMLogger"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,24 @@
import edu.umich.PowerTutor.util.SystemInfo;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -95,6 +99,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
//android.os.Debug.startMethodTracing("pt.trace");

createNotificationChannel();

if (intent.getBooleanExtra("stop", false)) {
stopSelf();
return START_NOT_STICKY;
Expand All @@ -107,6 +113,18 @@ public int onStartCommand(Intent intent, int flags, int startId) {

return START_NOT_STICKY;
}

private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}

@Override
public void onDestroy() {
Expand Down Expand Up @@ -136,7 +154,7 @@ public void onDestroy() {
} catch(NoSuchMethodException e) {
}
if(!foregroundSet) {
setForeground(false);
//setForeground(false); // TODO: Uncommented
notificationManager.cancel(NOTIFICATION_ID);
}

Expand All @@ -162,10 +180,19 @@ public void showNotification(){
/* the next two lines initialize the Notification, using the
* configurations above.
*/
notification = new Notification(icon, tickerText, when);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setWhen(when)
.setSmallIcon(R.drawable.icon)
.setContentIntent(contentIntent)
.build();
notification.tickerText = tickerText;
notification.iconLevel = 2;
notification.setLatestEventInfo(context, contentTitle,
contentText, contentIntent);

//notification = new Notification(icon, tickerText, when);
//notification.iconLevel = 2;
//notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

/* We need to set the service to run in the foreground so that system
* won't try to destroy the power logging service except in the most
Expand All @@ -187,7 +214,8 @@ public void showNotification(){
} catch(NoSuchMethodException e) {
}
if(!foregroundSet) {
setForeground(true);
//setForeground(true);
startForeground(1, notification);
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
Expand All @@ -197,6 +225,9 @@ public void showNotification(){
* 8% cpu utilization penalty.
*/
public void updateNotification(int level, double totalPower) {
// TODO: Updating notifcation not working right now
if (true) return;

notification.icon = R.drawable.level;
notification.iconLevel = level;

Expand Down Expand Up @@ -232,8 +263,8 @@ public void updateNotification(int level, double totalPower) {
notificationIntent.putExtra("isFromIcon", true);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(this, contentTitle, contentText,
contentIntent);
// TODO: Uncommented
//notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}

Expand Down