Code refactorings, added settings for panoramio.

master
Tomasz Półgrabia 2016-09-14 21:27:18 +02:00
parent 8137f0a847
commit 34e0d698c4
13 changed files with 194 additions and 150 deletions

View File

@ -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;
}

View File

@ -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:

View File

@ -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();
}
}

View File

@ -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<WikiAppObject> appObjects;
public FetchWikiLocationsCallback(WikiLocationsFragment wikiLocationsFragment, List<WikiAppObject> 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;
}
}

View File

@ -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<JSONObject> {
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);
}
}
}

View File

@ -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() + ")");
}
}

View File

@ -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);
}
}

View File

@ -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<JSONObject>() {
@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();
}
}

View File

@ -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<JSONObject>() {
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<JSONObject>() {
@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<JSONObject> 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
);
}
}

View File

@ -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">
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
tools:context=".activities.SettingsActivity"
android:id="@+id/settings_fragments">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="@string/large_text"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
</FrameLayout>

View File

@ -11,34 +11,6 @@
android:layout_height="wrap_content"
android:text="@string/location_info"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/location_xrange"/>
<EditText android:id="@+id/location_xrange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_weight="1"
android:text="0.05"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/location_yrange"/>
<EditText android:id="@+id/location_yrange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_weight="1"
android:text="0.05"/>
</LinearLayout>
<ListView
android:id="@+id/locations"
android:layout_width="match_parent"

View File

@ -1,27 +0,0 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="pl.tpolgrabia.urbanexplorer.fragments.SettingsFragment">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/location_update_frequency" />
<EditText android:id="@+id/location_update_frequency"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<Button android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/save"/>
</LinearLayout>

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Panoramio search settings"
android:key="pref_panoramio_settings">
<EditTextPreference
android:key="pref_panoramio_radiusx"
android:title="Radius X"
android:summary="Radius X"
android:defaultValue="0.05"
android:inputType="numberDecimal" />
<EditTextPreference
android:key="pref_panoramio_radiusy"
android:title="Radius Y"
android:summary="Radius Y"
android:defaultValue="0.05"
android:inputType="numberDecimal" />
</PreferenceCategory>
<PreferenceCategory
android:title="Wiki search settings"
android:key="pref_wiki_settings">
<EditTextPreference
android:key="pref_wiki_radius"
android:title="Wiki search radius"
android:summary="Wiki search radius[km]"
android:defaultValue="10.0"
android:inputType="numberDecimal" />
<EditTextPreference
android:key="pref_wiki_limit"
android:title="Wiki search results limit"
android:summary="Limit of wiki results"
android:defaultValue="100"
android:inputType="number" />
</PreferenceCategory>
</PreferenceScreen>