2016-07-06 17:31:24 +00:00
|
|
|
package com.android_app.matan.ara.sagi.thesocialnotework;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2016-07-06 19:41:47 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2016-07-07 20:41:16 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2016-07-06 17:31:24 +00:00
|
|
|
import android.location.Location;
|
|
|
|
import android.net.Uri;
|
2016-07-07 20:41:16 +00:00
|
|
|
import android.os.AsyncTask;
|
2016-07-06 17:31:24 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
//import android.app.Fragment;
|
2016-07-06 19:41:47 +00:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
2016-07-06 17:31:24 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import com.android.volley.Response;
|
|
|
|
import com.google.android.gms.maps.CameraUpdateFactory;
|
|
|
|
import com.google.android.gms.maps.GoogleMap;
|
|
|
|
import com.google.android.gms.maps.MapFragment;
|
|
|
|
import com.google.android.gms.maps.OnMapReadyCallback;
|
|
|
|
import com.google.android.gms.maps.SupportMapFragment;
|
2016-07-07 20:41:16 +00:00
|
|
|
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
|
|
|
|
import com.google.android.gms.maps.model.CameraPosition;
|
2016-07-06 17:31:24 +00:00
|
|
|
import com.google.android.gms.maps.model.LatLng;
|
2016-07-07 20:41:16 +00:00
|
|
|
import com.google.android.gms.maps.model.Marker;
|
2016-07-06 17:31:24 +00:00
|
|
|
import com.google.android.gms.maps.model.MarkerOptions;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
2016-07-07 21:59:07 +00:00
|
|
|
import org.json.JSONException;
|
2016-07-06 17:31:24 +00:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2016-07-07 20:41:16 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2016-07-06 17:31:24 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
2016-07-07 20:41:16 +00:00
|
|
|
import java.util.Timer;
|
2016-07-06 17:31:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
public class GmapFragment extends Fragment implements OnMapReadyCallback {
|
|
|
|
// TODO: Rename parameter arguments, choose names that match
|
|
|
|
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
|
|
|
private static final String ARG_PARAM1 = "param1";
|
|
|
|
private static final String ARG_PARAM2 = "param2";
|
|
|
|
private static final String TAG = "[TSN / GmapFragment]";
|
|
|
|
private GoogleMap mMap;
|
|
|
|
private GPSUtils gpsUtils;
|
|
|
|
private MainActivity mainActivity;
|
2016-07-07 21:59:07 +00:00
|
|
|
private final int MAX_ZOOM = 16, MIN_ZOOM = 9, DEFAULT_ZOOM = 12;
|
2016-07-06 17:31:24 +00:00
|
|
|
|
|
|
|
|
2016-07-07 21:16:47 +00:00
|
|
|
public GmapFragment() {
|
|
|
|
}
|
2016-07-06 17:31:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
public static GmapFragment newInstance(String param1, String param2) {
|
|
|
|
GmapFragment fragment = new GmapFragment();
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putString(ARG_PARAM1, param1);
|
|
|
|
args.putString(ARG_PARAM2, param2);
|
|
|
|
fragment.setArguments(args);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:41:16 +00:00
|
|
|
public GoogleMap getMap() {
|
|
|
|
return mMap;
|
|
|
|
}
|
|
|
|
|
2016-07-06 17:31:24 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2016-07-06 19:41:47 +00:00
|
|
|
mainActivity = (MainActivity) getActivity();
|
2016-07-06 17:31:24 +00:00
|
|
|
|
|
|
|
gpsUtils = mainActivity.getGPSUtils();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
// Inflate the layout for this fragment
|
|
|
|
return inflater.inflate(R.layout.fragment_gmap, container, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
|
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
SupportMapFragment frag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFragment);
|
|
|
|
frag.getMapAsync(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(Context context) {
|
|
|
|
super.onAttach(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetach() {
|
|
|
|
super.onDetach();
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:41:16 +00:00
|
|
|
|
2016-07-06 17:31:24 +00:00
|
|
|
@Override
|
|
|
|
public void onMapReady(GoogleMap googleMap) {
|
|
|
|
|
|
|
|
mMap = googleMap;
|
2016-07-07 20:41:16 +00:00
|
|
|
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCameraChange(CameraPosition cameraPosition) {
|
2016-07-07 21:06:09 +00:00
|
|
|
if (cameraPosition.zoom > MAX_ZOOM) {
|
|
|
|
getMap().animateCamera(CameraUpdateFactory.zoomTo(MAX_ZOOM));
|
2016-07-07 20:41:16 +00:00
|
|
|
}
|
2016-07-07 21:06:09 +00:00
|
|
|
if (cameraPosition.zoom < MIN_ZOOM) {
|
|
|
|
getMap().animateCamera(CameraUpdateFactory.zoomTo(MIN_ZOOM));
|
2016-07-07 20:41:16 +00:00
|
|
|
}
|
2016-07-06 17:31:24 +00:00
|
|
|
|
2016-07-07 20:41:16 +00:00
|
|
|
}
|
|
|
|
});
|
2016-07-07 21:59:07 +00:00
|
|
|
|
2016-07-06 17:31:24 +00:00
|
|
|
LatLng userLocation = new LatLng(gpsUtils.getLatitude(), gpsUtils.getLongitude());
|
2016-07-07 20:41:16 +00:00
|
|
|
// mMap.addMarker(new MarkerOptions().position(userLocation).title("I Am Here!"));
|
2016-07-06 19:41:47 +00:00
|
|
|
if (ActivityCompat.checkSelfPermission(mainActivity, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mainActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mMap.setMyLocationEnabled(true);
|
2016-07-07 21:59:07 +00:00
|
|
|
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, DEFAULT_ZOOM));
|
2016-07-06 17:31:24 +00:00
|
|
|
|
2016-07-07 21:59:07 +00:00
|
|
|
//get my notes
|
2016-07-07 21:18:40 +00:00
|
|
|
VolleyUtilSingleton.getInstance(getActivity()).get(Utils.BASE_URL + "/note/all?uid=" + mainActivity.getUserId(), getNotesSuccessListener, Utils.genericErrorListener);
|
|
|
|
|
2016-07-07 21:59:07 +00:00
|
|
|
JSONObject jsonObj = new JSONObject();
|
|
|
|
try {
|
|
|
|
jsonObj.put("id",mainActivity.getUserId());
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
//get other notes
|
|
|
|
VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/getPublic", jsonObj, getNotesSuccessListener, Utils.genericErrorListener);
|
|
|
|
|
2016-07-07 21:18:40 +00:00
|
|
|
|
2016-07-06 17:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//response listener for getting all user notes
|
|
|
|
Response.Listener<JSONObject> getNotesSuccessListener = new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
|
|
|
Log.d(TAG, "getNotesSuccessListener: " + response.toString());
|
|
|
|
List<Note> listOfNotes = new ArrayList<>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
//need to get all notes and add to listOfNotes
|
|
|
|
JSONArray noteObjectsArray = response.getJSONArray("notes");
|
|
|
|
Date time = new Date();
|
|
|
|
for (int i = 0; i < noteObjectsArray.length(); i++) {
|
|
|
|
JSONObject noteObject = noteObjectsArray.getJSONObject(i);
|
|
|
|
time.setTime(noteObject.getLong("created_at"));
|
2016-07-07 21:06:09 +00:00
|
|
|
listOfNotes.add(Utils.getNoteFromJsonObj(noteObject, time));
|
2016-07-06 17:31:24 +00:00
|
|
|
}
|
2016-07-07 20:41:16 +00:00
|
|
|
new getMarkersFromNotes(mMap).execute(listOfNotes);
|
2016-07-06 17:31:24 +00:00
|
|
|
// noteList.setAdapter(noteListAdapter);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(TAG, "newNoteSuccess:" + e.getMessage());
|
2016-07-07 20:41:16 +00:00
|
|
|
e.printStackTrace();
|
2016-07-06 17:31:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-07-07 20:41:16 +00:00
|
|
|
private class getMarkersFromNotes extends AsyncTask<List<Note>, MarkerOptions, List<MarkerOptions>> {
|
|
|
|
GoogleMap mMap;
|
|
|
|
|
|
|
|
public getMarkersFromNotes(GoogleMap map) {
|
|
|
|
mMap = map;
|
|
|
|
Log.d(TAG, "in async ctor");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onProgressUpdate(MarkerOptions... mo) {
|
|
|
|
mMap.addMarker(mo[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(List<MarkerOptions> markerOptionList) {
|
|
|
|
for (MarkerOptions mo : markerOptionList) {
|
|
|
|
mMap.addMarker(mo);
|
|
|
|
}
|
|
|
|
Log.d(TAG, "in async post");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<MarkerOptions> doInBackground(List<Note>... listOfNotes) {
|
|
|
|
Log.d(TAG, "in async BG");
|
|
|
|
|
|
|
|
List<MarkerOptions> markerOptionList = new ArrayList<>();
|
|
|
|
// for (int i = 0 ; i< listOfNotes.length; i++)
|
|
|
|
for (Note n : listOfNotes[0]) {
|
|
|
|
// markerOptionList.add(
|
|
|
|
MarkerOptions mo = new MarkerOptions()
|
|
|
|
.title(n.getTitle())
|
|
|
|
.position(new LatLng(n.getLat(), n.getLon()))
|
|
|
|
.snippet(n.getBody())
|
2016-07-07 21:59:07 +00:00
|
|
|
.icon(BitmapDescriptorFactory.fromBitmap(Bitmap.createScaledBitmap(Utils.getBitmapFromURL(n.getAvatar()), 80, 80, false)));
|
2016-07-07 20:41:16 +00:00
|
|
|
publishProgress(mo);
|
|
|
|
// );
|
|
|
|
|
|
|
|
}
|
|
|
|
return markerOptionList;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-06 17:31:24 +00:00
|
|
|
}
|