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

280 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-06-28 19:11:39 +02:00
2022-05-04 09:43:54 +02:00
import static app.fedilab.fedilabtube.MainActivity.PICK_INSTANCE;
2020-09-17 19:01:31 +02:00
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
2020-06-28 19:11:39 +02:00
import android.os.Bundle;
2020-09-08 10:11:11 +02:00
import android.os.Handler;
import android.os.Looper;
2020-06-28 19:11:39 +02:00
import android.text.Html;
import android.text.method.LinkMovementMethod;
2020-09-05 17:15:07 +02:00
import android.util.Patterns;
2020-06-28 19:11:39 +02:00
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
2020-06-29 15:43:35 +02:00
import java.util.Arrays;
2020-09-05 17:15:07 +02:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2020-06-29 15:43:35 +02:00
2020-06-28 19:11:39 +02:00
import app.fedilab.fedilabtube.client.APIResponse;
2020-09-26 10:22:11 +02:00
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
2020-06-28 19:11:39 +02:00
import app.fedilab.fedilabtube.client.entities.AccountCreation;
2020-11-04 18:39:45 +01:00
import app.fedilab.fedilabtube.databinding.ActivityRegisterPeertubeBinding;
2020-12-01 18:35:54 +01:00
import app.fedilab.fedilabtube.helper.HelperAcadInstance;
import app.fedilab.fedilabtube.helper.HelperInstance;
2020-06-28 19:11:39 +02:00
import es.dmoral.toasty.Toasty;
2020-09-08 10:11:11 +02:00
public class PeertubeRegisterActivity extends AppCompatActivity {
2020-06-28 19:11:39 +02:00
private String instance;
2020-11-04 18:39:45 +01:00
private ActivityRegisterPeertubeBinding binding;
2020-06-28 19:11:39 +02:00
2020-12-12 09:32:55 +01:00
@SuppressLint("SetTextI18n")
2020-06-28 19:11:39 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2020-11-04 18:39:45 +01:00
binding = ActivityRegisterPeertubeBinding.inflate(getLayoutInflater());
View mainView = binding.getRoot();
setContentView(mainView);
2020-06-28 19:11:39 +02:00
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2020-09-13 19:20:04 +02:00
2020-12-12 09:32:55 +01:00
if (BuildConfig.full_instances && BuildConfig.instance_switcher) {
2020-11-04 18:39:45 +01:00
binding.loginInstanceContainer.setVisibility(View.VISIBLE);
binding.titleLoginInstance.setVisibility(View.VISIBLE);
2020-09-18 18:10:04 +02:00
} else {
2020-11-04 18:39:45 +01:00
binding.loginInstanceContainer.setVisibility(View.GONE);
binding.titleLoginInstance.setVisibility(View.GONE);
2020-09-13 19:20:04 +02:00
}
2020-09-05 17:15:07 +02:00
2020-12-12 09:32:55 +01:00
2020-11-04 18:39:45 +01:00
binding.username.setOnFocusChangeListener((view, focused) -> {
if (!focused && binding.username.getText() != null) {
2020-09-05 17:15:07 +02:00
Pattern patternUsername = Pattern.compile("^[a-z0-9._]{1,50}$");
2020-11-04 18:39:45 +01:00
Matcher matcherMaxId = patternUsername.matcher(binding.username.getText().toString());
2020-09-05 17:15:07 +02:00
if (!matcherMaxId.matches()) {
2020-11-04 18:39:45 +01:00
binding.username.setError(getString(R.string.username_error));
2020-09-05 17:15:07 +02:00
}
}
});
2020-09-17 19:01:31 +02:00
2020-11-04 18:39:45 +01:00
binding.instanceHelp.setOnClickListener(v -> {
2020-09-17 19:01:31 +02:00
Intent intent = new Intent(PeertubeRegisterActivity.this, InstancePickerActivity.class);
startActivityForResult(intent, PICK_INSTANCE);
});
2020-11-04 18:39:45 +01:00
binding.email.setOnFocusChangeListener((view, focused) -> {
if (!focused && binding.email.getText() != null) {
2020-09-05 17:15:07 +02:00
Pattern patternUsername = Patterns.EMAIL_ADDRESS;
2020-11-04 18:39:45 +01:00
Matcher matcherMaxId = patternUsername.matcher(binding.email.getText().toString());
2020-09-05 17:15:07 +02:00
if (!matcherMaxId.matches()) {
2020-11-04 18:39:45 +01:00
binding.email.setError(getString(R.string.email_error));
2020-09-05 17:15:07 +02:00
}
}
});
2020-11-04 18:39:45 +01:00
binding.password.setOnFocusChangeListener((view, focused) -> {
if (!focused && binding.password.getText() != null) {
if (binding.password.getText().length() < 6) {
binding.password.setError(getString(R.string.password_length_error));
2020-09-05 17:15:07 +02:00
}
}
});
2020-11-04 18:39:45 +01:00
binding.passwordConfirm.setOnFocusChangeListener((view, focused) -> {
if (!focused && binding.passwordConfirm.getText() != null && binding.password.getText() != null) {
if (binding.passwordConfirm.getText().toString().compareTo(binding.password.getText().toString()) != 0) {
binding.passwordConfirm.setError(getString(R.string.password));
2020-09-05 17:15:07 +02:00
}
}
});
2020-09-20 15:31:56 +02:00
setTextAgreement();
2020-11-04 18:39:45 +01:00
binding.signup.setOnClickListener(view -> {
binding.errorMessage.setVisibility(View.GONE);
2020-11-11 10:34:28 +01:00
if (binding.username.getText() == null || binding.email.getText() == null || binding.password.getText() == null || binding.passwordConfirm.getText() == null || binding.username.getText().toString().trim().length() == 0 || binding.email.getText().toString().trim().length() == 0 ||
binding.password.getText().toString().trim().length() == 0 || binding.passwordConfirm.getText().toString().trim().length() == 0 || !binding.agreement.isChecked()) {
2020-06-28 19:11:39 +02:00
Toasty.error(PeertubeRegisterActivity.this, getString(R.string.all_field_filled)).show();
return;
}
2020-06-29 15:43:35 +02:00
2020-11-11 10:34:28 +01:00
if (!binding.password.getText().toString().trim().equals(binding.passwordConfirm.getText().toString().trim())) {
2020-06-28 19:11:39 +02:00
Toasty.error(PeertubeRegisterActivity.this, getString(R.string.password_error)).show();
return;
}
2020-11-11 10:34:28 +01:00
if (!android.util.Patterns.EMAIL_ADDRESS.matcher(binding.email.getText().toString().trim()).matches()) {
2020-06-28 19:11:39 +02:00
Toasty.error(PeertubeRegisterActivity.this, getString(R.string.email_error)).show();
return;
}
2020-11-11 10:34:28 +01:00
String[] emailArray = binding.email.getText().toString().split("@");
2020-10-15 19:02:12 +02:00
if (!BuildConfig.full_instances) {
2020-12-01 18:35:54 +01:00
if (emailArray.length > 1 && !Arrays.asList(HelperAcadInstance.valideEmails).contains(emailArray[1])) {
2020-10-15 19:02:12 +02:00
Toasty.error(PeertubeRegisterActivity.this, getString(R.string.email_error_domain, emailArray[1])).show();
return;
}
2020-06-29 15:43:35 +02:00
}
2020-06-29 16:43:20 +02:00
2020-11-11 10:34:28 +01:00
if (binding.password.getText().toString().trim().length() < 8) {
2020-06-28 19:11:39 +02:00
Toasty.error(PeertubeRegisterActivity.this, getString(R.string.password_too_short)).show();
return;
}
2020-11-11 10:34:28 +01:00
if (binding.username.getText().toString().matches("[a-z0-9_]")) {
2020-06-28 19:11:39 +02:00
Toasty.error(PeertubeRegisterActivity.this, getString(R.string.username_error)).show();
return;
}
2020-11-04 18:39:45 +01:00
binding.signup.setEnabled(false);
2020-06-29 16:43:20 +02:00
2020-09-13 19:20:04 +02:00
if (BuildConfig.full_instances) {
2020-11-11 10:34:28 +01:00
if (binding.loginInstance.getText() != null) {
instance = binding.loginInstance.getText().toString();
2020-09-18 18:10:04 +02:00
} else {
instance = "";
}
2020-11-04 18:39:45 +01:00
binding.loginInstance.setOnFocusChangeListener((view1, focus) -> {
2020-09-20 15:31:56 +02:00
if (!focus) {
setTextAgreement();
}
});
2020-09-13 19:20:04 +02:00
} else {
2020-12-14 14:42:28 +01:00
instance = HelperInstance.getLiveInstance(PeertubeRegisterActivity.this);
2020-09-13 19:20:04 +02:00
}
2020-10-17 18:50:20 +02:00
if (instance != null) {
instance = instance.toLowerCase().trim();
}
2020-09-13 19:20:04 +02:00
2020-06-28 19:11:39 +02:00
AccountCreation accountCreation = new AccountCreation();
2020-11-11 10:34:28 +01:00
accountCreation.setEmail(binding.email.getText().toString().trim());
accountCreation.setPassword(binding.password.getText().toString().trim());
accountCreation.setPasswordConfirm(binding.passwordConfirm.getText().toString().trim());
accountCreation.setUsername(binding.username.getText().toString().trim());
2020-09-05 17:15:07 +02:00
accountCreation.setInstance(instance);
2020-09-08 10:11:11 +02:00
new Thread(() -> {
try {
2020-09-26 10:22:11 +02:00
APIResponse apiResponse = new RetrofitPeertubeAPI(PeertubeRegisterActivity.this, instance, null).createAccount(accountCreation);
2020-09-08 10:11:11 +02:00
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
if (apiResponse.getError() != null) {
String errorMessage;
if (apiResponse.getError().getError() != null) {
try {
String[] resp = apiResponse.getError().getError().split(":");
if (resp.length == 2)
errorMessage = apiResponse.getError().getError().split(":")[1];
else if (resp.length == 3)
errorMessage = apiResponse.getError().getError().split(":")[2];
else
errorMessage = getString(R.string.toast_error);
} catch (Exception e) {
errorMessage = getString(R.string.toast_error);
}
} else {
errorMessage = getString(R.string.toast_error);
}
2020-11-04 18:39:45 +01:00
binding.errorMessage.setText(errorMessage);
binding.errorMessage.setVisibility(View.VISIBLE);
binding.signup.setEnabled(true);
2020-09-08 10:11:11 +02:00
return;
}
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(PeertubeRegisterActivity.this);
dialogBuilder.setCancelable(false);
dialogBuilder.setPositiveButton(R.string.validate, (dialog, which) -> {
dialog.dismiss();
finish();
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setTitle(getString(R.string.account_created));
alertDialog.setMessage(getString(R.string.account_created_message, apiResponse.getStringData()));
alertDialog.show();
};
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
2020-06-28 19:11:39 +02:00
});
2020-06-29 16:43:20 +02:00
setTitle(R.string.create_an_account);
2020-06-28 19:11:39 +02:00
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
2020-09-17 19:01:31 +02:00
@SuppressLint("ApplySharedPref")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_INSTANCE && resultCode == Activity.RESULT_OK) {
if (data != null && data.getData() != null) {
String instance = String.valueOf(data.getData());
2020-11-04 18:39:45 +01:00
binding.loginInstance.setText(instance);
binding.loginInstance.setSelection(instance.length());
2020-09-20 15:31:56 +02:00
setTextAgreement();
}
}
}
private void setTextAgreement() {
TextView agreement_text = findViewById(R.id.agreement_text);
String tos = getString(R.string.tos);
String serverrules = getString(R.string.server_rules);
String content_agreement = null;
agreement_text.setMovementMethod(null);
agreement_text.setText(null);
if (BuildConfig.full_instances) {
2020-11-11 10:34:28 +01:00
if (binding.loginInstance.getText() != null) {
2020-09-20 15:31:56 +02:00
content_agreement = getString(R.string.agreement_check_peertube,
2020-11-04 18:39:45 +01:00
"<a href='https://" + binding.loginInstance.getText().toString() + "/about/instance#terms-section' >" + tos + "</a>"
2020-09-20 15:31:56 +02:00
);
2020-09-17 19:01:31 +02:00
}
2020-09-20 15:31:56 +02:00
} else {
content_agreement = getString(R.string.agreement_check,
"<a href='https://apps.education.fr/cgu#peertube' >" + serverrules + "</a>",
"<a href='https://apps.education.fr/bonnes-pratiques/' >" + tos + "</a>"
);
}
agreement_text.setMovementMethod(LinkMovementMethod.getInstance());
if (content_agreement != null) {
agreement_text.setText(Html.fromHtml(content_agreement));
2020-09-17 19:01:31 +02:00
}
}
2020-06-28 19:11:39 +02:00
}