From 0901e67853b24a8760129a0dc238c56417a17115 Mon Sep 17 00:00:00 2001 From: Neslihan Date: Sat, 13 May 2017 13:41:53 +0300 Subject: [PATCH] Optimise unnecesarry method call --- .../nrw/commons/nearby/NearbyActivity.java | 95 +++++++++++++++++-- .../nrw/commons/nearby/NearbyController.java | 28 +++--- .../commons/nearby/NearbyListFragment.java | 53 ++++++++--- .../nrw/commons/nearby/NearbyMapFragment.java | 9 +- .../fr/free/nrw/commons/nearby/Place.java | 3 +- .../nrw/commons/utils/UriDeserializer.java | 19 ++++ .../free/nrw/commons/utils/UriSerializer.java | 17 ++++ app/src/main/res/layout/activity_nearby.xml | 7 ++ app/src/main/res/layout/fragment_nearby.xml | 7 -- 9 files changed, 192 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/fr/free/nrw/commons/utils/UriDeserializer.java create mode 100644 app/src/main/java/fr/free/nrw/commons/utils/UriSerializer.java diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java index 53e01af86..0d1c19d9b 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java @@ -1,35 +1,57 @@ package fr.free.nrw.commons.nearby; +import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import android.view.View; +import android.widget.ProgressBar; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; import fr.free.nrw.commons.R; +import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.location.LocationServiceManager; import fr.free.nrw.commons.theme.BaseActivity; +import fr.free.nrw.commons.utils.UriDeserializer; +import fr.free.nrw.commons.utils.UriSerializer; public class NearbyActivity extends BaseActivity { + @BindView(R.id.progressBar) + ProgressBar progressBar; private boolean isMapViewActive = false; + //public List placeList; private LocationServiceManager locationManager; + private LatLng curLatLang; + private Gson gson; + private String gsonPlaceList; + private String gsonCurLatLng; + private Bundle bundle; + private NearbyAsyncTask nearbyAsyncTask; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_nearby); + ButterKnife.bind(this); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } locationManager = new LocationServiceManager(this); locationManager.registerLocationManager(); + curLatLang = locationManager.getLatestLocation(); + nearbyAsyncTask = new NearbyAsyncTask(); + nearbyAsyncTask.execute(); - // Begin the transaction - FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); - NearbyListFragment fragment = new NearbyListFragment(); - ft.add(R.id.container, fragment); - ft.commit(); } @Override @@ -56,8 +78,15 @@ public class NearbyActivity extends BaseActivity { private void showMapView() { if (!isMapViewActive) { - getSupportFragmentManager().beginTransaction() - .replace(R.id.container, new NearbyMapFragment()).commit(); + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + NearbyMapFragment fragment = new NearbyMapFragment(); + fragment.setArguments(bundle); + ft.add(R.id.container, fragment); + ft.commit(); + + //NearbyController.loadAttractionsFromLocationToBaseMarkerOptions(curLatLang, placeList); + /*getSupportFragmentManager().beginTransaction() + .replace(R.id.container, new NearbyMapFragment()).commit();*/ isMapViewActive = true; } } @@ -68,6 +97,8 @@ public class NearbyActivity extends BaseActivity { } protected void refreshView() { + //placeList = NearbyController.loadAttractionsFromLocation(curLatLang, this); + nearbyAsyncTask.execute(); if (isMapViewActive) { getSupportFragmentManager().beginTransaction() .replace(R.id.container, new NearbyMapFragment()).commit(); @@ -86,4 +117,54 @@ public class NearbyActivity extends BaseActivity { super.onDestroy(); locationManager.unregisterLocationManager(); } + + private class NearbyAsyncTask extends AsyncTask> { + + @Override + protected void onPreExecute() { + super.onPreExecute(); + } + + @Override + protected void onProgressUpdate(Integer... values) { + super.onProgressUpdate(values); + } + + @Override + protected List doInBackground(Void... params) { + return NearbyController + .loadAttractionsFromLocation(curLatLang, getApplicationContext() + ); + } + + @Override + protected void onPostExecute(List placeList) { + super.onPostExecute(placeList); + + if (isCancelled()) { + return; + } + //placeList = NearbyController.loadAttractionsFromLocation(curLatLang, getApplicationContext()); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Uri.class, new UriSerializer()) + .create(); + gsonPlaceList = gson.toJson(placeList); + gsonCurLatLng = gson.toJson(curLatLang); + + bundle = new Bundle(); + bundle.putString("PlaceList", gsonPlaceList); + bundle.putString("CurLatLng", gsonCurLatLng); + + // Begin the transaction + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + NearbyListFragment fragment = new NearbyListFragment(); + fragment.setArguments(bundle); + ft.add(R.id.container, fragment); + ft.commit(); + + if (progressBar != null) { + progressBar.setVisibility(View.GONE); + } + } + } } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java index 0d0712e1b..aca2a2113 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java @@ -26,7 +26,7 @@ import timber.log.Timber; public class NearbyController { private static final int MAX_RESULTS = 1000; - private static List loadAttractionsFromLocation(LatLng curLatLng, Context context) { + public static List loadAttractionsFromLocation(LatLng curLatLng, Context context) { Timber.d("Loading attractions near %s", curLatLng); if (curLatLng == null) { return Collections.emptyList(); @@ -59,35 +59,33 @@ public class NearbyController { /** * Loads attractions from location for list view, we need to return Place data type. * @param curLatLng users current location - * @param context current activity + * @param placeList list of nearby places in Place data type * @return Place list that holds nearby places */ - public static List loadAttractionsFromLocationToPlaces(LatLng curLatLng, - Context context) { - - List places = loadAttractionsFromLocation(curLatLng,context); - places = places.subList(0, Math.min(places.size(), MAX_RESULTS)); - for (Place place: places) { + public static List loadAttractionsFromLocationToPlaces( + LatLng curLatLng, + List placeList) { + placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS)); + for (Place place: placeList) { String distance = formatDistanceBetween(curLatLng, place.location); place.setDistance(distance); } - return places; + return placeList; } /** *Loads attractions from location for map view, we need to return BaseMarkerOption data type. * @param curLatLng users current location - * @param context the activity + * @param placeList list of nearby places in Place data type * @return BaseMarkerOprions list that holds nearby places */ - public static List loadAttractionsFromLocationToBaseMarkerOptions( + public static List loadAttractionsFromLocationToBaseMarkerOptions ( LatLng curLatLng, - Context context) { + List placeList) { List baseMarkerOptionses = new ArrayList<>(); - List places = loadAttractionsFromLocation(curLatLng,context); - places = places.subList(0, Math.min(places.size(), MAX_RESULTS)); - for (Place place: places) { + placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS)); + for (Place place: placeList) { String distance = formatDistanceBetween(curLatLng, place.location); place.setDistance(distance); baseMarkerOptionses.add(new MarkerOptions() diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java index 5921c0e30..e72636ae6 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java @@ -11,6 +11,12 @@ import android.view.ViewGroup; import android.widget.ListView; import android.widget.ProgressBar; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.List; import butterknife.BindView; @@ -18,14 +24,18 @@ import butterknife.ButterKnife; import butterknife.OnItemClick; import fr.free.nrw.commons.R; import fr.free.nrw.commons.location.LatLng; +import fr.free.nrw.commons.utils.UriDeserializer; import timber.log.Timber; public class NearbyListFragment extends ListFragment { - private NearbyAsyncTask nearbyAsyncTask; + //private NearbyAsyncTask nearbyAsyncTask; + private Gson gson; + private List placeList; + private LatLng curLatLng; @BindView(R.id.listView) ListView listview; - @BindView(R.id.progressBar) ProgressBar progressBar; + private NearbyAdapter adapter; @@ -35,6 +45,19 @@ public class NearbyListFragment extends ListFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Bundle bundle = this.getArguments(); + gson = new GsonBuilder() + .registerTypeAdapter(Uri.class, new UriDeserializer()) + .create(); + if (bundle != null){ + String gsonPlaceList = bundle.getString("PlaceList"); + String gsonLatLng = bundle.getString("CurLatLng"); + Type listType = new TypeToken>() {}.getType(); + placeList = gson.fromJson(gsonPlaceList, listType); + Type curLatLngType = new TypeToken() {}.getType(); + curLatLng = gson.fromJson(gsonLatLng, curLatLngType); + NearbyController.loadAttractionsFromLocationToPlaces(curLatLng, placeList); + } setRetainInstance(true); } @@ -57,32 +80,37 @@ public class NearbyListFragment extends ListFragment { // to avoid double list when screen orientation changed if (savedInstanceState == null) { adapter.clear(); - nearbyAsyncTask = new NearbyAsyncTask(); - nearbyAsyncTask.execute(); - progressBar.setVisibility(View.VISIBLE); + //nearbyAsyncTask = new NearbyAsyncTask(); + //nearbyAsyncTask.execute(); + //progressBar.setVisibility(View.VISIBLE); Timber.d("Saved instance state is null, populating ListView"); } else { - progressBar.setVisibility(View.GONE); + //progressBar.setVisibility(View.GONE); } // If we are returning here from a screen orientation and the AsyncTask is still working, // re-create and display the progress dialog. if (isTaskRunning()) { - progressBar.setVisibility(View.VISIBLE); + //progressBar.setVisibility(View.VISIBLE); } + + adapter.clear(); + adapter.addAll(placeList); + adapter.notifyDataSetChanged(); } private boolean isTaskRunning() { - return nearbyAsyncTask != null && nearbyAsyncTask.getStatus() != AsyncTask.Status.FINISHED; + //return nearbyAsyncTask != null && nearbyAsyncTask.getStatus() != AsyncTask.Status.FINISHED; + return false; } @Override public void onDetach() { // All dialogs should be closed before leaving the activity in order to avoid // the: Activity has leaked window com.android.internal.policy... exception - if (progressBar != null && progressBar.isShown()) { + /* if (progressBar != null && progressBar.isShown()) { progressBar.setVisibility(View.GONE); - } + }*/ super.onDetach(); } @@ -92,10 +120,10 @@ public class NearbyListFragment extends ListFragment { // See http://stackoverflow.com/questions/18264408/incomplete-asynctask-crashes-my-app if (isTaskRunning()) { - nearbyAsyncTask.cancel(true); + //nearbyAsyncTask.cancel(true); } } - +/* private class NearbyAsyncTask extends AsyncTask> { @Override @@ -135,6 +163,7 @@ public class NearbyListFragment extends ListFragment { adapter.notifyDataSetChanged(); } } + */ @OnItemClick(R.id.listView) void onItemClicked(int position) { diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java index 7b627ecdf..3bf75e193 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java @@ -23,7 +23,7 @@ import java.util.List; import fr.free.nrw.commons.R; public class NearbyMapFragment extends android.support.v4.app.Fragment { - private NearbyAsyncTask nearbyAsyncTask; + //private NearbyAsyncTask nearbyAsyncTask; private fr.free.nrw.commons.location.LatLng currentLocation; private MapView mapView; @@ -60,8 +60,8 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment { @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - nearbyAsyncTask = new NearbyAsyncTask(); - nearbyAsyncTask.execute(); + //nearbyAsyncTask = new NearbyAsyncTask(); + //nearbyAsyncTask.execute(); } @Override @@ -93,7 +93,7 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment { mapView.onDestroy(); super.onDestroyView(); } - +/* private class NearbyAsyncTask extends AsyncTask> { @Override @@ -128,4 +128,5 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment { }); } } + */ } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/Place.java b/app/src/main/java/fr/free/nrw/commons/nearby/Place.java index 222a992cc..3439763c1 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/Place.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/Place.java @@ -5,7 +5,7 @@ import android.net.Uri; import fr.free.nrw.commons.location.LatLng; -public class Place { +public class Place{ public final String name; public final String description; @@ -17,6 +17,7 @@ public class Place { public Bitmap secondaryImage; public String distance; + public Place(String name, String description, String longDescription, Uri secondaryImageUrl, LatLng location) { this.name = name; diff --git a/app/src/main/java/fr/free/nrw/commons/utils/UriDeserializer.java b/app/src/main/java/fr/free/nrw/commons/utils/UriDeserializer.java new file mode 100644 index 000000000..e63cfefa8 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/utils/UriDeserializer.java @@ -0,0 +1,19 @@ +package fr.free.nrw.commons.utils; + + +import android.net.Uri; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; + +public class UriDeserializer implements JsonDeserializer { + @Override + public Uri deserialize(final JsonElement src, final Type srcType, + final JsonDeserializationContext context) throws JsonParseException { + return Uri.parse(src.getAsString()); + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/utils/UriSerializer.java b/app/src/main/java/fr/free/nrw/commons/utils/UriSerializer.java new file mode 100644 index 000000000..d5a4c28ec --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/utils/UriSerializer.java @@ -0,0 +1,17 @@ +package fr.free.nrw.commons.utils; + +import android.net.Uri; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import java.lang.reflect.Type; + + +public class UriSerializer implements JsonSerializer { + public JsonElement serialize(Uri src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.toString()); + } +} diff --git a/app/src/main/res/layout/activity_nearby.xml b/app/src/main/res/layout/activity_nearby.xml index d03ae8381..53f31056b 100644 --- a/app/src/main/res/layout/activity_nearby.xml +++ b/app/src/main/res/layout/activity_nearby.xml @@ -5,6 +5,13 @@ android:layout_height="match_parent" > + + - -