diff --git a/src/org/fox/ttrss/util/SimpleLoginManager.java b/src/org/fox/ttrss/util/SimpleLoginManager.java new file mode 100644 index 00000000..eb66da7f --- /dev/null +++ b/src/org/fox/ttrss/util/SimpleLoginManager.java @@ -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 map = new HashMap() { + { + 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 map = new HashMap() { + { + 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); + +}