From 9fb10281cb24e6f4d32b920afcb3d275aae6e330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Mon, 2 Mar 2020 21:01:11 +0100 Subject: [PATCH] Set base for adding login. --- app/build.gradle | 13 +++- app/src/main/AndroidManifest.xml | 10 ++- .../minicards/DeckPreviewActivity.java | 28 +++++++ .../com/programaker/api/ProgramakerApi.kt | 72 ++++++++++++++++++ .../programaker/api/ProgramakerCheckResult.kt | 5 ++ .../api/ProgramakerConfigurationException.kt | 5 ++ .../programaker/api/TokenNotFoundException.kt | 4 + app/src/main/res/layout/card_preview.xml | 14 ++-- .../main/res/layout/content_deck_preview.xml | 16 +++- app/src/main/res/values/dimens.xml | 3 + app/src/main/res/values/strings.xml | 10 +++ .../res/orig => asset-src}/ic_launcher.xcf | Bin .../orig => asset-src}/ic_launcher_round.xcf | Bin build.gradle | 8 +- 14 files changed, 172 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/com/programaker/api/ProgramakerApi.kt create mode 100644 app/src/main/java/com/programaker/api/ProgramakerCheckResult.kt create mode 100644 app/src/main/java/com/programaker/api/ProgramakerConfigurationException.kt create mode 100644 app/src/main/java/com/programaker/api/TokenNotFoundException.kt rename {app/src/main/res/orig => asset-src}/ic_launcher.xcf (100%) rename {app/src/main/res/orig => asset-src}/ic_launcher_round.xcf (100%) diff --git a/app/build.gradle b/app/build.gradle index 5f303e0..118e307 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,4 +1,6 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-android' android { compileSdkVersion 25 @@ -21,6 +23,8 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'com.android.support:support-annotations:28.0.0' + implementation 'android.arch.lifecycle:extensions:1.1.1' androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) @@ -28,7 +32,12 @@ dependencies { testImplementation 'junit:junit:4.12' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:25.4.0' - compile 'com.getbase:floatingactionbutton:1.10.1' - compile 'com.larswerkman:HoloColorPicker:1.5' + implementation 'com.getbase:floatingactionbutton:1.10.1' + implementation 'com.larswerkman:HoloColorPicker:1.5' implementation 'com.android.support:cardview-v7:25.4.0' + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'com.google.code.gson:gson:2.8.6' +} +repositories { + mavenCentral() } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1b40fe2..caac83e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,13 +1,13 @@ - - + + + + @@ -26,10 +30,12 @@ android:theme="@style/AppTheme.NoActionBar"> + + diff --git a/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java b/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java index eeb23a1..59925b5 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java +++ b/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java @@ -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() { + 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 diff --git a/app/src/main/java/com/programaker/api/ProgramakerApi.kt b/app/src/main/java/com/programaker/api/ProgramakerApi.kt new file mode 100644 index 0000000..e664944 --- /dev/null +++ b/app/src/main/java/com/programaker/api/ProgramakerApi.kt @@ -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 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}") + } +} diff --git a/app/src/main/java/com/programaker/api/ProgramakerCheckResult.kt b/app/src/main/java/com/programaker/api/ProgramakerCheckResult.kt new file mode 100644 index 0000000..9f8596b --- /dev/null +++ b/app/src/main/java/com/programaker/api/ProgramakerCheckResult.kt @@ -0,0 +1,5 @@ +package com.programaker.api + +class ProgramakerCheckResult(val success: Boolean) { + +} diff --git a/app/src/main/java/com/programaker/api/ProgramakerConfigurationException.kt b/app/src/main/java/com/programaker/api/ProgramakerConfigurationException.kt new file mode 100644 index 0000000..159a3c7 --- /dev/null +++ b/app/src/main/java/com/programaker/api/ProgramakerConfigurationException.kt @@ -0,0 +1,5 @@ +package com.programaker.api + +open class ProgramakerConfigurationException : Exception() { + +} diff --git a/app/src/main/java/com/programaker/api/TokenNotFoundException.kt b/app/src/main/java/com/programaker/api/TokenNotFoundException.kt new file mode 100644 index 0000000..3d383ca --- /dev/null +++ b/app/src/main/java/com/programaker/api/TokenNotFoundException.kt @@ -0,0 +1,4 @@ +package com.programaker.api + +class TokenNotFoundException : ProgramakerConfigurationException() { +} diff --git a/app/src/main/res/layout/card_preview.xml b/app/src/main/res/layout/card_preview.xml index fe9c125..785f886 100644 --- a/app/src/main/res/layout/card_preview.xml +++ b/app/src/main/res/layout/card_preview.xml @@ -11,23 +11,23 @@ android:layout_height="wrap_content" android:layout_marginLeft="10dp" - android:layout_marginRight="10dp" android:layout_marginTop="5dp" + android:layout_marginRight="10dp" - android:paddingBottom="5dp" android:paddingLeft="15dp" - android:paddingRight="15dp" android:paddingTop="5dp" + android:paddingRight="15dp" + android:paddingBottom="5dp" card_view:cardCornerRadius="5dp" - card_view:cardPreventCornerOverlap="true" + card_view:cardElevation="10dp" card_view:cardUseCompatPadding="true" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="3dp"> + android:layout_height="wrap_content" + android:minHeight="100dp"> diff --git a/app/src/main/res/layout/content_deck_preview.xml b/app/src/main/res/layout/content_deck_preview.xml index 9efa657..0f293e1 100644 --- a/app/src/main/res/layout/content_deck_preview.xml +++ b/app/src/main/res/layout/content_deck_preview.xml @@ -8,12 +8,24 @@ tools:context="com.codigoparallevar.minicards.DeckPreviewActivity" tools:showIn="@layout/activity_deck_preview"> + + + app:layout_constraintTop_toBottomOf="@id/login_in_programaker_button" /> diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 59a0b0c..617af6a 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,3 +1,6 @@ 16dp + + 16dp + 16dp diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d8705be..f9b1214 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,4 +1,14 @@ mini-cards DeckPreviewActivity + Login in PrograMaker + Sign in + Email + Password + Sign in or register + Sign in + "Welcome !" + Not a valid username + Password must be >5 characters + "Login failed" diff --git a/app/src/main/res/orig/ic_launcher.xcf b/asset-src/ic_launcher.xcf similarity index 100% rename from app/src/main/res/orig/ic_launcher.xcf rename to asset-src/ic_launcher.xcf diff --git a/app/src/main/res/orig/ic_launcher_round.xcf b/asset-src/ic_launcher_round.xcf similarity index 100% rename from app/src/main/res/orig/ic_launcher_round.xcf rename to asset-src/ic_launcher_round.xcf diff --git a/build.gradle b/build.gradle index f53f49f..c899a95 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,16 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - + ext.kotlin_version = '1.3.61' + repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.0' - + classpath 'com.android.tools.build:gradle:3.6.1' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files