Commit b9ced08f authored by Adrien Dorsaz's avatar Adrien Dorsaz

Tries to better manage exceptions and login process.

parent 6bf09069
......@@ -18,22 +18,23 @@ public class SettingsActivity extends PreferenceActivity {
public static final String DISPLAY_BEHAVIOUR_PREF = "todisplay_pref";
public static final String USER_SERVER_PREF = "username_server_pref";
public static final String PASSWORD_SERVER_PREF = "password_server_pref";
public static final String WANT_TO_EDIT_PREF = "want_to_edit_pref";
private static boolean mWantToEdit = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(SHARED_PREFERENCES);
getPreferenceManager().setSharedPreferencesMode(Activity.MODE_PRIVATE);
SharedPreferences pref = getSharedPreferences(SHARED_PREFERENCES,
Activity.MODE_PRIVATE);
Log.d("Preferences",
"URL Server : " + pref.getString(URL_SERVER_PREF, ""));
if (!pref.getString(URL_SERVER_PREF, "").equals("") && !pref.getBoolean(WANT_TO_EDIT_PREF, false)) {
if (!pref.getString(URL_SERVER_PREF, "").equals("") && !mWantToEdit){
Intent intent = new Intent(SettingsActivity.this,
ArticleListActivity.class);
startActivity(intent);
......@@ -44,8 +45,16 @@ public class SettingsActivity extends PreferenceActivity {
.edit();
editor.remove(WANT_TO_EDIT_PREF);
editor.commit();
mWantToEdit=false;
addPreferencesFromResource(R.xml.preferences);
}
}
}
\ No newline at end of file
/**
* This method permit to alert this class that we want to edit preferences
* */
public static void setWantToEdit() {
mWantToEdit=true;
}
}
......@@ -12,7 +12,6 @@ import ch.adorsaz.loungeDroid.activities.ArticleListActivity;
import ch.adorsaz.loungeDroid.activities.SettingsActivity;
import ch.adorsaz.loungeDroid.article.Article;
import ch.adorsaz.loungeDroid.article.ToDisplay;
import ch.adorsaz.loungeDroid.exception.AuthenticationFailLoungeException;
import ch.adorsaz.loungeDroid.exception.GetArticleListException;
import ch.adorsaz.loungeDroid.exception.ParseArticleException;
import android.app.Activity;
......@@ -28,7 +27,7 @@ public class ArticleListGetter extends
private ArticleListActivity mActivity = null;
/* Some urls needed to get feeds */
private final static String ARTICLES_PAGE_RSSLOUNGE = "/item/list";
private final static String ARTICLELIST_PAGE_RSSLOUNGE = "/item/list";
private final static String DISPLAY_ALL_PARAMS = "unread=0&starred=0";
private final static String DISPLAY_UNREAD_PARAMS = "unread=1&starred=0";
private final static String DISPLAY_STARRED_PARAMS = "unread=0&starred=1";
......@@ -39,8 +38,8 @@ public class ArticleListGetter extends
@Override
protected void onPreExecute() {
mSessionManager = SessionManager.getInstance(mActivity
.getApplicationContext());
mSessionManager = SessionManager.getInstance(mActivity);
mActivity.setProgressBarVisibility(true);
}
@Override
......@@ -49,14 +48,9 @@ public class ArticleListGetter extends
try {
articles = getArticles(toDisplay[0]);
} catch (AuthenticationFailLoungeException e) {
// TODO Pass to offline mode
Log.e(SessionManager.LOG_DEBUG_LOUNGE,
"Cannot log in. Check your connection. We'll check if we have saved data before.");
articles = null;
} catch (GetArticleListException e) {
// TODO Pass to offline mode
Log.e(SessionManager.LOG_DEBUG_LOUNGE,
Log.e(SessionManager.LOG_SERVER,
"Cannot get article list, are you correctly logged ? Remove cookie and try again. Using saved data if available.");
Editor editor = mActivity.getSharedPreferences(
SettingsActivity.SHARED_PREFERENCES, Activity.MODE_PRIVATE)
......@@ -66,18 +60,17 @@ public class ArticleListGetter extends
articles = null;
} catch (ParseArticleException e) {
// TODO Make Toast
Log.e(SessionManager.LOG_DEBUG_LOUNGE,
Log.e(SessionManager.LOG_SERVER,
"Cannot parse JSON response. Trying to display already parsed articles. Please contact developpers.");
e.printStackTrace();
articles = null;
} catch (ConnectException e) {
// TODO Pass to offline mode
Log.e(SessionManager.LOG_DEBUG_LOUNGE,
"There were an error with network connection. Remove cookie and try again. Using saved data if available.");
Editor editor = mActivity.getSharedPreferences(
SettingsActivity.SHARED_PREFERENCES, Activity.MODE_PRIVATE)
.edit();
editor.remove(SessionManager.SESSION_COOKIE_SETTINGS);
editor.commit();
Log.e(SessionManager.LOG_SERVER,
"There was an error with network connection. Removing cookie to try again later. Using saved data if available.");
setCookiePref(null);
e.printStackTrace();
articles = null;
}
return articles;
......@@ -85,21 +78,22 @@ public class ArticleListGetter extends
@Override
protected void onPostExecute(List<Article> allArticles) {
mActivity.setProgressBarVisibility(false);
if (allArticles != null) {
mActivity.updateArticleList(allArticles);
Log.d("loungeDroid", "Finish to update Activity");
Log.d(SessionManager.LOG_SERVER, "Finish to update Activity");
} else {
Toast.makeText(
mActivity,
"There were errors on log in. Please check your settings and refresh.",
"Unable to get rss feeds. Have you network connection ? Are your settings correct ?",
Toast.LENGTH_LONG).show();
}
}
private List<Article> getArticles(ToDisplay toDisplay)
throws GetArticleListException,
AuthenticationFailLoungeException,
ParseArticleException, ConnectException {
ParseArticleException,
ConnectException {
List<Article> articleList = new LinkedList<Article>();
JSONArray messages = null;
......@@ -116,12 +110,14 @@ public class ArticleListGetter extends
break;
}
JSONObject jsonResponse = mSessionManager.serverRequest(
ARTICLES_PAGE_RSSLOUNGE, httpParams);
ARTICLELIST_PAGE_RSSLOUNGE, httpParams);
try {
messages = jsonResponse.getJSONArray("messages");
} catch (JSONException e) {
throw new GetArticleListException();
} catch (NullPointerException e) {
throw new GetArticleListException();
}
try {
......@@ -151,4 +147,12 @@ public class ArticleListGetter extends
}
return articleList;
}
private void setCookiePref(String cookie) {
Editor editor = mActivity.getSharedPreferences(
SettingsActivity.SHARED_PREFERENCES, Activity.MODE_PRIVATE)
.edit();
editor.putString(SessionManager.SESSION_COOKIE_SETTINGS, cookie);
editor.commit();
}
}
package ch.adorsaz.loungeDroid.servercom;
import java.net.ConnectException;
import org.json.JSONException;
import org.json.JSONObject;
import ch.adorsaz.loungeDroid.activities.ArticleDetailActivity;
import ch.adorsaz.loungeDroid.article.Article;
import ch.adorsaz.loungeDroid.exception.AuthenticationFailLoungeException;
import ch.adorsaz.loungeDroid.exception.ReadStateUpdateException;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
public class ArticleReadStateUpdater extends
AsyncTask<Article, Object, Article> {
......@@ -25,6 +27,7 @@ public class ArticleReadStateUpdater extends
@Override
protected void onPreExecute() {
mActivity.setProgressBarVisibility(true);
mSessionManager = SessionManager.getInstance(mActivity
.getApplicationContext());
}
......@@ -33,14 +36,20 @@ public class ArticleReadStateUpdater extends
protected Article doInBackground(Article... article) {
try {
article[0] = updateArticle(article[0]);
} catch (AuthenticationFailLoungeException e) {
Log.e(SessionManager.LOG_DEBUG_LOUNGE,
"Cannot log in. Check your connection and try again to update the read state of this article.");
article=null;
} catch (ReadStateUpdateException e) {
Log.e(SessionManager.LOG_DEBUG_LOUNGE,
// TODO Manage exception
Log.e(SessionManager.LOG_SERVER,
"Error while updating. Try again later");
article=null;
e.printStackTrace();
article[0] = null;
} catch (ConnectException e) {
// TODO Auto-generated catch block
Log.e(SessionManager.LOG_SERVER,
"There was an error with network connection. Removing cookie to try again later. Using saved data if available.");
e.printStackTrace();
article[0] = null;
}
return article[0];
......@@ -48,14 +57,20 @@ public class ArticleReadStateUpdater extends
@Override
protected void onPostExecute(Article article) {
if(article!=null){
mActivity.setProgressBarVisibility(false);
if (article != null) {
mActivity.updateReadButton();
} else {
Toast.makeText(
mActivity,
"Unable to update rss feed. Have you network connection ? Are your settings correct ?",
Toast.LENGTH_LONG).show();
}
}
private Article updateArticle(Article article)
throws ReadStateUpdateException,
AuthenticationFailLoungeException {
ConnectException {
String httpParams = SessionManager.JSON_GET_RSSLOUNGE + "&"
+ ID_GET_RSSLOUNGE + "=" + article.getId();
JSONObject jsonResponse = mSessionManager.serverRequest(
......@@ -67,6 +82,7 @@ public class ArticleReadStateUpdater extends
throw new ReadStateUpdateException();
} catch (JSONException e) {
article.updateReadState();
e.printStackTrace();
}
return article;
......
package ch.adorsaz.loungeDroid.servercom;
import java.net.ConnectException;
import org.json.JSONException;
import org.json.JSONObject;
import ch.adorsaz.loungeDroid.activities.ArticleDetailActivity;
import ch.adorsaz.loungeDroid.article.Article;
import ch.adorsaz.loungeDroid.exception.AuthenticationFailLoungeException;
import ch.adorsaz.loungeDroid.exception.StarredStateUpdateException;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
public class ArticleStarredStateUpdater extends
AsyncTask<Article, Object, Article> {
......@@ -25,6 +27,7 @@ public class ArticleStarredStateUpdater extends
@Override
protected void onPreExecute() {
mActivity.setProgressBarVisibility(true);
mSessionManager = SessionManager.getInstance(mActivity
.getApplicationContext());
}
......@@ -33,14 +36,19 @@ public class ArticleStarredStateUpdater extends
protected Article doInBackground(Article... article) {
try {
article[0] = updateArticle(article[0]);
} catch (AuthenticationFailLoungeException e) {
Log.e(SessionManager.LOG_DEBUG_LOUNGE,
"Cannot log in. Check your connection and try again to update the read state of this article.");
article=null;
} catch (StarredStateUpdateException e) {
Log.e(SessionManager.LOG_DEBUG_LOUNGE,
Log.e(SessionManager.LOG_SERVER,
"Error while updating starred state. Try again later.");
article=null;
e.printStackTrace();
article[0] = null;
} catch (ConnectException e) {
// TODO Auto-generated catch block
Log.e(SessionManager.LOG_SERVER,
"There was an error with network connection. Removing cookie to try again later. Using saved data if available.");
e.printStackTrace();
article[0] = null;
}
return article[0];
......@@ -48,14 +56,20 @@ public class ArticleStarredStateUpdater extends
@Override
protected void onPostExecute(Article article) {
if(article!=null){
mActivity.setProgressBarVisibility(false);
if (article != null) {
mActivity.updateStarredButton();
} else {
Toast.makeText(
mActivity,
"Unable to update rss feed. Have you network connection ? Are your settings correct ?",
Toast.LENGTH_LONG).show();
}
}
private Article updateArticle(Article article)
throws StarredStateUpdateException,
AuthenticationFailLoungeException {
ConnectException {
String httpParams = SessionManager.JSON_GET_RSSLOUNGE + "&"
+ ID_GET_RSSLOUNGE + "=" + article.getId();
JSONObject jsonResponse = mSessionManager.serverRequest(
......
......@@ -6,6 +6,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
......@@ -15,7 +16,6 @@ import org.json.JSONException;
import org.json.JSONObject;
import ch.adorsaz.loungeDroid.activities.SettingsActivity;
import ch.adorsaz.loungeDroid.exception.AuthenticationFailLoungeException;
import android.app.Activity;
import android.content.Context;
......@@ -38,9 +38,14 @@ public class SessionManager {
private final static String PASSWORD_GET_RSSLOUNGE = "password";
protected final static String JSON_GET_RSSLOUNGE = "json=true";
protected static final String SESSION_COOKIE_NAME = "PHPSESSID";
protected static final String SESSION_COOKIE_SETTINGS = "session_cookie_settings";
protected final static String LOG_DEBUG_LOUNGE = "loungeDroid.server :";
protected final static String LOG_SERVER = "loungeDroid.server";
protected final static String BAD_LOGIN_ERROR = "Authentication failed : bad login and/or password.";
protected final static String MALFORMED_URL = "There's a typo in url. Please check url : ";
protected final static String NO_RESPONSE_SERVER_ERROR = "No server response. Check your settings.";
protected final static String UNEXPECTED_RESPONSE_ERROR = "There was a bug while connecting to server, please report it : ";
public final static SessionManager getInstance(Context context) {
if (mSessionManager == null) {
......@@ -52,7 +57,7 @@ public class SessionManager {
return mSessionManager;
}
private void loginLounge() throws AuthenticationFailLoungeException {
private void loginLounge() throws ConnectException {
try {
String urlParameters = LOGIN_GET_RSSLOUNGE + "="
+ URLEncoder.encode(mLogin, "UTF-8") + "&"
......@@ -64,28 +69,22 @@ public class SessionManager {
urlParameters);
if (jsonResponse != null) {
if (jsonResponse.getBoolean("success") == true) {
Log.d(LOG_DEBUG_LOUNGE, "Logged to the server.");
Log.d(LOG_SERVER, "Logged to the server.");
setCookiePref(mSessionCookie);
} else {
throw new AuthenticationFailLoungeException();
errorDisplayAndSettings(BAD_LOGIN_ERROR);
setCookiePref(null);
throw new ConnectException(BAD_LOGIN_ERROR);
}
} else {
throw new AuthenticationFailLoungeException();
}
} catch (AuthenticationFailLoungeException e) {
if (mSessionCookie == null) {
throw new AuthenticationFailLoungeException();
} else {
mSessionCookie = null;
setCookiePref(null);
loginLounge();
throw new ConnectException(NO_RESPONSE_SERVER_ERROR);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ConnectException(UNEXPECTED_RESPONSE_ERROR
+ " unsupported encoding exception.");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ConnectException(UNEXPECTED_RESPONSE_ERROR
+ " server didn't response success on login.");
}
}
......@@ -130,10 +129,11 @@ public class SessionManager {
}
private synchronized JSONObject doRequest(String pageUrl,
String httpParameters) {
String httpParameters) throws ConnectException {
JSONObject jsonResponse = null;
HttpURLConnection urlConnection = null;
Log.d(LOG_SERVER, "Try to connect with cookie : " + mSessionCookie);
try {
urlConnection = (HttpURLConnection) new URL(mServerUrl + pageUrl)
.openConnection();
......@@ -152,7 +152,13 @@ public class SessionManager {
if (urlConnection.getResponseCode() == 200) {
if (urlConnection.getHeaderField("Set-Cookie") != null) {
mSessionCookie = urlConnection.getHeaderField("Set-Cookie");
String cookie = urlConnection.getHeaderField("Set-Cookie");
if (cookie.startsWith(SESSION_COOKIE_NAME)) {
mSessionCookie = SESSION_COOKIE_NAME
+ cookie.substring(cookie.indexOf('='),
cookie.indexOf(';') + 1);
Log.d(LOG_SERVER, "Received cookie : " + mSessionCookie);
}
}
InputStream responseInput = urlConnection.getInputStream();
......@@ -161,20 +167,15 @@ public class SessionManager {
}
} catch (MalformedURLException e) {
Log.e(LOG_DEBUG_LOUNGE, "Malformed url : " + mServerUrl);
Toast.makeText(mApplicationContext,
"There's a typo in server url.\nPlease check it.",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(mApplicationContext,
SettingsActivity.class);
mApplicationContext.startActivity(intent);
errorDisplayAndSettings(MALFORMED_URL + pageUrl);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ConnectException(UNEXPECTED_RESPONSE_ERROR
+ " input/output exception for page request : " + pageUrl);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ConnectException(
UNEXPECTED_RESPONSE_ERROR
+ " unable to read json response correctly for page request "
+ pageUrl);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
......@@ -182,8 +183,8 @@ public class SessionManager {
}
if (jsonResponse == null) {
Log.e(LOG_DEBUG_LOUNGE,
"jsonResponse is null ! Cannot access page " + pageUrl);
Log.e(LOG_SERVER, "jsonResponse is null ! Cannot access page "
+ pageUrl);
}
return jsonResponse;
}
......@@ -192,12 +193,23 @@ public class SessionManager {
Editor editor = mApplicationContext.getSharedPreferences(
SettingsActivity.SHARED_PREFERENCES, Activity.MODE_PRIVATE)
.edit();
editor.putString(SESSION_COOKIE_SETTINGS, mSessionCookie);
editor.putString(SESSION_COOKIE_SETTINGS, cookie);
mSessionCookie = cookie;
editor.commit();
}
private void errorDisplayAndSettings(String message) {
Log.e(LOG_SERVER, message);
Toast.makeText(mApplicationContext, message, Toast.LENGTH_LONG).show();
SettingsActivity.setWantToEdit();
Intent intent = new Intent(mApplicationContext, SettingsActivity.class);
mApplicationContext.startActivity(intent);
}
protected JSONObject serverRequest(String pageUrl, String httpParameters)
throws AuthenticationFailLoungeException {
throws ConnectException {
if (mSessionCookie == null || mSessionCookie.length() == 0) {
loginLounge();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment