From fcf9f75aeea4dd4555b7c384cea4659c2e8e6e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Sun, 18 Sep 2016 11:29:16 +0200 Subject: [PATCH] Being a bit more request-friendly for Panoramio API. --- .../urbanexplorer/AppConstants.java | 3 + .../urbanexplorer/MainActivity.java | 17 +++++- .../urbanexplorer/fragments/HomeFragment.java | 58 +++++++++++-------- .../fragments/PanoramioShowerFragment.java | 2 + .../fragments/WikiLocationsFragment.java | 4 ++ app/src/main/res/xml/urban_expl_settings.xml | 7 +++ 6 files changed, 63 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java index 33f2214..c97aeae 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java @@ -21,4 +21,7 @@ public class AppConstants { public static final String DEF_HTTP_PROXY_PASSWORD = null; public static final String PREF_HTTP_PROXY_ENABLED_KEY = "pref_proxy_enabled"; public static final boolean DEF_HTTP_PROXY_ENABLED = false; + + public static final String PANORAMIO_BULK_SIZE_KEY = "pref_panoramio_bulk_size"; + public static final int PANORAMIO_BULK_SIZE_DEF_VALUE = 50; } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index 7147607..6f1145c 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -55,6 +55,7 @@ public class MainActivity extends ActionBarActivity { private static final int SETTINGS_ID_INTENT_REQUEST_ID = 2; private static final String PHOTO_INFO = "PHOTO_INFO"; private static final String FIRST_TIME_LAUNCH = "FIRST_TIME_LAUNCH_KEY"; + private static final String MAIN_BACKSTACK = "MAIN_BACKSTACK_KEY"; public static DisplayImageOptions options; private GestureDetectorCompat gestureDetector; private int currentFragmentId = 0; @@ -193,7 +194,12 @@ public class MainActivity extends ActionBarActivity { R.anim.slide_out_down, R.anim.slide_in_up, R.anim.slide_out_up); - ctx.replace(R.id.fragments, panoramioShower); + Fragment frag = fragmentManager.findFragmentByTag(PanoramioShowerFragment.TAG); + if (frag != null) { + ctx.replace(R.id.fragments, frag); + } else { + ctx.replace(R.id.fragments, panoramioShower, PanoramioShowerFragment.TAG); + } ctx.addToBackStack(PHOTO_BACKSTACK); ctx.commit(); @@ -242,8 +248,13 @@ public class MainActivity extends ActionBarActivity { } } - ctx.replace(R.id.fragments, fragment, tag); - ctx.addToBackStack(null); + Fragment frag = fragmentManager.findFragmentByTag(tag); + if (frag == null) { + ctx.replace(R.id.fragments, fragment, tag); + } else { + ctx.replace(R.id.fragments, frag); + } + ctx.addToBackStack(MAIN_BACKSTACK); ctx.commit(); updateSwipeHandler(); } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java index ff67e22..1f052e1 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -25,6 +25,7 @@ import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.PanoramioUtils; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Semaphore; @@ -34,21 +35,24 @@ import java.util.concurrent.Semaphore; */ public class HomeFragment extends Fragment { - private static final String CLASS_TAG = HomeFragment.class.getSimpleName(); private static final Logger lg = LoggerFactory.getLogger(HomeFragment.class); - private static final int PANORAMIA_BULK_DATA_SIZE = 10; public static final String TAG = HomeFragment.class.getSimpleName(); - public static final int FRAG_ID = 1; + private static final String PHOTO_LIST = "PHOTO_LIST_KEY"; private LocationManager locationService; private boolean initialized = false; private View inflatedView; private Long pageId; private Semaphore loading; - private List photos; + private ArrayList photos; private boolean noMorePhotos; + public int getPanoramioBulkDataSize() { + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); + return sharedPrefs.getInt(AppConstants.PANORAMIO_BULK_SIZE_KEY, AppConstants.PANORAMIO_BULK_SIZE_DEF_VALUE); + } + public HomeFragment() { // Required empty public constructor } @@ -59,7 +63,6 @@ public class HomeFragment extends Fragment { lg.trace("onCreate"); pageId = 1L; loading = new Semaphore(1, true); - photos = new ArrayList<>(); noMorePhotos = false; } @@ -88,23 +91,14 @@ public class HomeFragment extends Fragment { }); } - private Double safeParseDouble(CharSequence text) { - if (text == null) { - return null; - } - - try { - return Double.parseDouble(text.toString()); - } catch (NumberFormatException e) { - lg.warn("Wrong number format", e); - return null; - } - } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment + lg.trace("TAG: {}", getTag()); + for (Fragment frag : getFragmentManager().getFragments()) { + lg.trace("Fragment TAG {}", frag.getTag()); + } inflatedView = inflater.inflate(R.layout.fragment_home, container, false); ListView locations = (ListView)inflatedView.findViewById(R.id.locations); final ListView finalLocations = locations; @@ -119,6 +113,22 @@ public class HomeFragment extends Fragment { } }); + initialized = true; + + lg.trace("Saved instance state {}", savedInstanceState); + if (savedInstanceState == null) { + lg.trace("Saved instance state is null"); + photos = new ArrayList<>(); + } + else { + final Serializable serializable = savedInstanceState.getSerializable(PHOTO_LIST); + lg.trace("Photo list serializable {}", serializable); + photos = (ArrayList) serializable; + } + + locations.setAdapter(new PanoramioAdapter(getActivity(), R.layout.location_item, photos)); + lg.trace("Photos initialized {}", photos); + locations.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { @@ -158,8 +168,6 @@ public class HomeFragment extends Fragment { } }); - initialized = true; - return inflatedView; } @@ -203,7 +211,7 @@ public class HomeFragment extends Fragment { int offset = photos.size(); - lg.debug("Fetching additional photos offset: {}, count: {}", offset, PANORAMIA_BULK_DATA_SIZE); + lg.debug("Fetching additional photos offset: {}, count: {}", offset, getPanoramioBulkDataSize()); PanoramioUtils.fetchPanoramioImages( activity, @@ -211,7 +219,7 @@ public class HomeFragment extends Fragment { location.getLongitude(), fetchRadiusX(), fetchRadiusY(), - (long)(offset + PANORAMIA_BULK_DATA_SIZE), + (long)(offset), fetchLocationPageSize(), new PanoramioResponseCallback() { @Override @@ -275,8 +283,6 @@ public class HomeFragment extends Fragment { @Override public void callback(PanoramioResponseStatus status, List images, Long imagesCount) { Long pageSize = fetchLocationPageSize(); - Long start = (pageId - 1) * pageSize + 1; - Long end = pageId * pageSize; ArrayAdapter adapter = new PanoramioAdapter(activity, R.layout.location_item, @@ -299,7 +305,7 @@ public class HomeFragment extends Fragment { } private Long fetchLocationPageSize() { - return new Long(PANORAMIA_BULK_DATA_SIZE); + return new Long(getPanoramioBulkDataSize()); } private Double fetchRadiusX() { @@ -372,6 +378,8 @@ public class HomeFragment extends Fragment { super.onSaveInstanceState(outState); lg.trace("Saving state"); + outState.putSerializable(PHOTO_LIST, photos); + lg.trace("Saved photos: {}", photos); } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioShowerFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioShowerFragment.java index 3649e5f..425fcee 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioShowerFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioShowerFragment.java @@ -24,6 +24,7 @@ public class PanoramioShowerFragment extends Fragment { public static final String PANORAMIO_PHOTO_ARG_KEY = "PANORAMIO_PHOTO_ARG_KEY"; + public static final String TAG = "PANORAMIO_TAG"; private TextView photoTitle; private TextView photoUploadDate; private TextView photoAuthor; @@ -94,4 +95,5 @@ public class PanoramioShowerFragment extends Fragment { mainActivity.resetPhotoInfo(); } } + } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java index 6f88fd3..7bda993 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -55,6 +55,10 @@ public class WikiLocationsFragment extends Fragment { Bundle savedInstanceState) { // Inflate the layout for this fragment final View inflatedView = inflater.inflate(R.layout.fragment_wiki_locations, container, false); + lg.trace("TAG: {}", getTag()); + for (Fragment frag : getFragmentManager().getFragments()) { + lg.trace("Fragment TAG {}", frag.getTag()); + } locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); currentLocation = (TextView) inflatedView.findViewById(R.id.wiki_current_location); diff --git a/app/src/main/res/xml/urban_expl_settings.xml b/app/src/main/res/xml/urban_expl_settings.xml index a03ed55..8f57e0a 100644 --- a/app/src/main/res/xml/urban_expl_settings.xml +++ b/app/src/main/res/xml/urban_expl_settings.xml @@ -19,6 +19,13 @@ android:defaultValue="0.05" android:inputType="numberDecimal" /> + +