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

Commit

Permalink
Permissions, Initializing, Language Choice
Browse files Browse the repository at this point in the history
- 1.6.2 (2018-9-8) codename: engus
  * Fixed music not being recognized immediately
  * Permission to bypass new features
  * Language selection
  • Loading branch information
Austin Hogan committed Sep 8, 2019
1 parent 754f76a commit 76ad4f6
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Before the release of a new version of SLS, it will be available here to test fo
* markers in scrobble cache for logged in accounts
* Store scrobbles before user is logged in
* Manual scrobbling, for example Vinyl Records, CD, Cassette and other older media machines.
* Import database

## Features

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/assets/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ https://github.com/simple-last-fm-scrobbler/sls
For more details.

- 1.6.2 (2018-9-8) codename: engus
* Fixed old music app broadcast clash
* Fixed music not being recognized immediately
* Permission to bypass new features
* Language selection

Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/adam/aslfms/OptionsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
package com.adam.aslfms;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
Expand Down Expand Up @@ -56,13 +56,16 @@ public class OptionsActivity extends AppCompatPreferenceActivity {
private static final String KEY_EXPORT_DB = "export_database";
private static final String KEY_NOTIFICATION_PRIORITY = "notification_priority";
private static final String KEY_LANGUAGES_LIST = "languages_list";
private static final String KEY_PERMISSION_SHOW = "permission_activity_show";

private AppSettings settings;

private SeekBarPreference mScrobblePoint;
private PowerSpecificPrefs mBatteryOptions;
private PowerSpecificPrefs mPluggedOptions;

Context ctx = this;


@Override
protected void attachBaseContext(Context newBase) {
Expand Down Expand Up @@ -175,6 +178,7 @@ public PowerSpecificPrefs(PowerOptions power,
private PowerOptions power;
private PreferenceCategory category;

private Preference showPermissionActivity;
private ListPreference chooser;
private CheckBoxPreference active_app;
private CheckBoxPreference scrobble;
Expand All @@ -197,6 +201,7 @@ public void create() {
createNetPreference();
createRoamingPreference();
exportdatabase = findPreference(KEY_EXPORT_DB);
showPermissionActivity = findPreference(KEY_PERMISSION_SHOW);
notification_priority = (ListPreference) findPreference(KEY_NOTIFICATION_PRIORITY);
notification_priority.setDefaultValue(Util.notificationStringToInt(getApplicationContext()));
languages_list = (ListPreference) findPreference(KEY_LANGUAGES_LIST);
Expand All @@ -215,6 +220,12 @@ public void create() {
return false;
}
);
showPermissionActivity.setOnPreferenceClickListener((Preference preference) ->{
Intent i = new Intent(ctx, PermissionsActivity.class);
startActivity(i);
return false;
}
);
}

public boolean onClick(Preference pref) {
Expand Down
48 changes: 40 additions & 8 deletions app/src/main/java/com/adam/aslfms/PermissionsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
Expand All @@ -30,6 +31,7 @@
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
Expand All @@ -48,13 +50,14 @@ public class PermissionsActivity extends AppCompatActivity {
private static final String TAG = "PermissionsActivity";

int WRITE_EXTERNAL_STORAGE;
int REQUEST_READ_STORAGE;
int REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;
int disabledColor = Color.argb(25, 0,0,0);
int enabledColor = Color.argb(75, 0,255,0);
int warningColor = Color.argb(80,255,0,0);

Button btnContinue = null;
AppSettings settings = null;
Button btnSkip = null;
Context ctx = this;

@Override
protected void attachBaseContext(Context newBase) {
Expand All @@ -63,7 +66,7 @@ protected void attachBaseContext(Context newBase) {

@Override
public Resources.Theme getTheme() {
AppSettings settings = new AppSettings(this);
settings = new AppSettings(this);
Resources.Theme theme = super.getTheme();
theme.applyStyle(settings.getAppTheme(), true);
Log.d(TAG, getResources().getResourceName(settings.getAppTheme()));
Expand All @@ -83,7 +86,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permissions);

AppSettings settings = new AppSettings(this);
settings = new AppSettings(this);
setTheme(settings.getAppTheme());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Expand All @@ -95,7 +98,8 @@ protected void onCreate(Bundle savedInstanceState) {

public void checkCurrrentPermissions(){
boolean enabled;
btnContinue = findViewById(R.id.button_continue);
btnSkip = findViewById(R.id.button_skip);
btnSkip.setBackgroundColor(enabledColor);
Button externalPermBtn = findViewById(R.id.button_permission_external_storage);
Button notifiPermBtn = findViewById(R.id.button_permission_notification_listener);
Button batteryPermBtn = findViewById(R.id.button_permission_battery_optimizations);
Expand Down Expand Up @@ -171,6 +175,34 @@ public void checkCurrrentPermissions(){
colorPermission(true, batteryBasicPermBtn);
}
});

btnSkip.setOnClickListener((View view) -> {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
settings.setKeyBypassNewPermissions(1);
Intent intent = new Intent(ctx, SettingsActivity.class);
ctx.startActivity(intent);
Util.runServices(ctx);
break;

case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
String message = 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);
}
builder.setMessage(message).setPositiveButton(R.string.yes, dialogClickListener)
.setNegativeButton(R.string.no, dialogClickListener).show();
});
}

public void colorPermission(boolean enabled, Button button){
Expand All @@ -183,18 +215,18 @@ public void colorPermission(boolean enabled, Button button){

private void permsCheck() {
//PERMISSION CHECK

boolean allPermissionsGo = true;
allPermissionsGo = allPermissionsGo && Util.checkNotificationListenerPermission(this);
allPermissionsGo = allPermissionsGo && Util.checkExternalPermission(this);
allPermissionsGo = allPermissionsGo && Util.checkBatteryOptimizationsPermission(this);
allPermissionsGo = allPermissionsGo && Util.checkBatteryOptimizationBasicPermission(this);
Log.d(TAG,"All Permissions Go: " + allPermissionsGo);
if (allPermissionsGo) {
btnContinue.setBackgroundColor(enabledColor);
Intent intent = new Intent(this, SettingsActivity.class);
this.startActivity(intent);
} else {
btnContinue.setBackgroundColor(disabledColor);
settings.setKeyBypassNewPermissions(0);
Util.runServices(ctx);
}
}

Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/adam/aslfms/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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;
Expand Down Expand Up @@ -128,7 +127,7 @@ protected void onCreate(Bundle savedInstanceState) {

// TODO: VERIFY EVERYTHING BELOW IS SAFE
int v = Util.getAppVersionCode(this, getPackageName());
if (settings.getWhatsNewViewedVersion() < v) {
if (settings.getWhatsNewViewedVersion() < v && settings.getKeyBypassNewPermissions() != 2) {
new WhatsNewDialog(this).show();
settings.setWhatsNewViewedVersion(v);
}
Expand Down Expand Up @@ -258,6 +257,9 @@ private void credsCheck() {
}

private void permsCheck() {
if (settings.getKeyBypassNewPermissions() == 1){
return;
}
//PERMISSION CHECK
boolean allPermissionsGo = true;
allPermissionsGo = allPermissionsGo && Util.checkNotificationListenerPermission(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

import com.adam.aslfms.PermissionsActivity;
import com.adam.aslfms.R;
import com.adam.aslfms.util.Track;
import com.adam.aslfms.util.Util;

Expand Down Expand Up @@ -177,6 +180,10 @@ void parseTrack(Context ctx, Track.Builder b, Bundle bundle) {
long audioid = getAudioId(bundle);

if (shouldFetchFromMediaStore(ctx, audioid)) { // read from MediaStore
if(Build.VERSION_CODES.KITKAT >= Build.VERSION.SDK_INT && !Util.checkExternalPermission(ctx)){
Util.myNotify(ctx, ctx.getResources().getString(R.string.warning), ctx.getResources().getString(R.string.permission_external_storage), 81234, PermissionsActivity.class);
return;
}
readTrackFromMediaStore(ctx, b, audioid);
} else {
readTrackFromBundleData(b, bundle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.adam.aslfms.util.Util;

import java.util.HashSet;
import java.util.ResourceBundle;

/**
* @author a93h
* @since 1.5.8
Expand All @@ -59,6 +61,9 @@ 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);

Expand All @@ -76,6 +81,9 @@ public void onCreate() {

@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();
Expand All @@ -84,6 +92,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
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);
}
Expand Down Expand Up @@ -121,7 +130,11 @@ public void onListenerDisconnected() {
public void init(){
MediaSessionManager mediaSessionManager = null;
try {
Log.d(TAG,"Detecting initial media session");
mediaSessionManager = (MediaSessionManager) this.getApplicationContext().getSystemService(Context.MEDIA_SESSION_SERVICE) ;
ComponentName listenerComponent =
new ComponentName(this, ControllerReceiverService.class);
ControllerReceiverSession.initialListener(this, mediaSessionManager.getActiveSessions(listenerComponent));
mControllerReceiverSession = new ControllerReceiverSession(this);
mediaSessionManager.addOnActiveSessionsChangedListener(mControllerReceiverSession, new ComponentName(this, ControllerReceiverService.class));
Log.d(TAG, "media session manager loaded");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@
public class ControllerReceiverSession implements MediaSessionManager.OnActiveSessionsChangedListener {
private final static String TAG = "ContrlRcvrSession";

private HashMap<MediaSession.Token, Pair<MediaController, MediaController.Callback>> controllersMap = new HashMap<>();
private List<MediaController> mControllers = null;
private int sessionCount = 0;
private static HashMap<MediaSession.Token, Pair<MediaController, MediaController.Callback>> controllersMap = new HashMap<>();
private static List<MediaController> mControllers = null;
private static int sessionCount = 0;
private Context mContext;

public ControllerReceiverSession(Context context) {
super();
this.mContext = context;
}

@Override
public void onActiveSessionsChanged(List<MediaController> controllers) {
Log.d(TAG, "active session change detected");
mControllers = controllers;
Expand All @@ -79,6 +80,7 @@ public void onActiveSessionsChanged(List<MediaController> controllers) {
removeSessions(tokens,pacakgeNames);
}


public void removeSessions(HashSet<MediaSession.Token> tokens, HashSet<String> packageNames) {
Iterator it = controllersMap.entrySet().iterator();
while (it.hasNext()) {
Expand All @@ -98,4 +100,44 @@ public void removeSessions(HashSet<MediaSession.Token> tokens, HashSet<String> p
sessionCount = controllersMap.size();
Log.d(TAG,"number of active sessions: " + sessionCount);
}

public static void initialListener(Context context, List<MediaController> controllers){
Log.d(TAG, "initial active session change detected");
mControllers = controllers;
HashSet<MediaSession.Token> tokens = new HashSet<>();
HashSet<String> pacakgeNames = new HashSet<>();
if (controllers != null) {
for (MediaController controller : controllers) {
tokens.add(controller.getSessionToken());
pacakgeNames.add(controller.getPackageName());
if (!controllersMap.containsKey(controller.getSessionToken())) {
Log.d(TAG, "onActiveSessionsChanged [" + controllers.size() + "] : " + controller.getPackageName());
MediaController.Callback mccb = new ControllerReceiverCallback(context, controller.getPackageName(), controller);
controller.registerCallback(mccb);
Pair<MediaController,MediaController.Callback> pair = Pair.create(controller, mccb);
synchronized (controllersMap) {
controllersMap.put(controller.getSessionToken(), pair);
}
}
}
}
// Now remove old sessions that are not longer active.
Iterator it = controllersMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry group = (Map.Entry)it.next();
MediaSession.Token token = (MediaSession.Token) group.getKey();
Pair pair = (Pair) group.getValue();
MediaController mediaController = (MediaController) pair.first;
MediaController.Callback mediaControllerCallback = (MediaController.Callback) pair.second;
if ((tokens != null && !tokens.contains(token))) { // bad code ?? || (packageNames != null && packageNames.contains(mediaController.getPackageName()))
mediaController.unregisterCallback(mediaControllerCallback);
synchronized(controllersMap) {
it.remove();
Log.d(TAG,"controller " + mediaController.getPackageName() + " removed.");
}
}
}
sessionCount = controllersMap.size();
Log.d(TAG,"number of active sessions: " + sessionCount);
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/adam/aslfms/service/Handshaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ public void run() {

notifyAuthStatusUpdate(AuthStatus.AUTHSTATUS_OK);


// launch services after login
Util.runServices(mCtx);

// won't do anything if there aren't any scrobbles,
// but will submit those tracks that were prepared
// but interrupted by a badauth
Expand Down
Loading

0 comments on commit 76ad4f6

Please sign in to comment.