Fix instance registration

This commit is contained in:
tom79 2019-06-15 12:49:38 +02:00
parent 7bb183fa25
commit c41543adbf
7 changed files with 70 additions and 20 deletions

View File

@ -49,6 +49,8 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.jaredrummler.materialspinner.MaterialSpinner;
import org.json.JSONObject;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
@ -81,6 +83,7 @@ public class MastodonRegisterActivity extends BaseActivity implements OnRetrieve
private Button signup;
private String instance;
private TextView error_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -179,14 +182,16 @@ public class MastodonRegisterActivity extends BaseActivity implements OnRetrieve
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(MastodonRegisterActivity.this, getString(R.string.all_field_filled)).show();
return;
}
if(!password.getText().toString().trim().equals(password_confirm.toString().trim())){
if(!password.getText().toString().trim().equals(password_confirm.getText().toString().trim())){
Toasty.error(MastodonRegisterActivity.this, getString(R.string.password_error)).show();
return;
}
@ -208,7 +213,7 @@ public class MastodonRegisterActivity extends BaseActivity implements OnRetrieve
accountCreation.setPassword(password.getText().toString().trim());
accountCreation.setPasswordConfirm(password_confirm.getText().toString().trim());
accountCreation.setUsername(username.getText().toString().trim());
new CreateMastodonAccountAsyncTask(MastodonRegisterActivity.this, accountCreation, MastodonRegisterActivity.this).executeOnExecutor(THREAD_POOL_EXECUTOR);
new CreateMastodonAccountAsyncTask(MastodonRegisterActivity.this, accountCreation, instance, MastodonRegisterActivity.this).executeOnExecutor(THREAD_POOL_EXECUTOR);
});
@ -311,11 +316,24 @@ public class MastodonRegisterActivity extends BaseActivity implements OnRetrieve
@Override
public void onPostStatusAction(APIResponse apiResponse) {
if( apiResponse.getError() != null){
String errorMessage;
if( apiResponse.getError().getError() != null){
Toasty.error(MastodonRegisterActivity.this,apiResponse.getError().getError()).show();
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 {
Toasty.error(MastodonRegisterActivity.this,getString(R.string.toast_error)).show();
errorMessage = getString(R.string.toast_error);
}
error_message.setText(errorMessage);
error_message.setVisibility(View.VISIBLE);
signup.setEnabled(true);
return;
}
@ -330,9 +348,6 @@ public class MastodonRegisterActivity extends BaseActivity implements OnRetrieve
style = R.style.Dialog;
}
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MastodonRegisterActivity.this, style);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setTitle(getString(R.string.account_created));
alertDialog.setMessage(getString(R.string.account_created_message, this.instance));
dialogBuilder.setCancelable(false);
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
@ -341,6 +356,9 @@ public class MastodonRegisterActivity extends BaseActivity implements OnRetrieve
finish();
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setTitle(getString(R.string.account_created));
alertDialog.setMessage(getString(R.string.account_created_message, this.instance));
alertDialog.show();
}

View File

@ -51,7 +51,6 @@ import android.text.Html;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.Menu;

View File

@ -36,17 +36,18 @@ public class CreateMastodonAccountAsyncTask extends AsyncTask<Void, Void, Void>
private app.fedilab.android.client.Entities.Status status;
private AccountCreation accountCreation;
private WeakReference<Context> contextReference;
private String instance;
public CreateMastodonAccountAsyncTask(Context context, AccountCreation accountCreation, OnPostStatusActionInterface onPostStatusActionInterface){
public CreateMastodonAccountAsyncTask(Context context, AccountCreation accountCreation, String instance, OnPostStatusActionInterface onPostStatusActionInterface){
this.contextReference = new WeakReference<>(context);
this.listener = onPostStatusActionInterface;
this.accountCreation = accountCreation;
this.instance = instance;
}
@Override
protected Void doInBackground(Void... params) {
apiResponse = new API(contextReference.get()).createAccount(accountCreation);
apiResponse = new API(contextReference.get(), instance, null).createAccount(accountCreation);
return null;
}

View File

@ -16,7 +16,6 @@ package app.fedilab.android.asynctasks;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import java.lang.ref.WeakReference;

View File

@ -19,6 +19,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.google.gson.JsonArray;
@ -45,6 +46,7 @@ import java.util.Locale;
import java.util.Map;
import app.fedilab.android.R;
import app.fedilab.android.activities.LoginActivity;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.asynctasks.RetrieveOpenCollectiveAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
@ -532,14 +534,33 @@ public class API {
public APIResponse createAccount(AccountCreation accountCreation){
apiResponse = new APIResponse();
HashMap<String, String> params = new HashMap<>();
params.put("username", accountCreation.getUsername());
params.put("email", accountCreation.getEmail());
params.put("password", accountCreation.getPassword());
params.put("agreement", "true");
params.put("locale", Locale.getDefault().getLanguage());
try {
new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/accounts"), 60, params, null);
HashMap<String, String> params = new HashMap<>();
params.put(Helper.CLIENT_NAME, Helper.CLIENT_NAME_VALUE);
params.put(Helper.REDIRECT_URIS, Helper.REDIRECT_CONTENT);
params.put(Helper.SCOPES, Helper.OAUTH_SCOPES);
params.put(Helper.WEBSITE, Helper.WEBSITE_VALUE);
String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/apps"), 30, params, null);
JSONObject resobj = new JSONObject(response);
String client_id = resobj.getString("client_id");
String client_secret = resobj.getString("client_secret");
params = new HashMap<>();
params.put("grant_type", "client_credentials");
params.put("client_id", client_id);
params.put("client_secret", client_secret);
params.put("scope", "read write");
response = new HttpsConnection(context, this.instance).post("https://" + this.instance + "/oauth/token", 30, params, null);
JSONObject res = new JSONObject(response);
String app_token = res.getString("access_token");
params = new HashMap<>();
params.put("username", accountCreation.getUsername());
params.put("email", accountCreation.getEmail());
params.put("password", accountCreation.getPassword());
params.put("agreement", "true");
params.put("locale", Locale.getDefault().getLanguage());
new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/accounts"), 60, params, app_token);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
@ -549,6 +570,8 @@ public class API {
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return apiResponse;
}

View File

@ -15,7 +15,6 @@ package app.fedilab.android.drawers;
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

View File

@ -83,6 +83,17 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:visibility="gone"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="10dp"
android:padding="5dp"
android:id="@+id/error_message"
android:textColor="@color/red_1"
android:background="@drawable/red_border"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="20dp"
android:labelFor="@+id/username"