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 49a3ac7..bdff32e 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; @@ -90,7 +91,7 @@ public class MainActivity extends ActionBarActivity { super.onCreate(savedInstanceState); lg.trace("onCreate"); setContentView(R.layout.activity_main); - AQUtility.setDebug(true); + AQUtility.setDebug(false); NetUtils.setGlobalProxyAuth(this); @@ -114,8 +115,7 @@ public class MainActivity extends ActionBarActivity { gestureDetector = new GestureDetectorCompat(this, swipeHandler); locationCallback = new StandardLocationListener(); initLocalication(); - Fabric fabric = new Fabric.Builder(this).debuggable(true).kits(new Crashlytics()).build(); - Fabric.with(fabric); + Fabric.with(this, new Crashlytics()); Integer fragId = savedInstanceState != null ? savedInstanceState.getInt(FRAG_ID) : null; lg.trace("Restored orig frag id: {}", fragId); @@ -194,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(); @@ -202,6 +207,12 @@ public class MainActivity extends ActionBarActivity { } private void switchFragment() { + + if (photoInfo != null) { + switchToPhoto(photoInfo); + return; + } + switch (currentFragmentId) { case HOME_FRAGMENT_ID: // switch to home fragment @@ -243,8 +254,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/activities/SettingsActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/activities/SettingsActivity.java index 2672d46..62ec4a0 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/activities/SettingsActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/activities/SettingsActivity.java @@ -3,11 +3,15 @@ package pl.tpolgrabia.urbanexplorer.activities; import android.os.Bundle; import android.preference.PreferenceFragment; import android.support.v7.app.ActionBarActivity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.fragments.SettingsFragment; public class SettingsActivity extends ActionBarActivity { + private static final Logger lg = LoggerFactory.getLogger(SettingsActivity.class); + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -19,5 +23,26 @@ public class SettingsActivity extends ActionBarActivity { getFragmentManager().beginTransaction() .replace(R.id.settings_fragments, new SettingsFragment()) .commit(); + + lg.trace("onCreate"); + } + + @Override + protected void onResume() { + super.onResume(); + lg.trace("onResume"); + } + + @Override + protected void onPause() { + super.onPause(); + + lg.trace("onPause"); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + lg.trace("onDestroy"); } } 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..96c73f3 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,30 @@ 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 LocationManager locationService; + private static final String PHOTO_LIST = "PHOTO_LIST_KEY"; 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()); + final String sValue = sharedPrefs.getString(AppConstants.PANORAMIO_BULK_SIZE_KEY, + String.valueOf(AppConstants.PANORAMIO_BULK_SIZE_DEF_VALUE)); + try { + return Integer.parseInt(sValue); + } catch (NumberFormatException e) { + lg.warn("Invalid panoramio bulk data size {}", sValue, e); + return AppConstants.PANORAMIO_BULK_SIZE_DEF_VALUE; + } + } + public HomeFragment() { // Required empty public constructor } @@ -59,9 +69,9 @@ public class HomeFragment extends Fragment { lg.trace("onCreate"); pageId = 1L; loading = new Semaphore(1, true); - photos = new ArrayList<>(); noMorePhotos = false; + updateLocationInfo(); } @Override @@ -88,23 +98,18 @@ 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()) { + if (frag == null) { + lg.trace("Fragment is null"); + } else { + 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 +124,27 @@ public class HomeFragment extends Fragment { } }); + initialized = true; + + lg.trace("Saved instance state {}", savedInstanceState); + if (photos == null) { + 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; + if (photos == null) { + photos = new ArrayList<>(); + } + } + } + + 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) { @@ -157,41 +183,48 @@ public class HomeFragment extends Fragment { } }); - - initialized = true; + ; return inflatedView; } private void fetchAdditionalPhotos() throws InterruptedException { - if (noMorePhotos) { - lg.trace("No more photos - last query was zero result"); - return; - } - - if (!initialized) { - lg.trace("Fetching additional photos blocked till system is initialized"); - return; - } - - - if (getView() == null) { - lg.trace("Application still not initialized"); - return; - } - final FragmentActivity activity = getActivity(); if (activity == null) { lg.trace("Activity shouldn't be null. No headless fragment"); return; } + MainActivity mainActivity = (MainActivity)getActivity(); + + if (noMorePhotos) { + lg.trace("No more photos - last query was zero result"); + mainActivity.hideProgress(); + return; + } + + if (!initialized) { + lg.trace("Fetching additional photos blocked till system is initialized"); + mainActivity.hideProgress(); + return; + } + + + if (getView() == null) { + lg.trace("Application still not initialized"); + mainActivity.hideProgress(); + return; + } + + + LocationManager locationService = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); if (location == null) { lg.info("Location still not available"); Toast.makeText(activity, "Location still not available", Toast.LENGTH_SHORT).show(); + mainActivity.hideProgress(); return; } @@ -203,7 +236,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 +244,7 @@ public class HomeFragment extends Fragment { location.getLongitude(), fetchRadiusX(), fetchRadiusY(), - (long)(offset + PANORAMIA_BULK_DATA_SIZE), + (long)(offset), fetchLocationPageSize(), new PanoramioResponseCallback() { @Override @@ -255,9 +288,13 @@ public class HomeFragment extends Fragment { return; } + MainActivity mainActivity = (MainActivity) getActivity(); + + LocationManager locationService = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); if (location == null) { lg.info("Location is still not available"); + mainActivity.hideProgress(); Toast.makeText(getActivity(), "Location is still not available", Toast.LENGTH_SHORT).show(); return; } @@ -275,8 +312,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 +334,7 @@ public class HomeFragment extends Fragment { } private Long fetchLocationPageSize() { - return new Long(PANORAMIA_BULK_DATA_SIZE); + return new Long(getPanoramioBulkDataSize()); } private Double fetchRadiusX() { @@ -343,7 +378,7 @@ public class HomeFragment extends Fragment { lg.warn("Activity should'nt be null. No headless fragment"); return; } - locationService = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE); + LocationManager locationService = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE); Location currLocation = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); lg.trace("Current location: {}, locationInfo: {}", currLocation, locationInfo); if (currLocation != null && locationInfo != null) { @@ -372,6 +407,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..5b7e2d3 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -28,6 +28,7 @@ import pl.tpolgrabia.urbanexplorer.utils.NumberUtils; import pl.tpolgrabia.urbanexplorer.utils.WikiAppResponseCallback; import pl.tpolgrabia.urbanexplorer.utils.WikiUtils; +import java.util.ArrayList; import java.util.List; import static android.content.Context.LOCATION_SERVICE; @@ -42,19 +43,37 @@ public class WikiLocationsFragment extends Fragment { private static final double WIKI_DEF_RADIUS = 10.0; private static final long WIKI_DEF_LIMIT = 100; public static final String TAG = WikiLocationsFragment.class.getSimpleName(); + private static final String WIKI_APP_OBJECTS = "WIKI_APP_OBJECTS"; private LocationManager locationService; private TextView currentLocation; + private ArrayList appObjects; + private int lastFetchSize = -1; public WikiLocationsFragment() { // Required empty public constructor } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + lg.trace("onCreate"); + appObjects = savedInstanceState == null ? new ArrayList() + : (ArrayList)savedInstanceState.getSerializable(WIKI_APP_OBJECTS);; + } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, 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()) { + if (frag == null) { + lg.trace("Got null fragment"); + } else { + lg.trace("Fragment TAG {}", frag.getTag()); + } + } locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); currentLocation = (TextView) inflatedView.findViewById(R.id.wiki_current_location); @@ -63,19 +82,41 @@ public class WikiLocationsFragment extends Fragment { mainActivity.getLocationCallback().addCallback(new StandardLocationListenerCallback() { @Override public void callback(Location location) { + lastFetchSize = -1; + appObjects = new ArrayList<>(); updateLocationInfo(); fetchWikiLocations(); } }); + ListView locations = (ListView) inflatedView.findViewById(R.id.wiki_places); + locations.setOnItemLongClickListener(new FetchWikiLocationsCallback(WikiLocationsFragment.this, appObjects)); + locations.setAdapter(new WikiLocationsAdapter(getActivity(), appObjects)); + return inflatedView; } public void fetchWikiLocations() { lg.trace("Fetch wiki locations"); + + MainActivity mainActivity = (MainActivity) getActivity(); + + if (lastFetchSize == 0) { + lg.trace("There is no results"); + mainActivity.hideProgress(); + return; + } + + if (!appObjects.isEmpty()) { + lg.trace("There are fetched objects"); + mainActivity.hideProgress(); + return; + } + final FragmentActivity activity = getActivity(); if (activity == null) { lg.warn("Activity shouldn't be null. No headless fragment"); + mainActivity.hideProgress(); return; } final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); @@ -83,11 +124,13 @@ public class WikiLocationsFragment extends Fragment { if (location == null) { lg.info("Sorry, location is still not available"); Toast.makeText(activity, "Sorry, location is still not available", Toast.LENGTH_SHORT).show(); + mainActivity.hideProgress(); return; } if (getView() == null) { lg.info("Wiki view is not yet initialized"); + mainActivity.hideProgress(); return; } @@ -97,8 +140,12 @@ public class WikiLocationsFragment extends Fragment { fetchRadiusLimit(), fetchSearchLimit(), new WikiAppResponseCallback() { + @Override - public void callback(WikiStatus status, final List appObjects) { + public void callback(WikiStatus status, final List objects) { + appObjects.clear(); + appObjects.addAll(objects); + // handling here wiki locations if (status != WikiStatus.SUCCESS) { Toast.makeText(activity, "Sorry, currently we have problem with interfacing wiki" + @@ -111,7 +158,7 @@ public class WikiLocationsFragment extends Fragment { ListView locations = (ListView) getView().findViewById(R.id.wiki_places); locations.setOnItemLongClickListener(new FetchWikiLocationsCallback(WikiLocationsFragment.this, appObjects)); locations.setAdapter(new WikiLocationsAdapter(activity, appObjects)); - if (appObjects.isEmpty()) { + if (objects.isEmpty()) { Toast.makeText(getActivity(), "No results", Toast.LENGTH_SHORT).show(); } @@ -167,4 +214,11 @@ public class WikiLocationsFragment extends Fragment { super.onPause(); } + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + lg.trace("onSaveInstanceState"); + + outState.putSerializable(WIKI_APP_OBJECTS, appObjects); + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java index dbb802b..746146b 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java @@ -175,13 +175,15 @@ public class WikiUtils { } AQuery aq = NetUtils.createProxyAQueryInstance(ctx); - aq.ajax("https://en.wikipedia.org/w/api.php" + + final String queryUrl = "https://en.wikipedia.org/w/api.php" + "?action=query" + "&list=geosearch" + "&gscoord=" + latitude + "%7C" + longitude + "&gsradius=" + String.format("%.2f", radius) + "&gslimit=" + limit + - "&format=json", JSONObject.class, new AjaxCallback() { + "&format=json"; + lg.trace("GeoSearch wiki API url: {}", queryUrl); + aq.ajax(queryUrl, JSONObject.class, new AjaxCallback() { @Override public void callback(String url, JSONObject object, AjaxStatus status) { lg.trace("Finished waiting for {} with status {}:{} and response: {}", diff --git a/app/src/main/res/layout/fragment_panoramio_shower.xml b/app/src/main/res/layout/fragment_panoramio_shower.xml index b531e56..8ddaac0 100644 --- a/app/src/main/res/layout/fragment_panoramio_shower.xml +++ b/app/src/main/res/layout/fragment_panoramio_shower.xml @@ -5,65 +5,74 @@ android:orientation="vertical" tools:context="pl.tpolgrabia.urbanexplorer.fragments.PanoramioShowerFragment"> - + + - - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + 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" /> + +