Add basic user data store and automatic login

This commit is contained in:
RicomenPL 2017-07-20 23:30:33 +02:00
parent eb89007326
commit 939656f4a6
4 changed files with 128 additions and 25 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.wulkanowy">
package="io.github.wulkanowy"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

View File

@ -2,7 +2,9 @@ package io.github.wulkanowy.activity.main;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.widget.Toast;
@ -25,6 +27,8 @@ public class Login extends AsyncTask<Void, Void, Void> {
String password;
String county;
int check;
Map<String, String> loginCookies;
Activity activity;
@ -36,20 +40,21 @@ public class Login extends AsyncTask<Void, Void, Void> {
ProgressDialog progress;
public Login(String emailT, String passwordT, String countyT, Activity mainAC){
public Login(String emailT, String passwordT, String countyT, Activity mainAC, int check) {
activity = mainAC;
progress = new ProgressDialog(activity);
this.check = check;
if (countyT.equals("Debug")){
if (countyT.equals("Debug")) {
urlForStepOne = activity.getString(R.string.urlStepOneDebug);
urlForStepTwo = activity.getString(R.string.urlStepTwoDebug);
urlForStepThree = activity.getString(R.string.urlStepThreeDebug);
county = activity.getString(R.string.countyDebug);
email = emailT;
password = passwordT;
}
else{
} else {
urlForStepOne = activity.getString(R.string.urlStepOneRelease);
urlForStepTwo = activity.getString(R.string.urlStepTwoRelease);
urlForStepThree = activity.getString(R.string.urlStepThreeRelease);
@ -92,12 +97,10 @@ public class Login extends AsyncTask<Void, Void, Void> {
outputStream.flush();
userMesage = activity.getString(R.string.login_accepted);
}
else {
} else {
userMesage = activity.getString(R.string.login_denied);
}
}
catch (IOException e){
} catch (IOException e) {
userMesage = e.toString();
}
@ -153,14 +156,45 @@ public class Login extends AsyncTask<Void, Void, Void> {
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progress.dismiss();
if (!userMesage.isEmpty()){
Toast.makeText(activity, userMesage , Toast.LENGTH_LONG).show();
if (!userMesage.isEmpty()) {
Toast.makeText(activity, userMesage, Toast.LENGTH_LONG).show();
}
if (userMesage.equals(activity.getString(R.string.login_accepted))){
Intent intent = new Intent(activity,DashboardActivity.class);
activity.startActivity(intent);
if (userMesage.equals(activity.getString(R.string.login_accepted))) {
if (check == 0) {
if (createAccount()) {
Intent intent = new Intent(activity, DashboardActivity.class);
activity.startActivity(intent);
} else if (!createAccount()) {
Toast.makeText(activity, "Konto już istnieje", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(activity, DashboardActivity.class);
activity.startActivity(intent);
}
} else if (check == 1) {
Intent intent = new Intent(activity, DashboardActivity.class);
activity.startActivity(intent);
}
}
}
public boolean createAccount() {
SharedPreferences sharedPreferences = activity.getSharedPreferences("io.github.wulkanowy", Context.MODE_PRIVATE);
if (!sharedPreferences.contains(email)) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("wulkanowy",email);
editor.putString(email,email);
editor.putString("sandi" + email,password);
editor.putString("county" + email,county);
editor.commit();
return true;
}
else{
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("wulkanowy",email);
editor.commit();
return false;
}
}
}

View File

@ -16,6 +16,10 @@ import io.github.wulkanowy.R;
public class MainActivity extends AppCompatActivity {
String password;
String email;
String county;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -49,9 +53,9 @@ public class MainActivity extends AppCompatActivity {
EditText adressEmail = (EditText)findViewById(R.id.emailText);
EditText passwordText = (EditText)findViewById(R.id.passwordText);
EditText countyText = (EditText)findViewById(R.id.countyText);
String password = passwordText.getText().toString();
String email = adressEmail.getText().toString();
String county = countyText.getText().toString();
password = passwordText.getText().toString();
email = adressEmail.getText().toString();
county = countyText.getText().toString();
String[] keys = this.getResources().getStringArray(R.array.counties);
String[] values = this.getResources().getStringArray(R.array.counties_values);
@ -66,7 +70,7 @@ public class MainActivity extends AppCompatActivity {
}
if (!email.isEmpty() || !password.isEmpty() || !county.isEmpty()){
new Login(email, password, county, this).execute();
new Login(email, password, county, this, 0).execute();
}
else if (password.isEmpty() || email.isEmpty() || county.isEmpty()) {
Toast.makeText(this, R.string.data_text, Toast.LENGTH_SHORT).show();

View File

@ -1,18 +1,34 @@
package io.github.wulkanowy.activity.started;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.widget.Toast;
import io.github.wulkanowy.activity.dashboard.DashboardActivity;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import io.github.wulkanowy.R;
import io.github.wulkanowy.activity.main.Login;
import io.github.wulkanowy.activity.main.MainActivity;
public class LoadingTask extends AsyncTask<Void, Void, Void> {
Context activity;
Activity activity;
boolean isOnline;
String idAccount;
String email;
String password;
String county;
LoadingTask(Context main) {
final boolean SAVE_DATA = true;
LoadingTask(Activity main) {
activity = main;
}
@ -23,15 +39,63 @@ public class LoadingTask extends AsyncTask<Void, Void, Void> {
} catch (InterruptedException e) {
e.printStackTrace();
}
isOnline = isOnline();
return null;
}
protected void onPostExecute(Void result) {
/* Intent intent = new Intent(activity,MainActivity.class);
activity.startActivity(intent); */
if (isOnline) {
SharedPreferences sharedPreferences = activity.getSharedPreferences("io.github.wulkanowy", Context.MODE_PRIVATE);
Intent intent = new Intent(activity,MainActivity.class);
activity.startActivity(intent);
if (SAVE_DATA) {
if (sharedPreferences.contains("wulkanowy")) {
idAccount = sharedPreferences.getString("wulkanowy", "");
email = sharedPreferences.getString(idAccount, "");
password = sharedPreferences.getString("sandi" + email, "");
county = sharedPreferences.getString("county" + email, "");
if (!email.isEmpty() || !password.isEmpty() || !county.isEmpty()) {
new Login(email, password, county, activity, 1).execute();
} else if (password.isEmpty() || email.isEmpty() || county.isEmpty()) {
Toast.makeText(activity, R.string.data_text, Toast.LENGTH_SHORT).show();
}
} else {
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
}
}
else{
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
}
}
else{
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
Toast.makeText(activity,"Brak połączenia z internetem",Toast.LENGTH_SHORT ).show();
}
}
public boolean isOnline() {
try {
int timeoutMs = 1500;
Socket sock = new Socket();
SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);
sock.connect(sockaddr, timeoutMs);
sock.close();
return true;
} catch (IOException e) {
return false;
}
}
}