Integraded retrofit, created LdapService with api endpoints.

LdapUser + JwtToken models.
Getting proper jwt token after fakeValidate - now need to implement it on login and save in SharedPreferences(localstorage)
This commit is contained in:
Domagalski 2018-08-21 23:41:07 +02:00
parent e7db60071a
commit ad27db1a47
16 changed files with 341 additions and 22 deletions

View File

@ -26,4 +26,7 @@ dependencies {
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
} }

View File

@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" /> <uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<application <application
android:allowBackup="true" android:allowBackup="true"
@ -15,7 +16,7 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".StartupActivity" <activity android:name=".activity.StartupActivity"
android:label="@string/title_activity_startup" android:label="@string/title_activity_startup"
android:launchMode="singleInstance"> android:launchMode="singleInstance">
<intent-filter> <intent-filter>
@ -25,13 +26,13 @@
</activity> </activity>
<activity <activity
android:name=".MainActivity" android:name=".activity.MainActivity"
android:label="@string/title_activity_main" android:label="@string/title_activity_main"
android:launchMode="singleTop"> android:launchMode="singleTop">
</activity> </activity>
<activity <activity
android:name=".LoginActivity" android:name=".activity.LoginActivity"
android:excludeFromRecents="true" android:excludeFromRecents="true"
android:launchMode="singleTask" android:launchMode="singleTask"
android:label="@string/title_activity_login" android:label="@string/title_activity_login"

View File

@ -1,4 +1,4 @@
package com.uam.wmi.findmytutor; package com.uam.wmi.findmytutor.activity;
import android.animation.Animator; import android.animation.Animator;
import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorListenerAdapter;
@ -32,10 +32,22 @@ import android.widget.AutoCompleteTextView;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.model.JwtToken;
import com.uam.wmi.findmytutor.model.LdapUser;
import com.uam.wmi.findmytutor.network.RetrofitClientInstance;
import com.uam.wmi.findmytutor.service.LdapService;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.READ_CONTACTS;
/** /**
@ -86,6 +98,7 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
} }
}); });
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button); Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
mEmailSignInButton.setOnClickListener(new OnClickListener() { mEmailSignInButton.setOnClickListener(new OnClickListener() {
@Override @Override
@ -303,6 +316,7 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
private final String mEmail; private final String mEmail;
private final String mPassword; private final String mPassword;
// Constructor
UserLoginTask(String email, String password) { UserLoginTask(String email, String password) {
mEmail = email; mEmail = email;
mPassword = password; mPassword = password;
@ -310,22 +324,61 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service. LdapService service = RetrofitClientInstance.getRetrofitLoginInstance().create(LdapService.class);
LdapUser user = new LdapUser("string",mPassword,"string","string","string","string",mEmail);
try { try {
// Simulate network access. Response<JwtToken> loginResponse = service.fakeValidate(user).execute();
Thread.sleep(2000); if (loginResponse.isSuccessful()) {
} catch (InterruptedException e) { String authToken = loginResponse.body().getToken();
Log.e("Login", "jwt token: " + authToken);
SharedPreferences sharedPref = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
sharedPref.edit().putString("authToken", authToken).putBoolean("loggedIn", true).apply();
return true;
}
} catch (IOException e) {
Log.e("Login error", "nie udalo sie kurde...");
return false; return false;
} }
for (String credential : DUMMY_CREDENTIALS) { //To jest async i nie da sie zwrocic :(
String[] pieces = credential.split(":"); // Call<JwtToken> call = service.fakeValidate(user);
if (pieces[0].equals(mEmail)) { //
// Account exists, return true if the password matches. // call.enqueue(new Callback<JwtToken>() {
return pieces[1].equals(mPassword); // @Override
} // public void onResponse(Call<JwtToken> call, Response<JwtToken> response) {
} // Log.e("jwt", response.body().getToken());
// }
//
// @Override
// public void onFailure(Call<JwtToken> call, Throwable t) {
// Toast.makeText(LoginActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
// return false;
// }
// });
// TODO: attempt authentication against a network service.
// try {
// // Simulate network access.
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// return false;
// }
//
// for (String credential : DUMMY_CREDENTIALS) {
// String[] pieces = credential.split(":");
// if (pieces[0].equals(mEmail)) {
// // Account exists, return true if the password matches.
// return pieces[1].equals(mPassword);
// }
// }
// TODO: register the new account here. // TODO: register the new account here.
return false; return false;

View File

@ -1,4 +1,4 @@
package com.uam.wmi.findmytutor; package com.uam.wmi.findmytutor.activity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -10,6 +10,8 @@ import android.support.v7.widget.Toolbar;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import com.uam.wmi.findmytutor.R;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@Override @Override
@ -18,7 +20,7 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
SharedPreferences sharedPref = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE); SharedPreferences sharedPref = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
String text = sharedPref.getString("test",null); String text = sharedPref.getString("test",null);
Log.e("okurwaMainactivity", text); Log.e("Mainactivity", text);
} }
} }

View File

@ -1,10 +1,21 @@
package com.uam.wmi.findmytutor; package com.uam.wmi.findmytutor.activity;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.uam.wmi.findmytutor.model.JwtToken;
import com.uam.wmi.findmytutor.model.LdapUser;
import com.uam.wmi.findmytutor.network.RetrofitClientInstance;
import com.uam.wmi.findmytutor.service.LdapService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class StartupActivity extends AppCompatActivity { public class StartupActivity extends AppCompatActivity {
private static final int AUTHENTICATION_REQUEST_CODE = 666; private static final int AUTHENTICATION_REQUEST_CODE = 666;

View File

@ -0,0 +1,20 @@
package com.uam.wmi.findmytutor.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class JwtToken {
@SerializedName("token")
@Expose
private String token;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}

View File

@ -0,0 +1,96 @@
package com.uam.wmi.findmytutor.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class LdapUser {
@SerializedName("login")
@Expose
private String login;
@SerializedName("password")
@Expose
private String password;
@SerializedName("department")
@Expose
private String department;
@SerializedName("title")
@Expose
private String title;
@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("lastName")
@Expose
private String lastName;
@SerializedName("email")
@Expose
private String email;
public LdapUser(String login, String password, String department, String title, String firstName, String lastName, String email) {
this.login = login;
this.password = password;
this.department = department;
this.title = title;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

View File

@ -0,0 +1,32 @@
package com.uam.wmi.findmytutor.network;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
public abstract class NetworkConnectionInterceptor implements Interceptor {
public abstract boolean isInternetAvailable();
public abstract void onInternetUnavailable();
public abstract void onCacheUnavailable();
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!isInternetAvailable()) {
onInternetUnavailable();
request = request.newBuilder().header("Cache-Control",
"public, only-if-cached, max-stale=" + 60 * 60 * 24).build();
Response response = chain.proceed(request);
if (response.cacheResponse() == null) {
onCacheUnavailable();
}
return response;
}
return chain.proceed(request);
}
}

View File

@ -0,0 +1,71 @@
package com.uam.wmi.findmytutor.network;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClientInstance {
private static Retrofit retrofit;
private static final String BASE_URL = "https://s416084.projektstudencki.pl/develop/";
public static Retrofit getRetrofitLoginInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.client(provideOkHttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
private static OkHttpClient provideOkHttpClient() {
OkHttpClient.Builder okhttpClientBuilder = new OkHttpClient.Builder();
okhttpClientBuilder.connectTimeout(30, TimeUnit.SECONDS);
okhttpClientBuilder.readTimeout(30, TimeUnit.SECONDS);
okhttpClientBuilder.writeTimeout(30, TimeUnit.SECONDS);
return okhttpClientBuilder.build();
}
//
// private OkHttpClient provideokHttpClient() {
// OkHttpClient.Builder okhttpClientBuilder = new OkHttpClient.Builder();
// okhttpClientBuilder.connectTimeout(30, TimeUnit.SECONDS);
// okhttpClientBuilder.readTimeout(30, TimeUnit.SECONDS);
// okhttpClientBuilder.writeTimeout(30, TimeUnit.SECONDS);
//
// okhttpClientBuilder.addInterceptor(new NetworkConnectionInterceptor() {
// @Override
// public boolean isInternetAvailable() {
// return this.isInternetAvailable();
// }
//
// @Override
// public void onInternetUnavailable() {
// if (mInternetConnectionListener != null) {
// mInternetConnectionListener.onInternetUnavailable();
// }
// }
//
// @Override
// public void onCacheUnavailable() {
// if (mInternetConnectionListener != null) {
// mInternetConnectionListener.onCacheUnavailable();
// }
// }
// });
// }
}

View File

@ -0,0 +1,26 @@
package com.uam.wmi.findmytutor.service;
import com.uam.wmi.findmytutor.model.JwtToken;
import com.uam.wmi.findmytutor.model.LdapUser;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
public interface LdapService {
@POST("api/ldap/fakeValidate")
Call<JwtToken> fakeValidate(@Body LdapUser user);
@POST("api/ldap/validate")
Call<JwtToken> validate(@Body LdapUser user);
@GET("api/ldap/getUserData/{login}")
Call<LdapUser> getUserDataByLogin(@Path("login") String userLogin);
@GET("api/ldap/getUserData/email/{email}")
Call<LdapUser> getUserDataByMail(@Path("email") String userMail);
}

View File

@ -0,0 +1,4 @@
package com.uam.wmi.findmytutor.service;
public interface Session {
}

View File

@ -9,7 +9,7 @@
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoginActivity"> tools:context=".activity.LoginActivity">
<!-- Login progress --> <!-- Login progress -->
<ProgressBar <ProgressBar

View File

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> tools:context=".activity.MainActivity">
<android.support.design.widget.AppBarLayout <android.support.design.widget.AppBarLayout
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -4,6 +4,6 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".StartupActivity"> tools:context=".activity.StartupActivity">
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>

View File

@ -5,7 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity" tools:context=".activity.MainActivity"
tools:showIn="@layout/activity_main"> tools:showIn="@layout/activity_main">
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>