TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java

261 lines
12 KiB
Java
Raw Normal View History

2020-06-28 19:11:39 +02:00
package app.fedilab.fedilabtube;
import android.content.Intent;
import android.content.SharedPreferences;
2020-06-30 11:38:06 +02:00
import android.database.sqlite.SQLiteDatabase;
2020-06-28 19:11:39 +02:00
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.view.MenuItem;
2020-06-30 11:38:06 +02:00
import android.view.View;
2020-06-28 19:11:39 +02:00
import android.widget.Button;
import android.widget.EditText;
2020-06-30 11:38:06 +02:00
import android.widget.ImageView;
import android.widget.LinearLayout;
2020-06-28 19:11:39 +02:00
import android.widget.TextView;
import android.widget.Toast;
2020-06-30 11:38:06 +02:00
import androidx.appcompat.app.AlertDialog;
2020-06-28 19:11:39 +02:00
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
2020-06-29 16:43:20 +02:00
import java.util.Arrays;
2020-06-28 19:11:39 +02:00
import java.util.HashMap;
import app.fedilab.fedilabtube.asynctasks.UpdateAccountInfoAsyncTask;
import app.fedilab.fedilabtube.client.HttpsConnection;
2020-06-30 11:38:06 +02:00
import app.fedilab.fedilabtube.client.entities.Account;
2020-06-28 19:11:39 +02:00
import app.fedilab.fedilabtube.helper.Helper;
2020-06-30 11:38:06 +02:00
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
2020-06-28 19:11:39 +02:00
import es.dmoral.toasty.Toasty;
public class LoginActivity extends AppCompatActivity {
private static String client_id;
private static String client_secret;
private EditText login_uid;
private EditText login_passwd;
private Button connectionButton;
private String actionToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView create_an_account_peertube = findViewById(R.id.create_an_account_peertube);
2020-06-29 15:43:35 +02:00
2020-06-29 16:43:20 +02:00
SpannableString content_create = new SpannableString(getString(R.string.join_peertube));
content_create.setSpan(new UnderlineSpan(), 0, content_create.length(), 0);
content_create.setSpan(new ForegroundColorSpan(ContextCompat.getColor(LoginActivity.this, R.color.colorAccent)), 0, content_create.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
create_an_account_peertube.setText(content_create);
2020-06-28 19:11:39 +02:00
create_an_account_peertube.setOnClickListener(v -> {
Intent mainActivity = new Intent(LoginActivity.this, PeertubeRegisterActivity.class);
Bundle b = new Bundle();
mainActivity.putExtras(b);
startActivity(mainActivity);
});
login_uid = findViewById(R.id.login_uid);
login_passwd = findViewById(R.id.login_passwd);
connectionButton = findViewById(R.id.login_button);
2020-06-30 11:38:06 +02:00
LinearLayout connected = findViewById(R.id.connected);
LinearLayout not_connected = findViewById(R.id.not_connected);
2020-06-30 13:33:43 +02:00
if (Helper.isLoggedIn(LoginActivity.this)) {
2020-06-30 11:38:06 +02:00
not_connected.setVisibility(View.GONE);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(LoginActivity.this));
TextView instanceView = findViewById(R.id.instance);
Account account = new AccountDAO(LoginActivity.this, db).getUniqAccount(userId, instance);
2020-06-30 13:33:43 +02:00
if (account == null) {
2020-06-30 11:38:06 +02:00
account = new AccountDAO(LoginActivity.this, db).getUniqAccount(userId, Helper.getPeertubeUrl(instance));
}
2020-06-30 13:33:43 +02:00
if (account != null) {
2020-06-30 11:38:06 +02:00
ImageView profile_picture = findViewById(R.id.profile_picture);
TextView username = findViewById(R.id.username);
TextView displayname = findViewById(R.id.displayname);
Helper.loadGiF(LoginActivity.this, account, profile_picture);
username.setText(String.format("@%s", account.getUsername()));
displayname.setText(account.getDisplay_name());
instanceView.setText(account.getInstance());
Button logout_button = findViewById(R.id.logout_button);
Account finalAccount = account;
logout_button.setOnClickListener(v -> {
AlertDialog.Builder dialogBuilderLogoutAccount = new AlertDialog.Builder(LoginActivity.this);
dialogBuilderLogoutAccount.setMessage(getString(R.string.logout_account_confirmation, finalAccount.getUsername()));
dialogBuilderLogoutAccount.setPositiveButton(R.string.action_logout, (dialog, id) -> {
Helper.logoutCurrentUser(LoginActivity.this, finalAccount);
dialog.dismiss();
});
dialogBuilderLogoutAccount.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
AlertDialog alertDialogLogoutAccount = dialogBuilderLogoutAccount.create();
alertDialogLogoutAccount.show();
});
2020-06-30 13:33:43 +02:00
} else {
2020-06-30 11:38:06 +02:00
Helper.logoutCurrentUser(LoginActivity.this, null);
}
2020-06-30 13:33:43 +02:00
} else {
2020-06-30 11:38:06 +02:00
connected.setVisibility(View.GONE);
}
2020-06-28 19:11:39 +02:00
connectionButton.setOnClickListener(v -> {
2020-06-29 15:43:35 +02:00
2020-06-29 16:43:20 +02:00
if (!android.util.Patterns.EMAIL_ADDRESS.matcher(login_uid.getText().toString().trim()).matches()) {
Toasty.error(LoginActivity.this, getString(R.string.email_error)).show();
return;
2020-06-28 19:11:39 +02:00
}
2020-06-29 16:43:20 +02:00
String[] emailArray = login_uid.getText().toString().split("@");
if (emailArray.length > 1 && !Arrays.asList(Helper.valideEmails).contains(emailArray[1])) {
Toasty.error(LoginActivity.this, getString(R.string.email_error_domain, emailArray[1])).show();
return;
}
String host = emailArray[1];
String instance = Helper.getPeertubeUrl(host);
final HashMap<String, String> parameters = new HashMap<>();
connectionButton.setEnabled(false);
2020-06-28 19:11:39 +02:00
try {
2020-06-29 16:43:20 +02:00
instance = URLEncoder.encode(instance, "utf-8");
2020-06-28 19:11:39 +02:00
} catch (UnsupportedEncodingException e) {
2020-06-29 16:43:20 +02:00
Toasty.error(LoginActivity.this, getString(R.string.client_error), Toast.LENGTH_LONG).show();
2020-06-28 19:11:39 +02:00
}
2020-06-29 16:43:20 +02:00
actionToken = "/api/v1/oauth-clients/local";
parameters.clear();
parameters.put(Helper.CLIENT_NAME, Helper.CLIENT_NAME_VALUE);
parameters.put(Helper.REDIRECT_URIS, Helper.REDIRECT_CONTENT);
parameters.put(Helper.SCOPES, Helper.OAUTH_SCOPES_PEERTUBE);
parameters.put(Helper.WEBSITE, Helper.WEBSITE_VALUE);
String finalInstance = instance;
2020-06-28 19:11:39 +02:00
new Thread(() -> {
try {
2020-06-29 16:43:20 +02:00
String response = new HttpsConnection(LoginActivity.this).get("https://" + finalInstance + actionToken, 30, parameters, null);
JSONObject resobj;
try {
resobj = new JSONObject(response);
client_id = resobj.get(Helper.CLIENT_ID).toString();
client_secret = resobj.get(Helper.CLIENT_SECRET).toString();
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.CLIENT_ID, client_id);
editor.putString(Helper.CLIENT_SECRET, client_secret);
editor.apply();
parameters.clear();
parameters.put(Helper.CLIENT_ID, sharedpreferences.getString(Helper.CLIENT_ID, null));
parameters.put(Helper.CLIENT_SECRET, sharedpreferences.getString(Helper.CLIENT_SECRET, null));
parameters.put("grant_type", "password");
try {
parameters.put("username", URLEncoder.encode(login_uid.getText().toString().trim().toLowerCase(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
parameters.put("username", login_uid.getText().toString().trim().toLowerCase());
}
2020-06-28 19:11:39 +02:00
try {
2020-06-29 16:43:20 +02:00
parameters.put("password", URLEncoder.encode(login_passwd.getText().toString(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
parameters.put("password", login_passwd.getText().toString());
}
parameters.put("scope", "user");
String oauthUrl = "/api/v1/users/token";
try {
String responseLogin = new HttpsConnection(LoginActivity.this).post("https://" + finalInstance + oauthUrl, 30, parameters, null);
runOnUiThread(() -> {
JSONObject resobjLogin;
try {
resobjLogin = new JSONObject(responseLogin);
2020-06-30 11:38:06 +02:00
String token = resobjLogin.getString("access_token");
String refresh_token = resobjLogin.getString("refresh_token");
2020-06-29 16:43:20 +02:00
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
2020-06-30 11:38:06 +02:00
editor.putString(Helper.PREF_INSTANCE, host);
2020-06-29 16:43:20 +02:00
editor.apply();
//Update the account with the token;
new UpdateAccountInfoAsyncTask(LoginActivity.this, token, client_id, client_secret, refresh_token, host).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (JSONException e) {
e.printStackTrace();
runOnUiThread(() -> connectionButton.setEnabled(true));
}
});
} catch (final Exception e) {
2020-06-28 19:11:39 +02:00
e.printStackTrace();
2020-06-29 16:43:20 +02:00
runOnUiThread(() -> {
connectionButton.setEnabled(true);
String message;
if (e.getLocalizedMessage() != null && e.getLocalizedMessage().trim().length() > 0)
message = e.getLocalizedMessage();
else if (e.getMessage() != null && e.getMessage().trim().length() > 0)
message = e.getMessage();
else
message = getString(R.string.client_error);
Toasty.error(LoginActivity.this, message, Toast.LENGTH_LONG).show();
});
2020-06-28 19:11:39 +02:00
}
2020-06-29 16:43:20 +02:00
} catch (JSONException e) {
e.printStackTrace();
e.printStackTrace();
runOnUiThread(() -> connectionButton.setEnabled(true));
}
2020-06-28 19:11:39 +02:00
} catch (final Exception e) {
e.printStackTrace();
runOnUiThread(() -> {
connectionButton.setEnabled(true);
2020-06-29 16:43:20 +02:00
String message = null;
if (e.getLocalizedMessage() != null) {
message = e.getMessage();
}
if (message == null) {
message = getString(R.string.client_error);
}
Toasty.error(LoginActivity.this, message, Toast.LENGTH_LONG).show();
2020-06-28 19:11:39 +02:00
});
}
}).start();
});
}
2020-06-29 16:43:20 +02:00
@Override
protected void onResume() {
super.onResume();
2020-06-28 19:11:39 +02:00
}
2020-06-29 16:43:20 +02:00
2020-06-28 19:11:39 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}