intent api: restore key

This commit is contained in:
arrase 2016-11-27 20:07:49 +01:00
parent 5d3b171e21
commit bb7ffd9692
2 changed files with 934 additions and 1039 deletions

View File

@ -3,34 +3,6 @@
package org.torproject.android;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import java.util.Random;
import java.util.StringTokenizer;
import org.json.JSONArray;
import org.torproject.android.service.OrbotConstants;
import org.torproject.android.service.util.Prefs;
import org.torproject.android.service.TorService;
import org.torproject.android.service.TorServiceConstants;
import org.torproject.android.service.util.TorServiceUtils;
import org.torproject.android.settings.SettingsPreferences;
import org.torproject.android.ui.AppManager;
import org.torproject.android.ui.hiddenservices.HiddenServicesActivity;
import org.torproject.android.ui.ImageProgressView;
import org.torproject.android.ui.PromoAppsActivity;
import org.torproject.android.ui.Rotate3dAnimation;
import org.torproject.android.ui.hiddenservices.providers.HSContentProvider;
import org.torproject.android.vpn.VPNEnableActivity;
import org.torproject.android.ui.hiddenservices.backup.BackupUtils;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
@ -86,6 +58,36 @@ import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import org.json.JSONArray;
import org.torproject.android.service.OrbotConstants;
import org.torproject.android.service.TorService;
import org.torproject.android.service.TorServiceConstants;
import org.torproject.android.service.util.Prefs;
import org.torproject.android.service.util.TorServiceUtils;
import org.torproject.android.settings.SettingsPreferences;
import org.torproject.android.ui.AppManager;
import org.torproject.android.ui.ImageProgressView;
import org.torproject.android.ui.PromoAppsActivity;
import org.torproject.android.ui.Rotate3dAnimation;
import org.torproject.android.ui.hiddenservices.HiddenServicesActivity;
import org.torproject.android.ui.hiddenservices.providers.HSContentProvider;
import org.torproject.android.vpn.VPNEnableActivity;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import java.util.Random;
import java.util.StringTokenizer;
import static android.support.v4.content.FileProvider.getUriForFile;
public class OrbotMainActivity extends AppCompatActivity
implements OrbotConstants, OnLongClickListener, OnTouchListener {
@ -130,25 +132,17 @@ public class OrbotMainActivity extends AppCompatActivity
public final static String INTENT_ACTION_REQUEST_START_TOR = "org.torproject.android.START_TOR";
// for bridge loading from the assets default bridges.txt file
class Bridge
{
class Bridge {
String type;
String config;
}
private ArrayList<Bridge> alBridges = null;
private String[] mProjection = new String[]{
HSContentProvider.HiddenService._ID,
HSContentProvider.HiddenService.NAME,
HSContentProvider.HiddenService.DOMAIN,
HSContentProvider.HiddenService.PORT,
HSContentProvider.HiddenService.ONION_PORT};
//this is needed for backwards compat back to Android 2.3.*
@SuppressLint("NewApi")
public View onCreateView(View parent, String name, Context context, AttributeSet attrs)
{
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
if (Build.VERSION.SDK_INT >= 11)
return super.onCreateView(parent, name, context, attrs);
return null;
@ -156,12 +150,10 @@ public class OrbotMainActivity extends AppCompatActivity
private void migratePreferences() {
String hsPortString = mPrefs.getString("pref_hs_ports", "");
if(hsPortString.length() > 0)
{
if (hsPortString.length() > 0) {
StringTokenizer st = new StringTokenizer(hsPortString, ",");
ContentResolver cr = getContentResolver();
while (st.hasMoreTokens())
{
while (st.hasMoreTokens()) {
int hsPort = Integer.parseInt(st.nextToken().split(" ")[0]);
ContentValues fields = new ContentValues();
fields.put("name", hsPort);
@ -176,7 +168,9 @@ public class OrbotMainActivity extends AppCompatActivity
}
}
/** Called when the activity is first created. */
/**
* Called when the activity is first created.
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -263,8 +257,7 @@ public class OrbotMainActivity extends AppCompatActivity
}
};
private void doLayout ()
{
private void doLayout() {
setContentView(R.layout.layout_main);
setTitle(R.string.app_name);
@ -304,8 +297,7 @@ public class OrbotMainActivity extends AppCompatActivity
mGestureDetector = new GestureDetector(this, new MyGestureDetector());
mBtnStart = (Button) findViewById(R.id.btnStart);
mBtnStart.setOnClickListener(new View.OnClickListener()
{
mBtnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -321,8 +313,7 @@ public class OrbotMainActivity extends AppCompatActivity
});
mBtnBrowser = (Button) findViewById(R.id.btnBrowser);
mBtnBrowser.setOnClickListener(new View.OnClickListener ()
{
mBtnBrowser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -338,27 +329,21 @@ public class OrbotMainActivity extends AppCompatActivity
boolean canDoVPN = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
if (!canDoVPN)
{
if (!canDoVPN) {
//if not SDK 14 or higher, we can't use the VPN feature
mBtnVPN.setVisibility(View.GONE);
}
else
{
} else {
boolean useVPN = Prefs.useVpn();
mBtnVPN.setChecked(useVPN);
//auto start VPN if VPN is enabled
if (useVPN)
{
if (useVPN) {
startActivity(new Intent(OrbotMainActivity.this, VPNEnableActivity.class));
}
mBtnVPN.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
mBtnVPN.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
enableVPN(isChecked);
@ -371,18 +356,14 @@ public class OrbotMainActivity extends AppCompatActivity
mBtnBridges = (SwitchCompat) findViewById(R.id.btnBridges);
mBtnBridges.setChecked(Prefs.bridgesEnabled());
mBtnBridges.setOnClickListener(new View.OnClickListener ()
{
mBtnBridges.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.CPU_ABI.contains("arm"))
{
if (Build.CPU_ABI.contains("arm")) {
promptSetupBridges(); //if ARM processor, show all bridge options
}
else
{
} else {
showGetBridgePrompt(""); //if other chip ar, only stock bridges are supported
}
}
@ -397,8 +378,7 @@ public class OrbotMainActivity extends AppCompatActivity
ArrayList<String> cList = new ArrayList<String>();
cList.add(0, getString(R.string.vpn_default_world));
for (int i = 0; i < TorServiceConstants.COUNTRY_CODES.length; i++)
{
for (int i = 0; i < TorServiceConstants.COUNTRY_CODES.length; i++) {
Locale locale = new Locale("", TorServiceConstants.COUNTRY_CODES[i]);
cList.add(locale.getDisplayCountry());
@ -463,12 +443,10 @@ public class OrbotMainActivity extends AppCompatActivity
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_settings)
{
if (item.getItemId() == R.id.menu_settings) {
Intent intent = new Intent(OrbotMainActivity.this, SettingsPreferences.class);
startActivityForResult(intent, REQUEST_SETTINGS);
}
@ -478,31 +456,23 @@ public class OrbotMainActivity extends AppCompatActivity
startActivity(new Intent(OrbotMainActivity.this, PromoAppsActivity.class));
}*/
else if (item.getItemId() == R.id.menu_exit)
{
else if (item.getItemId() == R.id.menu_exit) {
//exit app
doExit();
}
else if (item.getItemId() == R.id.menu_about)
{
} else if (item.getItemId() == R.id.menu_about) {
showAbout();
}
else if (item.getItemId() == R.id.menu_scan)
{
} else if (item.getItemId() == R.id.menu_scan) {
IntentIntegrator integrator = new IntentIntegrator(OrbotMainActivity.this);
integrator.initiateScan();
}
else if (item.getItemId() == R.id.menu_share_bridge)
{
} else if (item.getItemId() == R.id.menu_share_bridge) {
String bridges = Prefs.getBridgesList();
if (bridges != null && bridges.length() > 0)
{
if (bridges != null && bridges.length() > 0) {
try {
bridges = "bridge://" + URLEncoder.encode(bridges, "UTF-8");
@ -515,17 +485,14 @@ public class OrbotMainActivity extends AppCompatActivity
}
}
}
else if (item.getItemId() == R.id.menu_hidden_services)
{
} else if (item.getItemId() == R.id.menu_hidden_services) {
startActivity(new Intent(this, HiddenServicesActivity.class));
}
return super.onOptionsItemSelected(item);
}
private void showAbout ()
{
private void showAbout() {
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.layout_about, null);
@ -563,29 +530,24 @@ public class OrbotMainActivity extends AppCompatActivity
}
protected void onPause() {
try
{
try {
super.onPause();
if (aDialog != null)
aDialog.dismiss();
}
catch (IllegalStateException ise)
{
} catch (IllegalStateException ise) {
//can happen on exit/shutdown
}
}
private void doTorCheck ()
{
private void doTorCheck() {
openBrowser(URL_TOR_CHECK, false);
}
private void enableVPN (boolean enable)
{
private void enableVPN(boolean enable) {
Prefs.putUseVpn(enable);
if (enable) {
@ -597,8 +559,7 @@ public class OrbotMainActivity extends AppCompatActivity
stopVpnService();
}
private void enableHiddenServicePort (String hsName, final int hsPort, int hsRemotePort, final boolean doBackup, final String keyZipPath) throws RemoteException, InterruptedException
{
private void enableHiddenServicePort(String hsName, final int hsPort, int hsRemotePort, final String backupToPackage, final String keyZipPath) throws RemoteException, InterruptedException {
String onionHostname = null;
if (hsName == null)
@ -608,12 +569,19 @@ public class OrbotMainActivity extends AppCompatActivity
hsRemotePort = hsPort;
ContentValues fields = new ContentValues();
fields.put("name", hsName);
fields.put("port", hsPort);
fields.put("onion_port", hsRemotePort);
fields.put(HSContentProvider.HiddenService.NAME, hsName);
fields.put(HSContentProvider.HiddenService.PORT, hsPort);
fields.put(HSContentProvider.HiddenService.ONION_PORT, hsRemotePort);
ContentResolver cr = getContentResolver();
Cursor row = cr.query(HSContentProvider.CONTENT_URI, mProjection, "port=" + hsPort, null, null);
Cursor row = cr.query(
HSContentProvider.CONTENT_URI,
HSContentProvider.PROJECTION,
HSContentProvider.HiddenService.ONION_PORT + "=" + hsPort,
null,
null
);
if (row == null) {
cr.insert(HSContentProvider.CONTENT_URI, fields);
@ -628,13 +596,11 @@ public class OrbotMainActivity extends AppCompatActivity
new Thread() {
public void run ()
{
public void run() {
String hostname = null;
String backupPath = null;
Intent nResult = new Intent();
while (hostname == null)
{
while (hostname == null) {
//we need to stop and start Tor
try {
Thread.sleep(3000); //wait three seconds
@ -643,34 +609,45 @@ public class OrbotMainActivity extends AppCompatActivity
e.printStackTrace();
}
Cursor onion = getContentResolver().query(HSContentProvider.CONTENT_URI, mProjection, "port=" + hsPort, null, null);
if(onion != null) {
Cursor onion = getContentResolver().query(
HSContentProvider.CONTENT_URI,
HSContentProvider.PROJECTION,
HSContentProvider.HiddenService.ONION_PORT + "=" + hsPort,
null,
null
);
if (onion != null && onion.getCount() > 0) {
hostname = onion.getString(onion.getColumnIndex(HSContentProvider.HiddenService.NAME));
nResult.putExtra("hs_host", hostname);
/* TODO
BackupUtils hsutils = new BackupUtils(getApplicationContext());
if(keyZipPath != null && keyZipPath.length() > 0)
{
// TODO
if (keyZipPath != null && keyZipPath.length() > 0) {
// hsutils.restoreZipBackup(hsPort, keyZipPath);
requestTorRereadConfig();
}
/* TODO
if(doBackup)
{
backupPath = hsutils.createZipBackup(hsPort);
}
*/
if (backupToPackage != null && backupToPackage.length() > 0) {
String servicePath = getFilesDir() + "/" + TorServiceConstants.HIDDEN_SERVICES_DIR + "/hs" + hsPort;
File hidden_service_key = new File(servicePath, "private_key");
Context context = getApplicationContext();
Uri contentUri = getUriForFile(
context,
"org.torproject.android.ui.hiddenservices.storage",
hidden_service_key
);
context.grantUriPermission(backupToPackage, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
nResult.setData(contentUri);
nResult.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
onion.close();
}
}
Intent nResult = new Intent();
nResult.putExtra("hs_host", hostname);
/* TODO
if(doBackup && backupPath != null) {
nResult.putExtra("hs_backup_path", backupPath);
}
*/
setResult(RESULT_OK, nResult);
finish();
}
@ -685,8 +662,7 @@ public class OrbotMainActivity extends AppCompatActivity
}
private synchronized void handleIntents ()
{
private synchronized void handleIntents() {
if (getIntent() == null)
return;
@ -705,8 +681,8 @@ public class OrbotMainActivity extends AppCompatActivity
final int hiddenServicePort = intent.getIntExtra("hs_port", -1);
final int hiddenServiceRemotePort = intent.getIntExtra("hs_onion_port", -1);
final String hiddenServiceName = intent.getStringExtra("hs_name");
final Boolean createBackup = intent.getBooleanExtra("hs_backup", false);
final String keyZipPath = intent.getStringExtra("hs_key_zip_path");
final String backupToPackage = intent.getStringExtra("hs_backup_to_package");
final String keyZipPath = intent.getStringExtra("hs_key_path");
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@ -716,7 +692,7 @@ public class OrbotMainActivity extends AppCompatActivity
try {
enableHiddenServicePort(
hiddenServiceName, hiddenServicePort,
hiddenServiceRemotePort, createBackup, keyZipPath
hiddenServiceRemotePort, backupToPackage, keyZipPath
);
} catch (RemoteException e) {
// TODO Auto-generated catch block
@ -785,8 +761,7 @@ public class OrbotMainActivity extends AppCompatActivity
}
private void setNewBridges (String newBridgeValue)
{
private void setNewBridges(String newBridgeValue) {
Prefs.setBridgesList(newBridgeValue); //set the string to a preference
Prefs.putBridgesEnabled(true);
@ -801,30 +776,21 @@ public class OrbotMainActivity extends AppCompatActivity
/*
* Launch the system activity for Uri viewing with the provided url
*/
private void openBrowser(final String browserLaunchUrl,boolean forceExternal)
{
private void openBrowser(final String browserLaunchUrl, boolean forceExternal) {
boolean isBrowserInstalled = appInstalledOrNot(TorServiceConstants.BROWSER_APP_USERNAME);
if (isBrowserInstalled)
{
if (isBrowserInstalled) {
startIntent(TorServiceConstants.BROWSER_APP_USERNAME, Intent.ACTION_VIEW, Uri.parse(browserLaunchUrl));
}
else if (mBtnVPN.isChecked()||forceExternal)
{
} else if (mBtnVPN.isChecked() || forceExternal) {
//use the system browser since VPN is on
startIntent(null, Intent.ACTION_VIEW, Uri.parse(browserLaunchUrl));
}
else if (Prefs.useTransparentProxying())
{
} else if (Prefs.useTransparentProxying()) {
startIntent(null, Intent.ACTION_VIEW, Uri.parse(browserLaunchUrl));
}
else
{
} else {
AlertDialog aDialog = new AlertDialog.Builder(OrbotMainActivity.this)
.setTitle(R.string.install_apps_)
.setMessage(R.string.it_doesn_t_seem_like_you_have_orweb_installed_want_help_with_that_or_should_we_just_open_the_browser_)
.setPositiveButton(R.string.install_orweb, new Dialog.OnClickListener ()
{
.setPositiveButton(R.string.install_orweb, new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
@ -846,8 +812,7 @@ public class OrbotMainActivity extends AppCompatActivity
mBtnVPN.setChecked(true);
}
})
.setNegativeButton(R.string.standard_browser, new Dialog.OnClickListener ()
{
.setNegativeButton(R.string.standard_browser, new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
@ -862,8 +827,7 @@ public class OrbotMainActivity extends AppCompatActivity
}
private void startIntent (String pkg, String action, Uri data)
{
private void startIntent(String pkg, String action, Uri data) {
Intent i;
PackageManager pm = getPackageManager();
@ -872,9 +836,7 @@ public class OrbotMainActivity extends AppCompatActivity
i = pm.getLaunchIntentForPackage(pkg);
if (i == null)
throw new PackageManager.NameNotFoundException();
}
else
{
} else {
i = new Intent();
}
@ -889,16 +851,12 @@ public class OrbotMainActivity extends AppCompatActivity
}
}
private boolean appInstalledOrNot(String uri)
{
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try
{
try {
PackageInfo pi = pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return pi.applicationInfo.enabled;
}
catch (PackageManager.NameNotFoundException e)
{
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
@ -907,48 +865,35 @@ public class OrbotMainActivity extends AppCompatActivity
protected void onActivityResult(int request, int response, Intent data) {
super.onActivityResult(request, response, data);
if (request == REQUEST_SETTINGS && response == RESULT_OK)
{
if (request == REQUEST_SETTINGS && response == RESULT_OK) {
OrbotApp.forceChangeLanguage(this);
if (data != null && data.getBooleanExtra("transproxywipe", false))
{
if (data != null && data.getBooleanExtra("transproxywipe", false)) {
boolean result = flushTransProxy();
if (result)
{
if (result) {
Toast.makeText(this, R.string.transparent_proxy_rules_flushed_, Toast.LENGTH_SHORT).show();
}
else
{
} else {
Toast.makeText(this, R.string.you_do_not_have_root_access_enabled, Toast.LENGTH_SHORT).show();
}
}
else if (torStatus == TorServiceConstants.STATUS_ON)
{
} else if (torStatus == TorServiceConstants.STATUS_ON) {
updateTransProxy();
// Toast.makeText(this, R.string.you_may_need_to_stop_and_start_orbot_for_settings_change_to_be_enabled_, Toast.LENGTH_SHORT).show();
}
}
else if (request == REQUEST_VPN)
{
} else if (request == REQUEST_VPN) {
if (response == RESULT_OK) {
sendIntentToService(TorServiceConstants.CMD_VPN);
}
else
{
} else {
Prefs.putUseVpn(false);
}
}
else if (request == REQUEST_VPN_APPS_SELECT)
{
} else if (request == REQUEST_VPN_APPS_SELECT) {
startActivity(new Intent(OrbotMainActivity.this, VPNEnableActivity.class));
}
@ -958,28 +903,23 @@ public class OrbotMainActivity extends AppCompatActivity
String results = scanResult.getContents();
if (results != null && results.length() > 0)
{
if (results != null && results.length() > 0) {
try {
int urlIdx = results.indexOf("://");
if (urlIdx!=-1)
{
if (urlIdx != -1) {
results = URLDecoder.decode(results, "UTF-8");
results = results.substring(urlIdx + 3);
showAlert(getString(R.string.bridges_updated), getString(R.string.restart_orbot_to_use_this_bridge_) + results, false);
setNewBridges(results);
}
else
{
} else {
JSONArray bridgeJson = new JSONArray(results);
StringBuffer bridgeLines = new StringBuffer();
for (int i = 0; i < bridgeJson.length(); i++)
{
for (int i = 0; i < bridgeJson.length(); i++) {
String bridgeLine = bridgeJson.getString(i);
bridgeLines.append(bridgeLine).append("\n");
}
@ -997,8 +937,7 @@ public class OrbotMainActivity extends AppCompatActivity
}
public void promptSetupBridges ()
{
public void promptSetupBridges() {
loadBridgeDefaults();
LayoutInflater li = LayoutInflater.from(this);
@ -1007,8 +946,7 @@ public class OrbotMainActivity extends AppCompatActivity
TextView versionName = (TextView) view.findViewById(R.id.diaglog);
versionName.setText(R.string.if_your_mobile_network_actively_blocks_tor_you_can_use_a_tor_bridge_to_access_the_network_another_way_to_get_bridges_is_to_send_an_email_to_bridges_torproject_org_please_note_that_you_must_send_the_email_using_an_address_from_one_of_the_following_email_providers_riseup_gmail_or_yahoo_);
if (mBtnBridges.isChecked())
{
if (mBtnBridges.isChecked()) {
new AlertDialog.Builder(this)
.setTitle(R.string.bridge_mode)
.setView(view)
@ -1017,8 +955,7 @@ public class OrbotMainActivity extends AppCompatActivity
// The 'which' argument contains the index position
// of the selected item
switch (which)
{
switch (which) {
case 0: //obfs 4;
setupBridgeType("obfs4");
enableBridges(true);
@ -1042,8 +979,7 @@ public class OrbotMainActivity extends AppCompatActivity
}
}
}).setNegativeButton(R.string.btn_cancel, new Dialog.OnClickListener()
{
}).setNegativeButton(R.string.btn_cancel, new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
@ -1054,16 +990,13 @@ public class OrbotMainActivity extends AppCompatActivity
.show();
}
else
{
} else {
enableBridges(false);
}
}
private void showGetBridgePrompt (final String type)
{
private void showGetBridgePrompt(final String type) {
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.layout_diag, null);
@ -1073,15 +1006,13 @@ public class OrbotMainActivity extends AppCompatActivity
new AlertDialog.Builder(this)
.setTitle(R.string.bridge_mode)
.setView(view)
.setNegativeButton(R.string.btn_cancel, new Dialog.OnClickListener()
{
.setNegativeButton(R.string.btn_cancel, new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do nothing
}
})
.setNeutralButton(R.string.get_bridges_email, new Dialog.OnClickListener ()
{
.setNeutralButton(R.string.get_bridges_email, new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
@ -1093,8 +1024,7 @@ public class OrbotMainActivity extends AppCompatActivity
})
.setPositiveButton(R.string.get_bridges_web, new Dialog.OnClickListener ()
{
.setPositiveButton(R.string.get_bridges_web, new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
@ -1107,20 +1037,16 @@ public class OrbotMainActivity extends AppCompatActivity
}).show();
}
private void sendGetBridgeEmail (String type)
{
private void sendGetBridgeEmail(String type) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"bridges@torproject.org"});
if (type != null)
{
if (type != null) {
intent.putExtra(Intent.EXTRA_SUBJECT, "get transport " + type);
intent.putExtra(Intent.EXTRA_TEXT, "get transport " + type);
}
else
{
} else {
intent.putExtra(Intent.EXTRA_SUBJECT, "get bridges");
intent.putExtra(Intent.EXTRA_TEXT, "get bridges");
@ -1129,15 +1055,12 @@ public class OrbotMainActivity extends AppCompatActivity
startActivity(Intent.createChooser(intent, getString(R.string.send_email)));
}
private void enableBridges (boolean enable)
{
private void enableBridges(boolean enable) {
Prefs.putBridgesEnabled(enable);
if (torStatus == TorServiceConstants.STATUS_ON)
{
if (torStatus == TorServiceConstants.STATUS_ON) {
String bridgeList = Prefs.getBridgesList();
if (bridgeList != null && bridgeList.length() > 0)
{
if (bridgeList != null && bridgeList.length() > 0) {
requestTorRereadConfig();
}
}
@ -1147,19 +1070,16 @@ public class OrbotMainActivity extends AppCompatActivity
sendIntentToService(TorServiceConstants.CMD_SIGNAL_HUP);
}
public void stopVpnService ()
{
public void stopVpnService() {
sendIntentToService(TorServiceConstants.CMD_VPN_CLEAR);
}
private boolean flushTransProxy ()
{
private boolean flushTransProxy() {
sendIntentToService(TorServiceConstants.CMD_FLUSH);
return true;
}
private boolean updateTransProxy ()
{
private boolean updateTransProxy() {
sendIntentToService(TorServiceConstants.CMD_UPDATE_TRANS_PROXY);
return true;
}
@ -1182,26 +1102,21 @@ public class OrbotMainActivity extends AppCompatActivity
//sometimes this can go haywire or crazy with too many error
//messages from Tor, and the user cannot stop or exit Orbot
//so need to ensure repeated error messages are not spamming this method
private void showAlert(String title, String msg, boolean button)
{
try
{
private void showAlert(String title, String msg, boolean button) {
try {
if (aDialog != null && aDialog.isShowing())
aDialog.dismiss();
}
catch (Exception e){} //swallow any errors
} catch (Exception e) {
} //swallow any errors
if (button)
{
if (button) {
aDialog = new AlertDialog.Builder(OrbotMainActivity.this)
.setIcon(R.drawable.onion32)
.setTitle(title)
.setMessage(msg)
.setPositiveButton(R.string.btn_okay, null)
.show();
}
else
{
} else {
aDialog = new AlertDialog.Builder(OrbotMainActivity.this)
.setIcon(R.drawable.onion32)
.setTitle(title)
@ -1228,20 +1143,17 @@ public class OrbotMainActivity extends AppCompatActivity
mBtnBrowser.setEnabled(true);
mBtnStart.setText(R.string.menu_stop);
if (torServiceMsg != null)
{
if (torServiceMsg != null) {
if (torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_HEADER)) {
lblStatus.setText(torServiceMsg);
}
}
else
} else
lblStatus.setText(getString(R.string.status_activated));
boolean showFirstTime = mPrefs.getBoolean("connect_first_time", true);
if (showFirstTime)
{
if (showFirstTime) {
Editor pEdit = mPrefs.edit();
pEdit.putBoolean("connect_first_time", false);
pEdit.commit();
@ -1249,8 +1161,7 @@ public class OrbotMainActivity extends AppCompatActivity
getString(R.string.connect_first_time), true);
}
if (autoStartFromIntent)
{
if (autoStartFromIntent) {
autoStartFromIntent = false;
Intent resultIntent = lastStatusIntent;
resultIntent.putExtra(TorServiceConstants.EXTRA_STATUS, torStatus);
@ -1260,17 +1171,14 @@ public class OrbotMainActivity extends AppCompatActivity
}
} else if (torStatus == TorServiceConstants.STATUS_STARTING) {
imgStatus.setImageResource(R.drawable.torstarting);
if (torServiceMsg != null)
{
if (torServiceMsg != null) {
if (torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_BOOTSTRAPPED))
lblStatus.setText(torServiceMsg);
}
else
} else
lblStatus.setText(getString(R.string.status_starting_up));
mBtnStart.setText("...");
@ -1296,8 +1204,7 @@ public class OrbotMainActivity extends AppCompatActivity
}
if (torServiceMsg != null && torServiceMsg.length() > 0)
{
if (torServiceMsg != null && torServiceMsg.length() > 0) {
mTxtOrbotLog.append(torServiceMsg + '\n');
}
}
@ -1365,13 +1272,11 @@ public class OrbotMainActivity extends AppCompatActivity
//now you can handle the intents properly
handleIntents();
}
else if (newTorStatus != null && !torStatus.equals(newTorStatus)) //status changed
} else if (newTorStatus != null && !torStatus.equals(newTorStatus)) //status changed
{
torStatus = newTorStatus;
updateStatus(log);
}
else if (log != null) //it is just a log
} else if (log != null) //it is just a log
updateStatus(log);
switch (msg.what) {
@ -1443,8 +1348,7 @@ public class OrbotMainActivity extends AppCompatActivity
private static final float ROTATE_FROM = 0.0f;
private static final float ROTATE_TO = 360.0f * 4f;// 3.141592654f * 32.0f;
public void spinOrbot (float direction)
{
public void spinOrbot(float direction) {
sendIntentToService(TorServiceConstants.CMD_NEWNYM);
@ -1465,8 +1369,7 @@ public class OrbotMainActivity extends AppCompatActivity
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (torStatus == TorServiceConstants.STATUS_ON)
{
if (torStatus == TorServiceConstants.STATUS_ON) {
float direction = 1f;
if (velocityX < 0)
direction = -1f;
@ -1479,14 +1382,11 @@ public class OrbotMainActivity extends AppCompatActivity
}
}
private void loadBridgeDefaults ()
{
if (alBridges == null)
{
private void loadBridgeDefaults() {
if (alBridges == null) {
alBridges = new ArrayList<Bridge>();
try
{
try {
BufferedReader in =
new BufferedReader(new InputStreamReader(getAssets().open("bridges.txt"), "UTF-8"));
String str;
@ -1509,17 +1409,14 @@ public class OrbotMainActivity extends AppCompatActivity
}
in.close();
}
catch (Exception e)
{
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void setupBridgeType (String type)
{
private void setupBridgeType(String type) {
StringBuffer sbConfig = new StringBuffer();
//we should randomly sort alBridges so we don't have the same bridge order each time
@ -1531,10 +1428,8 @@ public class OrbotMainActivity extends AppCompatActivity
int bridgeCount = 0;
//now go through the list to find the bridges we want
for (Bridge b : alBridges)
{
if (b.type.equals(type))
{
for (Bridge b : alBridges) {
if (b.type.equals(type)) {
sbConfig.append(b.type);
sbConfig.append(' ');

View File

@ -58,7 +58,7 @@ public class BackupUtils {
JSONObject config = new JSONObject();
try {
if (portData.getCount() != 1)
if (portData == null || portData.getCount() != 1)
return null;
portData.moveToNext();
@ -184,7 +184,7 @@ public class BackupUtils {
null
);
if (service.getCount() == 0) {
if (service == null || service.getCount() == 0) {
mResolver.insert(HSContentProvider.CONTENT_URI, fields);
} else {
mResolver.update(
@ -193,9 +193,9 @@ public class BackupUtils {
HSContentProvider.HiddenService.PORT + "=" + port,
null
);
}
service.close();
}
} catch (JSONException e) {
e.printStackTrace();