Commit 4f98dde5 authored by Christof Wolke's avatar Christof Wolke

Login Activity + Login request

parent 4cd8f919
......@@ -2,8 +2,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.uni_oldenburg.bidl">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
......@@ -20,12 +26,14 @@
</activity>
<activity
android:name=".base_view.SplashActivity"
android:screenOrientation="sensorPortrait">
android:screenOrientation="sensorPortrait"
android:parentActivityName=".base_view.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<activity android:name=".guest_view.LoginActivity"></activity>
</application>
</manifest>
\ No newline at end of file
package classes;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
/**
* Represent a User and al important informations
* Created by Christof on 04.08.2016.
*/
@JsonObject
public class User {
@JsonField(name = "id")
private long userId;
@JsonField(name = "name")
private String username;
@JsonField(name = "email")
private String email;
@JsonField(name = "image")
private String imagePath;
public User (){
}
/**
*
* @param username Name des Users
* @param email Email-Adresse des Users
* @param userId ID des Users
* @param imagePath Pfad zum Profilbild des Users
*/
public User(String username, String email, long userId, String imagePath) {
this.username = username;
this.email = email;
this.userId = userId;
this.imagePath = imagePath;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", username='" + username + '\'' +
", email='" + email + '\'' +
", imagePath='" + imagePath + '\'' +
'}';
}
}
......@@ -17,6 +17,7 @@ import java.util.List;
import java.util.UUID;
import de.uni_oldenburg.bidl.R;
import de.uni_oldenburg.bidl.guest_view.LoginActivity;
import pl.droidsonroids.gif.GifImageView;
import static de.uni_oldenburg.bidl.R.string.noBeaconFound;
......@@ -69,7 +70,7 @@ public class SplashActivity extends AppCompatActivity {
public void onBeaconsDiscovered(Region region, List<Beacon> list) {
if (!list.isEmpty()){
Beacon nearestBeacon = list.get(0);
toMainActivity();
toLoginActivity();
// TODO: 30.07.2016 save the Beacon in the database and overwrite the DB, when the nearestBeacon != the saved beacon.
} if (list.isEmpty()){
// TODO: 30.07.2016 toScrenn beacon not found...
......@@ -113,8 +114,8 @@ public class SplashActivity extends AppCompatActivity {
super.onPause();
}
private void toMainActivity() {
startActivity(new Intent(this, MainActivity.class));
private void toLoginActivity() {
startActivity(new Intent(this, LoginActivity.class));
}
private void showMessage() {
......
package de.uni_oldenburg.bidl.guest_view;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import classes.User;
import de.uni_oldenburg.bidl.R;
import de.uni_oldenburg.bidl.base_view.MainActivity;
import services.servercommunication.NetworkService;
public class LoginActivity extends AppCompatActivity {
/** GUI **/
private ImageView unilogo;
private EditText etEmail;
private EditText etPassword;
private CheckBox stayLoggedIn;
private Button btLogin;
private TextView tvRegister;
/** values **/
private String email;
private String password;
private boolean keepLoggedIn;
private static final String TOAST_ERROR_MESSAGE = "Falsche Login Daten";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
/** init **/
unilogo = (ImageView) findViewById(R.id.uniLogo);
etEmail = (EditText) findViewById(R.id.email);
etPassword = (EditText) findViewById(R.id.password);
stayLoggedIn = (CheckBox) findViewById(R.id.keepingLoggedIn);
btLogin = (Button) findViewById(R.id.loginButton);
tvRegister = (TextView) findViewById(R.id.register);
/** Fullscreen modus **/
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
/** Login **/
btLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
view.setEnabled(false);
email = etEmail.getText().toString();
password = etPassword.getText().toString();
keepLoggedIn = stayLoggedIn.isChecked();
NetworkService.login(email, password, new NetworkService.RequestLoginHandler() {
@Override
public void done(User user) {
//User cannot be null and cannot have the ID 0.
if (user != null && user.getUserId() > 0){
toMainActivity();
} else {
LoginActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), TOAST_ERROR_MESSAGE, Toast.LENGTH_SHORT).show();
view.setEnabled(true);
}
});
}
}
});
}
});
}
private void toMainActivity() {
startActivity(new Intent(this, MainActivity.class));
}
}
package services.servercommunication;
import com.bluelinelabs.logansquare.LoganSquare;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import classes.User;
/**
* Helfer Klasse, die häufig verwendete I/O Operationen im Netzwerk und der Datenbank erleichtert
......@@ -23,4 +35,51 @@ public abstract class NetworkService {
* @param password Passwort des Nutzers
* @param handler Callback handler
*/
public static void login(String email, String password, final RequestLoginHandler handler){
okHttpClient.setReadTimeout(15, TimeUnit.SECONDS);
/* okHTTPClient
Request body mit POST params
*/
RequestBody requestBody = new FormEncodingBuilder()
.add("email", email)
.add("pass", password)
.build();
//add body to POST-request
Request req = new Request.Builder()
.url(REST.SPRING_LOGIN)
.post(requestBody)
.build();
//Server call preparation
Call call = okHttpClient.newCall(req);
//ASync call
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
handler.done(null);
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
try {
//server response parsing
if (response.isSuccessful()){
String responseString = response.body().string();
//Userobject
User user = LoganSquare.parse(responseString, User.class);
handler.done(user);
}
}catch (IOException e){
handler.done(null);
e.printStackTrace();
}
}
});
}
public interface RequestLoginHandler {
void done(User user);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.uni_oldenburg.bidl.guest_view.LoginActivity"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/uniLogo"
android:src="@drawable/unilogo"
android:layout_margin="10dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:layout_gravity="center"
android:id="@+id/email"
android:hint="@string/hintTextEmail"
android:layout_marginTop="30dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_gravity="center"
android:id="@+id/password"
android:hint="@string/hintTextPass"
android:layout_marginTop="10dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkBoxDescription"
android:layout_gravity="center"
android:id="@+id/keepingLoggedIn"
android:checked="true"
android:layout_marginTop="10dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonDescription"
android:layout_gravity="center"
android:id="@+id/loginButton"
android:layout_marginTop="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/registerDescription"
android:textColor="@color/colorPrimary"
android:layout_gravity="center"
android:id="@+id/register"
android:layout_marginTop="5dp"/>
</LinearLayout>
......@@ -7,6 +7,14 @@
<string name="imageViewDescription">University Logo</string>
<string name="gifImageViewDescription">Loadingspinner</string>
<string name="noBeaconFound">Kein Beacon in der Nähe!</string>
<string name="title_activity_login">Sign in</string>
<!-- Login Activity !-->
<string name="hintTextEmail">E-Mail Adresse</string>
<string name="hintTextPass">Passwort</string>
<string name="checkBoxDescription">Eingeloggt bleiben</string>
<string name="buttonDescription">Login</string>
<string name="registerDescription">Registrieren</string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment