package co.polylearn.toeic; import android.accounts.Account; import android.app.IntentService; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.ResultReceiver; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.VolleyLog; import com.android.volley.toolbox.JsonObjectRequest; import com.google.android.gms.auth.GoogleAuthException; import com.google.android.gms.auth.GoogleAuthUtil; import com.google.android.gms.common.Scopes; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.HashMap; /** * An {@link IntentService} subclass for handling asynchronous task requests in * a service on a separate handler thread. *
* helper methods.
*/
public class AuthService extends IntentService {
public static final int SUCCESS_AUTH = 1;
public static final int ERROR_AUTH = 2;
public static final String KEY_STATUS_CODE = "co.polylearn.toeic.extra.STATUS_CODE";
public static final String KEY_NETWORK_RESPONSE = "co.polylearn.toeic.extra.NETWORK_RESPONSE";
private static final String ACTION_FACEBOOK_LOGIN = "co.polylearn.toeic.action.FACEBOOK_LOGIN";
private static final String ACTION_GOOGLE_LOGIN = "co.polylearn.toeic.action.GOOGLE_LOGIN";
private static final String ACTION_REGISTER = "co.polylearn.toeic.action.REGISTER";
private static final String ACTION_PASSWORD_LOGIN = "co.polylearn.toeic.action.PASSWORD_LOGIN";
private static final String EXTRA_ACCESS_TOKEN = "co.polylearn.toeic.extra.ACCESS_TOKEN";
private static final String EXTRA_RECEIVER = "co.polylearn.toeic.extra.RECEIVER";
private static final String EXTRA_EMAIL = "co.polylearn.toeic.extra.EMAIL";
private static final String EXTRA_PASSWORD = "co.polylearn.toeic.extra.PASSWORD";
public AuthService() {
super("AuthService");
}
/**
* Starts this service to perform action Facebook Login with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionFacebookLogin(Context context, String token, ResultReceiver resultReceiver) {
Intent intent = new Intent(context, AuthService.class);
intent.setAction(ACTION_FACEBOOK_LOGIN);
intent.putExtra(EXTRA_ACCESS_TOKEN, token);
intent.putExtra(EXTRA_RECEIVER, resultReceiver);
context.startService(intent);
}
/**
* Starts this service to perform action Google Login with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionGoogleLogin(Context context, String email, ResultReceiver resultReceiver) {
Intent intent = new Intent(context, AuthService.class);
intent.setAction(ACTION_GOOGLE_LOGIN);
intent.putExtra(EXTRA_EMAIL, email);
intent.putExtra(EXTRA_RECEIVER, resultReceiver);
context.startService(intent);
}
/**
* Starts this service to perform action Register Login with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionRegister(Context context, String email, String password, ResultReceiver resultReceiver) {
Intent intent = new Intent(context, AuthService.class);
intent.setAction(ACTION_REGISTER);
intent.putExtra(EXTRA_EMAIL, email);
intent.putExtra(EXTRA_PASSWORD, password);
intent.putExtra(EXTRA_RECEIVER, resultReceiver);
context.startService(intent);
}
public static void startActionPasswordLogin(Context context, String email, String password, ResultReceiver resultReceiver) {
Intent intent = new Intent(context, AuthService.class);
intent.setAction(ACTION_PASSWORD_LOGIN);
intent.putExtra(EXTRA_EMAIL, email);
intent.putExtra(EXTRA_PASSWORD, password);
intent.putExtra(EXTRA_RECEIVER, resultReceiver);
context.startService(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_RECEIVER);
if (ACTION_FACEBOOK_LOGIN.equals(action)) {
final String token = intent.getStringExtra(EXTRA_ACCESS_TOKEN);
handleActionFacebookLogin(token, receiver);
} else if (ACTION_GOOGLE_LOGIN.equals(action)) {
final String email = intent.getStringExtra(EXTRA_EMAIL);
handleActionGoogleLogin(email, receiver);
} else if (ACTION_PASSWORD_LOGIN.equals(action) || ACTION_REGISTER.equals(action)) {
final String email = intent.getStringExtra(EXTRA_EMAIL);
final String password = intent.getStringExtra(EXTRA_PASSWORD);
if (ACTION_PASSWORD_LOGIN.equals(action)) {
handleActionPasswordLogin(email, password, receiver);
} else {
handleActionRegister(email, password, receiver);
}
}
}
}
/**
* Handle action Password Login in the provided background thread with the provided
* parameters.
*/
private void handleActionPasswordLogin(final String email, final String password, final ResultReceiver receiver) {
final String URL = "https://api.polylearn.co/login";
sendWithCredentials(email, password, URL, receiver);
}
/**
* Handle action Password Login in the provided background thread with the provided
* parameters.
*/
private void handleActionRegister(final String email, final String password, final ResultReceiver receiver) {
final String URL = "https://api.polylearn.co/register";
sendWithCredentials(email, password, URL, receiver);
}
private void sendWithCredentials(final String email, final String password, final String URL, final ResultReceiver receiver) {
HashMap