Set base for adding login.

This commit is contained in:
Sergio Martínez Portela 2020-03-02 21:01:11 +01:00
parent a02d372b90
commit 9fb10281cb
14 changed files with 172 additions and 16 deletions

View File

@ -1,4 +1,6 @@
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
android { android {
compileSdkVersion 25 compileSdkVersion 25
@ -21,6 +23,8 @@ android {
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) 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', { androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations' exclude group: 'com.android.support', module: 'support-annotations'
}) })
@ -28,7 +32,12 @@ dependencies {
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:25.4.0' implementation 'com.android.support:design:25.4.0'
compile 'com.getbase:floatingactionbutton:1.10.1' implementation 'com.getbase:floatingactionbutton:1.10.1'
compile 'com.larswerkman:HoloColorPicker:1.5' implementation 'com.larswerkman:HoloColorPicker:1.5'
implementation 'com.android.support:cardview-v7:25.4.0' 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()
} }

View File

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codigoparallevar.minicards"> package="com.codigoparallevar.minicards">
<!-- OpenGL ES 2.0 --> <!-- OpenGL ES 2.0 -->
<uses-feature <uses-feature
android:glEsVersion="0x00020000" android:glEsVersion="0x00020000"
android:required="true" /> android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
@ -15,9 +15,13 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity
android:name=".ui.login.LoginActivity"
android:label="@string/title_activity_login"></activity>
<activity android:name=".CardActivity"> <activity android:name=".CardActivity">
<intent-filter> <intent-filter>
<action android:name="com.codigoparallevar.minicards.CARD" /> <action android:name="com.codigoparallevar.minicards.CARD" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
</activity> </activity>
@ -26,10 +30,12 @@
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
<intent-filter> <intent-filter>
<action android:name="com.codigoparallevar.minicards.DECK" /> <action android:name="com.codigoparallevar.minicards.DECK" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
</activity> </activity>

View File

@ -2,6 +2,7 @@ package com.codigoparallevar.minicards;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
@ -9,6 +10,7 @@ import android.support.v7.widget.Toolbar;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
@ -18,11 +20,18 @@ import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Vector; import java.util.Vector;
import com.programaker.api.ProgramakerApi;
public class DeckPreviewActivity extends ReloadableAppCompatActivity { public class DeckPreviewActivity extends ReloadableAppCompatActivity {
public static final String INTENT = "com.codigoparallevar.minicards.DECK"; public static final String INTENT = "com.codigoparallevar.minicards.DECK";
private ListView listView; private ListView listView;
private CardPreviewArrayAdapter cardArrayAdapter; private CardPreviewArrayAdapter cardArrayAdapter;
private ProgramakerApi ProgramakerApi = new ProgramakerApi();
protected void openLoginForm(View view) {
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { 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); 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 @Override

View 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}")
}
}

View File

@ -0,0 +1,5 @@
package com.programaker.api
class ProgramakerCheckResult(val success: Boolean) {
}

View File

@ -0,0 +1,5 @@
package com.programaker.api
open class ProgramakerConfigurationException : Exception() {
}

View File

@ -0,0 +1,4 @@
package com.programaker.api
class TokenNotFoundException : ProgramakerConfigurationException() {
}

View File

@ -11,23 +11,23 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="10dp" android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp" android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:paddingBottom="5dp"
android:paddingLeft="15dp" android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp" android:paddingTop="5dp"
android:paddingRight="15dp"
android:paddingBottom="5dp"
card_view:cardCornerRadius="5dp" card_view:cardCornerRadius="5dp"
card_view:cardPreventCornerOverlap="true" card_view:cardElevation="10dp"
card_view:cardUseCompatPadding="true" card_view:cardUseCompatPadding="true"
tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="3dp"> tools:layout_editor_absoluteY="3dp">
<RelativeLayout <RelativeLayout
android:minHeight="100dp"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:minHeight="100dp">
<TextView <TextView
android:id="@+id/card_preview_name" android:id="@+id/card_preview_name"
@ -44,9 +44,9 @@
android:id="@+id/card_preview_settings_button" android:id="@+id/card_preview_settings_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:alpha="1" android:alpha="1"
android:clickable="true" android:clickable="true"
android:src="@drawable/ic_settings_gray" /> android:src="@drawable/ic_settings_gray" />

View File

@ -8,12 +8,24 @@
tools:context="com.codigoparallevar.minicards.DeckPreviewActivity" tools:context="com.codigoparallevar.minicards.DeckPreviewActivity"
tools:showIn="@layout/activity_deck_preview"> tools:showIn="@layout/activity_deck_preview">
<android.support.v7.widget.AppCompatButton
android:id="@+id/login_in_programaker_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="@string/login_in_programaker"
android:textAllCaps="false"
app:backgroundTint="@color/colorAccent"
app:layout_constraintBottom_toTopOf="@id/card_deck_list"
app:layout_constraintTop_toTopOf="parent" />
<ListView <ListView
android:id="@+id/card_deck_list" android:id="@+id/card_deck_list"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:divider="@null" android:divider="@null"
android:dividerHeight="2dp"
android:paddingTop="5dp" android:paddingTop="5dp"
android:dividerHeight="2dp" /> app:layout_constraintTop_toBottomOf="@id/login_in_programaker_button" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>

View File

@ -1,3 +1,6 @@
<resources> <resources>
<dimen name="fab_margin">16dp</dimen> <dimen name="fab_margin">16dp</dimen>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources> </resources>

View File

@ -1,4 +1,14 @@
<resources> <resources>
<string name="app_name">mini-cards</string> <string name="app_name">mini-cards</string>
<string name="title_activity_deck_preview">DeckPreviewActivity</string> <string name="title_activity_deck_preview">DeckPreviewActivity</string>
<string name="login_in_programaker">Login in PrograMaker</string>
<string name="title_activity_login">Sign in</string>
<string name="prompt_email">Email</string>
<string name="prompt_password">Password</string>
<string name="action_sign_in">Sign in or register</string>
<string name="action_sign_in_short">Sign in</string>
<string name="welcome">"Welcome !"</string>
<string name="invalid_username">Not a valid username</string>
<string name="invalid_password">Password must be >5 characters</string>
<string name="login_failed">"Login failed"</string>
</resources> </resources>

View File

@ -1,14 +1,16 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript { buildscript {
ext.kotlin_version = '1.3.61'
repositories { repositories {
google() google()
jcenter() jcenter()
} }
dependencies { 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 // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files