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

Commit

Permalink
Merge pull request #538 from a93h/master
Browse files Browse the repository at this point in the history
Database Improvements
  • Loading branch information
Austin H authored Sep 21, 2019
2 parents 0ecbc15 + 89d1c9d commit 1e6bdae
Show file tree
Hide file tree
Showing 20 changed files with 499 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ public void testGettersFromDB() {
public void testEnabling() {
MusicAPI a = getMusicAPIA();
assertTrue(a.isEnabled());
a.setEnabled(ctx, false);
a.setEnabled(ctx, 0);
assertFalse(a.isEnabled());
MusicAPI b = MusicAPI.fromDatabase(ctx, a.getId());
assertFalse(b.isEnabled());
b.setEnabled(ctx, true);
b.setEnabled(ctx, 1);
assertTrue(b.isEnabled());
a = MusicAPI.fromDatabase(ctx, b.getId());
assertTrue(a.isEnabled());
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/assets/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ https://github.com/simple-last-fm-scrobbler/sls/wiki/Privacy-Concerns
For privacy concerns.

- 1.6.8 (2019-9-15) codename: kingus
* Correction Rules database problem fixed
* Database problems fixed
* Database migration
* Stop automatic app adding, so user must manually add music apps Android 5.0+
* Offline Heart Track
* A grey record of sent tracks is kept now
* Back button in permissions fixed
* Privacy information in permissions
* Russian thanks to alexesprit
Expand Down
33 changes: 31 additions & 2 deletions app/src/main/java/com/adam/aslfms/MusicAppsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,22 @@ public boolean onPreferenceTreeClick(PreferenceScreen prefScreen,
if (mapi != null) {
CheckBoxPreference cbp = (CheckBoxPreference) pref;
boolean checked = cbp.isChecked();
mapi.setEnabled(this, checked);
mapi.setEnabled(this, checked ? 1 : 0);
setSMASummary(pref, mapi);

switch (mapi.getEnabledValue()){
case 0:
pref.setIcon(android.R.drawable.checkbox_off_background);
break;
case 1:
pref.setIcon(android.R.drawable.checkbox_on_background);
break;
case 2:
default:
pref.setIcon(android.R.drawable.stat_sys_warning);
break;
}

if (checked && mScrobbleDroidInstalled
&& mapi.clashesWithScrobbleDroid()) {
Util.warningDialog(this, getString(
Expand Down Expand Up @@ -157,11 +170,26 @@ private void update() {

MusicAPI[] mapis = MusicAPI.all(this);
for (MusicAPI mapi : mapis) {

CheckBoxPreference appPref = new CheckBoxPreference(this, null);
appPref.setTitle(mapi.getName());
appPref.setPersistent(false);
appPref.setChecked(mapi.isEnabled());


switch (mapi.getEnabledValue()){
case 0:
appPref.setIcon(android.R.drawable.checkbox_off_background);
break;
case 1:
appPref.setIcon(android.R.drawable.checkbox_on_background);
break;
case 2:
default:
appPref.setIcon(android.R.drawable.stat_sys_warning);
break;
}

mSupportedMusicAppsList.addPreference(appPref);
mPrefsToMapisMap.put(appPref, mapi);
mMapisToPrefsMap.put(mapi, appPref);
Expand Down Expand Up @@ -197,7 +225,8 @@ private void setSMASummary(Preference pref, MusicAPI mapi) {
pref.setSummary(getString(R.string.incompatability_short)
.replaceAll("%1", mScrobbleDroidLabel));
} else {
pref.setSummary(mapi.getMessage());
if (!mapi.getMessage().equals("generic receiver")) pref.setSummary(mapi.getMessage());
else pref.setSummary("");
}
}
}
8 changes: 3 additions & 5 deletions app/src/main/java/com/adam/aslfms/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public boolean onPreferenceTreeClick(PreferenceScreen prefScreen,
Preference pref) {
if (pref == mScrobbleAllNow) {
checkNetwork();
int numInCache = mDb.queryNumberOfTracks();
int numInCache = mDb.queryNumberOfUnscrobbledTracks();
Util.scrobbleAllIfPossible(this, numInCache);
return true;
} else if (pref == mViewScrobbleCache) {
Expand All @@ -168,7 +168,8 @@ public boolean onPreferenceTreeClick(PreferenceScreen prefScreen,
* whether stuff is enabled or checked, etc.
*/
private void update() {
int numCache = mDb.queryNumberOfTracks();
mDb = new ScrobblesDatabase(this);
int numCache = mDb.queryNumberOfUnscrobbledTracks();
mScrobbleAllNow.setSummary(getString(R.string.scrobbles_cache).replace(
"%1", Integer.toString(numCache)));
mScrobbleAllNow.setEnabled(numCache > 0);
Expand Down Expand Up @@ -206,12 +207,9 @@ public void onReceive(Context context, Intent intent) {

private void runChecks(){
settings = new AppSettings(this);
// TODO: VERIFY EVERYTHING BELOW IS SAFE
int v = Util.getAppVersionCode(this, getPackageName());
if (settings.getWhatsNewViewedVersion() < v){
settings.setKeyBypassNewPermissions(2);
mDb.rebuildScrobblesDatabaseOnce(); // keep as not all users have the newest database.
// TODO: verify only needed databases are destroyed
}
if (settings.getKeyBypassNewPermissions() == 2){
startActivity(new Intent(this, PermissionsActivity.class));
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/adam/aslfms/StatusActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case MENU_SCROBBLE_NOW_ID:
int numInCache = mDb.queryNumberOfTracks();
int numInCache = mDb.queryNumberOfUnscrobbledTracks();
Util.scrobbleAllIfPossible(this, numInCache);
return true;
case R.id.MENU_RESET_STATS_ID:
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/adam/aslfms/ViewScrobbleCacheActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.content.IntentFilter;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
Expand Down Expand Up @@ -57,6 +58,8 @@

public class ViewScrobbleCacheActivity extends AppCompatActivity {
private static final String TAG = "VSCacheActivity";
private static final int disabledColor = Color.argb(25, 0,0,0);


private AppSettings settings;

Expand All @@ -65,6 +68,7 @@ public class ViewScrobbleCacheActivity extends AppCompatActivity {
* mNetApp == null means that we should view cache for all netapps
*/
private NetApp mNetApp;
private NetApp[] mNetApps;

private Cursor mScrobblesCursor = null;

Expand Down Expand Up @@ -178,6 +182,7 @@ private void refillData() {

private void fillData() {
SortField sf = settings.getCacheSortField();

if (mNetApp == null) {
mScrobblesCursor = mDb.fetchAllTracksCursor(sf);
} else {
Expand Down Expand Up @@ -218,6 +223,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
mScrobblesCursor);
}
return true;
case R.id.menu_clear_completed_cache:
if (mNetApp == null) {
Util.deleteAllScrobbledTracksFromAllCaches(this, mDb,
mScrobblesCursor);
} else {
Util.deleteAllScrobbledTracksFromCache(this, mDb, mNetApp,
mScrobblesCursor);
}
return true;
case R.id.menu_change_sort_order:
viewChangeSortOrder();
return true;
Expand Down Expand Up @@ -318,6 +332,15 @@ public MyAdapter(Context context, Cursor c) {

@Override
public void bindView(View view, Context context, Cursor cursor) {

if (mNetApp == null) {
mNetApps = mDb.fetchNetAppsForScrobble(cursor.getInt(cursor.getColumnIndex("_id")));
} else {
mNetApps = null;
}

if (mNetApps.length == 0) view.setBackgroundColor(disabledColor);

String track = cursor.getString(cursor.getColumnIndex("track"));
TextView trackView = (TextView) view.findViewById(R.id.track);
trackView.setText(track);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.os.Bundle;
import android.util.Log;

import com.adam.aslfms.MusicAppsActivity;
import com.adam.aslfms.R;
import com.adam.aslfms.UserCredActivity;
import com.adam.aslfms.service.ScrobblingService;
Expand Down Expand Up @@ -91,16 +92,6 @@ public final void onReceive(Context context, Intent intent) {
bundle = Bundle.EMPTY;
}

// we must be logged in to scrobble
AppSettings settings = new AppSettings(context);
if (!settings.isAnyAuthenticated()) {
Util.myNotify(context, context.getResources().getString(R.string.warning) , context.getResources().getString(R.string.not_logged_in),05233, UserCredActivity.class);
Log
.d(TAG,
"The user has not authenticated, won't propagate the submission request");
return;
}

mService = new Intent(context, ScrobblingService.class);
mService.setAction(ScrobblingService.ACTION_PLAYSTATECHANGED);

Expand All @@ -116,15 +107,19 @@ public final void onReceive(Context context, Intent intent) {
}

// check if the user wants to scrobble music from this MusicAPI
if (!mMusicAPI.isEnabled()) {
if (mMusicAPI.getEnabledValue() == 0) {
Log.d(TAG, "App: " + mMusicAPI.getName()
+ " has been disabled, won't propagate");
return;
} else if (mMusicAPI.getEnabledValue() == 2) {
Util.myNotify(context, mMusicAPI.getName(), context.getString(R.string.new_music_app), 12473, MusicAppsActivity.class);
Log.d(TAG, "App: " + mMusicAPI.getName()
+ " has been ignored, will propagate");
}

// submit track for the ScrobblingService
InternalTrackTransmitter.appendTrack(mTrack);

AppSettings settings = new AppSettings(context);
// start/call the Scrobbling Service
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && settings.isActiveAppEnabled(Util.checkPower(context))) {
context.startForegroundService(mService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ private void parseData(Context ctx, String action, Bundle bundle){
if (playerPackage != null && !playerPackage.isEmpty()) {
PackageManager packageManager = ctx.getPackageManager();
playerName = packageManager.getApplicationLabel(packageManager.getApplicationInfo(playerPackage, PackageManager.GET_META_DATA)).toString();
mMusicApi = MusicAPI.fromReceiver(ctx, playerName, playerPackage, null, false);
mMusicApi = MusicAPI.fromReceiver(ctx, playerName, playerPackage, "generic receiver", false);
setMusicAPI(mMusicApi);
}
}
} catch (Exception e) {
Log.w(TAG, e.toString());
}
if (mMusicApi == null) {
mMusicApi = MusicAPI.fromReceiver(ctx, ctx.getResources().getString(R.string.notification_controller), ctx.getPackageName(), null, false);
mMusicApi = MusicAPI.fromReceiver(ctx, ctx.getResources().getString(R.string.notification_controller), ctx.getPackageName(), "generic receiver", false);
setMusicAPI(mMusicApi);
}
if (bundle.containsKey("track")) {
Expand Down Expand Up @@ -142,7 +142,6 @@ private void parseData(Context ctx, String action, Bundle bundle){
break;
case 5:
default:
setState(Track.State.UNKNOWN_NONPLAYING);
break;
}
}
Expand Down
30 changes: 21 additions & 9 deletions app/src/main/java/com/adam/aslfms/receiver/MusicAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class MusicAPI {
int enabled;

MusicAPI(long id, String name, String pkg, String msg,
boolean clashWithScrobbleDroid, boolean enabled) {
boolean clashWithScrobbleDroid, int enabled) {
super();

if (name == null)
Expand All @@ -71,7 +71,7 @@ public class MusicAPI {
this.pkg = pkg;
this.msg = msg;
this.clashWithScrobbleDroid = clashWithScrobbleDroid ? 1 : 0;
this.enabled = enabled ? 1 : 0;
this.enabled = enabled;
}

/**
Expand Down Expand Up @@ -138,15 +138,26 @@ public boolean isEnabled() {
return enabled == 1;
}

/**
* Returns int if the user has not enabled or disabled scrobbling through this API / music
* app. Default is true.
*
* @return int of scrobbling from this API / music app
* @see MusicAppsActivity
*/
public int getEnabledValue() {
return enabled;
}

/**
* Enables / disables scrobbling from this API / music app.
*
* @param ctx context to enable database calls.
* @param enabled whether this API / app should be enabled or disabled
* @see MusicAppsActivity
*/
public void setEnabled(Context ctx, boolean enabled) {
int en = enabled ? 1 : 0;
public void setEnabled(Context ctx, int enabled) {
int en = enabled;
if (en == this.enabled)
return;

Expand Down Expand Up @@ -285,7 +296,8 @@ public static MusicAPI fromReceiver(Context ctx, String name, String pkg,
vals.put("pkg", pkg);
vals.put("msg", msg);
vals.put("sdclash", clashWithScrobbleDroid ? 1 : 0);
vals.put("enabled", 1);
if (msg.equals("generic receiver")) vals.put("enabled", 2);
else vals.put("enabled", 1);

long id = db.insert("music_api", null, vals);

Expand All @@ -296,8 +308,8 @@ public static MusicAPI fromReceiver(Context ctx, String name, String pkg,
Log.d(TAG, "new mapiinserted into db");
}

mapi = new MusicAPI(id, name, pkg, msg, clashWithScrobbleDroid,
true);
if (msg.equals("generic receiver")) mapi = new MusicAPI(id, name, pkg, msg, clashWithScrobbleDroid, 2);
else mapi = new MusicAPI(id, name, pkg, msg, clashWithScrobbleDroid, 1);
Log.d(TAG, mapi.toString());
}
DatabaseHelper.closeDatabase();
Expand Down Expand Up @@ -329,7 +341,7 @@ public static MusicAPI fromDatabase(Context ctx, long id) {
// track
// hasn't been played after the upgrade to v1.2.3
mapi = new MusicAPI(-1, ctx.getString(R.string.unknown_mapi),
NOT_AN_APPLICATION_PACKAGE + "pre_1_2_3", null, false, true);
NOT_AN_APPLICATION_PACKAGE + "pre_1_2_3", null, false, 1);
}
c.close();
DatabaseHelper.closeDatabase();
Expand Down Expand Up @@ -366,7 +378,7 @@ private static MusicAPI readMusicAPI(Cursor c) {
c.getString(c.getColumnIndex("pkg")), //
c.getString(c.getColumnIndex("msg")), //
c.getInt(c.getColumnIndex("sdclash")) == 1, //
c.getInt(c.getColumnIndex("enabled")) == 1);
c.getInt(c.getColumnIndex("enabled")));
}

static final String DATABASE_NAME = "music_apis";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,15 @@ public void onPlaybackStateChanged(PlaybackState state) {
case PlaybackState.STATE_ERROR:
case PlaybackState.STATE_PAUSED:
case PlaybackState.STATE_STOPPED:
case PlaybackState.STATE_NONE:
playing = 3; // pause
break;
case PlaybackState.STATE_SKIPPING_TO_NEXT:
case PlaybackState.STATE_SKIPPING_TO_PREVIOUS:
case PlaybackState.STATE_SKIPPING_TO_QUEUE_ITEM:
playing = 4; // complete
break;
case PlaybackState.STATE_NONE:
default:
playing = 5; // unknown
break;
}
localIntent.putExtra("playing", playing);
Expand Down
Loading

0 comments on commit 1e6bdae

Please sign in to comment.