Skip to content
This repository has been archived by the owner on Oct 18, 2021. It is now read-only.

Commit

Permalink
Fix service failing to stop, and force close on initial activation
Browse files Browse the repository at this point in the history
  • Loading branch information
biqqles committed Jan 14, 2018
1 parent bf695c3 commit ac3e893
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName '1.0'
versionName '1.0.1'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/eu/biqqles/nextlit/LedControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int getPattern() {
String cmd = MessageFormat.format("cat {0}\n", PATTERN_FILE);
try {
return Integer.parseInt(execCommand(cmd, true));
} catch (NumberFormatException ex) {
} catch (NumberFormatException e) {
return 0;
}
}
Expand Down Expand Up @@ -81,9 +81,9 @@ private String execCommand(String command, boolean getOutput) {
if (getOutput) {
return br.readLine();
}
} catch (IOException ioe) {
Log.e("NEXTLIT", MessageFormat.format("Failed to execute {} with error {}",
command, ioe));
} catch (IOException e) {
Log.e("NEXTLIT", MessageFormat.format("Failed to execute {0} with error {1}",
command, e));
}
return null;
}
Expand Down
23 changes: 16 additions & 7 deletions app/src/main/java/eu/biqqles/nextlit/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings;
import android.service.notification.NotificationListenerService;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
Expand Down Expand Up @@ -41,11 +40,11 @@ protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

prefs = this.getSharedPreferences("nextlit", MODE_PRIVATE);
prefs = getSharedPreferences("nextlit", MODE_PRIVATE);

try {
ledcontrol = new LedControl();
} catch (IOException ioe) {
} catch (IOException e) {
Toast.makeText(this, "App requires root access, closing", Toast.LENGTH_LONG).show();
finish();
}
Expand Down Expand Up @@ -101,19 +100,29 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public void onCheckedChanged(CompoundButton button, boolean checked) {
if (checked) {
// take user to Notification Access
// take user to Notification access if they haven't enabled the service already
String enabledNotificationListeners = Settings.Secure.getString(
getContentResolver(), "enabled_notification_listeners");
if (!enabledNotificationListeners.contains(getPackageName())) {

if (enabledNotificationListeners != null
&& !enabledNotificationListeners.contains(getPackageName())) {
startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
Toast.makeText(MainActivity.this, "Enable Nextlit", Toast.LENGTH_SHORT).show();
}
startService(new Intent(MainActivity.this, NotificationListenerService.class));

/*
Normal procedure here would be to call start/stopService, but for whatever reason
Android ignores these calls for NotificationListenerServices, and so effectively
the only thing that governs whether a service is enabled or not is if it's enabled
in Notification access. Instead, we accept that the service will always run and
just modify a static flag that says if the lights should be enabled or not.
*/
NotificationLightsService.enabled = true;
Toast.makeText(MainActivity.this, "Service started", Toast.LENGTH_SHORT).show();
}
else {
ledcontrol.clearPattern();
stopService(new Intent(MainActivity.this, NotificationListenerService.class));
NotificationLightsService.enabled = false;
Toast.makeText(MainActivity.this, "Service stopped", Toast.LENGTH_SHORT).show();
}
}
Expand Down
24 changes: 14 additions & 10 deletions app/src/main/java/eu/biqqles/nextlit/NotificationLightsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import java.io.IOException;

public class NotificationLightsService extends NotificationListenerService {
SharedPreferences prefs;
LedControl ledcontrol;
public static boolean enabled = false;

@Override
public void onCreate() {
super.onCreate();
prefs = this.getSharedPreferences("nextlit", MODE_PRIVATE);
try {
ledcontrol = new LedControl();
} catch (IOException ioe) {
} catch (IOException e) {
// MainActivity will close if its LedControl can't initialise; how did you manage to get
// here?
System.exit(0);
Expand All @@ -40,17 +42,19 @@ public IBinder onBind(Intent intent) {
}

@Override
public void onNotificationPosted(StatusBarNotification sbn){
if (getActiveNotifications().length > 0) {
int pattern = prefs.getInt("predef_pattern", 0); // get selected pattern from preferences
// if pattern isn't set already, set it
if (ledcontrol.getPattern() != pattern) {
ledcontrol.setPattern(pattern);
public void onNotificationPosted(StatusBarNotification sbn) {
Log.w("NEXTLIT", String.valueOf(enabled));
if (enabled) {
if (getActiveNotifications().length > 0) {
int pattern = prefs.getInt("predef_pattern", 0); // get selected pattern from preferences
// if pattern isn't set already, set it
if (ledcontrol.getPattern() != pattern) {
ledcontrol.setPattern(pattern);
}
} else {
ledcontrol.clearPattern();
}
}
else {
ledcontrol.clearPattern();
}
}

@Override
Expand Down

0 comments on commit ac3e893

Please sign in to comment.