Improves login page (no longer requires webview component)

This commit is contained in:
tom79 2017-05-21 16:57:59 +02:00
parent 0775271098
commit 760a8451ea
16 changed files with 108 additions and 261 deletions

View File

@ -42,10 +42,6 @@
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity android:name="fr.gouv.etalab.mastodon.activities.WebviewActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
/>
<activity android:name="fr.gouv.etalab.mastodon.activities.LoginActivity"
android:windowSoftInputMode="stateAlwaysHidden"
android:configChanges="orientation|screenSize"

View File

@ -15,26 +15,37 @@
package fr.gouv.etalab.mastodon.activities;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONException;
import org.json.JSONObject;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.util.HashMap;
import cz.msebera.android.httpclient.Header;
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
import fr.gouv.etalab.mastodon.client.MastalabSSLSocketFactory;
import fr.gouv.etalab.mastodon.client.OauthClient;
import fr.gouv.etalab.mastodon.helper.Helper;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.USER_AGENT;
/**
* Created by Thomas on 23/04/2017.
@ -64,9 +75,8 @@ public class LoginActivity extends AppCompatActivity {
private void retrievesClientId(){
final Button connectionButton = (Button) findViewById(R.id.login_button);
final Intent webviewIntent = new Intent(this, WebviewActivity.class);
String action = "/api/v1/apps";
HashMap<String,String> parameters = new HashMap<>();
final HashMap<String,String> parameters = new HashMap<>();
parameters.put(Helper.CLIENT_NAME, Helper.OAUTH_REDIRECT_HOST);
parameters.put(Helper.REDIRECT_URIS,"https://" + Helper.INSTANCE + Helper.REDIRECT_CONTENT);
parameters.put(Helper.SCOPES, Helper.OAUTH_SCOPES);
@ -105,8 +115,50 @@ public class LoginActivity extends AppCompatActivity {
connectionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(webviewIntent);
finish();
connectionButton.setEnabled(false);
AsyncHttpClient client = new AsyncHttpClient();
RequestParams requestParams = new RequestParams();
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
requestParams.add(Helper.CLIENT_ID, sharedpreferences.getString(Helper.CLIENT_ID, null));
requestParams.add(Helper.CLIENT_SECRET, sharedpreferences.getString(Helper.CLIENT_SECRET, null));
requestParams.add("grant_type", "password");
EditText login_uid = (EditText) findViewById(R.id.login_uid);
EditText login_passwd = (EditText) findViewById(R.id.login_passwd);
requestParams.add("username",login_uid.getText().toString().trim());
requestParams.add("password",login_passwd.getText().toString().trim());
client.setUserAgent(USER_AGENT);
try {
client.setSSLSocketFactory(new MastalabSSLSocketFactory(MastalabSSLSocketFactory.getKeystore()));
client.post("https://" + Helper.INSTANCE + "/oauth/token", requestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String response = new String(responseBody);
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).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
connectionButton.setEnabled(true);
Toast.makeText(getApplicationContext(),R.string.toast_error_login,Toast.LENGTH_LONG).show();
}
});
} catch (NoSuchAlgorithmException | KeyManagementException | UnrecoverableKeyException | KeyStoreException e) {
e.printStackTrace();
}
}
});
}

View File

@ -1,195 +0,0 @@
/* Copyright 2017 Thomas Schneider
*
* This file is a part of Mastodon Etalab for mastodon.etalab.gouv.fr
*
* 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.
*
* Mastodon Etalab 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 Thomas Schneider; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.activities;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import com.loopj.android.http.AsyncHttpResponseHandler;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import cz.msebera.android.httpclient.Header;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
import fr.gouv.etalab.mastodon.client.OauthClient;
import fr.gouv.etalab.mastodon.helper.Helper;
/**
* Created by Thomas on 24/04/2017.
* Webview to connect accounts
*/
public class WebviewActivity extends AppCompatActivity {
private Activity activity;
private WebView webView;
private Context context;
private AlertDialog alert;
private String clientId, clientSecret;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
this.activity = this;
this.context = this;
this.context = this.getApplicationContext();
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
clientId = sharedpreferences.getString(Helper.CLIENT_ID, null);
clientSecret = sharedpreferences.getString(Helper.CLIENT_SECRET, null);
webView = (WebView) findViewById(R.id.webviewConnect);
clearCookies(getApplicationContext());
final ProgressBar pbar = (ProgressBar) findViewById(R.id.progress_bar);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
if (progress < 100 && pbar.getVisibility() == ProgressBar.GONE) {
pbar.setVisibility(ProgressBar.VISIBLE);
}
pbar.setProgress(progress);
if (progress == 100) {
pbar.setVisibility(ProgressBar.GONE);
}
}
});
webView.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
super.shouldOverrideUrlLoading(view,url);
if( url.contains(Helper.REDIRECT_CONTENT)){
String val[] = url.split("code=");
String code = val[1];
String action = "/oauth/token";
HashMap<String,String> parameters = new HashMap<>();
parameters.put(Helper.CLIENT_ID, clientId);
parameters.put(Helper.CLIENT_SECRET, clientSecret);
parameters.put(Helper.REDIRECT_URI,"https://" + Helper.INSTANCE + Helper.REDIRECT_CONTENT);
parameters.put("grant_type", "authorization_code");
parameters.put("code",code);
new OauthClient().post(action, parameters, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String response = new String(responseBody);
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(WebviewActivity.this, true, token).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
error.printStackTrace();
}
});
return true;
}
return false;
}
});
webView.loadUrl(redirectUserToAuthorizeAndLogin());
}
@Override
public void onBackPressed() {
if (webView != null && webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
private String redirectUserToAuthorizeAndLogin() {
String queryString = Helper.CLIENT_ID + "="+ clientId;
queryString += "&" + Helper.REDIRECT_URI + "="+ Uri.encode("https://" + Helper.INSTANCE + "/redirect_mastodon_api");
queryString += "&" + Helper.RESPONSE_TYPE +"=code";
queryString += "&" + Helper.SCOPE +"=" + Helper.OAUTH_SCOPES;
/*try {
queryString = URLEncoder.encode(queryString, "utf-8");
} catch (UnsupportedEncodingException ignored) {}*/
return "https://" + Helper.INSTANCE + Helper.EP_AUTHORIZE + "?" + queryString;
}
private String getOauthRedirectUri() {
return Helper.OAUTH_SCHEME + "://" + Helper.OAUTH_REDIRECT_HOST + "/";
}
@Override
public void onDestroy() {
super.onDestroy();
if (alert != null) {
alert.dismiss();
alert = null;
}
}
@SuppressWarnings("deprecation")
public static void clearCookies(Context context)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
} else
{
CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
cookieSyncMngr.startSync();
CookieManager cookieManager=CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncMngr.stopSync();
cookieSyncMngr.sync();
}
}
}

View File

@ -20,13 +20,11 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnUpdateAccountInfoInterface;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
@ -39,22 +37,11 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
private Context context;
private String token;
private boolean fromWebview;
private boolean error;
private OnUpdateAccountInfoInterface listener;
public UpdateAccountInfoAsyncTask(Context context, String token, OnUpdateAccountInfoInterface onUpdateAccountInfoInterface){
public UpdateAccountInfoAsyncTask(Context context, String token){
this.context = context;
this.token = token;
this.fromWebview = false;
this.error = false;
this.listener = onUpdateAccountInfoInterface;
}
public UpdateAccountInfoAsyncTask(Context context, boolean fromWebview, String token){
this.context = context;
this.token = token;
this.fromWebview = fromWebview;
}
@ -78,8 +65,6 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
else {
if( account.getUsername() != null && account.getCreated_at() != null)
new AccountDAO(context, db).insertAccount(account);
else //Here the user credential in db doesn't match the remote one (it will be disconnected)
error = true;
}
return null;
}
@ -87,14 +72,10 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
if( fromWebview){
Intent mainActivity = new Intent(context, MainActivity.class);
mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainActivity);
((Activity) context).finish();
}else{
listener.onUpdateAccountInfo(error);
}
Intent mainActivity = new Intent(context, MainActivity.class);
mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainActivity);
((Activity) context).finish();
}

View File

@ -27,7 +27,7 @@ import javax.net.ssl.X509TrustManager;
* https://code.google.com/p/android/issues/detail?id=13117#c14 <p>&nbsp;</p> Warning! This omits SSL
* certificate validation on every device, use with caution
*/
class MastalabSSLSocketFactory extends com.loopj.android.http.MySSLSocketFactory {
public class MastalabSSLSocketFactory extends com.loopj.android.http.MySSLSocketFactory {
private final SSLContext sslContext = SSLContext.getInstance("TLS");
/**
@ -39,7 +39,7 @@ class MastalabSSLSocketFactory extends com.loopj.android.http.MySSLSocketFactory
* @throws KeyStoreException KeyStoreException
* @throws UnrecoverableKeyException UnrecoverableKeyException
*/
MastalabSSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
public MastalabSSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

View File

@ -59,6 +59,7 @@ public class OauthClient {
public void post(String action, HashMap<String, String> paramaters, AsyncHttpResponseHandler responseHandler) {
RequestParams params = hashToRequestParams(paramaters);
try {
client.setConnectTimeout(30000); //30s timeout
client.setUserAgent(USER_AGENT);
client.setSSLSocketFactory(new MastalabSSLSocketFactory(MastalabSSLSocketFactory.getKeystore()));
client.post(getAbsoluteUrl(action), params, responseHandler);

View File

@ -171,19 +171,16 @@ public class NotificationsListAdapter extends BaseAdapter {
//Adds attachment -> disabled, to enable them uncomment the line below
//loadAttachments(status, holder);
holder.notification_status_container.setVisibility(View.VISIBLE);
if( !status.getIn_reply_to_account_id().equals("null") || !status.getIn_reply_to_id().equals("null") ) {
holder.notification_status_content.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ShowConversationActivity.class);
Bundle b = new Bundle();
b.putString("statusId", status.getId()); //Your id
intent.putExtras(b); //Put your id to your next Intent
context.startActivity(intent);
}
});
}
holder.notification_status_content.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ShowConversationActivity.class);
Bundle b = new Bundle();
b.putString("statusId", status.getId());
intent.putExtras(b);
context.startActivity(intent);
}
});
switch (status.getVisibility()){
case "public":
holder.status_privacy.setImageResource(R.drawable.ic_action_globe);
@ -216,8 +213,8 @@ public class NotificationsListAdapter extends BaseAdapter {
public void onClick(View v) {
Intent intent = new Intent(context, TootActivity.class);
Bundle b = new Bundle();
b.putString("inReplyTo", notification.getStatus().getId()); //Your id
intent.putExtras(b); //Put your id to your next Intent
b.putString("inReplyTo", notification.getStatus().getId());
intent.putExtras(b);
context.startActivity(intent);
}
});

View File

@ -142,7 +142,7 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe
title = getContext().getResources().getString(R.string.notif_pouet, status.getAccount().getDisplay_name());
}catch (Exception e){
icon_notification = BitmapFactory.decodeResource(getContext().getResources(),
R.drawable.mastodon_logo);
R.drawable.mastodonlogo);
}
}
}

View File

@ -199,7 +199,7 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
icon_notification = imageLoaderNoty.loadImageSync(notificationUrl);
}catch (Exception e){
icon_notification = BitmapFactory.decodeResource(getContext().getResources(),
R.drawable.mastodon_logo);
R.drawable.mastodonlogo);
}
}
}

View File

@ -23,6 +23,6 @@
<item>
<bitmap
android:gravity="center"
android:src="@drawable/mastodon_logo"/>
android:src="@drawable/mastodonlogo"/>
</item>
</layer-list>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

View File

@ -24,25 +24,41 @@
android:orientation="vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="100dp"
android:layout_gravity="center_horizontal"
android:padding="20dp"
android:layout_weight="2"
android:src="@drawable/mastodon_logo"
android:layout_height="0dp"
android:src="@drawable/mastodonlogo"
android:layout_height="100dp"
tools:ignore="ContentDescription" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_margin="50dp"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:layout_height="wrap_content">
<EditText
android:id="@+id/login_uid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/email"
/>
<EditText
android:id = "@+id/login_passwd"
android:inputType="textPassword"
android:hint="@string/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/login_button"
android:textAllCaps="false"
android:drawableLeft="@drawable/mastodon_icon"
android:drawableStart="@drawable/mastodon_icon"
android:gravity="center"
android:layout_marginTop="20dp"
android:drawablePadding="10dp"
android:paddingLeft="15dp"
android:paddingStart="15dp"

View File

@ -92,7 +92,6 @@
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="@drawable/mastodon_logo"
android:layout_height="match_parent"
tools:ignore="ContentDescription" />
<LinearLayout
@ -109,7 +108,6 @@
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_height="0dp"
android:src="@drawable/mastodon_logo"
tools:ignore="ContentDescription" />
<LinearLayout
android:layout_weight="1"
@ -123,7 +121,6 @@
android:layout_weight="1"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:src="@drawable/mastodon_logo"
android:layout_height="wrap_content"
tools:ignore="ContentDescription" />
<ImageView
@ -133,7 +130,6 @@
android:layout_weight="1"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:src="@drawable/mastodon_logo"
android:layout_height="wrap_content"
tools:ignore="ContentDescription" />
</LinearLayout>

View File

@ -17,6 +17,8 @@
<string name="cancel">Annuler</string>
<string name="download">Télécharger</string>
<string name="download_file">Télécharger %1$s</string>
<string name="password">Mot de passe</string>
<string name="email">Email</string>
<!--- Menu -->
<string name="home_menu">Accueil</string>
<string name="home_timeline">Accueil</string>
@ -170,6 +172,7 @@
<string name="toast_unstatus">Le pouet a été supprimé !</string>
<string name="toast_error">Oups ! Une erreur s\'est produite !</string>
<string name="toast_error_loading_account">Une erreur s\'est produite en chargeant le compte !</string>
<string name="toast_error_login">Impossible de vous connecter !</string>
<!-- Settings -->
<string name="settings_title_optimisation">Optimisation du chargement</string>
<string name="set_toots_page">Nombre de pouets par chargement</string>

View File

@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:2.3.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files