diff --git a/app/src/main/java/il/co/anyway/app/AccidentDetailsDialogFragment.java b/app/src/main/java/il/co/anyway/app/AccidentDetailsDialogFragment.java index 23df0c8..dd9813e 100644 --- a/app/src/main/java/il/co/anyway/app/AccidentDetailsDialogFragment.java +++ b/app/src/main/java/il/co/anyway/app/AccidentDetailsDialogFragment.java @@ -17,7 +17,6 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { // get saved accident data Bundle mArgs = getArguments(); - Long id = mArgs.getLong("id"); String description = mArgs.getString("description"); String titleBySubType = mArgs.getString("titleBySubType"); @@ -36,16 +35,19 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { builder.setNeutralButton(getString(R.string.address_not_found_close), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - + dialog.cancel(); } }); // update dialog TextViews with the accident details - TextView tv = (TextView) v.findViewById(R.id.accident_details_desc); - tv.setText(created + "\n" + address + "\n" + description); + TextView tv; + tv = (TextView) v.findViewById(R.id.accident_details_desc); + if (tv != null) + tv.setText(created + "\n" + address + "\n" + description); tv = (TextView) v.findViewById(R.id.accident_details_title); - tv.setText(titleBySubType); + if (tv != null) + tv.setText(titleBySubType); return builder.create(); } diff --git a/app/src/main/java/il/co/anyway/app/AccidentsManager.java b/app/src/main/java/il/co/anyway/app/AccidentsManager.java index 4b8cf85..0424c78 100644 --- a/app/src/main/java/il/co/anyway/app/AccidentsManager.java +++ b/app/src/main/java/il/co/anyway/app/AccidentsManager.java @@ -1,8 +1,6 @@ package il.co.anyway.app; - import android.util.Log; - import java.util.ArrayList; import java.util.List; @@ -10,16 +8,33 @@ public class AccidentsManager { public static final boolean DO_RESET = true; public static final boolean DO_NOT_RESET = false; + private final String LOG_TAG = AccidentsManager.class.getSimpleName(); + private static AccidentsManager instance = null; private List accidentsList; - public AccidentsManager() { + // making the default constructor private make sure there will be only one instance of the accidents manager + private AccidentsManager() { accidentsList = new ArrayList<>(); } + public static AccidentsManager getInstance() { + if (instance == null) + instance = new AccidentsManager(); + return instance; + } + + /** + * check if an accident exist in the accident manager + * @param toCheck the Accident object to check + * @return true if exist, false if not or toCheck is null + */ private boolean isAccidentExist(Accident toCheck) { + if (toCheck == null) + return false; + for (Accident a : accidentsList) if (a.getId() == toCheck.getId()) { @@ -29,8 +44,16 @@ private boolean isAccidentExist(Accident toCheck) { return false; } + /** + * Add accident to the list + * @param toAdd the Accident object to add + * @return true if accident added, false if accident already exist, or toAdd is null + */ public boolean addAccident(Accident toAdd) { + if (toAdd == null) + return false; + if (!isAccidentExist(toAdd)) { accidentsList.add(toAdd); //TODO updateMap(); @@ -40,11 +63,20 @@ public boolean addAccident(Accident toAdd) { return true; } + /** + * Add a list of accidents to the list + * @param toAddList the list of Accident objects + * @param reset use to select if you want to reset the list before adding the new list + * @return How many accidents from the list actually taken(duplicate accident will ignore) + */ public int addAllAccidents(List toAddList, boolean reset) { if (reset == DO_RESET) accidentsList.clear(); + if (toAddList == null) + return 0; + int counter = 0; for (Accident a : toAddList) { @@ -60,8 +92,16 @@ public int addAllAccidents(List toAddList, boolean reset) { return counter; } + /** + * find a accident by it's marker ID + * @param markerID the marker id + * @return The Accident object if found, null if not found + */ public Accident getAccidentByMarkerID(String markerID) { + if (markerID == null) + return null; + for (Accident a : accidentsList) { if (a.getMarkerID().equals(markerID)) { return a; @@ -71,10 +111,18 @@ public Accident getAccidentByMarkerID(String markerID) { return null; } + /** + * + * @return the list of all accidents in the list + */ public List getAllAccidents() { return accidentsList; } + /** + * Get all the accidents that not on the map + * @return a list of accidents that not on the map + */ public List getAllNewAccidents() { List newAccidents = new ArrayList<>(); diff --git a/app/src/main/java/il/co/anyway/app/DisqusActivity.java b/app/src/main/java/il/co/anyway/app/DisqusActivity.java index 5f57cc3..1df2a5d 100644 --- a/app/src/main/java/il/co/anyway/app/DisqusActivity.java +++ b/app/src/main/java/il/co/anyway/app/DisqusActivity.java @@ -2,10 +2,15 @@ import android.app.Activity; import android.content.Intent; +import android.graphics.Bitmap; +import android.location.Address; +import android.location.Geocoder; +import android.net.Uri; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -14,17 +19,31 @@ import android.os.Build; import android.view.Window; import android.webkit.WebChromeClient; +import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; +import com.google.android.gms.maps.model.LatLng; + +import java.io.IOException; +import java.net.URL; +import java.util.List; +import java.util.Locale; + public class DisqusActivity extends ActionBarActivity { - private static final String DISQUS_ID = "testforanyway"; public static final String DISQUS_LOCATION_ID = "il.co.anyway.app.DISQUS_LOCATION"; + private static final String DISQUS_SHORT_NAME = "testforanyway"; + private static final String BASE_URL = "https://anywaydisqus.herokuapp.com/"; + private static final int PRECISION_LEVEL_OF_LOCATION = 6; + WebView mWebView; + String mDisqusPostID; + String mUrl; + String mTitle; @Override protected void onCreate(Bundle savedInstanceState) { @@ -32,13 +51,31 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_disqus); - mWebView = (WebView)findViewById(R.id.disqus); - mWebView.getSettings().setJavaScriptEnabled(true); - + // get the location of the discussion Intent intent = getIntent(); - String location = intent.getStringExtra(DISQUS_LOCATION_ID); + LatLng location = (LatLng)intent.getExtras().get(DISQUS_LOCATION_ID); - showDisqus(location); + // set the id of the discussion + // it's the PRECISION_LEVEL_OF_LOCATION numbers of the latitude and then same of the longitude(without the dot) + mDisqusPostID = Double.toString(location.latitude).substring(0,PRECISION_LEVEL_OF_LOCATION).replace(".", "") + + Double.toString(location.longitude).substring(0,PRECISION_LEVEL_OF_LOCATION).replace(".", ""); + + // TODO - find what the title of the page should be + mTitle = mDisqusPostID; + + // build url of Disqus + Uri builtUri = Uri.parse(BASE_URL).buildUpon() + .appendQueryParameter("diqus_shortname", DISQUS_SHORT_NAME) + .appendQueryParameter("disqus_id", mDisqusPostID) + .appendQueryParameter("title", mTitle) + .build(); + mUrl = builtUri.toString(); + + // get the web view + mWebView = (WebView)findViewById(R.id.disqus); + if(mWebView != null) { + showDisqus(); + } } @Override @@ -56,46 +93,54 @@ public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { + if (id == R.id.action_refresh_comments) { + mWebView.loadUrl(mUrl); return true; } return super.onOptionsItemSelected(item); } + private void showDisqus() { - private void showDisqus(String zone) { + WebSettings webSettings = mWebView.getSettings(); - mWebView.getSettings().setJavaScriptEnabled(true); + // enable javascript + webSettings.setJavaScriptEnabled(true); - final Activity activity = this; + mWebView.requestFocusFromTouch(); + + // the WebChromeClient is for links to be open inside the app and not in another browser mWebView.setWebChromeClient(new WebChromeClient() { }); + + // the WebViewClient is here to solve a bug in the login procedure of Disqus + // when login the page stays show "busy" icon and do nothing. + // here we catch that situation and handle it mWebView.setWebViewClient(new WebViewClient() { + @Override + public void onPageFinished(WebView view, String url) { + super.onPageFinished(view, url); + + if(url.indexOf("logout")>-1 || url.indexOf("disqus.com/next/login-success")>-1 ){ + view.loadUrl(mUrl); + + } + if(url.indexOf("disqus.com/_ax/twitter/complete")>-1||url.indexOf("disqus.com/_ax/facebook/complete")>-1||url.indexOf("disqus.com/_ax/google/complete")>-1){ + view.loadUrl(BASE_URL + "login.php"); + + } + if(url.indexOf(BASE_URL + "login.php") > -1) { + view.loadUrl(mUrl); + } + } + public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { - Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); + Log.i("disqus error", "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]"); } }); - String htmlComments = getHtmlComment(zone, DISQUS_ID); - mWebView.loadDataWithBaseURL("http://" + DISQUS_ID + ".disqus.com/", htmlComments, "text/html", "UTF-8", ""); - - } - - private String getHtmlComment(String idPost, String shortName) { - - return "
" - + ""; + mWebView.loadUrl(mUrl); } } diff --git a/app/src/main/java/il/co/anyway/app/EnableGpsDialogFragment.java b/app/src/main/java/il/co/anyway/app/EnableGpsDialogFragment.java index 56d575a..4301651 100644 --- a/app/src/main/java/il/co/anyway/app/EnableGpsDialogFragment.java +++ b/app/src/main/java/il/co/anyway/app/EnableGpsDialogFragment.java @@ -18,7 +18,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { - // Start activity + // Start location settings activity Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); @@ -27,6 +27,7 @@ public void onClick(DialogInterface dialog, int id) { .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog + dialog.cancel(); } }); // Create the AlertDialog object and return it diff --git a/app/src/main/java/il/co/anyway/app/FetchAccidents.java b/app/src/main/java/il/co/anyway/app/FetchAccidents.java index 58ee169..9457f0b 100644 --- a/app/src/main/java/il/co/anyway/app/FetchAccidents.java +++ b/app/src/main/java/il/co/anyway/app/FetchAccidents.java @@ -42,9 +42,9 @@ protected List doInBackground(String... params) { try { // Construct the URL for the Anyway accidents query - final String FORECAST_BASE_URL = "http://www.anyway.co.il/markers?"; + final String ANYWAY_BASE_URL = "http://www.anyway.co.il/markers?"; - Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon() + Uri builtUri = Uri.parse(ANYWAY_BASE_URL).buildUpon() .appendQueryParameter("ne_lat", params[Utility.JSON_STRING_NE_LAT]) .appendQueryParameter("ne_lng", params[Utility.JSON_STRING_NE_LNG]) .appendQueryParameter("sw_lat", params[Utility.JSON_STRING_SW_LAT]) @@ -134,6 +134,10 @@ protected void onPostExecute(List accidents) { } } + /** + * set the calling activity, this activity will be called when the accidents fetching ends + * @param callingActivity the calling activity (MainActivity instance) + */ public void setCallingActivity(MainActivity callingActivity) { this.callingActivity = callingActivity; } diff --git a/app/src/main/java/il/co/anyway/app/MainActivity.java b/app/src/main/java/il/co/anyway/app/MainActivity.java index f216db3..1f447b0 100644 --- a/app/src/main/java/il/co/anyway/app/MainActivity.java +++ b/app/src/main/java/il/co/anyway/app/MainActivity.java @@ -41,8 +41,6 @@ public class MainActivity extends ActionBarActivity implements OnInfoWindowClickListener, OnMapLongClickListener, OnCameraChangeListener, LocationListener { - - @SuppressWarnings("unused") private final String LOG_TAG = MainActivity.class.getSimpleName(); @@ -61,7 +59,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - mAccidentsManager = new AccidentsManager(); + mAccidentsManager = AccidentsManager.getInstance(); // first run set to true only when this is the first time onCreate called // used to handle the case of screen rotation @@ -160,16 +158,8 @@ public void onInfoWindowClick(Marker marker) { @Override public void onMapLongClick(LatLng latLng) { - double latitude = latLng.latitude; - double longitude = latLng.longitude; - - String disqusID = Double.toString(latitude).substring(0,5) + "|" - + Double.toString(longitude).substring(0,5); - - Toast.makeText(this, "disqusID:" + disqusID, Toast.LENGTH_LONG).show(); - Intent disqusIntent = new Intent(this, DisqusActivity.class); - disqusIntent.putExtra(DisqusActivity.DISQUS_LOCATION_ID, disqusID); + disqusIntent.putExtra(DisqusActivity.DISQUS_LOCATION_ID, latLng); startActivity(disqusIntent); } @@ -327,7 +317,7 @@ private void setUpMap(boolean firstRun) { // Disable toolbar on the right bottom corner(taking user to google maps app) mMap.getUiSettings().setMapToolbarEnabled(false); - mMap.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater())); + mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter(getLayoutInflater())); mMap.setOnInfoWindowClickListener(this); mMap.setOnMapLongClickListener(this); mMap.setOnCameraChangeListener(this); diff --git a/app/src/main/java/il/co/anyway/app/MarkerInfoWindowAdapter.java b/app/src/main/java/il/co/anyway/app/MarkerInfoWindowAdapter.java new file mode 100644 index 0000000..41fffec --- /dev/null +++ b/app/src/main/java/il/co/anyway/app/MarkerInfoWindowAdapter.java @@ -0,0 +1,48 @@ +package il.co.anyway.app; + +import android.annotation.SuppressLint; + +import android.view.LayoutInflater; +import android.view.View; +import android.widget.TextView; + +import com.google.android.gms.maps.GoogleMap; +import com.google.android.gms.maps.model.Marker; + +class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter { + + @SuppressWarnings("unused") + private final String LOG_TAG = MarkerInfoWindowAdapter.class.getSimpleName(); + + private View mInfoWindows = null; + private LayoutInflater mInflater = null; + + MarkerInfoWindowAdapter(LayoutInflater inflater) { + this.mInflater = inflater; + } + + @Override + public View getInfoWindow(Marker marker) { + return (null); + } + + @SuppressLint("InflateParams") + @Override + public View getInfoContents(Marker marker) { + + if (mInfoWindows == null) + mInfoWindows = mInflater.inflate(R.layout.marker_info_window, null); + + TextView tv; + + tv = (TextView) mInfoWindows.findViewById(R.id.title); + if (tv != null) + tv.setText(marker.getTitle()); + + tv = (TextView) mInfoWindows.findViewById(R.id.snippet); + if (tv != null) + tv.setText(marker.getSnippet()); + + return (mInfoWindows); + } +} \ No newline at end of file diff --git a/app/src/main/java/il/co/anyway/app/PopupAdapter.java b/app/src/main/java/il/co/anyway/app/PopupAdapter.java deleted file mode 100644 index 774e069..0000000 --- a/app/src/main/java/il/co/anyway/app/PopupAdapter.java +++ /dev/null @@ -1,44 +0,0 @@ -package il.co.anyway.app; - -import android.annotation.SuppressLint; - -import android.view.LayoutInflater; -import android.view.View; -import android.widget.TextView; - -import com.google.android.gms.maps.GoogleMap; -import com.google.android.gms.maps.model.Marker; - -class PopupAdapter implements GoogleMap.InfoWindowAdapter { - - @SuppressWarnings("unused") - private final String LOG_TAG = PopupAdapter.class.getSimpleName(); - - private View popup = null; - private LayoutInflater inflater = null; - - PopupAdapter(LayoutInflater inflater) { - this.inflater = inflater; - } - - @Override - public View getInfoWindow(Marker marker) { - return (null); - } - - @SuppressLint("InflateParams") - @Override - public View getInfoContents(Marker marker) { - - if (popup == null) { - popup = inflater.inflate(R.layout.popup, null); - } - - TextView tv = (TextView) popup.findViewById(R.id.title); - tv.setText(marker.getTitle()); - tv = (TextView) popup.findViewById(R.id.snippet); - tv.setText(marker.getSnippet()); - - return (popup); - } -} \ No newline at end of file diff --git a/app/src/main/java/il/co/anyway/app/Utility.java b/app/src/main/java/il/co/anyway/app/Utility.java index de19a2a..b9d9b2c 100644 --- a/app/src/main/java/il/co/anyway/app/Utility.java +++ b/app/src/main/java/il/co/anyway/app/Utility.java @@ -50,6 +50,7 @@ public static List getAccidentDataFromJson(String accidentJsonStr) // These are the names of the JSON objects that need to be extracted. final String ACCIDENT_LIST = "markers"; + final String ACCIDENT_ADDRESS = "address"; final String ACCIDENT_CREATED = "created"; final String ACCIDENT_DESC = "description"; @@ -63,7 +64,6 @@ public static List getAccidentDataFromJson(String accidentJsonStr) final String ACCIDENT_TITLE = "title"; // user, following, followers - not implemented right now at anyway - // TODO - implement users interface long user = 0; JSONObject accidentJson = new JSONObject(accidentJsonStr); @@ -75,35 +75,31 @@ public static List getAccidentDataFromJson(String accidentJsonStr) // Get the JSON object representing the day JSONObject accidentDetails = accidentsArray.getJSONObject(i); - // Date comes as 2013-12-30T21:00:00, needs to be converted - String created = accidentDetails.getString(ACCIDENT_CREATED); - Date createdDate = new Date(); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); - try { - createdDate = sdf.parse(created); - } catch (ParseException e) { - Log.e(LOG_TAG, e.getLocalizedMessage()); - e.printStackTrace(); - } - try { + // Date comes as 2013-12-30T21:00:00, needs to be converted + String created = accidentDetails.getString(ACCIDENT_CREATED); + Date createdDate = new Date(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + try { + createdDate = sdf.parse(created); + } catch (ParseException e) { + Log.e(LOG_TAG, e.getLocalizedMessage()); + e.printStackTrace(); + } String address = accidentDetails.getString(ACCIDENT_ADDRESS); String desc = accidentDetails.getString(ACCIDENT_DESC); String title = accidentDetails.getString(ACCIDENT_TITLE); Long id = accidentDetails.getLong(ACCIDENT_ID); - - // Log.d(LOG_TAG, "Accident id:" + id); - Double lat = accidentDetails.getDouble(ACCIDENT_LATITUDE); Double lng = accidentDetails.getDouble(ACCIDENT_LONGITUDE); - LatLng location = new LatLng(lat, lng); - Integer accuracy = accidentDetails.getInt(ACCIDENT_LOCATOIN_ACCURACY); Integer severity = accidentDetails.getInt(ACCIDENT_SEVERITY); Integer type = accidentDetails.getInt(ACCIDENT_TYPE); Integer subtype = accidentDetails.getInt(ACCIDENT_SUBTYPE); + LatLng location = new LatLng(lat, lng); + Accident acc = new Accident() .setId(id) .setUser(user) @@ -115,8 +111,7 @@ public static List getAccidentDataFromJson(String accidentJsonStr) .setCreated(createdDate) .setLocation(location) .setAddress(address) - .setLocationAccuracy(accuracy) - .setMarkerID(null); + .setLocationAccuracy(accuracy); resultList.add(acc); } catch (JSONException e) { @@ -136,6 +131,9 @@ public static List getAccidentDataFromJson(String accidentJsonStr) */ public static void getAccidentsFromASyncTask(LatLngBounds bounds, int zoomLevel, MainActivity callingActivity) { + if (bounds == null || callingActivity == null) + return; + FetchAccidents accidentTask = new FetchAccidents(); String[] params = new String[JSON_STRING_PARAMETERS_COUNT]; @@ -157,8 +155,8 @@ public static void getAccidentsFromASyncTask(LatLngBounds bounds, int zoomLevel, String toDate = sharedPrefs.getString(callingActivity.getString(R.string.pref_to_date_key), callingActivity.getString(R.string.pref_default_to_date)); // getting timestamp for Anyway API - params[JSON_STRING_START_DATE] = Utility.getTimeStamp(fromDate); - params[JSON_STRING_END_DATE] = Utility.getTimeStamp(toDate); + params[JSON_STRING_START_DATE] = getTimeStamp(fromDate); + params[JSON_STRING_END_DATE] = getTimeStamp(toDate); params[JSON_STRING_SHOW_FATAL] = show_fatal ? "1" : "0"; params[JSON_STRING_SHOW_SEVERE] = show_severe ? "1" : "0"; @@ -174,9 +172,12 @@ public static void getAccidentsFromASyncTask(LatLngBounds bounds, int zoomLevel, * Covert date object to timestamp * * @param date java.util.Date object - * @return TimeStamp, formatted to Anyway API requirements + * @return TimeStamp, formatted to Anyway API requirements or empty String if date is null */ - public static String getTimeStamp(Date date) { + private static String getTimeStamp(Date date) { + if (date == null) + return ""; + Long ts = new Timestamp(date.getTime()).getTime() / 1000; return Long.toString(ts); } @@ -185,9 +186,9 @@ public static String getTimeStamp(Date date) { * Covert date(saved as string) to timestamp * * @param dateStr Date as String, in dd/MM/yyyy format - * @return TimeStamp, formatted to Anyway API requirements + * @return TimeStamp, formatted to Anyway API requirements or empty String if date is in the wrong format */ - public static String getTimeStamp(String dateStr) { + private static String getTimeStamp(String dateStr) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); Date date = null; diff --git a/app/src/main/res/layout/popup.xml b/app/src/main/res/layout/marker_info_window.xml similarity index 100% rename from app/src/main/res/layout/popup.xml rename to app/src/main/res/layout/marker_info_window.xml diff --git a/app/src/main/res/menu/menu_disqus.xml b/app/src/main/res/menu/menu_disqus.xml index c942d01..814b73b 100644 --- a/app/src/main/res/menu/menu_disqus.xml +++ b/app/src/main/res/menu/menu_disqus.xml @@ -1,6 +1,11 @@ - + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2f0e9be..1377de8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -18,6 +18,7 @@ דיון + טען מחדש האם התכוונת ל: @@ -27,7 +28,7 @@ תוצאת חיפוש חפש - + אייקון תאונה