Set base for adding login.
This commit is contained in:
parent
a02d372b90
commit
9fb10281cb
14 changed files with 172 additions and 16 deletions
|
@ -2,6 +2,7 @@ package com.codigoparallevar.minicards;
|
|||
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
|
@ -9,6 +10,7 @@ import android.support.v7.widget.Toolbar;
|
|||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
@ -18,11 +20,18 @@ import java.io.IOException;
|
|||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import com.programaker.api.ProgramakerApi;
|
||||
|
||||
public class DeckPreviewActivity extends ReloadableAppCompatActivity {
|
||||
|
||||
public static final String INTENT = "com.codigoparallevar.minicards.DECK";
|
||||
private ListView listView;
|
||||
private CardPreviewArrayAdapter cardArrayAdapter;
|
||||
private ProgramakerApi ProgramakerApi = new ProgramakerApi();
|
||||
|
||||
protected void openLoginForm(View view) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -39,7 +48,26 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity {
|
|||
}
|
||||
});
|
||||
|
||||
Button loginButton = (Button) findViewById(R.id.login_in_programaker_button);
|
||||
loginButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
DeckPreviewActivity.this.openLoginForm(v);
|
||||
}
|
||||
});
|
||||
|
||||
listView = (ListView) findViewById(R.id.card_deck_list);
|
||||
|
||||
|
||||
new AsyncTask<ProgramakerApi, Void, Boolean>() {
|
||||
protected Boolean doInBackground(ProgramakerApi... api) {
|
||||
return api[0].Check();
|
||||
}
|
||||
|
||||
protected void onPostExecute(Boolean result) {
|
||||
Log.d("MiniCards", "check result: " + result);
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this.ProgramakerApi);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
72
app/src/main/java/com/programaker/api/ProgramakerApi.kt
Normal file
72
app/src/main/java/com/programaker/api/ProgramakerApi.kt
Normal file
|
@ -0,0 +1,72 @@
|
|||
package com.programaker.api
|
||||
|
||||
import android.os.Build
|
||||
import android.util.JsonReader
|
||||
import android.util.Log
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import java.io.*
|
||||
import java.lang.reflect.Type
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import java.net.URLConnection
|
||||
import javax.net.ssl.HttpsURLConnection
|
||||
|
||||
|
||||
class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") {
|
||||
private val LogTag: String = "ProgramakerApi"
|
||||
private val TokenEntry: String = "ProgramakerAuthToken"
|
||||
|
||||
// API
|
||||
fun Check(): Boolean {
|
||||
val conn = URL(getCheckUrl()).openConnection() as HttpURLConnection
|
||||
if (conn == null){
|
||||
Log.e(LogTag, "URL Connection not established, set to NULL")
|
||||
return false
|
||||
}
|
||||
try {
|
||||
addAuthHeader(conn)
|
||||
} catch(ex: TokenNotFoundException) {
|
||||
return false
|
||||
}
|
||||
|
||||
val result: ProgramakerCheckResult
|
||||
try {
|
||||
result = parseJson(conn.inputStream, ProgramakerCheckResult::class.java)
|
||||
} catch(ex: JsonParseException) {
|
||||
ex.logError(LogTag)
|
||||
return false
|
||||
}
|
||||
return result.success
|
||||
}
|
||||
|
||||
// Initialization
|
||||
init {
|
||||
// Disable connection reuse if necessary
|
||||
// HTTP connection reuse which was buggy pre-froyo
|
||||
if (Build.VERSION.SDK.toInt() < Build.VERSION_CODES.FROYO) {
|
||||
System.setProperty("http.keepAlive", "false")
|
||||
}
|
||||
}
|
||||
|
||||
// Private functions
|
||||
private fun getCheckUrl(): String {
|
||||
return "$ApiRoot/v0/ping"
|
||||
}
|
||||
|
||||
private fun addAuthHeader(conn: URLConnection) {
|
||||
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
private fun <T>parseJson(content: InputStream, resultClass: Type): T {
|
||||
val reader = InputStreamReader(content)
|
||||
val gson = Gson()
|
||||
return gson.fromJson(reader, resultClass)
|
||||
}
|
||||
}
|
||||
|
||||
class JsonParseException(private val content: String, private val cls: Type) : Exception() {
|
||||
fun logError(tag: String) {
|
||||
Log.e(tag, "Cannot JSON parse: ${this.content} as ${this.cls}")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.programaker.api
|
||||
|
||||
class ProgramakerCheckResult(val success: Boolean) {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.programaker.api
|
||||
|
||||
open class ProgramakerConfigurationException : Exception() {
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.programaker.api
|
||||
|
||||
class TokenNotFoundException : ProgramakerConfigurationException() {
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue