From 4183fccce11b24806c1695aff2a108eeb1122b85 Mon Sep 17 00:00:00 2001 From: ikarulus Date: Sun, 6 Jan 2019 03:09:14 +0100 Subject: [PATCH] add login activity --- .idea/misc.xml | 2 +- app/src/main/AndroidManifest.xml | 4 +- .../hochi/nextcompanion/LoginActivity.java | 212 ++++++++---------- .../hochi/nextcompanion/MainActivity.java | 25 ++- app/src/main/res/layout/activity_login.xml | 18 +- 5 files changed, 118 insertions(+), 143 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index c0f68ed..99202cc 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -25,7 +25,7 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0719016..df802cd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,9 +3,7 @@ package="com.example.hochi.nextcompanion"> - - - + { - /** - * Id to identity READ_CONTACTS permission request. - */ - private static final int REQUEST_READ_CONTACTS = 0; - - /** - * A dummy authentication store containing known user names and passwords. - * TODO: remove after connecting to a real authentication system. - */ - private static final String[] DUMMY_CREDENTIALS = new String[]{ - "foo@example.com:hello", "bar@example.com:world" - }; /** * Keep track of the login task to ensure we can cancel it if requested. */ private UserLoginTask mAuthTask = null; // UI references. - private AutoCompleteTextView mEmailView; - private EditText mPasswordView; + private TextView mPhoneView; + private EditText mPinView; private View mProgressView; private View mLoginFormView; @@ -67,11 +67,10 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks< super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // Set up the login form. - mEmailView = (AutoCompleteTextView) findViewById(R.id.email); - populateAutoComplete(); + mPhoneView = findViewById(R.id.phone); - mPasswordView = (EditText) findViewById(R.id.password); - mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() { + mPinView = findViewById(R.id.pin); + mPinView.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) { @@ -82,7 +81,7 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks< } }); - Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button); + Button mEmailSignInButton = findViewById(R.id.phone_sign_in_button); mEmailSignInButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { @@ -94,50 +93,6 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks< mProgressView = findViewById(R.id.login_progress); } - private void populateAutoComplete() { - if (!mayRequestContacts()) { - return; - } - - getLoaderManager().initLoader(0, null, this); - } - - private boolean mayRequestContacts() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { - return true; - } - if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) { - return true; - } - if (shouldShowRequestPermissionRationale(READ_CONTACTS)) { - Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE) - .setAction(android.R.string.ok, new View.OnClickListener() { - @Override - @TargetApi(Build.VERSION_CODES.M) - public void onClick(View v) { - requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS); - } - }); - } else { - requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS); - } - return false; - } - - /** - * Callback received when a permissions request has been completed. - */ - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, - @NonNull int[] grantResults) { - if (requestCode == REQUEST_READ_CONTACTS) { - if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - populateAutoComplete(); - } - } - } - - /** * Attempts to sign in or register the account specified by the login form. * If there are form errors (invalid email, missing fields, etc.), the @@ -149,31 +104,27 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks< } // Reset errors. - mEmailView.setError(null); - mPasswordView.setError(null); + mPhoneView.setError(null); + mPinView.setError(null); // Store values at the time of the login attempt. - String email = mEmailView.getText().toString(); - String password = mPasswordView.getText().toString(); + String email = mPhoneView.getText().toString(); + String password = mPinView.getText().toString(); boolean cancel = false; View focusView = null; // Check for a valid password, if the user entered one. if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) { - mPasswordView.setError(getString(R.string.error_invalid_password)); - focusView = mPasswordView; + mPinView.setError(getString(R.string.error_invalid_pin)); + focusView = mPinView; cancel = true; } // Check for a valid email address. if (TextUtils.isEmpty(email)) { - mEmailView.setError(getString(R.string.error_field_required)); - focusView = mEmailView; - cancel = true; - } else if (!isEmailValid(email)) { - mEmailView.setError(getString(R.string.error_invalid_email)); - focusView = mEmailView; + mPhoneView.setError(getString(R.string.error_field_required)); + focusView = mPhoneView; cancel = true; } @@ -190,14 +141,9 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks< } } - private boolean isEmailValid(String email) { - //TODO: Replace this with your own logic - return email.contains("@"); - } - private boolean isPasswordValid(String password) { //TODO: Replace this with your own logic - return password.length() > 4; + return password.length() == 6; } /** @@ -254,15 +200,8 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks< } @Override - public void onLoadFinished(Loader cursorLoader, Cursor cursor) { - List emails = new ArrayList<>(); - cursor.moveToFirst(); - while (!cursor.isAfterLast()) { - emails.add(cursor.getString(ProfileQuery.ADDRESS)); - cursor.moveToNext(); - } + public void onLoadFinished(Loader loader, Cursor data) { - addEmailsToAutoComplete(emails); } @Override @@ -270,16 +209,6 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks< } - private void addEmailsToAutoComplete(List emailAddressCollection) { - //Create adapter to tell the AutoCompleteTextView what to show in its dropdown list. - ArrayAdapter adapter = - new ArrayAdapter<>(LoginActivity.this, - android.R.layout.simple_dropdown_item_1line, emailAddressCollection); - - mEmailView.setAdapter(adapter); - } - - private interface ProfileQuery { String[] PROJECTION = { ContactsContract.CommonDataKinds.Email.ADDRESS, @@ -296,34 +225,79 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks< */ public class UserLoginTask extends AsyncTask { - private final String mEmail; - private final String mPassword; + private final String mPhone; + private final String mPin; - UserLoginTask(String email, String password) { - mEmail = email; - mPassword = password; + UserLoginTask(String phone, String pin) { + mPhone = URLEncoder.encode(phone); + mPin = pin; } @Override protected Boolean doInBackground(Void... params) { - // TODO: attempt authentication against a network service. + Boolean success = false; + String urlParameters = "apikey=rXXqTgQZUPZ89lzB&mobile=" + mPhone + "&pin=" + mPin; + HttpURLConnection connection = null; try { - // Simulate network access. - Thread.sleep(2000); - } catch (InterruptedException e) { - return false; - } + //Create connection + URL url = new URL("https://api.nextbike.net/api/login.json"); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", + "application/x-www-form-urlencoded"); - 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); + connection.setRequestProperty("Content-Length", "" + + Integer.toString(urlParameters.getBytes().length)); + connection.setRequestProperty("Content-Language", "en-US"); + + connection.setUseCaches (false); + connection.setDoInput(true); + connection.setDoOutput(true); + + //Send request + DataOutputStream wr = new DataOutputStream ( + connection.getOutputStream ()); + wr.writeBytes (urlParameters); + wr.flush (); + wr.close (); + + //Get Response + InputStream is = connection.getInputStream(); + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); + String line; + StringBuilder response = new StringBuilder(); + while((line = rd.readLine()) != null) { + response.append(line); + response.append('\r'); + } + rd.close(); + try { + JSONObject jObject = new JSONObject(response.toString()); + JSONObject userObject = jObject.getJSONObject("user"); + String loginkey = userObject.getString("loginkey"); + Log.d("DEBUG", loginkey); + success=true; + SharedPreferences sharedPref = getSharedPreferences("persistence", MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPref.edit(); + editor.putString("loginKey", loginkey); + editor.apply(); + } + catch (Exception e) { + e.printStackTrace(); + success=false; + } + + } catch (Exception e) { + + e.printStackTrace(); + + } finally { + + if(connection != null) { + connection.disconnect(); } } - - // TODO: register the new account here. return true; } @@ -335,8 +309,8 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks< if (success) { finish(); } else { - mPasswordView.setError(getString(R.string.error_incorrect_password)); - mPasswordView.requestFocus(); + mPinView.setError(getString(R.string.error_incorrect_pin)); + mPinView.requestFocus(); } } diff --git a/app/src/main/java/com/example/hochi/nextcompanion/MainActivity.java b/app/src/main/java/com/example/hochi/nextcompanion/MainActivity.java index b1238bc..e54c7e3 100644 --- a/app/src/main/java/com/example/hochi/nextcompanion/MainActivity.java +++ b/app/src/main/java/com/example/hochi/nextcompanion/MainActivity.java @@ -16,17 +16,6 @@ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { - //pre-condition: Is there a login key? - Context context = this; - SharedPreferences persistence = context.getSharedPreferences( - "loginKey", Context.MODE_PRIVATE); - String defaultValue = "nokey"; - String loginKey = persistence.getString("loginKey", defaultValue); - if (loginKey.equals("nokey")) { - Intent intent = new Intent(this, LoginActivity.class); - startActivity(intent); - } - //now this "every android activity" stuff super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); @@ -36,6 +25,20 @@ public class MainActivity extends AppCompatActivity { FloatingButton(); } + @Override + protected void onStart() { + super.onStart(); + //pre-condition: Is there a login key? + Context context = this; + SharedPreferences sharedPref = getSharedPreferences("persistence", MODE_PRIVATE); + String defaultValue = "nokey"; + String loginKey = sharedPref.getString("loginKey", defaultValue); + if (loginKey.equals("nokey")) { + Intent intent = new Intent(this, LoginActivity.class); + startActivity(intent); + } + } + @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index ff0a08b..de480af 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -26,7 +26,7 @@ android:layout_height="match_parent"> @@ -35,12 +35,12 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> - @@ -51,21 +51,21 @@ android:layout_height="wrap_content">