From 4f98dde561cc06d43bffcfa51a336dcbf17e574f Mon Sep 17 00:00:00 2001 From: Christof Wolke Date: Thu, 4 Aug 2016 17:41:57 +0200 Subject: [PATCH] Login Activity + Login request --- BIDL/BIDL/app/src/main/AndroidManifest.xml | 12 ++- BIDL/BIDL/app/src/main/java/classes/User.java | 85 +++++++++++++++++ .../bidl/base_view/SplashActivity.java | 7 +- .../bidl/guest_view/LoginActivity.java | 93 +++++++++++++++++++ .../servercommunication/NetworkService.java | 59 ++++++++++++ .../src/main/res/layout/activity_login.xml | 62 +++++++++++++ BIDL/BIDL/app/src/main/res/values/strings.xml | 8 ++ 7 files changed, 321 insertions(+), 5 deletions(-) create mode 100644 BIDL/BIDL/app/src/main/java/classes/User.java create mode 100644 BIDL/BIDL/app/src/main/java/de/uni_oldenburg/bidl/guest_view/LoginActivity.java create mode 100644 BIDL/BIDL/app/src/main/res/layout/activity_login.xml diff --git a/BIDL/BIDL/app/src/main/AndroidManifest.xml b/BIDL/BIDL/app/src/main/AndroidManifest.xml index b22f9b0..55368d4 100644 --- a/BIDL/BIDL/app/src/main/AndroidManifest.xml +++ b/BIDL/BIDL/app/src/main/AndroidManifest.xml @@ -2,8 +2,14 @@ + + + + + + + android:screenOrientation="sensorPortrait" + android:parentActivityName=".base_view.SplashActivity"> - + + \ No newline at end of file diff --git a/BIDL/BIDL/app/src/main/java/classes/User.java b/BIDL/BIDL/app/src/main/java/classes/User.java new file mode 100644 index 0000000..e1e5099 --- /dev/null +++ b/BIDL/BIDL/app/src/main/java/classes/User.java @@ -0,0 +1,85 @@ +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 + '\'' + + '}'; + } +} diff --git a/BIDL/BIDL/app/src/main/java/de/uni_oldenburg/bidl/base_view/SplashActivity.java b/BIDL/BIDL/app/src/main/java/de/uni_oldenburg/bidl/base_view/SplashActivity.java index 9a01f41..164466f 100644 --- a/BIDL/BIDL/app/src/main/java/de/uni_oldenburg/bidl/base_view/SplashActivity.java +++ b/BIDL/BIDL/app/src/main/java/de/uni_oldenburg/bidl/base_view/SplashActivity.java @@ -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 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() { diff --git a/BIDL/BIDL/app/src/main/java/de/uni_oldenburg/bidl/guest_view/LoginActivity.java b/BIDL/BIDL/app/src/main/java/de/uni_oldenburg/bidl/guest_view/LoginActivity.java new file mode 100644 index 0000000..b0eb5dc --- /dev/null +++ b/BIDL/BIDL/app/src/main/java/de/uni_oldenburg/bidl/guest_view/LoginActivity.java @@ -0,0 +1,93 @@ +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)); + } +} diff --git a/BIDL/BIDL/app/src/main/java/services/servercommunication/NetworkService.java b/BIDL/BIDL/app/src/main/java/services/servercommunication/NetworkService.java index e0b79b6..dfe9bdb 100644 --- a/BIDL/BIDL/app/src/main/java/services/servercommunication/NetworkService.java +++ b/BIDL/BIDL/app/src/main/java/services/servercommunication/NetworkService.java @@ -1,6 +1,18 @@ 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); + } } diff --git a/BIDL/BIDL/app/src/main/res/layout/activity_login.xml b/BIDL/BIDL/app/src/main/res/layout/activity_login.xml new file mode 100644 index 0000000..5946783 --- /dev/null +++ b/BIDL/BIDL/app/src/main/res/layout/activity_login.xml @@ -0,0 +1,62 @@ + + + + + + + + + + +