added location logger & json
This commit is contained in:
parent
41b4b47a36
commit
e393552fea
6 changed files with 708 additions and 329 deletions
|
@ -2,6 +2,11 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.android_app.matan.ara.sagi.thesocialnotework">
|
||||
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_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" />
|
||||
|
|
|
@ -0,0 +1,254 @@
|
|||
package com.android_app.matan.ara.sagi.thesocialnotework;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Address;
|
||||
import android.location.Geocoder;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by JERLocal on 7/1/2016.
|
||||
*/
|
||||
public class GPSUtils extends Service implements LocationListener {
|
||||
|
||||
private static final String TAG ="GPSUtils" ;
|
||||
private final Context mContext;
|
||||
|
||||
// flag for GPS status
|
||||
boolean isGPSEnabled = false;
|
||||
|
||||
// flag for network status
|
||||
boolean isNetworkEnabled = false;
|
||||
|
||||
// flag for GPS status
|
||||
boolean canGetLocation = false;
|
||||
|
||||
Location location; // location
|
||||
double latitude; // latitude
|
||||
double longitude; // longitude
|
||||
|
||||
protected LocationManager locationManager;
|
||||
|
||||
|
||||
public GPSUtils(Context context) {
|
||||
this.mContext = context;
|
||||
getLocation();
|
||||
}
|
||||
|
||||
// public Location getLocation() {
|
||||
// LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
|
||||
// if (locationManager != null) {
|
||||
// Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
// if (lastKnownLocationGPS != null) {
|
||||
// return lastKnownLocationGPS;
|
||||
// } else {
|
||||
// Location loc = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
|
||||
// System.out.println("1::"+loc);----getting null over here
|
||||
// System.out.println("2::"+loc.getLatitude());
|
||||
// return loc;
|
||||
// }
|
||||
// } else {
|
||||
// return null;
|
||||
// }
|
||||
public Location getLocation() {
|
||||
try {
|
||||
Log.d("TAG","in get location");
|
||||
locationManager =
|
||||
(LocationManager) mContext
|
||||
.getSystemService(LOCATION_SERVICE);
|
||||
|
||||
// getting GPS status
|
||||
isGPSEnabled = locationManager
|
||||
.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
||||
|
||||
// getting network status
|
||||
isNetworkEnabled = locationManager
|
||||
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
|
||||
|
||||
if (!isGPSEnabled && !isNetworkEnabled) {
|
||||
// no network provider is enabled
|
||||
showSettingsAlert();
|
||||
} else {
|
||||
this.canGetLocation = true;
|
||||
if (isNetworkEnabled) {
|
||||
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
Log.e("TAG", "No Permissions");
|
||||
((PersonalSpaceActivity)mContext).setLocationPermission(false);
|
||||
return null;
|
||||
}else{
|
||||
((PersonalSpaceActivity)mContext).setLocationPermission(true);
|
||||
}
|
||||
Log.d(TAG, "Network");
|
||||
if (locationManager != null) {
|
||||
location = locationManager
|
||||
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
||||
if (location != null) {
|
||||
latitude = location.getLatitude();
|
||||
longitude = location.getLongitude();
|
||||
}
|
||||
}
|
||||
}
|
||||
// if GPS Enabled get lat/long using GPS Services
|
||||
if (isGPSEnabled) {
|
||||
if (location == null) {
|
||||
|
||||
Log.d(TAG, "GPS Enabled");
|
||||
if (locationManager != null) {
|
||||
location = locationManager
|
||||
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
if (location != null) {
|
||||
latitude = location.getLatitude();
|
||||
longitude = location.getLongitude();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return location;
|
||||
}
|
||||
|
||||
public void showSettingsAlert(){
|
||||
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
|
||||
|
||||
// Setting Dialog Title
|
||||
alertDialog.setTitle("GPS is settings");
|
||||
|
||||
// Setting Dialog Message
|
||||
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
|
||||
|
||||
// On pressing Settings button
|
||||
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog,int which) {
|
||||
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
||||
mContext.startActivity(intent);
|
||||
}
|
||||
});
|
||||
|
||||
// on pressing cancel button
|
||||
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.cancel();
|
||||
}
|
||||
});
|
||||
|
||||
// Showing Alert Message
|
||||
alertDialog.show();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(String provider) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderDisabled(String provider) {
|
||||
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Address> getFromLocation(double latitude, double longitude, int maxResults){
|
||||
try {
|
||||
Geocoder geocoder;
|
||||
List<Address> addresses;
|
||||
geocoder = new Geocoder(this);
|
||||
if (latitude != 0 || longitude != 0) {
|
||||
addresses = geocoder.getFromLocation(latitude ,
|
||||
longitude, maxResults);
|
||||
return addresses;
|
||||
|
||||
} else {
|
||||
Toast.makeText(this, "latitude and longitude are null",
|
||||
Toast.LENGTH_LONG).show();
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get latitude
|
||||
* */
|
||||
public double getLatitude(){
|
||||
if(location != null){
|
||||
latitude = location.getLatitude();
|
||||
}else{
|
||||
Log.d("Tag", "Got NULL");
|
||||
}
|
||||
|
||||
// return latitude
|
||||
return latitude;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get longitude
|
||||
* */
|
||||
public double getLongitude(){
|
||||
if(location != null){
|
||||
longitude = location.getLongitude();
|
||||
}
|
||||
|
||||
// return longitude
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public Location getLastKnownLocation(){
|
||||
if (location == null)
|
||||
return getLocation();
|
||||
return location;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
String address="";
|
||||
|
||||
|
||||
|
||||
List<Address> listAddress = getFromLocation(getLatitude(),getLongitude(),1);
|
||||
for (int i = 0; i <= listAddress.get(0).getMaxAddressLineIndex(); i++) {
|
||||
listAddress.get(0).getAddressLine(i);
|
||||
address += listAddress.get(0).getAddressLine(i)+" ";
|
||||
}
|
||||
return address;
|
||||
}
|
||||
}
|
|
@ -40,314 +40,318 @@ import static android.Manifest.permission.READ_CONTACTS;
|
|||
*/
|
||||
public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<Cursor> {
|
||||
|
||||
/**
|
||||
* Id to identity READ_CONTACTS permission request.
|
||||
*/
|
||||
private static final int REQUEST_READ_CONTACTS = 0;
|
||||
/**
|
||||
* 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 View mProgressView;
|
||||
private View mLoginFormView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_login);
|
||||
// Set up the login form.
|
||||
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
|
||||
populateAutoComplete();
|
||||
|
||||
mPasswordView = (EditText) findViewById(R.id.password);
|
||||
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||
@Override
|
||||
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
|
||||
if (id == R.id.login || id == EditorInfo.IME_NULL) {
|
||||
attemptLogin();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
|
||||
mEmailSignInButton.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
attemptLogin();
|
||||
Intent personalSpaceActivity = new Intent(LoginActivity.this, PersonalSpaceActivity.class);
|
||||
startActivity(personalSpaceActivity);
|
||||
}
|
||||
});
|
||||
|
||||
mLoginFormView = findViewById(R.id.login_form);
|
||||
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
|
||||
* errors are presented and no actual login attempt is made.
|
||||
*/
|
||||
private void attemptLogin() {
|
||||
if (mAuthTask != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset errors.
|
||||
mEmailView.setError(null);
|
||||
mPasswordView.setError(null);
|
||||
|
||||
// Store values at the time of the login attempt.
|
||||
String email = mEmailView.getText().toString();
|
||||
String password = mPasswordView.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;
|
||||
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;
|
||||
cancel = true;
|
||||
}
|
||||
|
||||
if (cancel) {
|
||||
// There was an error; don't attempt login and focus the first
|
||||
// form field with an error.
|
||||
focusView.requestFocus();
|
||||
} else {
|
||||
// Show a progress spinner, and kick off a background task to
|
||||
// perform the user login attempt.
|
||||
showProgress(true);
|
||||
mAuthTask = new UserLoginTask(email, password);
|
||||
mAuthTask.execute((Void) null);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isEmailValid(String email) {
|
||||
//TODO: Replace this with your own logic
|
||||
return email.contains("@") && email.contains(".");
|
||||
}
|
||||
|
||||
private boolean isPasswordValid(String password) {
|
||||
//TODO: Replace this with your own logic
|
||||
return password.length() > 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the progress UI and hides the login form.
|
||||
*/
|
||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
|
||||
private void showProgress(final boolean show) {
|
||||
// On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
|
||||
// for very easy animations. If available, use these APIs to fade-in
|
||||
// the progress spinner.
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
|
||||
int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
|
||||
|
||||
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
|
||||
mLoginFormView.animate().setDuration(shortAnimTime).alpha(
|
||||
show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
});
|
||||
|
||||
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
|
||||
mProgressView.animate().setDuration(shortAnimTime).alpha(
|
||||
show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// The ViewPropertyAnimator APIs are not available, so simply show
|
||||
// and hide the relevant UI components.
|
||||
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
|
||||
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
|
||||
return new CursorLoader(this,
|
||||
// Retrieve data rows for the device user's 'profile' contact.
|
||||
Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
|
||||
ContactsContract.Contacts.Data.CONTENT_DIRECTORY), ProfileQuery.PROJECTION,
|
||||
|
||||
// Select only email addresses.
|
||||
ContactsContract.Contacts.Data.MIMETYPE +
|
||||
" = ?", new String[]{ContactsContract.CommonDataKinds.Email
|
||||
.CONTENT_ITEM_TYPE},
|
||||
|
||||
// Show primary email addresses first. Note that there won't be
|
||||
// a primary email address if the user hasn't specified one.
|
||||
ContactsContract.Contacts.Data.IS_PRIMARY + " DESC");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
|
||||
List<String> emails = new ArrayList<>();
|
||||
cursor.moveToFirst();
|
||||
while (!cursor.isAfterLast()) {
|
||||
emails.add(cursor.getString(ProfileQuery.ADDRESS));
|
||||
cursor.moveToNext();
|
||||
}
|
||||
|
||||
addEmailsToAutoComplete(emails);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<Cursor> cursorLoader) {
|
||||
|
||||
}
|
||||
|
||||
private void addEmailsToAutoComplete(List<String> emailAddressCollection) {
|
||||
//Create adapter to tell the AutoCompleteTextView what to show in its dropdown list.
|
||||
ArrayAdapter<String> 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,
|
||||
ContactsContract.CommonDataKinds.Email.IS_PRIMARY,
|
||||
/**
|
||||
* 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;
|
||||
|
||||
int ADDRESS = 0;
|
||||
int IS_PRIMARY = 1;
|
||||
}
|
||||
// UI references.
|
||||
private AutoCompleteTextView mEmailView;
|
||||
private EditText mPasswordView;
|
||||
private View mProgressView;
|
||||
private View mLoginFormView;
|
||||
|
||||
/**
|
||||
* Represents an asynchronous login/registration task used to authenticate
|
||||
* the user.
|
||||
*/
|
||||
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
|
||||
private final String REG_PATH = "http://thesocialnotework.appspot.com/api/register";
|
||||
private final String LOGIN_PATH = "http://thesocialnotework.appspot.com/api/login";
|
||||
|
||||
private final String mEmail;
|
||||
private final String mPassword;
|
||||
|
||||
UserLoginTask(String email, String password) {
|
||||
mEmail = email;
|
||||
mPassword = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... params) {
|
||||
// TODO: attempt authentication against a network service.
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_login);
|
||||
// Set up the login form.
|
||||
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
|
||||
populateAutoComplete();
|
||||
|
||||
try {
|
||||
// Simulate network access.
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
return false;
|
||||
}
|
||||
mPasswordView = (EditText) findViewById(R.id.password);
|
||||
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||
@Override
|
||||
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
|
||||
if (id == R.id.login || id == EditorInfo.IME_NULL) {
|
||||
attemptLogin();
|
||||
return true;
|
||||
}
|
||||
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);
|
||||
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
|
||||
mEmailSignInButton.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
attemptLogin();
|
||||
Intent personalSpaceActivity = new Intent(LoginActivity.this, PersonalSpaceActivity.class);
|
||||
startActivity(personalSpaceActivity);
|
||||
}
|
||||
});
|
||||
|
||||
mLoginFormView = findViewById(R.id.login_form);
|
||||
mProgressView = findViewById(R.id.login_progress);
|
||||
}
|
||||
|
||||
private void populateAutoComplete() {
|
||||
if (!mayRequestContacts()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: register the new account here.
|
||||
return true;
|
||||
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
|
||||
* errors are presented and no actual login attempt is made.
|
||||
*/
|
||||
private void attemptLogin() {
|
||||
if (mAuthTask != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset errors.
|
||||
mEmailView.setError(null);
|
||||
mPasswordView.setError(null);
|
||||
|
||||
// Store values at the time of the login attempt.
|
||||
String email = mEmailView.getText().toString();
|
||||
String password = mPasswordView.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;
|
||||
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;
|
||||
cancel = true;
|
||||
}
|
||||
|
||||
if (cancel) {
|
||||
// There was an error; don't attempt login and focus the first
|
||||
// form field with an error.
|
||||
focusView.requestFocus();
|
||||
} else {
|
||||
// Show a progress spinner, and kick off a background task to
|
||||
// perform the user login attempt.
|
||||
showProgress(true);
|
||||
mAuthTask = new UserLoginTask(email, password);
|
||||
mAuthTask.execute((Void) null);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isEmailValid(String email) {
|
||||
//TODO: Replace this with your own logic
|
||||
return email.contains("@") && email.contains(".");
|
||||
}
|
||||
|
||||
private boolean isPasswordValid(String password) {
|
||||
//TODO: Replace this with your own logic
|
||||
return password.length() > 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the progress UI and hides the login form.
|
||||
*/
|
||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
|
||||
private void showProgress(final boolean show) {
|
||||
// On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
|
||||
// for very easy animations. If available, use these APIs to fade-in
|
||||
// the progress spinner.
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
|
||||
int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
|
||||
|
||||
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
|
||||
mLoginFormView.animate().setDuration(shortAnimTime).alpha(
|
||||
show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
});
|
||||
|
||||
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
|
||||
mProgressView.animate().setDuration(shortAnimTime).alpha(
|
||||
show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// The ViewPropertyAnimator APIs are not available, so simply show
|
||||
// and hide the relevant UI components.
|
||||
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
|
||||
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(final Boolean success) {
|
||||
mAuthTask = null;
|
||||
showProgress(false);
|
||||
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
|
||||
return new CursorLoader(this,
|
||||
// Retrieve data rows for the device user's 'profile' contact.
|
||||
Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
|
||||
ContactsContract.Contacts.Data.CONTENT_DIRECTORY), ProfileQuery.PROJECTION,
|
||||
|
||||
if (success) {
|
||||
finish();
|
||||
} else {
|
||||
mPasswordView.setError(getString(R.string.error_incorrect_password));
|
||||
mPasswordView.requestFocus();
|
||||
}
|
||||
// Select only email addresses.
|
||||
ContactsContract.Contacts.Data.MIMETYPE +
|
||||
" = ?", new String[]{ContactsContract.CommonDataKinds.Email
|
||||
.CONTENT_ITEM_TYPE},
|
||||
|
||||
// Show primary email addresses first. Note that there won't be
|
||||
// a primary email address if the user hasn't specified one.
|
||||
ContactsContract.Contacts.Data.IS_PRIMARY + " DESC");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCancelled() {
|
||||
mAuthTask = null;
|
||||
showProgress(false);
|
||||
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
|
||||
List<String> emails = new ArrayList<>();
|
||||
cursor.moveToFirst();
|
||||
while (!cursor.isAfterLast()) {
|
||||
emails.add(cursor.getString(ProfileQuery.ADDRESS));
|
||||
cursor.moveToNext();
|
||||
}
|
||||
|
||||
addEmailsToAutoComplete(emails);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<Cursor> cursorLoader) {
|
||||
|
||||
}
|
||||
|
||||
private void addEmailsToAutoComplete(List<String> emailAddressCollection) {
|
||||
//Create adapter to tell the AutoCompleteTextView what to show in its dropdown list.
|
||||
ArrayAdapter<String> 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,
|
||||
ContactsContract.CommonDataKinds.Email.IS_PRIMARY,
|
||||
};
|
||||
|
||||
int ADDRESS = 0;
|
||||
int IS_PRIMARY = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents an asynchronous login/registration task used to authenticate
|
||||
* the user.
|
||||
*/
|
||||
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
|
||||
|
||||
private final String mEmail;
|
||||
private final String mPassword;
|
||||
|
||||
UserLoginTask(String email, String password) {
|
||||
mEmail = email;
|
||||
mPassword = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... params) {
|
||||
// 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.
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(final Boolean success) {
|
||||
mAuthTask = null;
|
||||
showProgress(false);
|
||||
|
||||
if (success) {
|
||||
finish();
|
||||
} else {
|
||||
mPasswordView.setError(getString(R.string.error_incorrect_password));
|
||||
mPasswordView.requestFocus();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCancelled() {
|
||||
mAuthTask = null;
|
||||
showProgress(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package com.android_app.matan.ara.sagi.thesocialnotework;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Dialog;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
@ -11,7 +15,6 @@ import android.widget.CompoundButton;
|
|||
import android.widget.EditText;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Switch;
|
||||
import android.widget.ToggleButton;
|
||||
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.Response;
|
||||
|
@ -20,35 +23,74 @@ import com.android.volley.toolbox.JsonArrayRequest;
|
|||
import com.android.volley.toolbox.JsonObjectRequest;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
//http://thesocialnotework.appspot.com/api/status | http://localhost:8080/api/note/all?uid=<userID>
|
||||
public class PersonalSpaceActivity extends AppCompatActivity {
|
||||
|
||||
protected ListView noteList;
|
||||
protected Button addBtn;
|
||||
private final String TAG = "Personal Space Activity";
|
||||
protected String url = "";
|
||||
private final int FINE_PERM = 0;
|
||||
private boolean locationPermission;
|
||||
private GPSUtils gpsUtils;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_personal_space);
|
||||
Note n1 = new Note(1, 100, 100, "location1", "My 1st Title", "ohh i'm so sexy1", System.currentTimeMillis() / 1000, true);
|
||||
Note n2 = new Note(2, 200, 200, "location2", "My 2st Title", "ohh i'm so sexy2", System.currentTimeMillis() / 1000, true);
|
||||
Note n3 = new Note(3, 300, 300, "hell", "My 3st Title", "ohh i'm so sexy3", System.currentTimeMillis() / 1000, true);
|
||||
Note n4 = new Note(4, 400, 400, "hell2", "My 4st Title", "ohh i'm so sexy4", System.currentTimeMillis() / 1000, true);
|
||||
List<Note> listOfNotes = new ArrayList<>();
|
||||
listOfNotes.add(n1);
|
||||
listOfNotes.add(n2);
|
||||
listOfNotes.add(n3);
|
||||
listOfNotes.add(n4);
|
||||
ListAdapter la = new ListAdapter(this, listOfNotes);
|
||||
|
||||
this.locationPermission = true;
|
||||
|
||||
//check for permission
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, FINE_PERM);
|
||||
// if (ContextCompat.checkSelfPermission(PersonalSpaceActivity.this,
|
||||
// Manifest.permission.INTERNET)
|
||||
// != PackageManager.PERMISSION_GRANTED) {
|
||||
// ActivityCompat.requestPermissions(PersonalSpaceActivity.this,
|
||||
// new String[]{Manifest.permission.INTERNET},
|
||||
// 1);
|
||||
// }
|
||||
|
||||
|
||||
this.noteList = (ListView) findViewById(R.id.ps_list_listview);
|
||||
addBtn = (Button) findViewById(R.id.ps_new_note_button);
|
||||
gpsUtils = new GPSUtils(this);
|
||||
|
||||
final JSONObject tempJson = new JSONObject();
|
||||
try {
|
||||
tempJson.put("username", "aran");
|
||||
tempJson.put("password", "1234");
|
||||
tempJson.put("email", "abc@a.a");
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.d(TAG, e.toString());
|
||||
}
|
||||
|
||||
//TODO remove
|
||||
Button tempBtn = (Button) findViewById(R.id.temp_btn);
|
||||
tempBtn.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
VolleyUtilSingleton.getInstance(PersonalSpaceActivity.this).newUser("http://thesocialnotework.appspot.com/api/register", tempJson);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
List<Note> listOfNotes = new ArrayList<>();
|
||||
//add demo notes to view
|
||||
addDemoNotes(listOfNotes);
|
||||
ListAdapter la = new ListAdapter(this, listOfNotes);
|
||||
noteList.setAdapter(la);
|
||||
|
||||
addBtn = (Button) findViewById(R.id.ps_new_note_button);
|
||||
addBtn.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
final Dialog dialog = new Dialog(PersonalSpaceActivity.this);
|
||||
|
@ -71,20 +113,40 @@ public class PersonalSpaceActivity extends AppCompatActivity {
|
|||
|
||||
|
||||
final Switch permissionSwitch = (Switch) dialog.findViewById(R.id.nvf_note_permission);
|
||||
EditText newTitle = (EditText) dialog.findViewById(R.id.nvf_note_title);
|
||||
EditText newBody = (EditText) dialog.findViewById(R.id.nvf_note_content);
|
||||
final EditText newTitle = (EditText) dialog.findViewById(R.id.nvf_note_title);
|
||||
final EditText newBody = (EditText) dialog.findViewById(R.id.nvf_note_content);
|
||||
Button saveBtn = (Button) dialog.findViewById(R.id.nvf_note_submit_btn);
|
||||
Button cancleBtn = (Button) dialog.findViewById(R.id.nvf_note_cancel_btn);
|
||||
Button cancelBtn = (Button) dialog.findViewById(R.id.nvf_note_cancel_btn);
|
||||
|
||||
cancleBtn.setOnClickListener(new View.OnClickListener() {
|
||||
cancelBtn.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
saveBtn.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
dialog.dismiss();
|
||||
//volley post
|
||||
final JSONObject noteJson = new JSONObject();
|
||||
try {
|
||||
// noteJson.put("id", 12345);
|
||||
noteJson.put("title", newTitle.getText());
|
||||
noteJson.put("lat", gpsUtils.getLatitude());
|
||||
noteJson.put("lng", gpsUtils.getLongitude());
|
||||
noteJson.put("address",gpsUtils.getAddress());
|
||||
noteJson.put("body",newBody.getText());
|
||||
noteJson.put("is_public",permissionSwitch.isChecked());
|
||||
// noteJson.put("tags",);
|
||||
Log.d(TAG,"Json: "+noteJson.toString());
|
||||
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.d(TAG, e.toString());
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -110,26 +172,43 @@ public class PersonalSpaceActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
|
||||
private void newNote(String show) {
|
||||
JsonArrayRequest request =
|
||||
new JsonArrayRequest(
|
||||
Request.Method.POST,
|
||||
url,
|
||||
null,
|
||||
new Response.Listener<JSONArray>() {
|
||||
@Override
|
||||
public void onResponse(JSONArray response) {
|
||||
Log.d(TAG, "success: response - " + response.toString());
|
||||
// loadData(response, true);
|
||||
}
|
||||
},
|
||||
new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
Log.d(TAG, "error: msg: " + error.getMessage());
|
||||
}
|
||||
}
|
||||
);
|
||||
VolleyUtilSingleton.getInstance(this).addToRequestQueue(request);
|
||||
public void addDemoNotes(List<Note> listOfNotes) {
|
||||
Note n1 = new Note(1, 100, 100, "location1", "My 1st Title", "ohh i'm so sexy1", System.currentTimeMillis() / 1000, true);
|
||||
Note n2 = new Note(2, 200, 200, "location2", "My 2st Title", "ohh i'm so sexy2", System.currentTimeMillis() / 1000, true);
|
||||
Note n3 = new Note(3, 300, 300, "hell", "My 3st Title", "ohh i'm so sexy3", System.currentTimeMillis() / 1000, true);
|
||||
Note n4 = new Note(4, 400, 400, "hell2", "My 4st Title", "ohh i'm so sexy4", System.currentTimeMillis() / 1000, true);
|
||||
listOfNotes.add(n1);
|
||||
listOfNotes.add(n2);
|
||||
listOfNotes.add(n3);
|
||||
listOfNotes.add(n4);
|
||||
}
|
||||
public void setLocationPermission(boolean locationPermission) {
|
||||
this.locationPermission = locationPermission;
|
||||
}
|
||||
|
||||
|
||||
// private void newUser(String url, JSONObject body) {
|
||||
// JsonObjectRequest request =
|
||||
// new JsonObjectRequest(
|
||||
// Request.Method.POST,
|
||||
// url,
|
||||
// body,
|
||||
// new Response.Listener<JSONObject>() {
|
||||
// @Override
|
||||
// public void onResponse(JSONObject response) {
|
||||
// Log.d(TAG, "success: response - " + response.toString());
|
||||
//// loadData(response, true);
|
||||
// }
|
||||
// },
|
||||
// new Response.ErrorListener() {
|
||||
// @Override
|
||||
// public void onErrorResponse(VolleyError error) {
|
||||
// Log.d(TAG, "error: msg: " + error.getMessage());
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
// VolleyUtilSingleton.getInstance(this).addToRequestQueue(request);
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,17 @@
|
|||
package com.android_app.matan.ara.sagi.thesocialnotework;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.RequestQueue;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.android.volley.toolbox.ImageLoader;
|
||||
import com.android.volley.toolbox.JsonObjectRequest;
|
||||
import com.android.volley.toolbox.Volley;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by aranz on 7/1/2016.
|
||||
*/
|
||||
|
@ -14,6 +21,7 @@ public class VolleyUtilSingleton {
|
|||
private RequestQueue mRequestQueue;
|
||||
// private ImageLoader mImageLoader;
|
||||
private static Context mCtx;
|
||||
private final String TAG = "VolleyUtilSingleton";
|
||||
|
||||
|
||||
private VolleyUtilSingleton(Context context) {
|
||||
|
@ -47,4 +55,27 @@ public class VolleyUtilSingleton {
|
|||
// return mImageLoader;
|
||||
// }
|
||||
|
||||
public void newUser(String url, JSONObject body) {
|
||||
JsonObjectRequest request =
|
||||
new JsonObjectRequest(
|
||||
Request.Method.POST,
|
||||
url,
|
||||
body,
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
Log.d(TAG, "success: response - " + response.toString());
|
||||
// loadData(response, true);
|
||||
}
|
||||
},
|
||||
new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
Log.d(TAG, "error: msg: " + error.getMessage());
|
||||
}
|
||||
}
|
||||
);
|
||||
addToRequestQueue(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,6 +23,12 @@
|
|||
android:text="Filters and Shit"
|
||||
android:layout_weight="0.8"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="New Button"
|
||||
android:id="@+id/temp_btn" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/ps_new_note_button"
|
||||
android:layout_width="0dp"
|
||||
|
|
Loading…
Reference in a new issue