diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java index b05c5ef..33f2214 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java @@ -11,4 +11,14 @@ public class AppConstants { public static final float PAMNORAMIO_DEF_RADIUSY = 0.05f; public static final long GPS_LOCATION_UPDATE_FREQ = 15000; public static final float GPS_LOCATION_DISTANCE_FREQ = MIN_DISTANCE; + public static final String PREF_HTTP_PROXY_HOST_KEY = "pref_proxy_host"; + public static final String DEF_HTTP_PROXY_HOST = "localhost"; + public static final String PREF_HTTP_PROXY_PORT_KEY = "pref_proxy_port"; + public static final String DEF_HTTP_PROXY_PORT = "8123"; + public static final String PREF_HTTP_PROXY_USER_KEY = "pref_proxy_user"; + public static final String DEF_HTTP_PROXY_USER = null; + public static final String PREF_HTTP_PROXY_PASSWORD_KEY = "pref_proxy_pass"; + 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; } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index ce645ce..70a8b97 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -16,6 +16,7 @@ import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.*; import android.widget.Toast; +import com.androidquery.util.AQUtility; import com.crashlytics.android.Crashlytics; import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache; import com.nostra13.universalimageloader.core.DisplayImageOptions; @@ -32,6 +33,7 @@ import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; import pl.tpolgrabia.urbanexplorer.handlers.SwipeHandler; import pl.tpolgrabia.urbanexplorer.utils.ImageLoaderUtils; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; +import pl.tpolgrabia.urbanexplorer.utils.NetUtils; import pl.tpolgrabia.urbanexplorer.utils.NumberUtils; import pl.tpolgrabia.urbanexplorer.views.CustomInterceptor; import pl.tpolgrabia.urbanexplorer.views.SwipeFrameLayout; @@ -46,6 +48,7 @@ public class MainActivity extends ActionBarActivity { private static final double MAX_FRAGMENT_ID = WIKI_FRAGMENT_ID; private static final double MIN_FRAGMENT_ID = HOME_FRAGMENT_ID; private static final String FRAG_ID = "FRAG_ID"; + private static final int SETTINGS_ID_INTENT_REQUEST_ID = 2; public static DisplayImageOptions options; private GestureDetectorCompat gestureDetector; private int currentFragmentId = 0; @@ -53,7 +56,7 @@ public class MainActivity extends ActionBarActivity { private boolean locationServicesActivated = false; private GestureDetector.OnGestureListener swipeHandler; - + private PanoramioImageInfo photoInfo; public StandardLocationListener getLocationCallback() { return locationCallback; @@ -70,6 +73,9 @@ public class MainActivity extends ActionBarActivity { super.onCreate(savedInstanceState); Log.v(CLASS_TAG, "onCreate"); setContentView(R.layout.activity_main); + AQUtility.setDebug(true); + + NetUtils.setGlobalProxyAuth(this); currentFragmentId = 0; @@ -114,7 +120,7 @@ public class MainActivity extends ActionBarActivity { switch (item.getItemId()) { case R.id.settings: final Intent intent = new Intent(this, SettingsActivity.class); - startActivity(intent); + startActivityForResult(intent, MainActivity.SETTINGS_ID_INTENT_REQUEST_ID, new Bundle()); return true; default: return super.onOptionsItemSelected(item); @@ -122,6 +128,7 @@ public class MainActivity extends ActionBarActivity { } public void switchToPhoto(PanoramioImageInfo photoInfo) { + this.photoInfo = photoInfo; FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ctx = fragmentManager.beginTransaction(); PanoramioShowerFragment panoramioShower = new PanoramioShowerFragment(); @@ -281,6 +288,9 @@ public class MainActivity extends ActionBarActivity { startActivity(intent); } break; + case SETTINGS_ID_INTENT_REQUEST_ID: + NetUtils.setGlobalProxyAuth(this); + break; default: super.onActivityResult(requestCode, resultCode, data); } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java index c242bcf..9a92e2c 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java @@ -13,6 +13,7 @@ import com.nostra13.universalimageloader.core.ImageLoader; import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.utils.NetUtils; import java.util.List; @@ -24,7 +25,7 @@ public class PanoramioAdapter extends ArrayAdapter { public PanoramioAdapter(FragmentActivity activity, int location_item, List photosDescriptions) { super(activity, location_item, photosDescriptions); - aq = new AQuery(activity); + aq = NetUtils.createProxyAQueryInstance(activity); } @Override 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 02123f8..1882761 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioShowerFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioShowerFragment.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -83,4 +84,13 @@ public class PanoramioShowerFragment extends Fragment { return inflatedView; } + @Override + public void onDestroy() { + super.onDestroy(); + + FragmentActivity acc = getActivity(); + if (acc != null) { + MainActivity mainActivity = (MainActivity)acc; + } + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/SwipeHandler.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/SwipeHandler.java index 7106534..f789a08 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/SwipeHandler.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/SwipeHandler.java @@ -12,7 +12,7 @@ public class SwipeHandler implements GestureDetector.OnGestureListener { private static final String CLASS_TAG = SwipeHandler.class.getSimpleName(); private final MainActivity activity; private static final float SWIPE_THRESHOLD = 50; - private static final float SWIPE_VELOCITY_THRESHOLD = 20; + private static final float SWIPE_VELOCITY_THRESHOLD = 10; public SwipeHandler(MainActivity activity) { this.activity = activity; diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NetUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NetUtils.java new file mode 100644 index 0000000..8f737a8 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NetUtils.java @@ -0,0 +1,92 @@ +package pl.tpolgrabia.urbanexplorer.utils; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.util.Log; +import com.androidquery.AQuery; +import pl.tpolgrabia.urbanexplorer.AppConstants; + +import java.net.Authenticator; +import java.net.PasswordAuthentication; + +/** + * Created by Tomasz Półgrabia (c310702) on 15.09.2016. + */ +public class NetUtils { + private static final String CLASS_TAG = NetUtils.class.getSimpleName(); + + public static AQuery createProxyAQueryInstance(Context ctx) { + final AQuery aq = new AQuery(ctx); + try { + Log.v(CLASS_TAG, "Creating aquery proxy instance"); + SharedPreferences sharedPrefs = getDefaultSharedPreferences(ctx); + boolean enabled = isProxyEnabled(ctx); + Log.v(CLASS_TAG, "Proxy is enabled: " + enabled); + if (!enabled) { + return aq; + } + + String httpProxyHost = sharedPrefs.getString(AppConstants.PREF_HTTP_PROXY_HOST_KEY, + AppConstants.DEF_HTTP_PROXY_HOST); + + String httpProxyPort = sharedPrefs.getString(AppConstants.PREF_HTTP_PROXY_PORT_KEY, + AppConstants.DEF_HTTP_PROXY_PORT); + + Log.v(CLASS_TAG, "Proxy is enabled, host: " + httpProxyHost + ", port: " + httpProxyPort); + + + if (httpProxyHost == null) { + return aq; + } + + Log.v(CLASS_TAG, "Proxy host: " + httpProxyHost + ", proxy port: " + httpProxyPort); + aq.proxy(httpProxyHost, Integer.parseInt(httpProxyPort)); + } catch (NumberFormatException e) { + Log.w(CLASS_TAG, "Invalid proxy auth number format", e); + } + + return aq; + } + + public static void setGlobalProxyAuth(Context ctx) { + Log.v(CLASS_TAG, "Setting proxy auth"); + if (isProxyEnabled(ctx)) { + Log.v(CLASS_TAG, "Setting custom proxy auth"); + SharedPreferences sharedPrefs = getDefaultSharedPreferences(ctx); + final String httpProxyUser = sharedPrefs.getString(AppConstants.PREF_HTTP_PROXY_USER_KEY, + AppConstants.DEF_HTTP_PROXY_USER); + + final String httpProxyPass = sharedPrefs.getString(AppConstants.PREF_HTTP_PROXY_PASSWORD_KEY, + AppConstants.DEF_HTTP_PROXY_PASSWORD); + + setGlobalProxyAuth(httpProxyUser, httpProxyPass); + } else { + Authenticator.setDefault(null); + Log.v(CLASS_TAG, "Setting empty proxy auth"); + } + } + + private static SharedPreferences getDefaultSharedPreferences(Context ctx) { + return PreferenceManager.getDefaultSharedPreferences(ctx); + } + + private static void setGlobalProxyAuth(final String httpProxyUser, final String httpProxyPass) { + Log.v(CLASS_TAG, "Proxy user: " + httpProxyUser + ", proxy pass: " + httpProxyPass); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + Log.v(CLASS_TAG, "Proxy auth try"); + return new PasswordAuthentication(httpProxyUser,httpProxyPass.toCharArray()); + } + }); + } + + public static boolean isProxyEnabled(Context ctx) { + SharedPreferences sharedPrefs = getDefaultSharedPreferences(ctx); + return sharedPrefs.getBoolean( + AppConstants.PREF_HTTP_PROXY_ENABLED_KEY, + AppConstants.DEF_HTTP_PROXY_ENABLED); + } + +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/PanoramioUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/PanoramioUtils.java index 9477411..c9fb795 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/PanoramioUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/PanoramioUtils.java @@ -38,7 +38,7 @@ public class PanoramioUtils { Long offset, Long count, final PanoramioResponseCallback callback) { - AQuery aq = new AQuery(ctx); + AQuery aq = NetUtils.createProxyAQueryInstance(ctx); final String aqQuery = "http://www.panoramio.com/map/get_panoramas.php?" + "set=public" + "&from=" + offset + 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 2c6061c..94ce226 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java @@ -41,7 +41,8 @@ public class WikiUtils { final Long resultsLimit, final Long radiusLimit, final WikiResponseCallback callback) { - final AQuery aq = new AQuery(ctx); + final AQuery aq = NetUtils.createProxyAQueryInstance(ctx); + aq.ajax("TODO", JSONObject.class, new AjaxCallback(){ @Override public void callback(String url, JSONObject object, AjaxStatus status) { @@ -174,7 +175,7 @@ public class WikiUtils { limit = WIKI_STD_LIMIT; } - AQuery aq = new AQuery(ctx); + AQuery aq = NetUtils.createProxyAQueryInstance(ctx); aq.ajax("https://en.wikipedia.org/w/api.php" + "?action=query" + "&list=geosearch" + @@ -312,7 +313,7 @@ public class WikiUtils { } public static void fetchPageInfos(Context ctx, List pageIds, final WikiResponseCallback callback) { - AQuery aq = new AQuery(ctx); + AQuery aq = NetUtils.createProxyAQueryInstance(ctx); aq.ajax("https://en.wikipedia.org/w/api.php" + "?action=query" + "&prop=coordinates%7Cpageimages%7Cpageterms" + @@ -343,7 +344,7 @@ public class WikiUtils { public static void fetchSingleWikiInfoItemAndRunWikiPage(Context ctx, Long pageId, AjaxCallback callback) { - new AQuery(ctx).ajax( + NetUtils.createProxyAQueryInstance(ctx).ajax( "https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=" + pageId + "&inprop=url&format=json", JSONObject.class, diff --git a/app/src/main/res/xml/urban_expl_settings.xml b/app/src/main/res/xml/urban_expl_settings.xml index f628945..a03ed55 100644 --- a/app/src/main/res/xml/urban_expl_settings.xml +++ b/app/src/main/res/xml/urban_expl_settings.xml @@ -62,4 +62,46 @@ + + + + + + + + + + + + + + + diff --git a/build.gradle b/build.gradle index e0b366a..168f129 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files