Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

Commit

Permalink
Resolves #525
Browse files Browse the repository at this point in the history
- 1.6.5 (2019-9-14) codename: ingus
  * Better Service Management
  * Listener warning notification for Android 8.0+
  * Improve Exit Button
  * Fixed occasional blank music notification
  • Loading branch information
Austin Hogan committed Sep 14, 2019
1 parent 19f0fcb commit 2f137a3
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 60 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.adam.aslfms"
minSdkVersion 14
targetSdkVersion 26
versionCode = 56
versionName = "1.6.5"
versionCode = 57
versionName = "1.6.6"

testApplicationId "com.adam.aslfms.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/assets/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ Visit,
https://github.com/simple-last-fm-scrobbler/sls
For more details.

- 1.6.5 (2019-9-14) codename: ingus
* Better Service Management
* Listener warning notification for Android 8.0+
* Improve Exit Button
* Fixed occasional blank music notification

- 1.6.5 (2019-9-13) codename: hingus
* Better Service Management
* Update Permissions UI
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/adam/aslfms/PermissionsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import android.widget.TextView;

import com.adam.aslfms.util.AppSettings;
import com.adam.aslfms.util.InternalTrackTransmitter;
import com.adam.aslfms.util.MyContextWrapper;
import com.adam.aslfms.util.Util;
/**
Expand Down Expand Up @@ -178,10 +177,10 @@ public void onClick(DialogInterface dialog, int which) {
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String message = ctx.getResources().getString(R.string.are_you_sure);
String message = ctx.getResources().getString(R.string.warning) + "! " + ctx.getResources().getString(R.string.are_you_sure);
if (Build.VERSION_CODES.O <= Build.VERSION.SDK_INT && !Util.checkNotificationListenerPermission(ctx)){
message += " " + ctx.getResources().getString(R.string.warning_will_not_scrobble);
message += "/" + ctx.getResources().getString(R.string.permission_notification_listener);
message += " - " + ctx.getResources().getString(R.string.warning_will_not_scrobble);
message += " - " + ctx.getResources().getString(R.string.permission_notification_listener);
}
builder.setMessage(message).setPositiveButton(R.string.yes, dialogClickListener)
.setNegativeButton(R.string.no, dialogClickListener).show();
Expand Down
49 changes: 40 additions & 9 deletions app/src/main/java/com/adam/aslfms/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.database.SQLException;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.adam.aslfms.service.NetApp;
import com.adam.aslfms.service.ScrobblingService;
import com.adam.aslfms.util.AppSettings;
import com.adam.aslfms.util.MyContextWrapper;
Expand Down Expand Up @@ -207,14 +210,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
new WhatsNewDialog(this).show();
return true;
case R.id.menu_exit:
boolean currentActiveState = settings.isActiveAppEnabled(Util.checkPower(this));
settings.setActiveAppEnabled(Util.checkPower(this),false);
Util.runServices(this);
Util.stopAllServices(this);
settings.setActiveAppEnabled(Util.checkPower(this),currentActiveState);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
handleAppExit();
return true;
}
return super.onOptionsItemSelected(item);
Expand All @@ -233,4 +229,39 @@ private void checkNetwork() {
Snackbar.make(getListView(), getString(R.string.limited_network), Snackbar.LENGTH_SHORT).show();
}
}

private void handleAppExit(){
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
boolean currentActiveState = settings.isActiveAppEnabled(Util.checkPower(mCtx));
settings.setActiveAppEnabled(Util.checkPower(mCtx),false);
settings.setTempExitAppEnabled(Util.checkPower(mCtx),true);
Util.runServices(mCtx);
Util.stopAllServices(mCtx);
settings.setActiveAppEnabled(Util.checkPower(mCtx),currentActiveState);
settings.setTempExitAppEnabled(Util.checkPower(mCtx), false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
finishAndRemoveTask();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
SettingsActivity.this.finishAffinity();
}
ActivityCompat.finishAffinity(SettingsActivity.this );
SettingsActivity.this.finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String message = mCtx.getResources().getString(R.string.warning) + "! " + mCtx.getResources().getString(R.string.are_you_sure) + " - " + mCtx.getResources().getString(R.string.warning_will_not_scrobble);
builder.setMessage(message).setPositiveButton(R.string.yes, dialogClickListener)
.setNegativeButton(R.string.no, dialogClickListener).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class ControllerReceiverService extends NotificationListenerService {

private static final String TAG = "ControllerReceiverSrvc";
private ControllerReceiverSession mControllerReceiverSession;
AppSettings settings = null;
Bundle extras = null;

@Override
public IBinder onBind(Intent intent) {
Expand All @@ -60,40 +62,26 @@ public IBinder onBind(Intent intent) {
@Override
public void onCreate() {
super.onCreate();
if (!Util.checkNotificationListenerPermission(this)){
return;
}

Log.d(TAG,"created");
AppSettings settings = new AppSettings(this);
settings = new AppSettings(this);

Bundle extras = new Bundle();
extras.putString("track", "");
extras.putString("artist", "");
extras.putString("album", "");
extras.putString("app_name", "");
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, this));
init();
init(extras);
if (!settings.isActiveAppEnabled(Util.checkPower(this))) {
this.stopForeground(true);
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!Util.checkNotificationListenerPermission(this)){
return Service.START_NOT_STICKY;
}
Log.d(TAG,"started");
AppSettings settings = new AppSettings(this);
Bundle extras = new Bundle();
extras.putString("track", "");
extras.putString("artist", "");
extras.putString("album", "");
extras.putString("app_name", "");
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, this));
init();
if (!settings.isActiveAppEnabled(Util.checkPower(this))) {
this.stopForeground(true);
settings = new AppSettings(this);
if (intent != null) {
extras = intent.getExtras();
}
init(extras);
if (settings.isTempExitAppEnabled(Util.checkPower(this))) {
return Service.START_NOT_STICKY;
}
return Service.START_STICKY;
Expand All @@ -117,7 +105,7 @@ public void onDestroy() {
@Override
public void onListenerConnected() {
super.onListenerConnected();
init();
init(extras);
}

@Override
Expand All @@ -127,7 +115,14 @@ public void onListenerDisconnected() {
requestRebind(new ComponentName(getApplicationContext(), ControllerReceiverService.class));
}

public void init(){
public void init(Bundle extras){
// foreground
if (settings.isActiveAppEnabled(Util.checkPower(this))) {
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, this));
} else {
this.stopForeground(true);
}
// media manager
MediaSessionManager mediaSessionManager = null;
try {
Log.d(TAG,"Detecting initial media session");
Expand Down
34 changes: 28 additions & 6 deletions app/src/main/java/com/adam/aslfms/service/ScrobblingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.util.Log;
import android.widget.Toast;

import com.adam.aslfms.PermissionsActivity;
import com.adam.aslfms.R;
import com.adam.aslfms.util.AppSettings;
import com.adam.aslfms.util.InternalTrackTransmitter;
Expand Down Expand Up @@ -98,8 +99,7 @@ public int onStartCommand(Intent i, int flags, int startId) {

foreGroundService();

if (!settings.isActiveAppEnabled(Util.checkPower(mCtx))) {
this.stopForeground(true);
if (settings.isTempExitAppEnabled(Util.checkPower(mCtx))) {
return Service.START_NOT_STICKY;
}
return Service.START_STICKY;
Expand All @@ -112,8 +112,25 @@ private void handleCommand(Intent i, int startId) {
}
String action = i.getAction();
Bundle extras = i.getExtras();
if (action == null || action.equals(ACTION_START_SCROBBLER_SERVICE )) {
//
if (action == null){
// weird null action
} else if (action.equals(ACTION_START_SCROBBLER_SERVICE )) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !Util.isMyServiceRunning(this, ControllerReceiverService.class)) {
if (!Util.checkNotificationListenerPermission(this)){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Util.myNotify(this, this.getResources().getString(R.string.warning), this.getResources().getString(R.string.permission_notification_listener_notice), 72135, PermissionsActivity.class);
}
} else {
Intent ii = new Intent(this, ControllerReceiverService.class);
ii.putExtras(bundleTrack());
Log.d(TAG, "(re)starting controllerreceiver");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.startForegroundService(ii);
} else {
this.startService(ii);
}
}
}
} else if (action.equals(ACTION_CLEARCREDS)) {
if (extras.getBoolean("clearall", false)) {
mNetManager.launchClearAllCreds();
Expand Down Expand Up @@ -457,7 +474,7 @@ private void scrobble(boolean playbackComplete) {
}
}

private void foreGroundService(){
private Bundle bundleTrack(){
Bundle extras = new Bundle();
if (mCurrentTrack != null) {
extras.putString("track", mCurrentTrack.getTrack());
Expand All @@ -470,9 +487,14 @@ private void foreGroundService(){
extras.putString("album", "");
extras.putString("app_name", "");
}
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, mCtx));
return extras;
}

private void foreGroundService(){
if (!settings.isActiveAppEnabled(Util.checkPower(mCtx))) {
this.stopForeground(true);
} else {
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(bundleTrack(), mCtx));
}
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/adam/aslfms/util/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class AppSettings {
private static final String KEY_SESSION = "sessionKey";
private static final String KEY_SCROBBLES = "totalScrobbles";

private static final String KEY_TEMP_APP_ENABLE = "enable_temp_app";
private static final String KEY_ACTIVE_APP_ENABLE = "enable_active_app";
private static final String KEY_SCROBBLING_ENABLE = "enable_scrobbling";
private static final String KEY_NOWPLAYING_ENABLE = "enable_nowplaying";
Expand Down Expand Up @@ -500,6 +501,16 @@ && getAdvancedOptions_raw(PowerOptions.PLUGGED_IN) == AdvancedOptions.SAME_AS_BA
getAdvancedOptions(pow).isActiveAppEnabled());
}

public void setTempExitAppEnabled(PowerOptions pow, boolean b) {
Editor e = prefs.edit();
e.putBoolean(KEY_TEMP_APP_ENABLE + pow.getSettingsPath(), b);
e.commit();
}

public boolean isTempExitAppEnabled(PowerOptions pow) {
return prefs.getBoolean(KEY_TEMP_APP_ENABLE + pow.getSettingsPath(), false);
}

public void setScrobblingEnabled(PowerOptions pow, boolean b) {
Editor e = prefs.edit();
e.putBoolean(KEY_SCROBBLING_ENABLE + pow.getSettingsPath(), b);
Expand Down
15 changes: 1 addition & 14 deletions app/src/main/java/com/adam/aslfms/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ public static void stopAllServices(Context context) {
}


private static boolean isMyServiceRunning(Context context, Class serviceClass) {
public static boolean isMyServiceRunning(Context context, Class serviceClass) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
Expand All @@ -609,19 +609,6 @@ private static boolean isMyServiceRunning(Context context, Class serviceClass) {
}

public static void runServices(Context context) {
// Start listening service if applicable
AppSettings appSettings = new AppSettings(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Util.checkNotificationListenerPermission(context)) {
if (!isMyServiceRunning(context, ControllerReceiverService.class)) {
Log.d(TAG, "(re)starting controllerreceiver");
Intent ii = new Intent(context, ControllerReceiverService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(ii);
} else {
context.startService(ii);
}
}
}
if (!isMyServiceRunning(context, ScrobblingService.class)) {
Log.d(TAG, "(re)starting scrobbleservice");
Intent i = new Intent(context, ScrobblingService.class);
Expand Down

0 comments on commit 2f137a3

Please sign in to comment.