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

Exit Feature & Active App Feature #488

Merged
merged 1 commit into from
Aug 29, 2019
Merged
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ Before the release of a new version of SLS, it will be available here to test fo

## Notices

* SLS is not being maintained as well as previous years. A temporary alternative may be PanoScrobbler.
* SLS is maintained by a few dedicated programmers.
* Please be patient, a large renovation of the app framework is being considered and formulated.

## Developers

Expand Down Expand Up @@ -106,6 +104,7 @@ Before the release of a new version of SLS, it will be available here to test fo
* [Notification Level](https://github.com/simple-last-fm-scrobbler/sls/issues/346)
* Unsecured http:// support for custom servers
* ListenBrainz API for Total Listen Count
* Word blacklist for blocking things like podcasts or tracks on mixed media apps. "Intelligent noise filter"
* Multiple Account Login
* Sequential storage of all tracks, scrobbled, skipped, hearted, and failed with explanations
* markers in scrobble cache for logged in accounts
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/adam/aslfms/OptionsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public void create() {
public boolean onClick(Preference pref) {
if (pref == active_app) {
settings.setActiveAppEnabled(power, active_app.isChecked());
Util.runServices(pref.getContext()); // Scrobbler, Controller, Notification
} else if (pref == scrobble) {
settings.setScrobblingEnabled(power, scrobble.isChecked());
return true;
Expand Down Expand Up @@ -278,6 +279,7 @@ private void createActiveAppPreference() {
active_app = new CheckBoxPreference(OptionsActivity.this);
category.addPreference(active_app);
active_app.setTitle(R.string.active_app);
active_app.setSummary(R.string.active_app_summary);
}

private void createScrobbleEnablePreference() {
Expand Down
42 changes: 15 additions & 27 deletions app/src/main/java/com/adam/aslfms/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
import android.view.MenuItem;
import android.widget.Toast;

import com.adam.aslfms.service.ControllerReceiverService;
import com.adam.aslfms.service.NetApp;
import com.adam.aslfms.service.NotificationBarService;
import com.adam.aslfms.service.ScrobblingService;
import com.adam.aslfms.util.AppSettings;
import com.adam.aslfms.util.ScrobblesDatabase;
Expand All @@ -70,6 +68,8 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
private static final String KEY_COPY_CURRENT_TRACK = "my_copy_button";
private static final String KEY_THEME = "my_theme";

public static final String ACTION_KILL_SERVICE = "action_kill_service";

private AppSettings settings;

private ScrobblesDatabase mDb;
Expand All @@ -84,6 +84,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
int REQUEST_READ_STORAGE;
int REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;

Context mCtx;

@Override
public Resources.Theme getTheme() {
Expand All @@ -106,6 +107,7 @@ protected void onCreate(Bundle savedInstanceState) {
setTheme(settings.getAppTheme());

mDb = new ScrobblesDatabase(this);
mCtx = this;

try {
mDb.open();
Expand Down Expand Up @@ -133,31 +135,7 @@ protected void onCreate(Bundle savedInstanceState) {
settings.setWhatsNewViewedVersion(v);
mDb.rebuildDataBaseOnce(); // TODO: VERSION 1.5.8 only!
}
// Start listening service if applicable
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.d(TAG, "(re)starting controllerreceiver");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.startForegroundService(new Intent(this, ControllerReceiverService.class));
} else {
this.startService(new Intent(this, ControllerReceiverService.class));
}
}
Intent i = new Intent(this, NotificationBarService.class);
i.setAction(NotificationBarService.ACTION_NOTIFICATION_BAR_UPDATE);
i.putExtra("track", "");
i.putExtra("artist", "");
i.putExtra("album", "");
i.putExtra("app_name", "");
Intent ii = new Intent(this, ScrobblingService.class);
ii.setAction(ScrobblingService.ACTION_START_SCROBBLER_SERVICE);
Log.d(TAG, "(re)starting scrobbleservice, notificationbarservice");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
this.startForegroundService(i);
this.startForegroundService(ii);
} else {
this.startService(i);
this.startService(ii);
}
Util.runServices(this); // Scrobbler, Controller, Notification
}

@Override
Expand Down Expand Up @@ -243,6 +221,16 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.menu_whats_new:
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);
finish();
settings.setActiveAppEnabled(Util.checkPower(this),currentActiveState);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
return true;
}
return super.onOptionsItemSelected(item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.adam.aslfms.service;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
Expand All @@ -21,7 +22,9 @@
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;

import com.adam.aslfms.util.AppSettings;
import com.adam.aslfms.util.NotificationCreator;
import com.adam.aslfms.util.Util;

import java.lang.ref.WeakReference;
import java.util.Set;
Expand All @@ -30,7 +33,7 @@
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
public class ControllerReceiverService extends android.service.notification.NotificationListenerService implements RemoteController.OnClientUpdateListener {

private static final String TAG = "ControllerReceiverSrvc";
private static WeakReference<RemoteController> mRemoteController = new WeakReference<>(null);
private ControllerReceiverCallback controllerReceiverCallback;
Expand All @@ -49,7 +52,8 @@ public IBinder onBind(Intent intent) {
@SuppressWarnings("NewApi")
public void onCreate() {
super.onCreate();

AppSettings settings = new AppSettings(this);

registerControllerReceiverCallback();

Bundle extras = new Bundle();
Expand All @@ -58,10 +62,14 @@ public void onCreate() {
extras.putString("album", album);
extras.putString("app_name", albumArtist);
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, this));
if (!settings.isActiveAppEnabled(Util.checkPower(this))) {
this.stopForeground(true); // TODO: test if this conflicts/stops scrobbles
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
AppSettings settings = new AppSettings(this);

registerControllerReceiverCallback();

Expand All @@ -71,7 +79,11 @@ public int onStartCommand(Intent intent, int flags, int startId) {
extras.putString("album", album);
extras.putString("app_name", albumArtist);
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, this));
return START_STICKY;
if (!settings.isActiveAppEnabled(Util.checkPower(this))) {
this.stopForeground(true); // TODO: test if this conflicts/stops scrobbles
return Service.START_NOT_STICKY;
}
return Service.START_STICKY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public void onCreate() {
mDb.open();
mNetManager = new NetworkerManager(this, mDb);

if (settings.isActiveAppEnabled(Util.checkPower(mCtx))) {
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, mCtx));
} else {
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, mCtx));
if (!settings.isActiveAppEnabled(Util.checkPower(mCtx))) {
this.stopForeground(true); // TODO: test if this conflicts/stops scrobbles
}
}
Expand All @@ -54,11 +53,10 @@ public void onDestroy() {
@Override
public int onStartCommand(Intent i, int flags, int startId) {
handleCommand(i, startId);

if (settings.isActiveAppEnabled(Util.checkPower(mCtx))) {
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, mCtx));
} else {
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, mCtx));
if (!settings.isActiveAppEnabled(Util.checkPower(mCtx))) {
this.stopForeground(true); // TODO: test if this conflicts/stops scrobbles
return Service.START_NOT_STICKY;
}
return Service.START_STICKY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public void onCreate() {
extras.putString("app_name", "");
}
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, mCtx));
if (!settings.isActiveAppEnabled(Util.checkPower(mCtx))) {
this.stopForeground(true); // TODO: test if this conflicts/stops scrobbles
}
}

@Override
Expand All @@ -119,6 +122,10 @@ public int onStartCommand(Intent i, int flags, int startId) {
extras.putString("app_name", "");
}
this.startForeground(NotificationCreator.FOREGROUND_ID, NotificationCreator.prepareNotification(extras, mCtx));
if (!settings.isActiveAppEnabled(Util.checkPower(mCtx))) {
this.stopForeground(true); // TODO: test if this conflicts/stops scrobbles
return Service.START_NOT_STICKY;
}
return Service.START_STICKY;
}

Expand Down
23 changes: 18 additions & 5 deletions app/src/main/java/com/adam/aslfms/util/NotificationCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,31 @@ public class NotificationCreator {
private static final String TAG = "NotificationCreator";
public final static int FOREGROUND_ID = 1098733;
public final static String FOREGROUND_CHANNEL_ID = "com.adam.aslfms";
private static AppSettings settings;

private NetworkerManager mNetManager;

private NotificationManager mNotificationManager;

public static void initChannels(Context context) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (!settings.isActiveAppEnabled(Util.checkPower(context))) {
notificationManager.cancel(FOREGROUND_ID);
}
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(FOREGROUND_CHANNEL_ID,
context.getString(R.string.app_name_short),
Util.notificationStringToInt(context));
channel.setDescription(context.getString(R.string.app_name));
channel.setSound(null,null);
channel.setSound(null, null);
notificationManager.createNotificationChannel(channel);
}

public static Notification prepareNotification(Bundle extras, Context context) {
settings = new AppSettings(context);
// handle build version above android oreo
initChannels(context);
////
Expand Down Expand Up @@ -95,13 +100,21 @@ public static Notification prepareNotification(Bundle extras, Context context) {
notificationBuilder = new NotificationCompat.Builder(context);
}

if (settings.isActiveAppEnabled(Util.checkPower(context))){
notificationBuilder
.setAutoCancel(false)
.setOngoing(true);
} else {
notificationBuilder
.setAutoCancel(true)
.setOngoing(false);
}

notificationBuilder
.setContentTitle(track + " " + context.getString(R.string.by) + " " + artist)
.setSmallIcon(R.drawable.ic_icon)
.setContentText(album + " : " + app_name)
.setContentIntent(contentIntent)
.setOngoing(true)
.setAutoCancel(false)
.setColor(Color.RED)
.setPriority(Util.oldNotificationStringToInt(context))
.addAction(heartAction)
Expand Down
44 changes: 44 additions & 0 deletions app/src/main/java/com/adam/aslfms/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@

import com.adam.aslfms.R;
import com.adam.aslfms.SettingsActivity;
import com.adam.aslfms.service.ControllerReceiverService;
import com.adam.aslfms.service.NetApp;
import com.adam.aslfms.service.NotificationBarService;
import com.adam.aslfms.service.ScrobblingService;
import com.adam.aslfms.util.enums.NetworkOptions;
import com.adam.aslfms.util.enums.PowerOptions;
Expand Down Expand Up @@ -559,5 +561,47 @@ public static int notificationStringToInt(Context ctx){
}
return NotificationManager.IMPORTANCE_DEFAULT;
}
public static void stopMyService(Intent i, Context context){
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.stopService(i);
}

public static void stopAllServices(Context context){
Intent i = new Intent(context, NotificationBarService.class);
Intent ii = new Intent(context, ScrobblingService.class);
Intent iii = new Intent(context, ControllerReceiverService.class);
stopMyService(i, context);
stopMyService(ii, context);
stopMyService(iii, context);
}

public static void runServices(Context context){
// Start listening service if applicable
Intent i = new Intent(context, NotificationBarService.class);
Intent ii = new Intent(context, ScrobblingService.class);
Intent iii = new Intent(context, ControllerReceiverService.class);
i.setAction(NotificationBarService.ACTION_NOTIFICATION_BAR_UPDATE);
i.putExtra("track", "");
i.putExtra("artist", "");
i.putExtra("album", "");
i.putExtra("app_name", "");
ii.setAction(ScrobblingService.ACTION_START_SCROBBLER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.d(TAG, "(re)starting controllerreceiver");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(iii);
} else {
context.startService(iii);
}
}
Log.d(TAG, "(re)starting scrobbleservice, notificationbarservice");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(i);
context.startForegroundService(ii);
} else {
context.startService(i);
context.startService(ii);
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
android:id="@+id/menu_whats_new"
android:icon="@android:drawable/ic_menu_agenda"
android:title="@string/whats_new" />
<item
android:id="@+id/menu_exit"
android:icon="@android:drawable/ic_menu_close_clear_cancel"
android:title="@string/menu_exit" />
</menu>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<string name="will_submit_stuff">Will submit listening information to this website</string>
<string name="also_disable_np_title">Also disable now-playing</string>
<string name="also_disable_np_summary">[Hmpf]</string>
<string name="menu_exit">Exit</string>
<!-- END original strings -->

<!-- START about strings -->
Expand Down Expand Up @@ -139,9 +140,12 @@
<string name="unknown_mapi">Unknown app (pre SLS v1.2.3)</string>
<string name="notification_controller">Notification Controller</string>
<string name="new_music_app">New Music App Detected, Click Here</string>
<string name="clear_apps">Clear Apps</string> <!-- TODO: Translate -->
<!-- END mapis strings -->

<!-- START options_prefs strings -->
<string name="active_app">Active App Notification</string>
<string name="active_app_summary">WARNING, may skip scrobbles.</string>
<string name="options_title">Options</string>
<string name="advanced_options_title">Advanced options</string>
<string name="scrobbling">Scrobble</string>
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/values/strings_nontranslatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
<string name="website_url" translatable="false">https://github.com/simple-last-fm-scrobbler/sls</string>
<string name="issues_url" translatable="false">https://github.com/simple-last-fm-scrobbler/sls/issues</string>
<string name="trouble_shoot" translatable="false">https://github.com/simple-last-fm-scrobbler/sls/blob/master/TroubleShooting.md</string>
<string name="active_app" translatable="false">Active App</string>
<string name="clear_apps" translatable="false">Clear Apps</string> <!-- TODO: Translate -->
<string name="nixtape_url" translatable="false">Nixtape URL</string>
<string name="gnukebox_url" translatable="false">Gnukebox URL</string>
<string name="listen_brainz_url" translatable="false">ListenBrainz URL</string>
Expand Down