Skip to content

Commit

Permalink
Added deleting and logging out functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
VigneshSK17 committed Apr 8, 2024
1 parent f327c92 commit 51c7a85
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 59 deletions.
110 changes: 90 additions & 20 deletions app/src/main/java/com/t1r2340/spotifystats/FireBaseActivity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.t1r2340.spotifystats;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultCallback;
Expand All @@ -18,12 +20,23 @@
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.spotify.sdk.android.auth.AuthorizationClient;
import com.spotify.sdk.android.auth.AuthorizationRequest;
import com.spotify.sdk.android.auth.AuthorizationResponse;
import com.t1r2340.spotifystats.databinding.ActivityFireBaseBinding;
import com.t1r2340.spotifystats.helpers.FailureCallback;
import com.t1r2340.spotifystats.helpers.FirestoreHelper;
import com.t1r2340.spotifystats.helpers.SpotifyApiHelper;

import java.util.Arrays;
import java.util.List;

public class FireBaseActivity extends AppCompatActivity {
import okhttp3.Call;

public class FireBaseActivity extends AppCompatActivity implements FailureCallback {

private String mAccessToken;
private Call mCall;

private final ActivityResultLauncher<Intent> signInLauncher = registerForActivityResult(
new FirebaseAuthUIActivityResultContract(),
Expand Down Expand Up @@ -72,30 +85,87 @@ private void onSignInResult(FirebaseAuthUIAuthenticationResult result) {
// TODO: Do stuff with fragment
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Toast.makeText(this, "Welcome " + user.getDisplayName() , Toast.LENGTH_SHORT).show();
getToken();

} else {
Log.d("Firebase", "Login failed");
}
}
public void signOut() {
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});

/**
* Get token from Spotify
* This method will open the Spotify login activity and get the token
* What is token?
* https://developer.spotify.com/documentation/general/guides/authorization-guide/
*/
public void getToken() {
final AuthorizationRequest request = getAuthenticationRequest(AuthorizationResponse.Type.TOKEN);
AuthorizationClient.openLoginActivity(FireBaseActivity.this, 0, request);

}

/**
* When the app leaves this activity to momentarily get a token/code, this function
* fetches the result of that external activity to get the response from Spotify
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final AuthorizationResponse response = AuthorizationClient.getResponse(resultCode, data);

// Check which request code is present (if any)
if (0 == requestCode) {
mAccessToken = response.getAccessToken();

Log.d("Firebase", "Spotify token received: " + mAccessToken);

}
}

public void delete() {
// [START auth_fui_delete]
AuthUI.getInstance()
.delete(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
// [END auth_fui_delete]
// TODO: Implement into wrapped generation
/**
* Creates a UI thread to update a TextView in the background
* Reduces UI latency and makes the system perform more consistently
*
* @param text the text to set
* @param textView TextView object to update
*/
private void setTextAsync(final String text, TextView textView) {
runOnUiThread(() -> textView.setText(text));
}

/**
* Get authentication request
*
* @param type the type of the request
* @return the authentication request
*/
private AuthorizationRequest getAuthenticationRequest(AuthorizationResponse.Type type) {
String clientId = getString(R.string.client_id);
String redirectUri = getString(R.string.redirect_uri);
return new AuthorizationRequest.Builder(clientId, type, Uri.parse(redirectUri).toString())
.setShowDialog(true)
.setScopes(new String[] { "user-read-email", "user-follow-read", "user-top-read" }) // <--- Change the scope of your requested token here
.setCampaign("your-campaign-token")
.build();
}

@Override
public void onFailure(Exception e) {
Log.d("HTTP", "Failed to fetch data: " + e);
Toast.makeText(FireBaseActivity.this, "Failed to fetch data",
Toast.LENGTH_SHORT).show();
}

private void cancelCall() {
if (mCall != null) {
mCall.cancel();
}
}
@Override
protected void onDestroy() {
cancelCall();
// spotifyAppRemoteHelper.disconnect(); // TODO: Add this for app remote use cases
super.onDestroy();
}
}
51 changes: 18 additions & 33 deletions app/src/main/java/com/t1r2340/spotifystats/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
public class MainActivity extends AppCompatActivity implements FailureCallback {

// TODO: Move auth logic to separate class
public static final String CLIENT_ID = "1d7e65ad5ac447908a52e0b5de50ca92";
public static final String REDIRECT_URI = "spotifystats://auth";
public final String CLIENT_ID = getString(R.string.client_id);
public final String REDIRECT_URI = getString(R.string.redirect_uri);

public static final int AUTH_TOKEN_REQUEST_CODE = 0;
public static final int AUTH_CODE_REQUEST_CODE = 1;
Expand Down Expand Up @@ -102,13 +102,6 @@ public void checkCurrentUser() {
}
}


/**
* Get token from Spotify
* This method will open the Spotify login activity and get the token
* What is token?
* https://developer.spotify.com/documentation/general/guides/authorization-guide/
*/
public void getToken() {
final AuthorizationRequest request = getAuthenticationRequest(AuthorizationResponse.Type.TOKEN);
AuthorizationClient.openLoginActivity(MainActivity.this, AUTH_TOKEN_REQUEST_CODE, request);
Expand Down Expand Up @@ -184,30 +177,6 @@ public void onGetUserProfileClicked() {

}

/**
* Creates a UI thread to update a TextView in the background
* Reduces UI latency and makes the system perform more consistently
*
* @param text the text to set
* @param textView TextView object to update
*/
private void setTextAsync(final String text, TextView textView) {
runOnUiThread(() -> textView.setText(text));
}

/**
* Get authentication request
*
* @param type the type of the request
* @return the authentication request
*/
private AuthorizationRequest getAuthenticationRequest(AuthorizationResponse.Type type) {
return new AuthorizationRequest.Builder(CLIENT_ID, type, getRedirectUri().toString())
.setShowDialog(false)
.setScopes(new String[] { "user-read-email", "user-follow-read", "user-top-read" }) // <--- Change the scope of your requested token here
.setCampaign("your-campaign-token")
.build();
}

/**
* Gets the redirect Uri for Spotify
Expand Down Expand Up @@ -259,6 +228,22 @@ private void testAppRemote() {
spotifyAppRemoteHelper.connectAndRun("spotify:track:6FGrBYBdIAS2asaP54AnZo");
}

/**
* Get authentication request
*
* @param type the type of the request
* @return the authentication request
*/
private AuthorizationRequest getAuthenticationRequest(AuthorizationResponse.Type type) {
String clientId = getString(R.string.client_id);
String redirectUri = getString(R.string.redirect_uri);
return new AuthorizationRequest.Builder(clientId, type, Uri.parse(redirectUri).toString())
.setShowDialog(false)
.setScopes(new String[] { "user-read-email", "user-follow-read", "user-top-read" }) // <--- Change the scope of your requested token here
.setCampaign("your-campaign-token")
.build();
}

@Override
public void onFailure(Exception e) {
Log.d("HTTP", "Failed to fetch data: " + e);
Expand Down
37 changes: 31 additions & 6 deletions app/src/main/java/com/t1r2340/spotifystats/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.t1r2340.spotifystats;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
Expand All @@ -19,6 +20,9 @@
import com.google.android.material.appbar.MaterialToolbar;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.spotify.sdk.android.auth.AuthorizationClient;
import com.spotify.sdk.android.auth.AuthorizationRequest;
import com.spotify.sdk.android.auth.AuthorizationResponse;
import com.t1r2340.spotifystats.databinding.ActivitySettingsBinding;

public class SettingsActivity extends AppCompatActivity {
Expand Down Expand Up @@ -50,24 +54,45 @@ protected void onCreate(Bundle savedInstanceState) {
private void setupButtons() {
setupExitButton();
setupDeleteButton();
setupSignOutButton();
}

private void setupSignOutButton() {
binding.btnLogout.setOnClickListener(v -> {
signOutAccount();
signOutSpotifyAccount();
});
}

/**
* Sets up the delete account button
*/
private void setupDeleteButton() {
binding.btnDeleteAccount.setOnClickListener(v -> deleteAccount());
binding.btnDeleteAccount.setOnClickListener(v -> {
deleteAccount();
signOutSpotifyAccount();
});
}

/**
* Sets up the exit button to finish the activity
*/
private void setupExitButton() {
MaterialToolbar toolbar = binding.toolbar;
toolbar.setNavigationOnClickListener(v -> {
Intent intent = new Intent(SettingsActivity.this, FireBaseActivity.class);
startActivity(intent);
});
binding.toolbar.setNavigationOnClickListener(v -> finish());
}

private void signOutAccount() {
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener(task -> {
finish();
});
}

private void signOutSpotifyAccount() {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.spotify.com/logout/"));
Toast.makeText(SettingsActivity.this, "Sign Out of the Spotify App", Toast.LENGTH_SHORT).show();
startActivity(intent);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<resources>
<string name="app_name">Spotify Stats</string>
<string name="title_activity_main2">MainActivity2</string>

<string name="client_id">1d7e65ad5ac447908a52e0b5de50ca92</string>
<string name="redirect_uri">spotifystats://auth</string>

<!-- Strings used for fragments for navigation -->
<string name="first_fragment_label">First Fragment</string>
<string name="second_fragment_label">Second Fragment</string>
Expand Down

0 comments on commit 51c7a85

Please sign in to comment.