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

272 lines
12 KiB
Java
Raw Normal View History

2020-06-28 19:11:39 +02:00
package app.fedilab.fedilabtube;
2020-07-01 16:36:08 +02:00
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program 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.
*
* TubeLab 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.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
2020-07-01 15:08:42 +02:00
2020-09-29 17:42:15 +02:00
import android.annotation.SuppressLint;
2020-06-28 19:11:39 +02:00
import android.content.Intent;
import android.content.SharedPreferences;
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-09-13 19:20:04 +02:00
import android.view.View;
2020-07-09 17:57:01 +02:00
import android.view.ViewGroup;
2020-06-28 19:11:39 +02:00
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
2020-09-13 19:20:04 +02:00
import com.google.android.material.textfield.TextInputEditText;
2020-07-09 17:57:01 +02:00
import com.google.android.material.textfield.TextInputLayout;
2020-06-29 16:43:20 +02:00
import java.util.Arrays;
2020-06-28 19:11:39 +02:00
2020-09-26 16:46:51 +02:00
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
2020-09-27 16:33:43 +02:00
import app.fedilab.fedilabtube.client.entities.Error;
2020-09-26 16:46:51 +02:00
import app.fedilab.fedilabtube.client.entities.Oauth;
import app.fedilab.fedilabtube.client.entities.OauthParams;
2020-09-27 16:33:43 +02:00
import app.fedilab.fedilabtube.client.entities.Token;
2020-06-28 19:11:39 +02:00
import app.fedilab.fedilabtube.helper.Helper;
import es.dmoral.toasty.Toasty;
2020-09-26 16:46:51 +02:00
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.updateCredential;
2020-09-08 15:54:07 +02:00
2020-06-28 19:11:39 +02:00
2020-09-03 19:08:53 +02:00
public class LoginActivity extends AppCompatActivity {
2020-06-28 19:11:39 +02:00
private static String client_id;
private static String client_secret;
private EditText login_uid;
private EditText login_passwd;
private Button connectionButton;
2020-09-13 19:20:04 +02:00
private TextInputEditText login_instance;
2020-06-28 19:11:39 +02:00
@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 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);
2020-09-15 16:07:05 +02:00
content_create.setSpan(new ForegroundColorSpan(ContextCompat.getColor(LoginActivity.this, Helper.getColorAccent())), 0, content_create.length(),
2020-06-29 16:43:20 +02:00
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);
});
2020-09-13 19:20:04 +02:00
TextInputLayout login_instance_container = findViewById(R.id.login_instance_container);
login_instance = findViewById(R.id.login_instance);
if (BuildConfig.full_instances) {
login_instance_container.setVisibility(View.VISIBLE);
}
2020-06-28 19:11:39 +02:00
login_uid = findViewById(R.id.login_uid);
login_passwd = findViewById(R.id.login_passwd);
2020-07-09 17:57:01 +02:00
if (Helper.isTablet(LoginActivity.this)) {
2020-10-17 18:50:20 +02:00
ViewGroup.LayoutParams layoutParamsI = login_instance_container.getLayoutParams();
layoutParamsI.width = (int) Helper.convertDpToPixel(300, LoginActivity.this);
login_instance_container.setLayoutParams(layoutParamsI);
2020-07-09 17:57:01 +02:00
TextInputLayout login_uid_container = findViewById(R.id.login_uid_container);
ViewGroup.LayoutParams layoutParamsU = login_uid_container.getLayoutParams();
layoutParamsU.width = (int) Helper.convertDpToPixel(300, LoginActivity.this);
login_uid_container.setLayoutParams(layoutParamsU);
TextInputLayout login_passwd_container = findViewById(R.id.login_passwd_container);
ViewGroup.LayoutParams layoutParamsP = login_passwd_container.getLayoutParams();
layoutParamsP.width = (int) Helper.convertDpToPixel(300, LoginActivity.this);
login_passwd_container.setLayoutParams(layoutParamsP);
}
2020-06-28 19:11:39 +02:00
connectionButton = findViewById(R.id.login_button);
2020-09-19 13:01:27 +02:00
if (!BuildConfig.full_instances) {
login_uid.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) {
if (android.util.Patterns.EMAIL_ADDRESS.matcher(login_uid.getText().toString().trim()).matches()) {
String[] emailArray = login_uid.getText().toString().split("@");
if (emailArray.length > 1 && Arrays.asList(Helper.openid).contains(emailArray[1])) {
connectionButton.callOnClick();
}
2020-07-04 18:07:01 +02:00
}
}
2020-09-19 13:01:27 +02:00
});
}
2020-06-30 11:38:06 +02:00
2020-06-28 19:11:39 +02:00
connectionButton.setOnClickListener(v -> {
2020-06-29 15:43:35 +02:00
2020-09-19 13:01:27 +02:00
if (login_uid.getText().toString().contains("@") && !android.util.Patterns.EMAIL_ADDRESS.matcher(login_uid.getText().toString().trim()).matches()) {
2020-06-29 16:43:20 +02:00
Toasty.error(LoginActivity.this, getString(R.string.email_error)).show();
return;
2020-06-28 19:11:39 +02:00
}
2020-09-13 19:20:04 +02:00
connectionButton.setEnabled(false);
String instance, host;
if (!BuildConfig.full_instances) {
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();
2020-10-11 17:21:45 +02:00
connectionButton.setEnabled(true);
2020-09-13 19:20:04 +02:00
return;
}
host = emailArray[1];
instance = Helper.getPeertubeUrl(host);
} else {
if (login_instance == null || login_instance.getText() == null || login_instance.getText().toString().trim().length() == 0) {
2020-09-14 15:53:31 +02:00
Toasty.error(LoginActivity.this, getString(R.string.not_valide_instance)).show();
2020-10-11 17:21:45 +02:00
connectionButton.setEnabled(true);
2020-09-13 19:20:04 +02:00
return;
}
2020-09-15 16:07:05 +02:00
instance = host = login_instance.getText().toString().trim().toLowerCase();
2020-06-28 19:11:39 +02:00
}
2020-09-15 16:07:05 +02:00
if (Arrays.asList(Helper.openid).contains(host) && !BuildConfig.full_instances) {
2020-07-03 17:04:04 +02:00
new Thread(() -> {
2020-06-29 16:43:20 +02:00
try {
2020-09-29 17:42:15 +02:00
Oauth oauth = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).oauthClient(null, null, null, null);
2020-09-26 16:46:51 +02:00
if (oauth == null) {
2020-07-03 17:04:04 +02:00
runOnUiThread(() -> {
connectionButton.setEnabled(true);
Toasty.error(LoginActivity.this, getString(R.string.client_error), Toast.LENGTH_LONG).show();
});
return;
}
2020-09-26 16:46:51 +02:00
client_id = oauth.getClient_id();
client_secret = oauth.getClient_secret();
2020-06-29 16:43:20 +02:00
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();
2020-07-03 17:04:04 +02:00
Intent intent = new Intent(LoginActivity.this, WebviewConnectActivity.class);
Bundle b = new Bundle();
b.putString("url", "https://" + Helper.getPeertubeUrl(host) + "/plugins/auth-openid-connect/0.0.1/auth/openid-connect");
intent.putExtras(b);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
runOnUiThread(() -> {
connectionButton.setEnabled(true);
2020-09-27 16:33:43 +02:00
Toasty.error(LoginActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2020-07-03 17:04:04 +02:00
});
}
}).start();
} else {
new Thread(() -> {
2020-09-29 17:42:15 +02:00
Oauth oauth = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).oauthClient(Helper.CLIENT_NAME_VALUE, Helper.WEBSITE_VALUE, Helper.OAUTH_SCOPES_PEERTUBE, Helper.WEBSITE_VALUE);
if (oauth == null) {
runOnUiThread(() -> {
connectionButton.setEnabled(true);
Toasty.error(LoginActivity.this, getString(R.string.client_error), Toast.LENGTH_LONG).show();
});
return;
}
2020-09-26 16:46:51 +02:00
2020-09-29 17:42:15 +02:00
client_id = oauth.getClient_id();
client_secret = oauth.getClient_secret();
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();
OauthParams oauthParams = new OauthParams();
oauthParams.setClient_id(client_id);
oauthParams.setClient_secret(client_secret);
oauthParams.setGrant_type("password");
oauthParams.setScope("user");
oauthParams.setUsername(login_uid.getText().toString().trim());
oauthParams.setPassword(login_passwd.getText().toString());
try {
Token token = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).manageToken(oauthParams);
proceedLogin(token, host);
} catch (final Exception | Error e) {
oauthParams.setUsername(login_uid.getText().toString().toLowerCase().trim());
2020-09-26 16:46:51 +02:00
try {
2020-09-29 17:42:15 +02:00
Token token = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).manageToken(oauthParams);
2020-09-27 16:33:43 +02:00
proceedLogin(token, host);
2020-09-29 17:42:15 +02:00
} catch (Error error) {
Error.displayError(LoginActivity.this, error);
error.printStackTrace();
2020-10-11 17:21:45 +02:00
runOnUiThread(() -> connectionButton.setEnabled(true));
2020-07-03 17:04:04 +02:00
}
2020-06-29 16:43:20 +02:00
}
2020-07-03 17:04:04 +02:00
}).start();
}
2020-06-28 19:11:39 +02:00
});
}
2020-09-29 17:42:15 +02:00
@SuppressLint("ApplySharedPref")
2020-09-27 16:33:43 +02:00
private void proceedLogin(Token token, String host) {
runOnUiThread(() -> {
2020-09-27 16:33:43 +02:00
if (token != null) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token.getAccess_token());
editor.putString(Helper.PREF_INSTANCE, host);
2020-09-29 17:42:15 +02:00
editor.commit();
2020-09-27 16:33:43 +02:00
//Update the account with the token;
updateCredential(LoginActivity.this, token.getAccess_token(), client_id, client_secret, token.getRefresh_token(), host);
} else {
connectionButton.setEnabled(true);
}
});
}
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);
}
2020-07-01 15:08:42 +02:00
@Override
public void onDestroy() {
super.onDestroy();
}
2020-06-28 19:11:39 +02:00
}