From 754f76a619ad73ac45e04163eb432ed761327620 Mon Sep 17 00:00:00 2001 From: Austin Hogan Date: Sun, 8 Sep 2019 16:16:47 -0400 Subject: [PATCH 1/4] Language Options ContextWrapper for customizing the default language. --- app/build.gradle | 4 +- app/src/main/assets/changelog.txt | 5 + .../com/adam/aslfms/ChangeThemeActivity.java | 9 + .../com/adam/aslfms/MusicAppsActivity.java | 7 + .../java/com/adam/aslfms/OptionsActivity.java | 31 ++- .../com/adam/aslfms/PermissionsActivity.java | 7 + .../com/adam/aslfms/SettingsActivity.java | 8 +- .../com/adam/aslfms/UserCredActivity.java | 6 + .../adam/aslfms/UserCredsListActivity.java | 7 + .../aslfms/ViewCorrectionRulesActivity.java | 6 + .../aslfms/ViewScrobbleCacheActivity.java | 6 + .../com/adam/aslfms/util/AppSettings.java | 17 ++ .../adam/aslfms/util/MyContextWrapper.java | 72 +++++ .../main/java/com/adam/aslfms/util/Util.java | 6 +- .../main/res/layout/edit_user_credentials.xml | 2 +- app/src/main/res/values-en-rUS/strings.xml | 260 ++++++++++++++++++ app/src/main/res/values-fr/strings.xml | 106 ++++++- app/src/main/res/values/strings.xml | 44 +-- .../res/values/strings_nontranslatable.xml | 16 ++ app/src/main/res/xml/options_prefs.xml | 8 + 20 files changed, 585 insertions(+), 42 deletions(-) create mode 100644 app/src/main/java/com/adam/aslfms/util/MyContextWrapper.java create mode 100644 app/src/main/res/values-en-rUS/strings.xml diff --git a/app/build.gradle b/app/build.gradle index a9b27502..ba38a2c8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "com.adam.aslfms" minSdkVersion 14 targetSdkVersion 26 - versionCode = 52 - versionName = "1.6.1" + versionCode = 53 + versionName = "1.6.2" testApplicationId "com.adam.aslfms.test" testInstrumentationRunner "android.test.InstrumentationTestRunner" diff --git a/app/src/main/assets/changelog.txt b/app/src/main/assets/changelog.txt index 2d9253dc..4f7cc296 100644 --- a/app/src/main/assets/changelog.txt +++ b/app/src/main/assets/changelog.txt @@ -2,6 +2,11 @@ Visit, 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 + * Permission to bypass new features + * Language selection + - 1.6.1 (2018-9-7) codename: dingus * Fixed icon kitkat diff --git a/app/src/main/java/com/adam/aslfms/ChangeThemeActivity.java b/app/src/main/java/com/adam/aslfms/ChangeThemeActivity.java index 0e43a61a..c9d02243 100644 --- a/app/src/main/java/com/adam/aslfms/ChangeThemeActivity.java +++ b/app/src/main/java/com/adam/aslfms/ChangeThemeActivity.java @@ -22,14 +22,18 @@ 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.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import com.adam.aslfms.util.AppSettings; +import com.adam.aslfms.util.MyContextWrapper; + /** * @author a93h * @since 1.5.8 @@ -38,6 +42,11 @@ public class ChangeThemeActivity extends AppCompatActivity { private static final String TAG = "ChangeThemeActivity"; private AppSettings settings; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(MyContextWrapper.wrap(newBase)); + } + @Override public Resources.Theme getTheme() { settings = new AppSettings(this); diff --git a/app/src/main/java/com/adam/aslfms/MusicAppsActivity.java b/app/src/main/java/com/adam/aslfms/MusicAppsActivity.java index c7a60f2c..74985e84 100644 --- a/app/src/main/java/com/adam/aslfms/MusicAppsActivity.java +++ b/app/src/main/java/com/adam/aslfms/MusicAppsActivity.java @@ -21,6 +21,7 @@ package com.adam.aslfms; +import android.content.Context; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; @@ -36,6 +37,7 @@ import com.adam.aslfms.receiver.MusicAPI; import com.adam.aslfms.util.AppSettings; +import com.adam.aslfms.util.MyContextWrapper; import com.adam.aslfms.util.Util; import com.example.android.supportv7.app.AppCompatPreferenceActivity; @@ -58,6 +60,11 @@ public class MusicAppsActivity extends AppCompatPreferenceActivity { private AppSettings settings; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(MyContextWrapper.wrap(newBase)); + } + @Override public Resources.Theme getTheme() { settings = new AppSettings(this); diff --git a/app/src/main/java/com/adam/aslfms/OptionsActivity.java b/app/src/main/java/com/adam/aslfms/OptionsActivity.java index 3f5af1bf..476a355a 100644 --- a/app/src/main/java/com/adam/aslfms/OptionsActivity.java +++ b/app/src/main/java/com/adam/aslfms/OptionsActivity.java @@ -21,7 +21,9 @@ package com.adam.aslfms; +import android.content.Context; import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.ListPreference; @@ -34,6 +36,7 @@ import android.view.MenuItem; import com.adam.aslfms.util.AppSettings; +import com.adam.aslfms.util.MyContextWrapper; import com.adam.aslfms.util.Util; import com.adam.aslfms.util.enums.AdvancedOptions; import com.adam.aslfms.util.enums.AdvancedOptionsWhen; @@ -41,6 +44,8 @@ import com.adam.aslfms.util.enums.PowerOptions; import com.example.android.supportv7.app.AppCompatPreferenceActivity; +import java.util.Arrays; + public class OptionsActivity extends AppCompatPreferenceActivity { private static final String TAG = "OptionsGeneralScreen"; @@ -50,6 +55,7 @@ public class OptionsActivity extends AppCompatPreferenceActivity { private static final String KEY_PLUGGED = "ao_plugged"; 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 AppSettings settings; @@ -57,6 +63,12 @@ public class OptionsActivity extends AppCompatPreferenceActivity { private PowerSpecificPrefs mBatteryOptions; private PowerSpecificPrefs mPluggedOptions; + + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(MyContextWrapper.wrap(newBase)); + } + @Override public Resources.Theme getTheme() { settings = new AppSettings(this); @@ -173,6 +185,7 @@ public PowerSpecificPrefs(PowerOptions power, private CheckBoxPreference roaming; private Preference exportdatabase; private ListPreference notification_priority; + private ListPreference languages_list; public void create() { createChooserPreference(); @@ -186,6 +199,22 @@ public void create() { exportdatabase = findPreference(KEY_EXPORT_DB); notification_priority = (ListPreference) findPreference(KEY_NOTIFICATION_PRIORITY); notification_priority.setDefaultValue(Util.notificationStringToInt(getApplicationContext())); + languages_list = (ListPreference) findPreference(KEY_LANGUAGES_LIST); + notification_priority.setOnPreferenceChangeListener((Preference preference, Object object) -> { + settings.setKeyNotificationPriority(notification_priority.getValue()); + return false; + } + ); + languages_list.setOnPreferenceChangeListener((Preference preference, Object object) -> { + String userSelection = (String) object; + String[] country_codes = getResources().getStringArray(R.array.language_codes); + String[] langauge_list = getResources().getStringArray(R.array.language_list); + int position = Arrays.asList(langauge_list).indexOf(userSelection); + settings.setAppLocale(country_codes[position]); + recreate(); + return false; + } + ); } public boolean onClick(Preference pref) { @@ -207,8 +236,6 @@ public boolean onClick(Preference pref) { return true; } else if (pref == exportdatabase) { Util.exportAllDatabases(getApplicationContext()); - } else if (pref == notification_priority){ - settings.setKeyNotificationPriority(notification_priority.getValue()); } return false; } diff --git a/app/src/main/java/com/adam/aslfms/PermissionsActivity.java b/app/src/main/java/com/adam/aslfms/PermissionsActivity.java index b34acd6c..e8e3fffb 100644 --- a/app/src/main/java/com/adam/aslfms/PermissionsActivity.java +++ b/app/src/main/java/com/adam/aslfms/PermissionsActivity.java @@ -21,6 +21,7 @@ package com.adam.aslfms; import android.Manifest; +import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.graphics.Color; @@ -36,6 +37,7 @@ import android.widget.TextView; import com.adam.aslfms.util.AppSettings; +import com.adam.aslfms.util.MyContextWrapper; import com.adam.aslfms.util.Util; /** * @author a93h @@ -54,6 +56,11 @@ public class PermissionsActivity extends AppCompatActivity { Button btnContinue = null; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(MyContextWrapper.wrap(newBase)); + } + @Override public Resources.Theme getTheme() { AppSettings settings = new AppSettings(this); diff --git a/app/src/main/java/com/adam/aslfms/SettingsActivity.java b/app/src/main/java/com/adam/aslfms/SettingsActivity.java index 8bd4f10d..29f5c63a 100644 --- a/app/src/main/java/com/adam/aslfms/SettingsActivity.java +++ b/app/src/main/java/com/adam/aslfms/SettingsActivity.java @@ -27,6 +27,7 @@ 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; @@ -39,6 +40,7 @@ import com.adam.aslfms.service.NetApp; import com.adam.aslfms.service.ScrobblingService; import com.adam.aslfms.util.AppSettings; +import com.adam.aslfms.util.MyContextWrapper; import com.adam.aslfms.util.ScrobblesDatabase; import com.adam.aslfms.util.Util; import com.example.android.supportv7.app.AppCompatPreferenceActivity; @@ -79,6 +81,11 @@ public class SettingsActivity extends AppCompatPreferenceActivity { Context mCtx; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(MyContextWrapper.wrap(newBase)); + } + @Override public Resources.Theme getTheme() { settings = new AppSettings(this); @@ -124,7 +131,6 @@ protected void onCreate(Bundle savedInstanceState) { if (settings.getWhatsNewViewedVersion() < v) { new WhatsNewDialog(this).show(); settings.setWhatsNewViewedVersion(v); - mDb.rebuildDataBaseOnce(); // TODO: VERSION 1.5.9 only! } Util.runServices(this); // Scrobbler, Controller, Notification } diff --git a/app/src/main/java/com/adam/aslfms/UserCredActivity.java b/app/src/main/java/com/adam/aslfms/UserCredActivity.java index c4068556..4f07c576 100644 --- a/app/src/main/java/com/adam/aslfms/UserCredActivity.java +++ b/app/src/main/java/com/adam/aslfms/UserCredActivity.java @@ -39,6 +39,7 @@ import com.adam.aslfms.service.NetApp; import com.adam.aslfms.service.ScrobblingService; import com.adam.aslfms.util.AppSettings; +import com.adam.aslfms.util.MyContextWrapper; import com.adam.aslfms.util.Util; import com.example.android.supportv7.app.AppCompatPreferenceActivity; @@ -61,6 +62,11 @@ public class UserCredActivity extends AppCompatPreferenceActivity { private Preference mClearCreds; private Preference mCreateUser; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(MyContextWrapper.wrap(newBase)); + } + @Override public Resources.Theme getTheme() { settings = new AppSettings(this); diff --git a/app/src/main/java/com/adam/aslfms/UserCredsListActivity.java b/app/src/main/java/com/adam/aslfms/UserCredsListActivity.java index 0dcd1a83..d4abc93b 100644 --- a/app/src/main/java/com/adam/aslfms/UserCredsListActivity.java +++ b/app/src/main/java/com/adam/aslfms/UserCredsListActivity.java @@ -38,6 +38,7 @@ import com.adam.aslfms.service.NetApp; import com.adam.aslfms.service.ScrobblingService; import com.adam.aslfms.util.AppSettings; +import com.adam.aslfms.util.MyContextWrapper; import com.adam.aslfms.util.Util; import com.example.android.supportv7.app.AppCompatPreferenceActivity; @@ -57,6 +58,12 @@ public class UserCredsListActivity extends AppCompatPreferenceActivity { private HashMap mUserCredsAppToPrefMap; private Preference mClearAllCreds; + + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(MyContextWrapper.wrap(newBase)); + } + @Override public Resources.Theme getTheme() { settings = new AppSettings(this); diff --git a/app/src/main/java/com/adam/aslfms/ViewCorrectionRulesActivity.java b/app/src/main/java/com/adam/aslfms/ViewCorrectionRulesActivity.java index a2170fda..2de0ad92 100644 --- a/app/src/main/java/com/adam/aslfms/ViewCorrectionRulesActivity.java +++ b/app/src/main/java/com/adam/aslfms/ViewCorrectionRulesActivity.java @@ -41,6 +41,7 @@ import android.widget.TextView; import com.adam.aslfms.util.AppSettings; +import com.adam.aslfms.util.MyContextWrapper; import com.adam.aslfms.util.ScrobblesDatabase; import com.adam.aslfms.util.CorrectionRule; import com.adam.aslfms.util.Util; @@ -61,6 +62,11 @@ public class ViewCorrectionRulesActivity extends AppCompatActivity { private ListView rulesListView; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(MyContextWrapper.wrap(newBase)); + } + @Override public Resources.Theme getTheme() { settings = new AppSettings(this); diff --git a/app/src/main/java/com/adam/aslfms/ViewScrobbleCacheActivity.java b/app/src/main/java/com/adam/aslfms/ViewScrobbleCacheActivity.java index eb4d1c39..c9b817e5 100644 --- a/app/src/main/java/com/adam/aslfms/ViewScrobbleCacheActivity.java +++ b/app/src/main/java/com/adam/aslfms/ViewScrobbleCacheActivity.java @@ -52,6 +52,7 @@ import com.adam.aslfms.service.NetApp; import com.adam.aslfms.service.ScrobblingService; import com.adam.aslfms.util.AppSettings; +import com.adam.aslfms.util.MyContextWrapper; import com.adam.aslfms.util.ScrobblesDatabase; import com.adam.aslfms.util.Track; import com.adam.aslfms.util.Util; @@ -73,6 +74,11 @@ public class ViewScrobbleCacheActivity extends AppCompatActivity { private ListView mListView; private TextView mSortHeaderTextView; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(MyContextWrapper.wrap(newBase)); + } + @Override public Resources.Theme getTheme() { settings = new AppSettings(this); diff --git a/app/src/main/java/com/adam/aslfms/util/AppSettings.java b/app/src/main/java/com/adam/aslfms/util/AppSettings.java index efab9ca1..c80e6d5d 100644 --- a/app/src/main/java/com/adam/aslfms/util/AppSettings.java +++ b/app/src/main/java/com/adam/aslfms/util/AppSettings.java @@ -38,6 +38,8 @@ import com.adam.aslfms.util.enums.SortField; import com.adam.aslfms.util.enums.SubmissionType; +import java.util.Locale; + import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; @@ -92,6 +94,7 @@ public class AppSettings { private static final String KEY_WIDGET_ALSO_DISABLE_NP = "widget_also_disable_np"; private static final String KEY_THEME = "my_theme"; + private static final String KEY_LOCALE = "my_locale"; private final Context mCtx; private final SharedPreferences prefs; @@ -126,6 +129,7 @@ public boolean hasAnyCreds() { } public void setAppTheme(int i){ + Log.d(TAG,"new app theme: " + i); Editor e = prefs.edit(); e.putInt(KEY_THEME, i); e.commit(); @@ -135,6 +139,18 @@ public int getAppTheme(){ return prefs.getInt(KEY_THEME, R.style.AppTheme); } + + public void setAppLocale(String i){ + Log.d(TAG,"new app locale " + i); + Editor e = prefs.edit(); + e.putString(KEY_LOCALE, i); + e.commit(); + } + + public String getAppLocale(){ + return prefs.getString(KEY_LOCALE, Locale.getDefault().getLanguage()); + } + public void setUsername(NetApp napp, String s) { Editor e = prefs.edit(); e.putString(napp.getSettingsPrefix() + KEY_USERNAME, s); @@ -234,6 +250,7 @@ public String getKeyNotificationPriority() { } public void setKeyNotificationPriority(String s) { + Log.d(TAG,"notification priority set to: " + s); Editor e = prefs.edit(); e.putString(KEY_NOTIFICATION_PRIORITY, s); e.commit(); diff --git a/app/src/main/java/com/adam/aslfms/util/MyContextWrapper.java b/app/src/main/java/com/adam/aslfms/util/MyContextWrapper.java new file mode 100644 index 00000000..3e194727 --- /dev/null +++ b/app/src/main/java/com/adam/aslfms/util/MyContextWrapper.java @@ -0,0 +1,72 @@ +/** + * This file is part of Simple Scrobbler. + *

+ * https://github.com/simple-last-fm-scrobbler/sls + *

+ * Copyright 2011 Simple Scrobbler Team + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.adam.aslfms.util; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; + +import java.util.Locale; + +public class MyContextWrapper{ + + private static final String TAG = "MyContextWrapper"; + + public static Context wrap(Context context) { + AppSettings appSettings = new AppSettings(context); + String language = appSettings.getAppLocale(); + String[] language_region = language.split("-r"); + Locale locale = null; + if (language_region.length == 1) { + locale = new Locale(language_region[0]); + } else if (language_region.length == 2){ + locale = new Locale(language_region[0],language_region[1]); + } else { + + } + + Locale.setDefault(locale); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return updateResourcesLocale(context, locale); + } + + return updateResourcesLocaleLegacy(context, locale); + } + + @TargetApi(Build.VERSION_CODES.N) + private static Context updateResourcesLocale(Context context, Locale locale) { + Configuration configuration = context.getResources().getConfiguration(); + configuration.setLocale(locale); + return context.createConfigurationContext(configuration); + } + + @SuppressWarnings("deprecation") + private static Context updateResourcesLocaleLegacy(Context context, Locale locale) { + Resources resources = context.getResources(); + Configuration configuration = resources.getConfiguration(); + configuration.locale = locale; + resources.updateConfiguration(configuration, resources.getDisplayMetrics()); + return context; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/adam/aslfms/util/Util.java b/app/src/main/java/com/adam/aslfms/util/Util.java index bee20211..b13d5cca 100644 --- a/app/src/main/java/com/adam/aslfms/util/Util.java +++ b/app/src/main/java/com/adam/aslfms/util/Util.java @@ -584,10 +584,12 @@ public static void stopMyService(Intent i, Context context) { 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); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + Intent iii = new Intent(context, ControllerReceiverService.class); + stopMyService(iii, context); + } } public static void runServices(Context context) { diff --git a/app/src/main/res/layout/edit_user_credentials.xml b/app/src/main/res/layout/edit_user_credentials.xml index e3a9e925..342f9555 100644 --- a/app/src/main/res/layout/edit_user_credentials.xml +++ b/app/src/main/res/layout/edit_user_credentials.xml @@ -187,7 +187,7 @@ android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" - android:inputType="textUri" + android:inputType="textPassword" android:maxLines="1" /> diff --git a/app/src/main/res/values-en-rUS/strings.xml b/app/src/main/res/values-en-rUS/strings.xml new file mode 100644 index 00000000..cb7198b9 --- /dev/null +++ b/app/src/main/res/values-en-rUS/strings.xml @@ -0,0 +1,260 @@ + + + + Close + Remove + Reset + Clear + Are you sure? + Warning + Will submit listening information to this website + Also disable now-playing + [Hmpf] + Exit + + + by + Issues: %s + Website: %s + Trouble Shoot: %s + by Adam Renberg + Released under Apache License v2.0 + A lightweight scrobbler that submits information to Last.fm (and/or Libre.fm) on what music you play on your android phone. Last.fm then lets you view listening statistics and recommends new music you might like. + Scrobbling/now playing is disabled. + File error! + That which is new + About + What\'s new + Supported websites: %1 + Supported apps: %s and more… + + + Scrobbles in local cache: + Scrobbles in local cache: %1 + Database error + Scrobble now + Reset statistics + Scrobble cache + Are you sure you don\'t want to scrobble this track to %1? This operation cannot be undone. + Are you sure you don\'t want to scrobble this track? This operation cannot be undone. + Are you sure you want to clear all tracks in the cache for %1? This operation cannot be undone. + Are you sure you want to clear all tracks in the cache? This operation cannot be undone. + There are no cached scrobbles + View scrobble cache + View details + Remove from cache + Clear cache + Track info + Scrobble cache for %1 + View and edit the list of unsubmitted scrobbles + all websites + Ordered by: %1 + Sort order + Change sort order + Time played, ascending + Time played, descending + Artist, ascending + Artist, descending + Album, ascending + Album, descending + Track name, ascending + Track name, descending + + + User credentials + Set username and password for your %1 account + Set username and password for your account(s) + Authenticate + Cancel + Clear credentials + Clears username and session key + Supported websites + Username + Password + This version of the app is deprecated, please upgrade + Wrong username/password/token + Server error: %1, retrying… + Client side timing error, retrying… + Network error, retrying… + Not allowed to submit data on network, change in Options screen + Authenticating… + Internal error + Error, retrying… + Logged in as: + Logged in as: %1 + Sign in to + Not logged in + Sign up + Sign up for an account at %1 + Are you sure you want to clear your user credentials for %1? This will also remove any unsubmitted scrobbles. + Are you sure you want to clear your user credentials for all logged in accounts? This will also remove any unsubmitted scrobbles. + + + Song Options + Love + Love Current Track at Last.fm (Libre.fm) + Heart Track Failed + Loved Track + Please enter your last.fm credentials + Song Ready + + + Copy + Copy Current Track to Clip Board + Copy Track Failed + No Current Song Available + + + Theme Selection + Select a Theme + Dark + Light + Default + + + Permission REQUIRED to work properly + Credentials REQUIRED to work properly + Notification access REQUIRED to work properly + Limited Network + + + Warning: %1$s is installed and may cause double scrobbling. + Warning: %1$s is installed and may cause incompatibilities with scrobbling. Disable scrobbling from this app, or uninstall %1$s. + Enable/disable scrobbling from apps + Enabled apps + Won\'t scrobble music from this app + This app is not installed + No music apps detected + 1 music app detected + %1 music apps detected + Play music in a supported app and it will appear here + Unknown app (pre SLS v1.2.3) + Notification Default + New Music App Detected, Click Here + Clear Apps + + + + Active App Notification + WARNING, may skip scrobbles. + Options + Advanced options + Scrobble + Submit listening information when you\'ve listened to a song + Notify now playing + Broadcast what song you currently are listening to + Scrobble point + Will cache scrobble after %1% of it has been played + On battery + Plugged in + Including battery conserving settings + Phone Notifications + Custom scrobbling options + When to scrobble + Also on playlist finish + Also scrobble when you\'ve finished listening to a playlist + Also when plugged in + Also scrobble when you\'re plugged in to a power source + You should also disable now playing notifications to better conserve battery + Standard + Conserve Battery + Same as for battery + Custom + After every track + After five tracks + After ten tracks + After 25 tracks + When you press the scrobble now button + Network + Submit data on: %1 + Any network + 3G and Wifi + Wifi only + Roaming + Won\'t submit data when roaming + Will submit data when roaming + Notification Priority + Use SSL, https://? + + min + low + default + high + max + + Languages (* are incomplete) + + + + Chinese* + Czech* + Czech, Czech* + English + English, US + French + German* + Japan* + Spanish* + Polish* + Portuguese* + Russian* + Ukraine* + + + + Status + Scrobble status + Other + Status + Show statistics and overall status + Status info + Scrobbling is disabled + Last scrobble submitted at: + Last scrobble submission failed at: + Now playing notifications are disabled + Last now playing notification submitted at: + Last scrobble submission failed at: + Total tracks scrobbled: + Never + Total now playing notifications: + It was: + Are you sure you want to reset your submission statistics for %1? This operation cannot be undone. + Profile page + + + Developer Options + Export Database + + + Activate + Required Permissions + External Storage + Notification Listener + Battery Optimizations + Battery Basic + Continue + Find Notifications access list in Android Settings, add Simple Scrobbler + Find Battery Optimization ignore list in Android Settings, add Simple Scrobbler + Enable Notifications Access for Simple Scrobbler in Android Settings + + + Track Title + New Track Title + Artist Title + New Artist Title + Album Title + New Album Title + Save As Rule + Delete All Rules + Add New Rule + Edit Rule + Delete Rule + Field + Initial Value + Changed Value + Track + Album + Artist + ... + + diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index f31c3ffe..d9e58b37 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -1,6 +1,5 @@ - + - Fermer Supprimer @@ -11,8 +10,8 @@ Enverra des informations sur la musique écoutée à ce site Également désactiver l\'affichage du titre en cours de lecture [Hmpf] + Sortie - par Suivi de problèmes : %s @@ -29,7 +28,6 @@ Sites web supportés : %1 Applications supportées : %s et plus… - Scrobbles dans le cache local : Scrobbles dans le cache local : %1 @@ -62,7 +60,6 @@ Piste, ascendent Piste, descendent - Identifiants de l\'utilisateur Nom d\'utilisateur et mot de passe de votre compte %1 @@ -92,8 +89,6 @@ Voulez vous vraiment effacer vos identifiants pour %1 ? Cela effacera aussi tous les scrobbles non envoyés. Voulez vous vraiment effacer vos identifiants pour tous les comptes enregistrés ? Cela effacera aussi tous les scrobbles non envoyés. - - Options de chanson J\'adore @@ -103,20 +98,25 @@ Entrez vos informations d\'identification last.fm Chanson est préparé - Copie Copier la chanson en cours dans le presse-papiers Échec de la copie de la chanson Aucune chanson actuelle disponible - + + Sélection du Thème + Sélectionnez un Thème. + Foncé + Claire + Défaut + Autorisation nécessaire pour fonctionner correctement Vérification des identifiants nécessaire pour fonctionner correctement + Accès de notification REQUIS pour fonctionner correctement Réseau Limitées - Attention : %1$s est installé et peut provoquer un double scrobbling. Attention : %1$s est installé et peut provoquer des incompatibilités avec le scrobbling. Désactivez le scrobbling de cette apllication, ou désinstallez %1$s. @@ -129,9 +129,14 @@ %1 lecteurs de musique détectés Jouez de la musique dans un lecteur de musique supporté et il apparaîtra ici Application inconnue (pre SLS v1.2.3) + Notification par Défaut + Nouvelle Application Musicale Détectée, Cliquez Ici + Effacer les Applications + - + Notification d\'Application Active + ATTENTION, peut ignorer scrobbles. Options Options avancées Scrobbler @@ -168,8 +173,46 @@ Itinérance N\'enverra pas les données en itinérance Enverra les données en itinérance + Priorité de Notification + + min + basse + défaut + élevé + max + + Langues (* sont incomplètes) + + + + + Chinois* + + Tchèque* + + Tchèque, Tchèque* + + Anglais + + Anglais, US + + Français + + Allemand* + + Japon* + + Espanol* + + Polonais* + + Portugais* + + Russe* + + Ukrainien* + - Status Statut du scrobble @@ -190,5 +233,40 @@ Êtes vous sûr de vouloir remettre à zéro les statistiques de %1 ? Cette operation ne peux pas être annulée. Page de profil - - \ No newline at end of file + + Options de Développeur + Exporter la Base de Données + + + Activer + Autorisations Requises + Stockage Externe + Écouteur de Notification. + Optimisation de la Batterie + Batterie de Base + Continuer + Rechercher la liste d\'accès aux notifications dans les paramètres Android, ajouter Simple Scrobbler + Recherchez la liste des ignorés de l\'optimisation de la batterie dans les paramètres Android, ajoutez Simple Scrobbler + Activer les Accès aux Notifications pour Simple Scrobbler dans les paramètres Android + + + Titre de la piste + Nouveau titre de piste + Titre d\'artiste + Nouveau titre d\'artiste + Titre de l\'album + Nouveau titre de l\'album + Enregistrer en Règle + Supprimer Toutes les Règles + Ajouter une nouvelle règle + Modifier la règle + Supprimer la règle + Champ de saisie + Valeur initiale + Valeur modifiée + Piste + Album + Artiste + ... + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e6ddfbdc..52ce8abc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,6 +1,5 @@ - + - Close Remove @@ -13,7 +12,6 @@ [Hmpf] Exit - by Issues: %s @@ -30,7 +28,6 @@ Supported websites: %1 Supported apps: %s and more… - Scrobbles in local cache: Scrobbles in local cache: %1 @@ -63,7 +60,6 @@ Track name, ascending Track name, descending - User credentials Set username and password for your %1 account @@ -93,7 +89,6 @@ Are you sure you want to clear your user credentials for %1? This will also remove any unsubmitted scrobbles. Are you sure you want to clear your user credentials for all logged in accounts? This will also remove any unsubmitted scrobbles. - Song Options Love @@ -103,14 +98,12 @@ Please enter your last.fm credentials Song Ready - Copy Copy Current Track to Clip Board Copy Track Failed No Current Song Available - Theme Selection Select a Theme @@ -118,14 +111,12 @@ Light Default - Permission REQUIRED to work properly Credentials REQUIRED to work properly Notification access REQUIRED to work properly Limited Network - Warning: %1$s is installed and may cause double scrobbling. Warning: %1$s is installed and may cause incompatibilities with scrobbling. Disable scrobbling from this app, or uninstall %1$s. @@ -138,11 +129,11 @@ %1 music apps detected Play music in a supported app and it will appear here Unknown app (pre SLS v1.2.3) - Notification Controller + Notification Default New Music App Detected, Click Here - Clear Apps + Clear Apps + - Active App Notification WARNING, may skip scrobbles. @@ -191,8 +182,25 @@ high max - - + Languages (* are incomplete) + + + + Chinese* + Czech* + Czech, Czech* + English + English, US + French + German* + Japan* + Spanish* + Polish* + Portuguese* + Russian* + Ukraine* + + Status Scrobble status @@ -213,12 +221,10 @@ Are you sure you want to reset your submission statistics for %1? This operation cannot be undone. Profile page - Developer Options Export Database - Activate Required Permissions @@ -227,12 +233,10 @@ Battery Optimizations Battery Basic Continue - Find Notifications ignore list in Android Settings, add Simple Scrobbler + Find Notifications access list in Android Settings, add Simple Scrobbler Find Battery Optimization ignore list in Android Settings, add Simple Scrobbler Enable Notifications Access for Simple Scrobbler in Android Settings - - Track Title New Track Title diff --git a/app/src/main/res/values/strings_nontranslatable.xml b/app/src/main/res/values/strings_nontranslatable.xml index 7828e802..0910e9de 100644 --- a/app/src/main/res/values/strings_nontranslatable.xml +++ b/app/src/main/res/values/strings_nontranslatable.xml @@ -24,6 +24,22 @@ DAAP Media Player, xenoAmp, Marine, MediaMonkey, Pulsar, + + zh-rCN + cs + cs-rCZ + en + en-rUS + fr + de + ja + es + pl + pt-rBR + ru + uk + + last.fm libre.fm diff --git a/app/src/main/res/xml/options_prefs.xml b/app/src/main/res/xml/options_prefs.xml index 16cf8360..57b41a3d 100644 --- a/app/src/main/res/xml/options_prefs.xml +++ b/app/src/main/res/xml/options_prefs.xml @@ -9,6 +9,14 @@ android:negativeButtonText="@null" android:positiveButtonText="@null" /> + + Date: Sun, 8 Sep 2019 19:48:19 -0400 Subject: [PATCH 2/4] Permissions, Initializing, Language Choice - 1.6.2 (2018-9-8) codename: engus * Fixed music not being recognized immediately * Permission to bypass new features * Language selection --- README.md | 1 + app/src/main/assets/changelog.txt | 2 +- .../java/com/adam/aslfms/OptionsActivity.java | 13 ++++- .../com/adam/aslfms/PermissionsActivity.java | 48 +++++++++++++++---- .../com/adam/aslfms/SettingsActivity.java | 6 ++- .../receiver/BuiltInMusicAppReceiver.java | 7 +++ .../service/ControllerReceiverService.java | 13 +++++ .../service/ControllerReceiverSession.java | 48 +++++++++++++++++-- .../com/adam/aslfms/service/Handshaker.java | 4 ++ .../com/adam/aslfms/util/AppSettings.java | 17 ++++--- .../main/java/com/adam/aslfms/util/Util.java | 9 +++- .../main/res/layout/activity_permissions.xml | 4 +- app/src/main/res/values-en-rUS/strings.xml | 6 ++- app/src/main/res/values-fr/strings.xml | 5 +- app/src/main/res/values/strings.xml | 6 ++- app/src/main/res/xml/options_prefs.xml | 4 ++ 16 files changed, 164 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 6e979374..1d942d9d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/src/main/assets/changelog.txt b/app/src/main/assets/changelog.txt index 4f7cc296..5c3474b4 100644 --- a/app/src/main/assets/changelog.txt +++ b/app/src/main/assets/changelog.txt @@ -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 diff --git a/app/src/main/java/com/adam/aslfms/OptionsActivity.java b/app/src/main/java/com/adam/aslfms/OptionsActivity.java index 476a355a..406c33cb 100644 --- a/app/src/main/java/com/adam/aslfms/OptionsActivity.java +++ b/app/src/main/java/com/adam/aslfms/OptionsActivity.java @@ -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; @@ -56,6 +56,7 @@ 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; @@ -63,6 +64,8 @@ public class OptionsActivity extends AppCompatPreferenceActivity { private PowerSpecificPrefs mBatteryOptions; private PowerSpecificPrefs mPluggedOptions; + Context ctx = this; + @Override protected void attachBaseContext(Context newBase) { @@ -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; @@ -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); @@ -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) { diff --git a/app/src/main/java/com/adam/aslfms/PermissionsActivity.java b/app/src/main/java/com/adam/aslfms/PermissionsActivity.java index e8e3fffb..9f071223 100644 --- a/app/src/main/java/com/adam/aslfms/PermissionsActivity.java +++ b/app/src/main/java/com/adam/aslfms/PermissionsActivity.java @@ -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; @@ -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; @@ -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) { @@ -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())); @@ -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) { @@ -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); @@ -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){ @@ -183,6 +215,7 @@ 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); @@ -190,11 +223,10 @@ private void permsCheck() { 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); } } diff --git a/app/src/main/java/com/adam/aslfms/SettingsActivity.java b/app/src/main/java/com/adam/aslfms/SettingsActivity.java index 29f5c63a..f9d3ca9c 100644 --- a/app/src/main/java/com/adam/aslfms/SettingsActivity.java +++ b/app/src/main/java/com/adam/aslfms/SettingsActivity.java @@ -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; @@ -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); } @@ -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); diff --git a/app/src/main/java/com/adam/aslfms/receiver/BuiltInMusicAppReceiver.java b/app/src/main/java/com/adam/aslfms/receiver/BuiltInMusicAppReceiver.java index b14de0e1..3082a4d8 100644 --- a/app/src/main/java/com/adam/aslfms/receiver/BuiltInMusicAppReceiver.java +++ b/app/src/main/java/com/adam/aslfms/receiver/BuiltInMusicAppReceiver.java @@ -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; @@ -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); diff --git a/app/src/main/java/com/adam/aslfms/service/ControllerReceiverService.java b/app/src/main/java/com/adam/aslfms/service/ControllerReceiverService.java index 04310b4d..8bc9b460 100644 --- a/app/src/main/java/com/adam/aslfms/service/ControllerReceiverService.java +++ b/app/src/main/java/com/adam/aslfms/service/ControllerReceiverService.java @@ -39,6 +39,8 @@ import com.adam.aslfms.util.Util; import java.util.HashSet; +import java.util.ResourceBundle; + /** * @author a93h * @since 1.5.8 @@ -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); @@ -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(); @@ -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); } @@ -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"); diff --git a/app/src/main/java/com/adam/aslfms/service/ControllerReceiverSession.java b/app/src/main/java/com/adam/aslfms/service/ControllerReceiverSession.java index bbe440c0..2e4f3a8e 100644 --- a/app/src/main/java/com/adam/aslfms/service/ControllerReceiverSession.java +++ b/app/src/main/java/com/adam/aslfms/service/ControllerReceiverSession.java @@ -45,9 +45,9 @@ public class ControllerReceiverSession implements MediaSessionManager.OnActiveSessionsChangedListener { private final static String TAG = "ContrlRcvrSession"; - private HashMap> controllersMap = new HashMap<>(); - private List mControllers = null; - private int sessionCount = 0; + private static HashMap> controllersMap = new HashMap<>(); + private static List mControllers = null; + private static int sessionCount = 0; private Context mContext; public ControllerReceiverSession(Context context) { @@ -55,6 +55,7 @@ public ControllerReceiverSession(Context context) { this.mContext = context; } + @Override public void onActiveSessionsChanged(List controllers) { Log.d(TAG, "active session change detected"); mControllers = controllers; @@ -79,6 +80,7 @@ public void onActiveSessionsChanged(List controllers) { removeSessions(tokens,pacakgeNames); } + public void removeSessions(HashSet tokens, HashSet packageNames) { Iterator it = controllersMap.entrySet().iterator(); while (it.hasNext()) { @@ -98,4 +100,44 @@ public void removeSessions(HashSet tokens, HashSet p sessionCount = controllersMap.size(); Log.d(TAG,"number of active sessions: " + sessionCount); } + + public static void initialListener(Context context, List controllers){ + Log.d(TAG, "initial active session change detected"); + mControllers = controllers; + HashSet tokens = new HashSet<>(); + HashSet 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 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); + } } diff --git a/app/src/main/java/com/adam/aslfms/service/Handshaker.java b/app/src/main/java/com/adam/aslfms/service/Handshaker.java index 8e16f57f..970858d7 100644 --- a/app/src/main/java/com/adam/aslfms/service/Handshaker.java +++ b/app/src/main/java/com/adam/aslfms/service/Handshaker.java @@ -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 diff --git a/app/src/main/java/com/adam/aslfms/util/AppSettings.java b/app/src/main/java/com/adam/aslfms/util/AppSettings.java index c80e6d5d..3815dd57 100644 --- a/app/src/main/java/com/adam/aslfms/util/AppSettings.java +++ b/app/src/main/java/com/adam/aslfms/util/AppSettings.java @@ -73,8 +73,6 @@ public class AppSettings { private static final String KEY_SCROBBLING_ENABLE = "enable_scrobbling"; private static final String KEY_NOWPLAYING_ENABLE = "enable_nowplaying"; - private static final String KEY_ACTIVE_MUSIC_NOTIFICATION_APP = "active_music_notification_app"; - private static final String KEY_NOTIFICATION_PRIORITY = "key_notification_priority"; private static final String KEY_AUTH_STATUS = "authstatus"; @@ -90,11 +88,11 @@ public class AppSettings { private static final String KEY_ADVANCED_OPTIONS_NETWORK = "advanced_options_network"; private static final String KEY_ADVANCED_OPTIONS_ROAMING = "advanced_options_roaming"; - // Widget stuff private static final String KEY_WIDGET_ALSO_DISABLE_NP = "widget_also_disable_np"; private static final String KEY_THEME = "my_theme"; private static final String KEY_LOCALE = "my_locale"; + private static final String KEY_BYPASS_NEW_PERMISSIONS = "key_bypass_permissions"; private final Context mCtx; private final SharedPreferences prefs; @@ -231,13 +229,18 @@ public void setListenBrainzApiUrl(NetApp napp, String s) { e.commit(); } - public String getKeyActiveMusicNotificationApp() { - return prefs.getString(KEY_ACTIVE_MUSIC_NOTIFICATION_APP, null); + public int getKeyBypassNewPermissions() { + return prefs.getInt(KEY_BYPASS_NEW_PERMISSIONS, 2); } - public void setKeyActiveMusicNotificationApp(String s) { + /** + * + * + * @param i 0,1,2 false, true, unknown + */ + public void setKeyBypassNewPermissions(int i) { Editor e = prefs.edit(); - e.putString(KEY_ACTIVE_MUSIC_NOTIFICATION_APP, s); + e.putInt(KEY_BYPASS_NEW_PERMISSIONS, i); e.commit(); } diff --git a/app/src/main/java/com/adam/aslfms/util/Util.java b/app/src/main/java/com/adam/aslfms/util/Util.java index b13d5cca..1e9f4c7f 100644 --- a/app/src/main/java/com/adam/aslfms/util/Util.java +++ b/app/src/main/java/com/adam/aslfms/util/Util.java @@ -49,6 +49,7 @@ import android.util.Log; import android.widget.Toast; +import com.adam.aslfms.PermissionsActivity; import com.adam.aslfms.R; import com.adam.aslfms.SettingsActivity; import com.adam.aslfms.service.ControllerReceiverService; @@ -513,6 +514,10 @@ public static void myNotify(Context context, String title, String content, int n } private static void exportDB(String dbName, Context ctx) { + if(!Util.checkExternalPermission(ctx)){ + Util.myNotify(ctx, ctx.getResources().getString(R.string.warning), ctx.getResources().getString(R.string.permission_external_storage), 81234, PermissionsActivity.class); + return; + } File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); File data = Environment.getDataDirectory(); FileChannel source = null; @@ -586,7 +591,7 @@ public static void stopAllServices(Context context) { Intent ii = new Intent(context, ScrobblingService.class); stopMyService(i, context); stopMyService(ii, context); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Util.checkNotificationListenerPermission(context)) { Intent iii = new Intent(context, ControllerReceiverService.class); stopMyService(iii, context); } @@ -602,7 +607,7 @@ public static void runServices(Context context) { i.putExtra("album", ""); i.putExtra("app_name", ""); ii.setAction(ScrobblingService.ACTION_START_SCROBBLER_SERVICE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Util.checkNotificationListenerPermission(context)) { Intent iii = new Intent(context, ControllerReceiverService.class); Log.d(TAG, "(re)starting controllerreceiver"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { diff --git a/app/src/main/res/layout/activity_permissions.xml b/app/src/main/res/layout/activity_permissions.xml index 626fe642..95a07834 100644 --- a/app/src/main/res/layout/activity_permissions.xml +++ b/app/src/main/res/layout/activity_permissions.xml @@ -162,10 +162,10 @@ android:gravity="center_horizontal|center_vertical" >