the-social-notebook-android/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java

480 lines
20 KiB
Java
Raw Normal View History

2016-07-06 17:31:24 +00:00
package com.android_app.matan.ara.sagi.thesocialnotework;
import android.app.AlertDialog;
import android.app.Dialog;
2016-07-06 17:31:24 +00:00
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
2016-07-06 17:31:24 +00:00
import android.location.Location;
import android.net.Uri;
import android.os.AsyncTask;
2016-07-06 17:31:24 +00:00
import android.os.Bundle;
//import android.app.Fragment;
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 android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
2016-07-06 17:31:24 +00:00
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-08 18:29:26 +00:00
import com.google.android.gms.maps.model.BitmapDescriptor;
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;
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;
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.HashMap;
2016-07-06 17:31:24 +00:00
import java.util.List;
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;
private HashMap<Marker, Note> eventMarkerMap;
private ImageButton dateFilter;
private ImageButton locationFilter;
private ImageButton userFilter;
private Button map_small_filter;
private Button map_medium_filter;
private Button map_large_filter;
private LinearLayout mapFilters;
private boolean dateFilterIsVisible = false;
private boolean locationFilterIsVisible = false;
private boolean userFilterIsVisible = false;
private final String day = "24 hours";
private final String week = "Week";
private final String month = "Month";
private final String hundredMeters = "100 meters";
private final String kilometer = "1 Km";
private final String threeKilometer = "3 Km";
private final String mine = "mine";
2016-07-09 16:04:11 +00:00
private final String others = "others";
private final String all = "all";
2016-07-06 17:31:24 +00:00
public GmapFragment() {
eventMarkerMap = new HashMap<Marker, Note>();
}
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;
}
public GoogleMap getMap() {
return mMap;
}
2016-07-06 17:31:24 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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
View view = inflater.inflate(R.layout.fragment_personal, container, false);
2016-07-06 17:31:24 +00:00
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);
dateFilter = (ImageButton) view.findViewById(R.id.map_date_filter);
locationFilter = (ImageButton) view.findViewById(R.id.map_location_filter);
userFilter = (ImageButton) view.findViewById(R.id.map_user_filter);
map_small_filter = (Button) view.findViewById(R.id.map_small_filter);
map_medium_filter = (Button) view.findViewById(R.id.map_medium_filter);
map_large_filter = (Button) view.findViewById(R.id.map_large_filter);
mapFilters = (LinearLayout) view.findViewById(R.id.map_filter_options);
dateFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2016-07-09 16:04:11 +00:00
// Log.d(TAG, "" + v.getId());
if (dateFilterIsVisible) {
dateFilterIsVisible = false;
mapFilters.setVisibility(View.INVISIBLE);
} else {
mapFilters.setVisibility(View.VISIBLE);
dateFilterIsVisible = true;
locationFilterIsVisible = false;
userFilterIsVisible = false;
// set text button in the right filter string
map_small_filter.setText(day);
map_medium_filter.setText(week);
map_large_filter.setText(month);
}
}
});
locationFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (locationFilterIsVisible) {
locationFilterIsVisible = false;
mapFilters.setVisibility(View.INVISIBLE);
} else {
mapFilters.setVisibility(View.VISIBLE);
locationFilterIsVisible = true;
dateFilterIsVisible = false;
userFilterIsVisible = false;
// set text button in the right filter string
map_small_filter.setText(hundredMeters);
map_medium_filter.setText(kilometer);
map_large_filter.setText(threeKilometer);
}
}
});
userFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (userFilterIsVisible) {
userFilterIsVisible = false;
mapFilters.setVisibility(View.INVISIBLE);
} else {
mapFilters.setVisibility(View.VISIBLE);
userFilterIsVisible = true;
dateFilterIsVisible = false;
locationFilterIsVisible = false;
// set text button in the right filter string
map_small_filter.setText(mine);
2016-07-09 16:04:11 +00:00
map_medium_filter.setText(others);
map_large_filter.setText(all);
}
}
});
2016-07-06 17:31:24 +00:00
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
2016-07-06 17:31:24 +00:00
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
if (cameraPosition.zoom > MAX_ZOOM) {
getMap().animateCamera(CameraUpdateFactory.zoomTo(MAX_ZOOM));
}
if (cameraPosition.zoom < MIN_ZOOM) {
getMap().animateCamera(CameraUpdateFactory.zoomTo(MIN_ZOOM));
}
2016-07-06 17:31:24 +00:00
}
});
mMap.setInfoWindowAdapter(infoWindowAdapter);
2016-07-07 21:59:07 +00:00
2016-07-06 17:31:24 +00:00
LatLng userLocation = new LatLng(gpsUtils.getLatitude(), gpsUtils.getLongitude());
// mMap.addMarker(new MarkerOptions().position(userLocation).title("I Am Here!"));
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
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());
2016-07-07 21:59:07 +00:00
} catch (JSONException e) {
e.printStackTrace();
}
//get other notes
VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/getPublic", jsonObj, getNotesSuccessListener, Utils.genericErrorListener);
2016-07-06 17:31:24 +00:00
}
GoogleMap.InfoWindowAdapter infoWindowAdapter = new GoogleMap.InfoWindowAdapter() { // Use default InfoWindow frame
@Override
public View getInfoWindow(Marker args) {
return null;
}
// Defines the contents of the InfoWindow
@Override
public View getInfoContents(Marker args) {
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
public void onInfoWindowClick(final Marker marker) {
final Note note = eventMarkerMap.get(marker);
final Dialog noteViewDialog = new Dialog(getActivity());
noteViewDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
noteViewDialog.setContentView(R.layout.note_display_full);
boolean isOwner = note.getOwnerId().equals(mainActivity.getUserId());
// if (isOwner)
// noteViewDialog.setTitle("You wrote...");
// else
// noteViewDialog.setTitle("Someone wrote...");
noteViewDialog.show();
//get note_view_full layout elements
final TextView title = (TextView) noteViewDialog.findViewById(R.id.ndf_title_textview);
final TextView body = (TextView) noteViewDialog.findViewById(R.id.ndf_body_textview);
final TextView time = (TextView) noteViewDialog.findViewById(R.id.ndf_time_textview);
final TextView date = (TextView) noteViewDialog.findViewById(R.id.ndf_date_textview);
final TextView location = (TextView) noteViewDialog.findViewById(R.id.ndf_address_textview);
final TextView likes = (TextView) noteViewDialog.findViewById(R.id.ndf_likes_textview);
// final TextView tags = (TextView) noteViewDialog.findViewById(R.id.ndf_tags_textview);
final TextView permission = (TextView) noteViewDialog.findViewById(R.id.ndf_permission_textview);
final ImageView avatar = (ImageView) noteViewDialog.findViewById(R.id.note_user_avatar);
final ImageView permissionImg = (ImageView) noteViewDialog.findViewById(R.id.permission_image);
final ImageButton deleteBtn = (ImageButton) noteViewDialog.findViewById(R.id.ndf_delete_imagebutton);
title.setText(note.getTitle());
body.setText(note.getBody());
time.setText(note.getTime());
date.setText(note.getDate());
location.setText("" + note.getAddress());
likes.setText("" + note.getLikes());
// tags.setText("Tags: " + note.getTags().toString());
Utils.URLtoImageView(avatar, note.getAvatar());
if (isOwner) {
permission.setText("" + (note.isPublic() ? "Public" : "Private"));
} else {
permissionImg.setVisibility(View.INVISIBLE);
permission.setText("");
deleteBtn.setBackgroundResource(R.drawable.like_icon);
}
2016-07-08 13:32:29 +00:00
if (isOwner)
{
deleteBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Put up the Yes/No message box
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder
.setTitle("Delete Note")
.setMessage("Are you sure you want to delete the note?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Yes button clicked, do something
Toast.makeText(getActivity(), "Item Deleted!",
Toast.LENGTH_SHORT).show();
//TODO send delete
JSONObject delNote = new JSONObject();
try {
delNote.put("uid", mainActivity.getUserId());
delNote.put("nid", note.getId());
VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/delete", delNote, Utils.deleteNoteSuccessListener, Utils.genericErrorListener);
// listOfNotes.remove(position);
marker.remove();
} catch (JSONException e) {
Toast.makeText(getActivity(), "Something went wrong.\n Failed to delete note...", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
// noteList.setAdapter(noteListAdapter);
noteViewDialog.dismiss();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Yes button clicked, do something
Toast.makeText(getActivity(), "Canceled",
Toast.LENGTH_SHORT).show();
noteViewDialog.dismiss();
}
}) //Do nothing on no
.show();
}
});
}
2016-07-08 13:32:29 +00:00
else{
//like Btn
deleteBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//add like only if user is didnt like already
if (!mainActivity.getUser().getLiked_notes().contains(note.getId())) {
JSONObject jsonObj = new JSONObject();
try {
jsonObj.put("uid", mainActivity.getUserId());
jsonObj.put("nid", note.getId());
} catch (JSONException e) {
e.printStackTrace();
}
VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/like", jsonObj, getNotesSuccessListener, Utils.genericErrorListener);
mainActivity.getUser().getLiked_notes().add(note.getId());
2016-07-09 14:38:54 +00:00
mainActivity.getUser().updateUser(mainActivity);
2016-07-08 13:32:29 +00:00
likes.setText("Likes: "+(note.getLikes()+1));
}
}
});
}
}
2016-07-08 13:32:29 +00:00
});
return null;
}
2016-07-08 13:32:29 +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"));
listOfNotes.add(Utils.getNoteFromJsonObj(noteObject, time));
2016-07-06 17:31:24 +00:00
}
new getMarkersFromNotes(mMap, eventMarkerMap).execute(listOfNotes);
2016-07-06 17:31:24 +00:00
} catch (Exception e) {
Log.e(TAG, "newNoteSuccess:" + e.getMessage());
e.printStackTrace();
2016-07-06 17:31:24 +00:00
}
}
};
2016-07-08 23:24:32 +00:00
private class getMarkersFromNotes extends AsyncTask<List<Note>, MarkerNoteStruct, Void> {
GoogleMap mMap;
HashMap<Marker, Note> eventMarkerMap;
public getMarkersFromNotes(GoogleMap map, HashMap<Marker, Note> eventMarkerMap) {
Log.d(TAG, "in async ctor");
this.eventMarkerMap = eventMarkerMap;
mMap = map;
}
@Override
protected void onProgressUpdate(MarkerNoteStruct... mo) {
eventMarkerMap.put(mMap.addMarker(mo[0].getMarker()),mo[0].getNote());
}
@Override
protected Void doInBackground(List<Note>... listOfNotes) {
Log.d(TAG, "in async BG");
2016-07-08 18:29:26 +00:00
BitmapDescriptor b;
for (Note n : listOfNotes[0]) {
2016-07-08 18:29:26 +00:00
b = BitmapDescriptorFactory.fromBitmap(Bitmap.createScaledBitmap(Utils.getBitmapFromURL(n.getAvatar()), 80, 80, false));
MarkerOptions mo = new MarkerOptions()
.title(n.getTitle())
.position(new LatLng(n.getLat(), n.getLon()))
.snippet(n.getBody())
2016-07-08 18:29:26 +00:00
.icon(b);
publishProgress(new MarkerNoteStruct(n,mo));
}
return null;
}
}
// public Marker placeMarker(Note eventInfo) {
//
// Marker m = getMap().addMarker(new MarkerOptions()
//
// .position(eventInfo.getLatLong())
//
// .title(eventInfo.getName()));
//
//
//
// return m;
//
// }
2016-07-06 17:31:24 +00:00
}