switch commonshareactivity to simpleloginmanager

This commit is contained in:
Andrew Dolgov 2013-10-17 16:50:12 +04:00
parent c79080545c
commit f4a992ed4e
5 changed files with 36 additions and 84 deletions

View File

@ -74,7 +74,7 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
super.execute(map); super.execute(map);
} }
protected int getErrorMessage() { public int getErrorMessage() {
switch (m_lastError) { switch (m_lastError) {
case NO_ERROR: case NO_ERROR:
return R.string.error_unknown; return R.string.error_unknown;

View File

@ -5,6 +5,7 @@ import java.util.HashMap;
import org.fox.ttrss.ApiRequest; import org.fox.ttrss.ApiRequest;
import org.fox.ttrss.PreferencesActivity; import org.fox.ttrss.PreferencesActivity;
import org.fox.ttrss.R; import org.fox.ttrss.R;
import org.fox.ttrss.util.SimpleLoginManager;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -54,7 +55,6 @@ public abstract class CommonShareActivity extends CommonActivity {
protected abstract void onLoggingIn(int requestId); protected abstract void onLoggingIn(int requestId);
@SuppressWarnings({ "serial" })
public void login(int requestId) { public void login(int requestId) {
if (m_prefs.getString("ttrss_url", "").trim().length() == 0) { if (m_prefs.getString("ttrss_url", "").trim().length() == 0) {
@ -80,85 +80,36 @@ public abstract class CommonShareActivity extends CommonActivity {
alert.show(); alert.show();
} else { } else {
LoginRequest ar = new LoginRequest(getApplicationContext(), requestId); SimpleLoginManager loginManager = new SimpleLoginManager() {
HashMap<String, String> map = new HashMap<String, String>() { @Override
{ protected void onLoginSuccess(int requestId, String sessionId, int apiLevel) {
put("op", "login"); m_sessionId = sessionId;
put("user", m_prefs.getString("login", "").trim()); m_apiLevel = apiLevel;
put("password", m_prefs.getString("password", "").trim());
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<String, String> map = new HashMap<String, String>() {
{
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) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.preferences: case R.id.preferences:

View File

@ -1,5 +1,6 @@
package org.fox.ttrss.tasker; package org.fox.ttrss.tasker;
import org.fox.ttrss.ApiRequest;
import org.fox.ttrss.CommonActivity; import org.fox.ttrss.CommonActivity;
import org.fox.ttrss.OnlineActivity; import org.fox.ttrss.OnlineActivity;
import org.fox.ttrss.offline.OfflineDownloadService; import org.fox.ttrss.offline.OfflineDownloadService;
@ -41,8 +42,8 @@ public class TaskerReceiver extends BroadcastReceiver {
} }
@Override @Override
protected void onLoginFailed(int requestId) { protected void onLoginFailed(int requestId, ApiRequest ar) {
Toast toast = Toast.makeText(fContext, "Could not download articles: login failed", Toast.LENGTH_SHORT); Toast toast = Toast.makeText(fContext, fContext.getString(ar.getErrorMessage()), Toast.LENGTH_SHORT);
toast.show(); toast.show();
} }

View File

@ -24,7 +24,7 @@ public abstract class SimpleLoginManager {
m_context = context; m_context = context;
m_requestId = requestId; m_requestId = requestId;
} }
protected void onPostExecute(JsonElement result) { protected void onPostExecute(JsonElement result) {
Log.d(TAG, "onPostExecute"); Log.d(TAG, "onPostExecute");
@ -34,7 +34,7 @@ public abstract class SimpleLoginManager {
if (content != null) { if (content != null) {
m_sessionId = content.get("session_id").getAsString(); m_sessionId = content.get("session_id").getAsString();
Log.d(TAG, "Authenticated!"); Log.d(TAG, "[SLM] Authenticated!");
ApiRequest req = new ApiRequest(m_context) { ApiRequest req = new ApiRequest(m_context) {
protected void onPostExecute(JsonElement result) { 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); onLoginSuccess(m_requestId, m_sessionId, m_apiLevel);
} }
@ -75,11 +75,11 @@ public abstract class SimpleLoginManager {
m_sessionId = null; m_sessionId = null;
onLoginFailed(m_requestId); onLoginFailed(m_requestId, this);
} }
} }
public void logIn(Context context, int requestId, final String login, final String password) { public void logIn(Context context, int requestId, final String login, final String password) {
LoginRequest ar = new LoginRequest(context, requestId); 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 onLoginSuccess(int requestId, String sessionId, int apiLevel);
protected abstract void onLoginFailed(int requestId); protected abstract void onLoginFailed(int requestId, ApiRequest ar);
} }

View File

@ -110,7 +110,7 @@ public class WidgetUpdateService extends Service {
} }
@Override @Override
protected void onLoginFailed(int requestId) { protected void onLoginFailed(int requestId, ApiRequest ar) {
view.setViewVisibility(R.id.progress, View.GONE); view.setViewVisibility(R.id.progress, View.GONE);
view.setTextViewText(R.id.counter, "?"); view.setTextViewText(R.id.counter, "?");