From f4a992ed4efd165be516e6250d4e6e79c5866cac Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 17 Oct 2013 16:50:12 +0400 Subject: [PATCH] switch commonshareactivity to simpleloginmanager --- src/org/fox/ttrss/ApiRequest.java | 2 +- .../fox/ttrss/share/CommonShareActivity.java | 99 +++++-------------- src/org/fox/ttrss/tasker/TaskerReceiver.java | 5 +- .../fox/ttrss/util/SimpleLoginManager.java | 12 +-- .../fox/ttrss/widget/WidgetUpdateService.java | 2 +- 5 files changed, 36 insertions(+), 84 deletions(-) diff --git a/src/org/fox/ttrss/ApiRequest.java b/src/org/fox/ttrss/ApiRequest.java index ac4b681b..65e97e8e 100644 --- a/src/org/fox/ttrss/ApiRequest.java +++ b/src/org/fox/ttrss/ApiRequest.java @@ -74,7 +74,7 @@ public class ApiRequest extends AsyncTask, Integer, JsonE super.execute(map); } - protected int getErrorMessage() { + public int getErrorMessage() { switch (m_lastError) { case NO_ERROR: return R.string.error_unknown; diff --git a/src/org/fox/ttrss/share/CommonShareActivity.java b/src/org/fox/ttrss/share/CommonShareActivity.java index 984ad152..165d38f7 100644 --- a/src/org/fox/ttrss/share/CommonShareActivity.java +++ b/src/org/fox/ttrss/share/CommonShareActivity.java @@ -5,6 +5,7 @@ import java.util.HashMap; import org.fox.ttrss.ApiRequest; import org.fox.ttrss.PreferencesActivity; import org.fox.ttrss.R; +import org.fox.ttrss.util.SimpleLoginManager; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -54,7 +55,6 @@ public abstract class CommonShareActivity extends CommonActivity { protected abstract void onLoggingIn(int requestId); - @SuppressWarnings({ "serial" }) public void login(int requestId) { if (m_prefs.getString("ttrss_url", "").trim().length() == 0) { @@ -80,85 +80,36 @@ public abstract class CommonShareActivity extends CommonActivity { alert.show(); } else { - - LoginRequest ar = new LoginRequest(getApplicationContext(), requestId); - - HashMap map = new HashMap() { - { - put("op", "login"); - put("user", m_prefs.getString("login", "").trim()); - put("password", m_prefs.getString("password", "").trim()); + + SimpleLoginManager loginManager = new SimpleLoginManager() { + + @Override + protected void onLoginSuccess(int requestId, String sessionId, int apiLevel) { + m_sessionId = sessionId; + m_apiLevel = apiLevel; + + CommonShareActivity.this.onLoggedIn(requestId); + } + + @Override + protected void onLoginFailed(int requestId, ApiRequest ar) { + toast(ar.getErrorMessage()); + setProgressBarIndeterminateVisibility(false); + } + + @Override + protected void onLoggingIn(int requestId) { + CommonShareActivity.this.onLoggingIn(requestId); } }; - - onLoggingIn(requestId); - ar.execute(map); + String login = m_prefs.getString("login", "").trim(); + String password = m_prefs.getString("password", "").trim(); + + loginManager.logIn(this, requestId, login, password); } } - protected class LoginRequest extends ApiRequest { - private int m_requestId; - - public LoginRequest(Context context, int requestId) { - super(context); - m_requestId = requestId; - } - - protected void onPostExecute(JsonElement result) { - if (result != null) { - try { - JsonObject content = result.getAsJsonObject(); - if (content != null) { - m_sessionId = content.get("session_id").getAsString(); - - Log.d(TAG, "Authenticated!"); - - ApiRequest req = new ApiRequest(m_context) { - protected void onPostExecute(JsonElement result) { - m_apiLevel = 0; - - if (result != null) { - try { - m_apiLevel = result.getAsJsonObject() - .get("level").getAsInt(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - Log.d(TAG, "Received API level: " + m_apiLevel); - - onLoggedIn(m_requestId); - } - }; - - @SuppressWarnings("serial") - HashMap map = new HashMap() { - { - put("sid", m_sessionId); - put("op", "getApiLevel"); - } - }; - - req.execute(map); - - return; - } - - } catch (Exception e) { - e.printStackTrace(); - } - } - - m_sessionId = null; - - toast(getErrorMessage()); - setProgressBarIndeterminateVisibility(false); - } - - } - public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.preferences: diff --git a/src/org/fox/ttrss/tasker/TaskerReceiver.java b/src/org/fox/ttrss/tasker/TaskerReceiver.java index b1200b38..6a61e30b 100644 --- a/src/org/fox/ttrss/tasker/TaskerReceiver.java +++ b/src/org/fox/ttrss/tasker/TaskerReceiver.java @@ -1,5 +1,6 @@ package org.fox.ttrss.tasker; +import org.fox.ttrss.ApiRequest; import org.fox.ttrss.CommonActivity; import org.fox.ttrss.OnlineActivity; import org.fox.ttrss.offline.OfflineDownloadService; @@ -41,8 +42,8 @@ public class TaskerReceiver extends BroadcastReceiver { } @Override - protected void onLoginFailed(int requestId) { - Toast toast = Toast.makeText(fContext, "Could not download articles: login failed", Toast.LENGTH_SHORT); + protected void onLoginFailed(int requestId, ApiRequest ar) { + Toast toast = Toast.makeText(fContext, fContext.getString(ar.getErrorMessage()), Toast.LENGTH_SHORT); toast.show(); } diff --git a/src/org/fox/ttrss/util/SimpleLoginManager.java b/src/org/fox/ttrss/util/SimpleLoginManager.java index eb66da7f..072c0062 100644 --- a/src/org/fox/ttrss/util/SimpleLoginManager.java +++ b/src/org/fox/ttrss/util/SimpleLoginManager.java @@ -24,7 +24,7 @@ public abstract class SimpleLoginManager { m_context = context; m_requestId = requestId; } - + protected void onPostExecute(JsonElement result) { Log.d(TAG, "onPostExecute"); @@ -34,7 +34,7 @@ public abstract class SimpleLoginManager { if (content != null) { m_sessionId = content.get("session_id").getAsString(); - Log.d(TAG, "Authenticated!"); + Log.d(TAG, "[SLM] Authenticated!"); ApiRequest req = new ApiRequest(m_context) { protected void onPostExecute(JsonElement result) { @@ -49,7 +49,7 @@ public abstract class SimpleLoginManager { } } - Log.d(TAG, "Received API level: " + m_apiLevel); + Log.d(TAG, "[SLM] Received API level: " + m_apiLevel); onLoginSuccess(m_requestId, m_sessionId, m_apiLevel); } @@ -75,11 +75,11 @@ public abstract class SimpleLoginManager { m_sessionId = null; - onLoginFailed(m_requestId); + onLoginFailed(m_requestId, this); } } - + public void logIn(Context context, int requestId, final String login, final String password) { LoginRequest ar = new LoginRequest(context, requestId); @@ -100,6 +100,6 @@ public abstract class SimpleLoginManager { protected abstract void onLoginSuccess(int requestId, String sessionId, int apiLevel); - protected abstract void onLoginFailed(int requestId); + protected abstract void onLoginFailed(int requestId, ApiRequest ar); } diff --git a/src/org/fox/ttrss/widget/WidgetUpdateService.java b/src/org/fox/ttrss/widget/WidgetUpdateService.java index ce245044..00c8002f 100644 --- a/src/org/fox/ttrss/widget/WidgetUpdateService.java +++ b/src/org/fox/ttrss/widget/WidgetUpdateService.java @@ -110,7 +110,7 @@ public class WidgetUpdateService extends Service { } @Override - protected void onLoginFailed(int requestId) { + protected void onLoginFailed(int requestId, ApiRequest ar) { view.setViewVisibility(R.id.progress, View.GONE); view.setTextViewText(R.id.counter, "?");