Skip to content

Commit

Permalink
Merge pull request #1 from epic-guys/android-account
Browse files Browse the repository at this point in the history
Save credentials implementation
  • Loading branch information
WAPEETY authored Nov 16, 2023
2 parents f165f72 + 48b517d commit aa10091
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 44 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ dependencies {
// JWT
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-gson:0.12.3'
//Secure storage
implementation 'com.github.adorsys:secure-storage-android:0.0.2' //unable to find artifact, fix later



implementation 'com.google.code.gson:gson:2.10.1'

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


<application
android:allowBackup="true"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/epic_guys/esse4/API/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import okhttp3.OkHttpClient;
import okhttp3.Credentials;
import okhttp3.Request;

import org.conscrypt.BuildConfig;
import org.epic_guys.esse4.API.services.AnagraficheService;
import org.epic_guys.esse4.API.services.ApiService;
import org.epic_guys.esse4.API.services.JwtService;
Expand Down
45 changes: 17 additions & 28 deletions app/src/main/java/org/epic_guys/esse4/activities/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import android.util.Log;

import de.adorsys.android.securestoragelibrary.SecurePreferences;

public class LoginActivity extends AppCompatActivity {


Expand Down Expand Up @@ -44,38 +46,25 @@ protected void onCreate(Bundle savedInstanceState) {
}
}).thenAccept(persona -> {
runOnUiThread(() -> {
Toast.makeText(getApplicationContext(), "Login effettuato", Toast.LENGTH_SHORT).show();
Log.i("LoginActivity", "Login effettuato");

Account a = new Account(matricola, "org.epic_guys.esse4");

Bundle usrData = new Bundle();

AccountManager am = AccountManager.get(this);
Log.i("LoginActivity", "AccountManager: " + am.toString());

Log.i("LoginActivity", "Adding account");
Toast.makeText(getApplicationContext(), "Login effettuato", Toast.LENGTH_SHORT).show();
Log.i("LoginActivity", "Login effettuato");
});

if(am.addAccountExplicitly(
a,
password,
usrData
)){
Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, matricola);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, "org.epic_guys.esse4");
}
try {
SecurePreferences.setValue("matricola", matricola, this);
SecurePreferences.setValue("password", password, this);

Log.i("LoginActivity", "Account: " + a.toString());
Log.i("LoginActivity", "Account added");
}
catch (Exception e) {
Log.e("LoginActivity", "Failed to save credentials");
}

Intent mainActivity = new Intent(this, MainActivity.class);
startActivity(mainActivity);
Log.i("LoginActivity", "Starting MainActivity, finishing LoginActivity");
finish();
});

//TextView fullname_view = findViewById(R.id.text_fullname);
//fullname_view.setText(persona.getNome());
Intent mainActivity = new Intent(this, MainActivity.class);
startActivity(mainActivity);
Log.i("LoginActivity", "Starting MainActivity, finishing LoginActivity");
finish();
});

});
Expand Down
71 changes: 58 additions & 13 deletions app/src/main/java/org/epic_guys/esse4/activities/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package org.epic_guys.esse4.activities;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.epic_guys.esse4.API.API;

import org.epic_guys.esse4.R;

import de.adorsys.android.securestoragelibrary.SecurePreferences;

public class MainActivity extends AppCompatActivity {

private void launchLoginActivity() {
Expand All @@ -22,22 +23,66 @@ private void launchLoginActivity() {

@Override
protected void onCreate(Bundle savedInstanceState) {
String matricola = "";
String password = "";
super.onCreate(savedInstanceState);

AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccountsByType("org.epic_guys.esse4");
Log.i("MainActivity", "Accounts: " + accounts.length);
if (accounts.length == 0) {
Log.i("MainActivity", "No accounts found");
/*
* 1. Check if the user is logged in
* 2. If not, launch the login activity
* 3. If yes, show the main activity
* 4. If the login fails, launch the login activity
* 5. If the login succeeds, continue with the main activity
*
* we know that on the first launch the user will login two times... but it's a feature!
*/

// checks if secure preferences are set, if not launch login activity
try{
matricola = SecurePreferences.getStringValue("matricola",this, "");
password = SecurePreferences.getStringValue( "password", this, "");
}
catch (Exception e) {
Log.i("MainActivity", "Secure preferences not set");
launchLoginActivity();
}
if (matricola.equals("") || password.equals("")) {
launchLoginActivity();
}else{
runOnUiThread(() -> {
Log.i("MainActivity", "Returned to main activity");
});

// try to login with saved credentials
API.login(matricola, password).thenComposeAsync(isLogged -> {
if (isLogged)
return API.getBasicData();
else {
throw new RuntimeException("Failed to login");
}
}).thenAccept(persona -> {
runOnUiThread(() -> {
Log.i("MainActivity", "Login effettuato");
Toast.makeText(this, "Benvenuto " + persona.getNome(), Toast.LENGTH_SHORT).show();
});
}).exceptionally(e -> {
runOnUiThread(() -> {
Log.i("MainActivity", "Login fallito");
Toast.makeText(this, "Login fallito", Toast.LENGTH_SHORT).show();
});

launchLoginActivity();
} else {
Log.i("MainActivity", "Account found");
// Manage account, get token, etc.
// if login fails, delete account and return to login activity
}
return null;
});

setContentView(R.layout.activity_main);

// do all the stuff needed to show the main activity
//logout button
findViewById(R.id.btn_logout).setOnClickListener(v -> {
SecurePreferences.setValue("matricola", "", this);
SecurePreferences.setValue("password", "", this);
launchLoginActivity();
});
}
}
}
24 changes: 24 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,28 @@
android:layout_height="match_parent"
tools:context=".activities.MainActivity">

<TextView
android:id="@+id/text_fullname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_fullname" />


</androidx.constraintlayout.widget.ConstraintLayout>
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ android.useAndroidX=true
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
android.enableJetifier=true
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
}

Expand Down

0 comments on commit aa10091

Please sign in to comment.