diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java index 3915e21..e318b97 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java @@ -7,4 +7,6 @@ public class AppConstants { public static final String GOOGLE_API_KEY = "AIzaSyDAnmEK6cgovRrefUuYojL1pxPEbIBLZUw"; public static final long MIN_TIME = 60000; public static final float MIN_DISTANCE = 100; + public static final float PAMNORAMIO_DEF_RADIUSX = 0.05f; + public static final float PAMNORAMIO_DEF_RADIUSY = 0.05f; } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index 0813424..7b5785f 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -252,9 +252,12 @@ public class MainActivity extends ActionBarActivity { String locationProvider = LocationUtils.getDefaultLocation(this); if (locationProvider == null) { // sadly, nothing to do except from notifing user that program is not enable working - Toast.makeText(this, "Sorry location services are not working." + - " Program cannot work properly - check location settings to allow program working correctly", - Toast.LENGTH_LONG).show(); +// Toast.makeText(this, "Sorry location services are not working." + +// " Program cannot work properly - check location settings to allow program working correctly", +// Toast.LENGTH_LONG).show(); + + Intent intent = new Intent(this, SettingsActivity.class); + startActivity(intent); } break; default: 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 fa8bc74..2672d46 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/activities/SettingsActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/activities/SettingsActivity.java @@ -1,15 +1,23 @@ package pl.tpolgrabia.urbanexplorer.activities; import android.os.Bundle; +import android.preference.PreferenceFragment; import android.support.v7.app.ActionBarActivity; import pl.tpolgrabia.urbanexplorer.R; +import pl.tpolgrabia.urbanexplorer.fragments.SettingsFragment; public class SettingsActivity extends ActionBarActivity { @Override - protected void onCreate(Bundle savedInstanceState) { + public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.activity_settings); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setTitle("Urban explorer settings"); + + // Display the fragment as the main content. + getFragmentManager().beginTransaction() + .replace(R.id.settings_fragments, new SettingsFragment()) + .commit(); } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchWikiLocationsCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchWikiLocationsCallback.java new file mode 100644 index 0000000..a7d07de --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchWikiLocationsCallback.java @@ -0,0 +1,32 @@ +package pl.tpolgrabia.urbanexplorer.callbacks; + +import android.view.View; +import android.widget.AdapterView; +import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; +import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; +import pl.tpolgrabia.urbanexplorer.utils.WikiUtils; + +import java.util.List; + +/** + * Created by tpolgrabia on 14.09.16. + */ +public class FetchWikiLocationsCallback implements AdapterView.OnItemLongClickListener { + private WikiLocationsFragment wikiLocationsFragment; + private final List appObjects; + + public FetchWikiLocationsCallback(WikiLocationsFragment wikiLocationsFragment, List appObjects) { + this.wikiLocationsFragment = wikiLocationsFragment; + this.appObjects = appObjects; + } + + @Override + public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { + final WikiAppObject item = appObjects.get(position); + WikiUtils.fetchSingleWikiInfoItemAndRunWikiPage( + wikiLocationsFragment.getActivity(), + item.getPageId(), + new WikiInfoRunBrowserCallback(wikiLocationsFragment, item)); + return false; + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiInfoRunBrowserCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiInfoRunBrowserCallback.java new file mode 100644 index 0000000..0f6e110 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiInfoRunBrowserCallback.java @@ -0,0 +1,50 @@ +package pl.tpolgrabia.urbanexplorer.callbacks; + +import android.content.Intent; +import android.net.Uri; +import android.util.Log; +import android.widget.Toast; +import com.androidquery.callback.AjaxCallback; +import com.androidquery.callback.AjaxStatus; +import org.json.JSONException; +import org.json.JSONObject; +import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; +import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; + +/** + * Created by tpolgrabia on 14.09.16. + */ +public class WikiInfoRunBrowserCallback extends AjaxCallback { + private static final String CLASS_TAG = WikiInfoRunBrowserCallback.class.getSimpleName(); + private WikiLocationsFragment wikiLocationsFragment; + private final WikiAppObject item; + + public WikiInfoRunBrowserCallback(WikiLocationsFragment wikiLocationsFragment, WikiAppObject item) { + this.wikiLocationsFragment = wikiLocationsFragment; + this.item = item; + } + + @Override + public void callback(String url, JSONObject object, AjaxStatus status) { + if (status.getCode() != 200) { + Toast.makeText(wikiLocationsFragment.getActivity(), + "Sorry, network error code: " + status.getCode(), + Toast.LENGTH_LONG) + .show(); + return; + } + + + try { + String wikiUrl = object.getJSONObject("query") + .getJSONObject("pages") + .getJSONObject(item.getPageId().toString()) + .getString("fullurl"); + Intent intent = new Intent(Intent.ACTION_VIEW, + Uri.parse(wikiUrl)); + wikiLocationsFragment.startActivity(intent); + } catch (JSONException e) { + Log.e(CLASS_TAG, "Error", e); + } + } +} 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 0fcda8a..b8a88a1 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -1,9 +1,11 @@ package pl.tpolgrabia.urbanexplorer.fragments; import android.content.Context; +import android.content.SharedPreferences; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; @@ -11,6 +13,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.*; +import pl.tpolgrabia.urbanexplorer.AppConstants; import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.callbacks.PanoramioResponseCallback; @@ -273,13 +276,23 @@ public class HomeFragment extends Fragment { } private Double fetchRadiusX() { - final TextView radiusxTextView = (TextView) inflatedView.findViewById(R.id.location_xrange); - return safeParseDouble(radiusxTextView.getText()); + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + final String pref_panoramio_radiusx = sharedPreferences.getString( + "pref_panoramio_radiusx", + String.valueOf(AppConstants.PAMNORAMIO_DEF_RADIUSX)); + Log.d(CLASS_TAG, "Panoramio radiusx pref equals " + pref_panoramio_radiusx); + return Double.parseDouble( + pref_panoramio_radiusx); } private Double fetchRadiusY() { - final TextView radiusyTextView = (TextView) inflatedView.findViewById(R.id.location_yrange); - return safeParseDouble(radiusyTextView.getText()); + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + final String pref_panoramio_radiusy = sharedPreferences.getString( + "pref_panoramio_radiusy", + String.valueOf(AppConstants.PAMNORAMIO_DEF_RADIUSX)); + Log.d(CLASS_TAG, "Panoramio radiusy pref equals " + pref_panoramio_radiusy); + return Double.parseDouble( + pref_panoramio_radiusy); } @Override @@ -302,7 +315,10 @@ public class HomeFragment extends Fragment { Log.v(CLASS_TAG, "Current location: " + currLocation + ", locationInfo: " + locationInfo); if (currLocation != null && locationInfo != null) { // update home fragment's location info - locationInfo.setText("Location: " + currLocation.getLatitude() + "," + currLocation.getLongitude()); + locationInfo.setText("Your current location: (" + + currLocation.getLatitude() + + "," + + currLocation.getLongitude() + ")"); } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/SettingsFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/SettingsFragment.java index 194f62b..c585820 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/SettingsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/SettingsFragment.java @@ -1,30 +1,16 @@ package pl.tpolgrabia.urbanexplorer.fragments; - import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - +import android.preference.PreferenceFragment; +import android.support.v7.app.ActionBarActivity; import pl.tpolgrabia.urbanexplorer.R; -/** - * A simple {@link Fragment} subclass. - */ -public class SettingsFragment extends Fragment { - - - public SettingsFragment() { - // Required empty public constructor - } - +public class SettingsFragment extends PreferenceFragment { @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_settings, container, false); - } + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.urban_expl_settings); + } } 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 f07dd83..f642247 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -1,10 +1,9 @@ package pl.tpolgrabia.urbanexplorer.fragments; -import android.content.Intent; +import android.content.Context; import android.location.Location; import android.location.LocationManager; -import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.Editable; @@ -15,12 +14,11 @@ import android.view.ViewGroup; import android.widget.*; import com.androidquery.AQuery; import com.androidquery.callback.AjaxCallback; -import com.androidquery.callback.AjaxStatus; -import org.json.JSONException; import org.json.JSONObject; import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter; +import pl.tpolgrabia.urbanexplorer.callbacks.FetchWikiLocationsCallback; import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListenerCallback; import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus; import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; @@ -42,7 +40,6 @@ public class WikiLocationsFragment extends Fragment { private static final String CLASS_TAG = WikiLocationsFragment.class.getSimpleName(); private LocationManager locationService; private TextView currentLocation; - private Button fetchPlaces; public WikiLocationsFragment() { // Required empty public constructor @@ -107,47 +104,7 @@ public class WikiLocationsFragment extends Fragment { // TODO on success ListView locations = (ListView) getView().findViewById(R.id.wiki_places); - locations.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { - @Override - public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { -// WikiPage item = response.getPages().get(position); -// Intent intent = new Intent(android.content.Intent.ACTION_VIEW, -// Uri.parse(item.get); -// startActivity(intent); - final WikiAppObject item = appObjects.get(position); - new AQuery(getActivity()).ajax( - "https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=" - + item.getPageId() + "&inprop=url&format=json", - JSONObject.class, - new AjaxCallback() { - @Override - public void callback(String url, JSONObject object, AjaxStatus status) { - if (status.getCode() != 200) { - Toast.makeText(getActivity(), - "Sorry, network error code: " + status.getCode(), - Toast.LENGTH_LONG) - .show(); - return; - } - - - try { - String wikiUrl = object.getJSONObject("query") - .getJSONObject("pages") - .getJSONObject(item.getPageId().toString()) - .getString("fullurl"); - Intent intent = new Intent(Intent.ACTION_VIEW, - Uri.parse(wikiUrl)); - startActivity(intent); - } catch (JSONException e) { - Log.e(CLASS_TAG, "Error", e); - } - } - } - ); - return false; - } - }); + locations.setOnItemLongClickListener(new FetchWikiLocationsCallback(WikiLocationsFragment.this, appObjects)); locations.setAdapter(new WikiLocationsAdapter(getActivity(), appObjects)); } } @@ -164,7 +121,10 @@ public class WikiLocationsFragment extends Fragment { public void updateLocationInfo() { final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); if (location != null) { - currentLocation.setText("Location: " + location.getLatitude() + "," + location.getLongitude()); + currentLocation.setText("Your current location: (" + + location.getLatitude() + + "," + + location.getLongitude() + ")"); } } @@ -172,4 +132,5 @@ public class WikiLocationsFragment extends Fragment { public void onPause() { super.onPause(); } + } 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 2e46640..48d9321 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java @@ -170,7 +170,7 @@ public class WikiUtils { } AQuery aq = new AQuery(ctx); - aq.ajax("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gscoord=52.2181737%7C21.1530673&gsradius=10000&gslimit=10&format=json", JSONObject.class, new AjaxCallback() { + aq.ajax("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gscoord=" + latitude + "%7C" + longitude + "&gsradius=10000&gslimit=" + limit + "&format=json", JSONObject.class, new AjaxCallback() { @Override public void callback(String url, JSONObject object, AjaxStatus status) { Log.v(CLASS_TAG, "Finished waiting for " + url @@ -324,4 +324,14 @@ public class WikiUtils { } + public static void fetchSingleWikiInfoItemAndRunWikiPage(Context ctx, + Long pageId, + AjaxCallback callback) { + new AQuery(ctx).ajax( + "https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=" + + pageId + "&inprop=url&format=json", + JSONObject.class, + callback + ); + } } diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 82f7d99..2194fdf 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -4,21 +4,8 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".activities.SettingsActivity"> - - + tools:context=".activities.SettingsActivity" + android:id="@+id/settings_fragments"> - - - - - - - - - + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index de1bd00..d3ad459 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -11,34 +11,6 @@ android:layout_height="wrap_content" android:text="@string/location_info"/> - - - - - - - - - - - - - - - - - - - - -