added location logger & json

This commit is contained in:
aranzaiger 2016-07-02 00:28:39 +03:00
parent 41b4b47a36
commit e393552fea
6 changed files with 708 additions and 329 deletions

View file

@ -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" />

View file

@ -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;
}
}

View file

@ -63,6 +63,10 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
private View mProgressView;
private View mLoginFormView;
private final String REG_PATH = "http://thesocialnotework.appspot.com/api/register";
private final String LOGIN_PATH = "http://thesocialnotework.appspot.com/api/login";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

View file

@ -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);
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);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "error: msg: " + error.getMessage());
}
}
);
VolleyUtilSingleton.getInstance(this).addToRequestQueue(request);
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);
// }
}

View file

@ -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);
}
}

View file

@ -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"