Skip to content

Commit

Permalink
scroblling improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
naman14 committed Nov 14, 2016
1 parent 2504ee0 commit 7f30745
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 127 deletions.
18 changes: 9 additions & 9 deletions app/src/main/java/com/naman14/timber/MusicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@

import com.naman14.timber.helpers.MediaButtonIntentReceiver;
import com.naman14.timber.helpers.MusicPlaybackTrack;
import com.naman14.timber.lastfmapi.LastFmClient;
import com.naman14.timber.lastfmapi.models.ScrobbleQuery;
import com.naman14.timber.lastfmapi.LastFmClient;
import com.naman14.timber.lastfmapi.models.ScrobbleQuery;
import com.naman14.timber.permissions.Nammu;
import com.naman14.timber.provider.MusicPlaybackState;
import com.naman14.timber.provider.RecentStore;
Expand Down Expand Up @@ -452,11 +452,11 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
return START_NOT_STICKY; //no sense to use START_STICKY with using startForeground
}

void scrobble() {
Log.d("Scrobble","to LastFM");
LastFmClient.getInstance(this).Scrobble(new ScrobbleQuery(getArtistName(),getTrackName(),(System.currentTimeMillis()-duration())/1000));
}

void scrobble() {
Log.d("Scrobble","to LastFM");
LastFmClient.getInstance(this).Scrobble(new ScrobbleQuery(getArtistName(),getTrackName(),(System.currentTimeMillis()-duration())/1000));
}

private void releaseServiceUiAndStop() {
if (isPlaying()
|| mPausedByTransientLossOfFocus
Expand Down Expand Up @@ -2157,7 +2157,7 @@ public void handleMessage(final Message msg) {
}
break;
case TRACK_WENT_TO_NEXT:
mService.get().scrobble();
mService.get().scrobble();
service.setAndRecordPlayPos(service.mNextPlayPos);
service.setNextTrack();
if (service.mCursor != null) {
Expand All @@ -2169,7 +2169,7 @@ public void handleMessage(final Message msg) {
service.updateNotification();
break;
case TRACK_ENDED:
mService.get().scrobble();
mService.get().scrobble();
if (service.mRepeatMode == REPEAT_CURRENT) {
service.seek(0);
service.play();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public int getLightToolbarMode() {
return Config.LIGHT_TOOLBAR_AUTO;
}

//@Override
@Override
public int getToolbarColor() {
return Color.TRANSPARENT;
}
Expand Down
56 changes: 31 additions & 25 deletions app/src/main/java/com/naman14/timber/dialogs/LastFmLoginDialog.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package com.naman14.timber.dialogs;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Build;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.preference.Preference;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.naman14.timber.MusicPlayer;
import com.naman14.timber.R;
import com.naman14.timber.fragments.PlaylistFragment;
import com.naman14.timber.fragments.SettingsFragment;
import com.naman14.timber.lastfmapi.LastFmClient;
import com.naman14.timber.lastfmapi.callbacks.UserListener;
Expand All @@ -26,29 +21,40 @@
*/
public class LastFmLoginDialog extends DialogFragment {
public static final String FRAGMENT_NAME = "LastFMLogin";

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new MaterialDialog.Builder(getActivity()).positiveText("Login").negativeText("Cancel").customView(R.layout.dialog_lastfm_login,false).onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
String username = ((EditText)dialog.findViewById(R.id.lastfm_username)).getText().toString();
String password = ((EditText)dialog.findViewById(R.id.lastfm_password)).getText().toString();
if(username.length()==0||password.length()==0)return;
LastFmClient.getInstance(getActivity()).getUserLoginInfo(new UserLoginQuery(username, password), new UserListener() {

return new MaterialDialog.Builder(getActivity()).
positiveText("Login").
negativeText("Cancel").
title("Login to LastFM").
customView(R.layout.dialog_lastfm_login, false).
onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void userSuccess() {
if (getTargetFragment() instanceof SettingsFragment) {
((SettingsFragment) getTargetFragment()).updateLastFM();
}
}
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
String username = ((EditText) dialog.findViewById(R.id.lastfm_username)).getText().toString();
String password = ((EditText) dialog.findViewById(R.id.lastfm_password)).getText().toString();
if (username.length() == 0 || password.length() == 0) return;
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Logging in..");
progressDialog.show();
LastFmClient.getInstance(getActivity()).getUserLoginInfo(new UserLoginQuery(username, password), new UserListener() {

@Override
public void userInfoFailed() {
Toast.makeText(getTargetFragment().getActivity(),"Failed to Login",Toast.LENGTH_SHORT).show();
@Override
public void userSuccess() {
progressDialog.dismiss();
if (getTargetFragment() instanceof SettingsFragment) {
((SettingsFragment) getTargetFragment()).updateLastFM();
}
}

@Override
public void userInfoFailed() {
progressDialog.dismiss();
Toast.makeText(getTargetFragment().getActivity(), "Failed to Login", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}).build();
}).build();
}
}
159 changes: 79 additions & 80 deletions app/src/main/java/com/naman14/timber/lastfmapi/LastFmClient.java
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
/*
* Copyright (C) 2015 Naman Dwivedi
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/

package com.naman14.timber.lastfmapi;

import android.content.Context;
import android.util.Log;

import com.naman14.timber.lastfmapi.callbacks.ArtistInfoListener;
/*
* Copyright (C) 2015 Naman Dwivedi
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/

package com.naman14.timber.lastfmapi;

import android.content.Context;
import android.util.Log;

import com.naman14.timber.lastfmapi.callbacks.ArtistInfoListener;
import com.naman14.timber.lastfmapi.callbacks.UserListener;
import com.naman14.timber.lastfmapi.models.AlbumInfo;
import com.naman14.timber.lastfmapi.models.AlbumQuery;
import com.naman14.timber.lastfmapi.models.ArtistInfo;
import com.naman14.timber.lastfmapi.models.ArtistQuery;
import com.naman14.timber.lastfmapi.models.AlbumInfo;
import com.naman14.timber.lastfmapi.models.AlbumQuery;
import com.naman14.timber.lastfmapi.models.ArtistInfo;
import com.naman14.timber.lastfmapi.models.ArtistQuery;
import com.naman14.timber.lastfmapi.models.LastfmUserSession;
import com.naman14.timber.lastfmapi.models.ScrobbleInfo;
import com.naman14.timber.lastfmapi.models.ScrobbleQuery;
import com.naman14.timber.lastfmapi.models.UserLoginInfo;
import com.naman14.timber.lastfmapi.models.UserLoginQuery;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class LastFmClient {


import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class LastFmClient {

//TODO update the api keys
public static final String API_KEY = "62ac1851456e4558bef1c41747b1aec2";
public static final String API_SECRET = "b4ae8965723d67fb18e35d207014d6f3";

public static final String JSON = "json";

public static final String BASE_API_URL = "http://ws.audioscrobbler.com/2.0";
public static final String BASE_API_URL = "http://ws.audioscrobbler.com/2.0";
public static final String BASE_SECURE_API_URL = "https://ws.audioscrobbler.com/2.0";

private static LastFmClient sInstance;
private LastFmRestService mRestService;

private static LastFmClient sInstance;
private LastFmRestService mRestService;
private LastFmUserRestService mUserRestService;


private Context context;

private LastfmUserSession mUserSession;
private static final Object sLock = new Object();

public static LastFmClient getInstance(Context context) {
synchronized (sLock) {
if (sInstance == null) {
sInstance = new LastFmClient();
private static final Object sLock = new Object();

public static LastFmClient getInstance(Context context) {
synchronized (sLock) {
if (sInstance == null) {
sInstance = new LastFmClient();
sInstance.context = context;
sInstance.mRestService = RestServiceFactory.createStatic(context, BASE_API_URL, LastFmRestService.class);
sInstance.mUserRestService = RestServiceFactory.create(context, BASE_SECURE_API_URL, LastFmUserRestService.class);
sInstance.mUserSession = LastfmUserSession.getSession(context);

}
return sInstance;
}
}

static String generateMD5(String in) {
}
return sInstance;
}
}

private static String generateMD5(String in) {
byte[] bytesOfMessage = new byte[0];
try {
bytesOfMessage = in.getBytes("UTF-8");
Expand All @@ -89,36 +89,35 @@ static String generateMD5(String in) {

}

public void getAlbumInfo(AlbumQuery albumQuery) {
mRestService.getAlbumInfo(albumQuery.mArtist, albumQuery.mALbum, new Callback<AlbumInfo>() {
@Override
public void success(AlbumInfo albumInfo, Response response) {

}

@Override
public void failure(RetrofitError error) {

error.printStackTrace();
}
});
}

public void getArtistInfo(ArtistQuery artistQuery, final ArtistInfoListener listener) {
mRestService.getArtistInfo(artistQuery.mArtist, new Callback<ArtistInfo>() {
@Override
public void success(ArtistInfo artistInfo, Response response) {
listener.artistInfoSucess(artistInfo.mArtist);
}

@Override
public void failure(RetrofitError error) {
listener.artistInfoFailed();
Log.d("lol", "failed");
error.printStackTrace();
}
});
}
public void getAlbumInfo(AlbumQuery albumQuery) {
mRestService.getAlbumInfo(albumQuery.mArtist, albumQuery.mALbum, new Callback<AlbumInfo>() {
@Override
public void success(AlbumInfo albumInfo, Response response) {

}

@Override
public void failure(RetrofitError error) {

error.printStackTrace();
}
});
}

public void getArtistInfo(ArtistQuery artistQuery, final ArtistInfoListener listener) {
mRestService.getArtistInfo(artistQuery.mArtist, new Callback<ArtistInfo>() {
@Override
public void success(ArtistInfo artistInfo, Response response) {
listener.artistInfoSucess(artistInfo.mArtist);
}

@Override
public void failure(RetrofitError error) {
listener.artistInfoFailed();
error.printStackTrace();
}
});
}

public void getUserLoginInfo(UserLoginQuery userLoginQuery, final UserListener listener) {
mUserRestService.getUserLoginInfo(UserLoginQuery.Method, JSON, API_KEY, generateMD5(userLoginQuery.getSignature()), userLoginQuery.mUsername, userLoginQuery.mPassword, new Callback<UserLoginInfo>() {
Expand Down Expand Up @@ -160,4 +159,4 @@ public String getUsername(){
if(mUserSession!=null)return mUserSession.mUsername;
return null;
}
}
}
5 changes: 3 additions & 2 deletions app/src/main/res/layout/dialog_lastfm_login.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:padding="15dp">

<com.afollestad.appthemeengine.views.ATEEditText
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lastfm_username"
android:hint="Username"
android:inputType="textVisiblePassword|textNoSuggestions"/>
android:inputType="textNoSuggestions"/>

<com.afollestad.appthemeengine.views.ATEEditText
android:layout_width="match_parent"
Expand Down
22 changes: 12 additions & 10 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@

</com.naman14.timber.widgets.ThemedPreferenceCategory>

<com.naman14.timber.widgets.ThemedPreferenceCategory android:title="LastFM">

<Preference
android:key="lastfm_login"
android:summary="Login to LastFM to scrobble"
android:title="Login" />


</com.naman14.timber.widgets.ThemedPreferenceCategory>

<com.naman14.timber.widgets.ThemedPreferenceCategory android:title="@string/advanced">

<com.afollestad.appthemeengine.prefs.ATECheckBoxPreference
Expand All @@ -119,14 +129,6 @@
app:ateKey_pref_checkBox="?ate_key" />

</com.naman14.timber.widgets.ThemedPreferenceCategory>

<com.naman14.timber.widgets.ThemedPreferenceCategory android:title="LastFM">

<Preference
android:key="lastfm_login"
android:summary="Login to LastFM to scrobble"
android:title="Login" />


</com.naman14.timber.widgets.ThemedPreferenceCategory>


</PreferenceScreen>

0 comments on commit 7f30745

Please sign in to comment.