Skip to content

Commit

Permalink
Google Java Format
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions authored and VigneshSK17 committed Apr 8, 2024
1 parent d61bbfe commit b246561
Show file tree
Hide file tree
Showing 24 changed files with 1,281 additions and 1,272 deletions.
240 changes: 113 additions & 127 deletions app/src/main/java/com/t1r2340/spotifystats/FireBaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,157 +7,143 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.FirebaseAuthUIActivityResultContract;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.data.model.FirebaseAuthUIAuthenticationResult;
import com.google.android.gms.tasks.OnCompleteListener;
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;

import okhttp3.Call;

public class FireBaseActivity extends AppCompatActivity implements FailureCallback {

private String mAccessToken;
private String mAccessToken;

private final ActivityResultLauncher<Intent> signInLauncher = registerForActivityResult(
new FirebaseAuthUIActivityResultContract(),
result -> onSignInResult(result)
);
private final ActivityResultLauncher<Intent> signInLauncher =
registerForActivityResult(
new FirebaseAuthUIActivityResultContract(), result -> onSignInResult(result));

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_base);
createSignInIntent();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_base);
createSignInIntent();
}

protected void onStart() {
super.onStart();
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser == null) {
createSignInIntent();
}

protected void onStart() {
super.onStart();
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser == null) {
createSignInIntent();
}
}

public void createSignInIntent() {
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build());

Intent signInIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build();
signInLauncher.launch(signInIntent);
Log.d("Firebase", "Sign in intent launched");
}
private void onSignInResult(FirebaseAuthUIAuthenticationResult result) {
IdpResponse response = result.getIdpResponse();
if (result.getResultCode() == RESULT_OK) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Toast.makeText(this, "Welcome " + user.getDisplayName() , Toast.LENGTH_SHORT).show();
getToken();

} else {
Log.d("Firebase", "Login failed");
}
}

public void createSignInIntent() {
List<AuthUI.IdpConfig> providers = Arrays.asList(new AuthUI.IdpConfig.EmailBuilder().build());

Intent signInIntent =
AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(providers).build();
signInLauncher.launch(signInIntent);
Log.d("Firebase", "Sign in intent launched");
}

private void onSignInResult(FirebaseAuthUIAuthenticationResult result) {
IdpResponse response = result.getIdpResponse();
if (result.getResultCode() == RESULT_OK) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Toast.makeText(this, "Welcome " + user.getDisplayName(), Toast.LENGTH_SHORT).show();
getToken();

} else {
Log.d("Firebase", "Login failed");
}

/**
* 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);

}

/**
* 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);
}

/**
* 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);

}
}

// 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();
}

// TODO: INclude in wrapped generation
// private void cancelCall() {
// if (mCall != null) {
// mCall.cancel();
// }
// }

// TODO: include in wrapped generation
// @Override
// protected void onDestroy() {
// cancelCall();
// spotifyAppRemoteHelper.disconnect(); // TODO: Add this for app remote use cases
// super.onDestroy();
// }
}

// 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();
}

// TODO: INclude in wrapped generation
// private void cancelCall() {
// if (mCall != null) {
// mCall.cancel();
// }
// }

// TODO: include in wrapped generation
// @Override
// protected void onDestroy() {
// cancelCall();
// spotifyAppRemoteHelper.disconnect(); // TODO: Add this for app remote use cases
// super.onDestroy();
// }
}
50 changes: 22 additions & 28 deletions app/src/main/java/com/t1r2340/spotifystats/HomePageFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,40 @@
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.google.firebase.auth.FirebaseAuth;
import com.t1r2340.spotifystats.databinding.FragmentHomePageBinding;

public class HomePageFragment extends Fragment {

private FragmentHomePageBinding binding;
private FragmentHomePageBinding binding;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

binding = FragmentHomePageBinding.inflate(inflater, container, false);
binding = FragmentHomePageBinding.inflate(inflater, container, false);

setupButtons();
setupButtons();

return binding.getRoot();
}
return binding.getRoot();
}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}

}
private void setupButtons() {

private void setupButtons() {
setupSettingsButton();
}

setupSettingsButton();
private void setupSettingsButton() {

}
ImageButton settingsButton = binding.btnSettings;

private void setupSettingsButton() {

ImageButton settingsButton = binding.btnSettings;

settingsButton.setOnClickListener(v -> {
Intent intent = new Intent(getActivity(), SettingsActivity.class);
startActivity(intent);
settingsButton.setOnClickListener(
v -> {
Intent intent = new Intent(getActivity(), SettingsActivity.class);
startActivity(intent);
});


}


}
}
}
Loading

0 comments on commit b246561

Please sign in to comment.