package app.fedilab.fedilabtube; /* 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 . */ import android.os.Bundle; import android.text.Html; import android.text.method.LinkMovementMethod; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import java.util.Arrays; import app.fedilab.fedilabtube.asynctasks.CreatePeertubeAccountAsyncTask; import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.entities.AccountCreation; import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.interfaces.OnPostStatusActionInterface; import es.dmoral.toasty.Toasty; import static android.os.AsyncTask.THREAD_POOL_EXECUTOR; public class PeertubeRegisterActivity extends AppCompatActivity implements OnPostStatusActionInterface { private Button signup; private TextView error_message; private String instance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register_peertube); if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true); signup = findViewById(R.id.signup); EditText username = findViewById(R.id.username); EditText email = findViewById(R.id.email); EditText password = findViewById(R.id.password); EditText password_confirm = findViewById(R.id.password_confirm); CheckBox agreement = findViewById(R.id.agreement); error_message = findViewById(R.id.error_message); signup.setOnClickListener(view -> { error_message.setVisibility(View.GONE); if (username.getText().toString().trim().length() == 0 || email.getText().toString().trim().length() == 0 || password.getText().toString().trim().length() == 0 || password_confirm.getText().toString().trim().length() == 0 || !agreement.isChecked()) { Toasty.error(PeertubeRegisterActivity.this, getString(R.string.all_field_filled)).show(); return; } if (!password.getText().toString().trim().equals(password_confirm.getText().toString().trim())) { Toasty.error(PeertubeRegisterActivity.this, getString(R.string.password_error)).show(); return; } if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email.getText().toString().trim()).matches()) { Toasty.error(PeertubeRegisterActivity.this, getString(R.string.email_error)).show(); return; } String[] emailArray = email.getText().toString().split("@"); if (emailArray.length > 1 && !Arrays.asList(Helper.valideEmails).contains(emailArray[1])) { Toasty.error(PeertubeRegisterActivity.this, getString(R.string.email_error_domain, emailArray[1])).show(); return; } if (password.getText().toString().trim().length() < 8) { Toasty.error(PeertubeRegisterActivity.this, getString(R.string.password_too_short)).show(); return; } if (username.getText().toString().matches("[a-z0-9_]")) { Toasty.error(PeertubeRegisterActivity.this, getString(R.string.username_error)).show(); return; } signup.setEnabled(false); String host = emailArray[1]; instance = Helper.getPeertubeUrl(host); AccountCreation accountCreation = new AccountCreation(); accountCreation.setEmail(email.getText().toString().trim()); accountCreation.setPassword(password.getText().toString().trim()); accountCreation.setPasswordConfirm(password_confirm.getText().toString().trim()); accountCreation.setUsername(username.getText().toString().trim()); new CreatePeertubeAccountAsyncTask(PeertubeRegisterActivity.this, accountCreation, Helper.getPeertubeUrl(instance), PeertubeRegisterActivity.this).executeOnExecutor(THREAD_POOL_EXECUTOR); }); TextView agreement_text = findViewById(R.id.agreement_text); String tos = getString(R.string.tos); String serverrules = getString(R.string.server_rules); String content_agreement = getString(R.string.agreement_check, "" + serverrules + "", "" + tos + "" ); agreement_text.setMovementMethod(LinkMovementMethod.getInstance()); agreement_text.setText(Html.fromHtml(content_agreement)); setTitle(R.string.create_an_account); } @Override protected void onResume() { super.onResume(); } @Override public void onPostStatusAction(APIResponse apiResponse) { 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); } error_message.setText(errorMessage); error_message.setVisibility(View.VISIBLE); signup.setEnabled(true); 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(); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); return true; } return super.onOptionsItemSelected(item); } }