Skip to content

Commit

Permalink
Add ability to set a proxy.
Browse files Browse the repository at this point in the history
If Orbot is installed, a request to start its connection will be made if the proxy is enabled and Orbot is not connected.
  • Loading branch information
velazcod committed Jan 4, 2014
1 parent 5c32e3e commit 7aa201a
Show file tree
Hide file tree
Showing 8 changed files with 649 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import android.preference.PreferenceManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import com.danvelazco.fbwrapper.activity.BaseFacebookWebViewActivity;
import com.danvelazco.fbwrapper.preferences.FacebookPreferences;
import com.danvelazco.fbwrapper.util.Logger;
import com.danvelazco.fbwrapper.util.OrbotHelper;

/**
* Facebook web wrapper activity.
Expand Down Expand Up @@ -194,14 +196,33 @@ private void loadPreferences() {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
}

// Get the URL load and check-in settings
// Get the URL to load, check-in and proxy settings
boolean anyDomain = mSharedPreferences.getBoolean(FacebookPreferences.OPEN_LINKS_INSIDE, false);
boolean allowCheckins = mSharedPreferences.getBoolean(FacebookPreferences.ALLOW_CHECKINS, false);
boolean enableProxy = mSharedPreferences.getBoolean(FacebookPreferences.KEY_PROXY_ENABLED, false);
String proxyHost = mSharedPreferences.getString(FacebookPreferences.KEY_PROXY_HOST, null);
String proxyPort = mSharedPreferences.getString(FacebookPreferences.KEY_PROXY_PORT, null);

// Set the flags for loading URLs and allowing geolocation
setAllowCheckins(allowCheckins);
setAllowAnyDomain(anyDomain);

if (enableProxy && !TextUtils.isEmpty(proxyHost) && !TextUtils.isEmpty(proxyPort)) {
int proxyPortInt = -1;
try {
proxyPortInt = Integer.parseInt(proxyPort);
} catch (Exception e) {
e.printStackTrace();
}
setProxy(proxyHost, proxyPortInt);

// If Orbot is installed and not running, request to start it
OrbotHelper orbotHelper = new OrbotHelper(this);
if (orbotHelper.isOrbotInstalled() && !orbotHelper.isOrbotRunning()) {
orbotHelper.requestOrbotStart(this);
}
}

// Whether the site should be loaded as the mobile or desktop version
String mode = mSharedPreferences.getString(FacebookPreferences.SITE_MODE,
FacebookPreferences.SITE_MODE_AUTO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieSyncManager;
Expand All @@ -39,6 +40,8 @@
import android.widget.ProgressBar;
import com.danvelazco.fbwrapper.R;
import com.danvelazco.fbwrapper.util.Logger;
import com.danvelazco.fbwrapper.util.OrbotHelper;
import com.danvelazco.fbwrapper.util.WebViewProxyUtil;
import com.danvelazco.fbwrapper.webview.FacebookWebChromeClient;
import com.danvelazco.fbwrapper.webview.FacebookWebView;
import com.danvelazco.fbwrapper.webview.FacebookWebViewClient;
Expand Down Expand Up @@ -221,6 +224,18 @@ public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

/**
* Set a proxy for the {@link com.danvelazco.fbwrapper.webview.FacebookWebView}
*
* @param host {@link String}
* @param port {@link int}
*/
protected final void setProxy(String host, int port) {
if (mWebView != null && !TextUtils.isEmpty(host) && port > 0) {
WebViewProxyUtil.setProxy(getApplicationContext(), mWebView, host, port);
}
}

/**
* Restore the state of the {@link FacebookWebView}
*
Expand Down Expand Up @@ -379,17 +394,20 @@ private void updateCacheMode() {
* {@inheritDoc}
*/
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
// Handle file uploads
if (requestCode == RESULT_CODE_FILE_UPLOAD) {
if (null == mUploadMessage) {
return;
}
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case OrbotHelper.REQUEST_CODE_START_ORBOT:
mWebView.reload();
break;
case RESULT_CODE_FILE_UPLOAD:
if (null == mUploadMessage) {
return;
}
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
Expand All @@ -37,16 +37,23 @@ public class FacebookPreferences extends PreferenceActivity {
// Custom preferences
public final static String MENU_DRAWER_SHOWED_OPENED = "drawer_shown_opened";

// Shared preferences
// Shared preference keys
public final static String CAT_GENERAL = "pref_cat_general";
public final static String ALLOW_CHECKINS = "prefs_allow_checkins";
public final static String OPEN_LINKS_INSIDE = "prefs_open_links_inside";
public final static String KEY_PROXY_ENABLED = "prefs_enable_proxy";
public final static String KEY_PROXY_HOST = "prefs_proxy_host";
public final static String KEY_PROXY_PORT = "prefs_proxy_port";
public final static String SITE_MODE = "prefs_mobile_site";
public final static String SITE_MODE_AUTO = "auto";
public final static String SITE_MODE_MOBILE = "mobile";
public final static String SITE_MODE_DESKTOP = "desktop";
public final static String ABOUT = "pref_about";

// Preferences
private EditTextPreference mPrefProxyHost = null;
private EditTextPreference mPrefProxyPort = null;

/**
* {@inheritDoc}
*/
Expand All @@ -55,6 +62,41 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
addPreferencesFromResource(R.xml.main_preferences);

mPrefProxyHost = (EditTextPreference) findPreference(KEY_PROXY_HOST);
mPrefProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String newProxyHostValue = (String) newValue;
mPrefProxyHost.setSummary(newProxyHostValue);
return true;
}
});

mPrefProxyPort = (EditTextPreference) findPreference(KEY_PROXY_PORT);
mPrefProxyPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String newProxyPortValue = (String) newValue;
mPrefProxyPort.setSummary(newProxyPortValue);
return true;
}
});
}

/**
* {@inheritDoc}
*/
@Override
protected void onResume() {
super.onResume();

if (mPrefProxyHost != null) {
mPrefProxyHost.setSummary(mPrefProxyHost.getText());
}
if (mPrefProxyPort != null) {
mPrefProxyPort.setSummary(mPrefProxyPort.getText());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.danvelazco.fbwrapper.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import com.danvelazco.fbwrapper.R;

/**
* Modified utility class from OnionKit library
*/
public class OrbotHelper {

// Constants
public final static String URI_ORBOT = "org.torproject.android";
public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor";
public final static String ACTION_START_TOR = "org.torproject.android.START_TOR";
public final static int REQUEST_CODE_START_ORBOT = 80010;

// Members
private Context mContext = null;

/**
* Constructor
*
* @param context {@link Context}
*/
public OrbotHelper(Context context) {
mContext = context;
}

/**
* Check whether or not Orbot is running.
*
* @return {@link boolean}
*/
public boolean isOrbotRunning() {
int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH);
return (procId != -1);
}

/**
* Check whether or not Orbot is installed.
*
* @return {@link boolean}
*/
public boolean isOrbotInstalled() {
return isAppInstalled(URI_ORBOT);
}

private boolean isAppInstalled(String uri) {
PackageManager pm = mContext.getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}

/**
* Request Orbot to start and connect
*
* @param activity {@link Activity}
*/
public void requestOrbotStart(final Activity activity) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
downloadDialog.setTitle(R.string.start_orbot_);
downloadDialog.setMessage(R.string.orbot_not_running_start_it_question);
downloadDialog.setPositiveButton(R.string.lbl_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(URI_ORBOT);
intent.setAction(ACTION_START_TOR);
activity.startActivityForResult(intent, REQUEST_CODE_START_ORBOT);
}
});
downloadDialog.setNegativeButton(R.string.lbl_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
downloadDialog.show();

}

}
Loading

0 comments on commit 7aa201a

Please sign in to comment.