add simpleloginmanager

This commit is contained in:
Andrew Dolgov 2013-10-17 16:22:19 +04:00
parent 8b37c8cc2c
commit 0ebcf48603

View File

@ -0,0 +1,105 @@
package org.fox.ttrss.util;
import java.util.HashMap;
import org.fox.ttrss.ApiRequest;
import android.content.Context;
import android.util.Log;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public abstract class SimpleLoginManager {
private final String TAG = this.getClass().getSimpleName();
protected class LoginRequest extends ApiRequest {
private int m_requestId;
protected String m_sessionId;
protected int m_apiLevel;
protected Context m_context;
public LoginRequest(Context context, int requestId) {
super(context);
m_context = context;
m_requestId = requestId;
}
protected void onPostExecute(JsonElement result) {
Log.d(TAG, "onPostExecute");
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);
onLoginSuccess(m_requestId, m_sessionId, m_apiLevel);
}
};
@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;
onLoginFailed(m_requestId);
}
}
public void logIn(Context context, int requestId, final String login, final String password) {
LoginRequest ar = new LoginRequest(context, requestId);
HashMap<String, String> map = new HashMap<String, String>() {
{
put("op", "login");
put("user", login.trim());
put("password", password.trim());
}
};
onLoggingIn(requestId);
ar.execute(map);
}
protected abstract void onLoggingIn(int requestId);
protected abstract void onLoginSuccess(int requestId, String sessionId, int apiLevel);
protected abstract void onLoginFailed(int requestId);
}