diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7915a1317..1ba554438 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -104,7 +104,14 @@ android:windowSoftInputMode="stateAlwaysHidden" android:configChanges="orientation|screenSize" android:label="@string/app_name" - /> + > + + + + + + + parameters = new HashMap<>(); + parameters.put(Helper.CLIENT_ID, client_id); + parameters.put(Helper.CLIENT_SECRET, client_secret); + parameters.put(Helper.REDIRECT_URI,Helper.REDIRECT_CONTENT_WEB); + parameters.put("grant_type", "authorization_code"); + parameters.put("code",code); + new Thread(new Runnable(){ + @Override + public void run() { + try { + final String response = new HttpsConnection(LoginActivity.this).post(Helper.instanceWithProtocol(instance) + action, 30, parameters, null); + JSONObject resobj; + try { + resobj = new JSONObject(response); + String token = resobj.get("access_token").toString(); + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token); + editor.apply(); + //Update the account with the token; + new UpdateAccountInfoAsyncTask(LoginActivity.this, token, instance).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } catch (JSONException ignored) {} + } catch (Exception ignored) {} + }}).start(); }else { - setTheme(R.style.AppThemeDark); - } - setContentView(R.layout.activity_login); - - if( theme == Helper.THEME_DARK) { - changeDrawableColor(getApplicationContext(), R.drawable.mastodon_icon, R.color.mastodonC2); - }else { - changeDrawableColor(getApplicationContext(), R.drawable.mastodon_icon, R.color.mastodonC3); - } - final Button connectionButton = findViewById(R.id.login_button); - login_instance = findViewById(R.id.login_instance); - login_uid = findViewById(R.id.login_uid); - login_passwd = findViewById(R.id.login_passwd); - - - if( theme == Helper.THEME_LIGHT) { - connectionButton.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white)); - } - login_instance.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) {} - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); + int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); + if (theme == Helper.THEME_LIGHT) { + setTheme(R.style.AppTheme); + } else { + setTheme(R.style.AppThemeDark); } - @Override - public void afterTextChanged(Editable s) { - if( s.length() > 2 && !isLoadingInstance){ - final String action = "/instances/search"; - final HashMap parameters = new HashMap<>(); - parameters.put("q", s.toString().trim()); - parameters.put("count", String.valueOf(5)); - parameters.put("name", String.valueOf(true)); - isLoadingInstance = true; - new Thread(new Runnable(){ - @Override - public void run() { - try { - final String response = new HttpsConnection(LoginActivity.this).get("https://instances.social/api/1.0" + action, 30, parameters, Helper.THEKINRAR_SECRET_TOKEN ); - runOnUiThread(new Runnable() { - public void run() { - isLoadingInstance = false; - String[] instances; - try { - JSONObject jsonObject = new JSONObject(response); - JSONArray jsonArray = jsonObject.getJSONArray("instances"); - if( jsonArray != null){ - instances = new String[jsonArray.length()]; - for(int i = 0 ; i < jsonArray.length() ; i++){ - instances[i] = jsonArray.getJSONObject(i).get("name").toString(); + setContentView(R.layout.activity_login); + + if (theme == Helper.THEME_DARK) { + changeDrawableColor(getApplicationContext(), R.drawable.mastodon_icon, R.color.mastodonC2); + } else { + changeDrawableColor(getApplicationContext(), R.drawable.mastodon_icon, R.color.mastodonC3); + } + final Button connectionButton = findViewById(R.id.login_button); + login_instance = findViewById(R.id.login_instance); + login_uid = findViewById(R.id.login_uid); + login_passwd = findViewById(R.id.login_passwd); + + + if (theme == Helper.THEME_LIGHT) { + connectionButton.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white)); + } + login_instance.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + + @Override + public void afterTextChanged(Editable s) { + if (s.length() > 2 && !isLoadingInstance) { + final String action = "/instances/search"; + final HashMap parameters = new HashMap<>(); + parameters.put("q", s.toString().trim()); + parameters.put("count", String.valueOf(5)); + parameters.put("name", String.valueOf(true)); + isLoadingInstance = true; + new Thread(new Runnable() { + @Override + public void run() { + try { + final String response = new HttpsConnection(LoginActivity.this).get("https://instances.social/api/1.0" + action, 30, parameters, Helper.THEKINRAR_SECRET_TOKEN); + runOnUiThread(new Runnable() { + public void run() { + isLoadingInstance = false; + String[] instances; + try { + JSONObject jsonObject = new JSONObject(response); + JSONArray jsonArray = jsonObject.getJSONArray("instances"); + if (jsonArray != null) { + instances = new String[jsonArray.length()]; + for (int i = 0; i < jsonArray.length(); i++) { + instances[i] = jsonArray.getJSONObject(i).get("name").toString(); + } + } else { + instances = new String[]{}; } - }else { - instances = new String[]{}; + login_instance.setAdapter(null); + ArrayAdapter adapter = + new ArrayAdapter<>(LoginActivity.this, android.R.layout.simple_list_item_1, instances); + login_instance.setAdapter(adapter); + if (login_instance.hasFocus() && !LoginActivity.this.isFinishing()) + login_instance.showDropDown(); + + } catch (JSONException ignored) { + isLoadingInstance = false; } - login_instance.setAdapter(null); - ArrayAdapter adapter = - new ArrayAdapter<>(LoginActivity.this, android.R.layout.simple_list_item_1, instances); - login_instance.setAdapter(adapter); - if( login_instance.hasFocus() && !LoginActivity.this.isFinishing()) - login_instance.showDropDown(); + } + }); - } catch (JSONException ignored) {isLoadingInstance = false;} - } - }); - - } catch (HttpsConnection.HttpsConnectionException e) { - isLoadingInstance = false; - } catch (Exception e) { - isLoadingInstance = false; + } catch (HttpsConnection.HttpsConnectionException e) { + isLoadingInstance = false; + } catch (Exception e) { + isLoadingInstance = false; + } } - } - }).start(); + }).start(); + } } - } - }); + }); - connectionButton.setEnabled(false); - login_two_step = findViewById(R.id.login_two_step); - login_two_step.setVisibility(View.GONE); - login_two_step.setPaintFlags(login_two_step.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); - login_two_step.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - client_id_for_webview = true; - retrievesClientId(); - } - }); - - login_instance.setOnFocusChangeListener(new View.OnFocusChangeListener() { - @Override - public void onFocusChange(View v, boolean hasFocus) { - connectionButton.setEnabled(false); - login_two_step.setVisibility(View.INVISIBLE); - TextInputLayout login_instance_layout = findViewById(R.id.login_instance_layout); - if (!hasFocus) { + connectionButton.setEnabled(false); + login_two_step = findViewById(R.id.login_two_step); + login_two_step.setVisibility(View.GONE); + login_two_step.setPaintFlags(login_two_step.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + login_two_step.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + client_id_for_webview = true; retrievesClientId(); - if(login_instance.getText() == null || login_instance.getText().toString().length() == 0 ) { - login_instance_layout.setError(getString(R.string.toast_error_instance)); - login_instance_layout.setErrorEnabled(true); - } - }else{ - login_instance_layout.setErrorEnabled(false); } - } - }); + }); - - final TextView login_issue = findViewById(R.id.login_issue); - SpannableString content = new SpannableString(getString(R.string.issue_login_title)); - content.setSpan(new UnderlineSpan(), 0, content.length(), 0); - login_issue.setText(content); - login_issue.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this); - builder.setTitle(R.string.issue_login_title); - TextView message = new TextView(LoginActivity.this); - final SpannableString s = - new SpannableString(getText(R.string.issue_login_message)); - Linkify.addLinks(s, Linkify.WEB_URLS); - message.setText(s); - message.setPadding((int) convertDpToPixel(10,LoginActivity.this), (int) convertDpToPixel(10,LoginActivity.this), (int) convertDpToPixel(10,LoginActivity.this), (int) convertDpToPixel(10,LoginActivity.this)); - message.setMovementMethod(LinkMovementMethod.getInstance()); - builder.setView(message); - builder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); + login_instance.setOnFocusChangeListener(new View.OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + connectionButton.setEnabled(false); + login_two_step.setVisibility(View.INVISIBLE); + TextInputLayout login_instance_layout = findViewById(R.id.login_instance_layout); + if (!hasFocus) { + retrievesClientId(); + if (login_instance.getText() == null || login_instance.getText().toString().length() == 0) { + login_instance_layout.setError(getString(R.string.toast_error_instance)); + login_instance_layout.setErrorEnabled(true); + } + } else { + login_instance_layout.setErrorEnabled(false); } - }); - builder.setIcon(android.R.drawable.ic_dialog_alert).show(); - } - }); - } + } + }); + final TextView login_issue = findViewById(R.id.login_issue); + SpannableString content = new SpannableString(getString(R.string.issue_login_title)); + content.setSpan(new UnderlineSpan(), 0, content.length(), 0); + login_issue.setText(content); + login_issue.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this); + builder.setTitle(R.string.issue_login_title); + TextView message = new TextView(LoginActivity.this); + final SpannableString s = + new SpannableString(getText(R.string.issue_login_message)); + Linkify.addLinks(s, Linkify.WEB_URLS); + message.setText(s); + message.setPadding((int) convertDpToPixel(10, LoginActivity.this), (int) convertDpToPixel(10, LoginActivity.this), (int) convertDpToPixel(10, LoginActivity.this), (int) convertDpToPixel(10, LoginActivity.this)); + message.setMovementMethod(LinkMovementMethod.getInstance()); + builder.setView(message); + builder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + builder.setIcon(android.R.drawable.ic_dialog_alert).show(); + } + }); + } + } + @Override protected void onResume(){ super.onResume(); Button connectionButton = findViewById(R.id.login_button); - if (login_instance.getText() != null && login_instance.getText().toString().length() > 0 && client_id_for_webview) { + if (login_instance != null &&login_instance.getText() != null && login_instance.getText().toString().length() > 0 && client_id_for_webview) { connectionButton.setEnabled(false); client_id_for_webview = false; retrievesClientId(); @@ -270,14 +302,19 @@ public class LoginActivity extends BaseActivity { connectionButton.setEnabled(true); login_two_step.setVisibility(View.VISIBLE); if( client_id_for_webview){ - Intent i = new Intent(LoginActivity.this, WebviewConnectActivity.class); - i.putExtra("instance", instance); - startActivity(i); + boolean embedded_browser = sharedpreferences.getBoolean(Helper.SET_EMBEDDED_BROWSER, true); + if( embedded_browser) { + Intent i = new Intent(LoginActivity.this, WebviewConnectActivity.class); + i.putExtra("instance", instance); + startActivity(i); + }else{ + String url = redirectUserToAuthorizeAndLogin(client_id, instance); + Helper.openBrowser(getApplicationContext(), url); + } } } catch (JSONException ignored) {ignored.printStackTrace();} } }); - } catch (final Exception e) { e.printStackTrace(); runOnUiThread(new Runnable() { @@ -365,7 +402,6 @@ public class LoginActivity extends BaseActivity { } } }).start(); - } }); } @@ -375,6 +411,10 @@ public class LoginActivity extends BaseActivity { public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main_login, menu); + CheckBox checkBox= (CheckBox) menu.findItem(R.id.action_custom_tabs).getActionView(); + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); + boolean embedded_browser = sharedpreferences.getBoolean(Helper.SET_EMBEDDED_BROWSER, true); + checkBox.setChecked(!embedded_browser); return true; } @@ -395,7 +435,23 @@ public class LoginActivity extends BaseActivity { }else if(id == R.id.action_proxy){ Intent intent = new Intent(getApplicationContext(), ProxyActivity.class); startActivity(intent); + }else if(id == R.id.action_custom_tabs){ + item.setChecked(!item.isChecked()); + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putBoolean(Helper.SET_EMBEDDED_BROWSER, !item.isChecked()); + editor.apply(); + return false; } return super.onOptionsItemSelected(item); } + + + public static String redirectUserToAuthorizeAndLogin(String clientId, String instance) { + String queryString = Helper.CLIENT_ID + "="+ clientId; + queryString += "&" + Helper.REDIRECT_URI + "="+ Uri.encode(Helper.REDIRECT_CONTENT_WEB); + queryString += "&" + Helper.RESPONSE_TYPE +"=code"; + queryString += "&" + Helper.SCOPE +"=" + Helper.OAUTH_SCOPES; + return Helper.instanceWithProtocol(instance) + Helper.EP_AUTHORIZE + "?" + queryString; + } } \ No newline at end of file diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewConnectActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewConnectActivity.java index 35864e9cd..bed1aba9a 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewConnectActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewConnectActivity.java @@ -139,7 +139,7 @@ public class WebviewConnectActivity extends BaseActivity { } }); - webView.loadUrl(redirectUserToAuthorizeAndLogin()); + webView.loadUrl(LoginActivity.redirectUserToAuthorizeAndLogin(clientId, instance)); } @@ -154,15 +154,6 @@ public class WebviewConnectActivity extends BaseActivity { - private String redirectUserToAuthorizeAndLogin() { - - String queryString = Helper.CLIENT_ID + "="+ clientId; - queryString += "&" + Helper.REDIRECT_URI + "="+ Uri.encode(Helper.REDIRECT_CONTENT_WEB); - queryString += "&" + Helper.RESPONSE_TYPE +"=code"; - queryString += "&" + Helper.SCOPE +"=" + Helper.OAUTH_SCOPES; - return Helper.instanceWithProtocol(instance) + Helper.EP_AUTHORIZE + "?" + queryString; - } - @Override public void onDestroy() { diff --git a/app/src/main/res/menu/main_login.xml b/app/src/main/res/menu/main_login.xml index 931cafe33..e837816e3 100644 --- a/app/src/main/res/menu/main_login.xml +++ b/app/src/main/res/menu/main_login.xml @@ -13,4 +13,8 @@ android:id="@+id/action_proxy" android:title="@string/proxy_set" app:showAsAction="never" /> +