1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-24 13:22:08 +01:00

Avoid passing MainActivity to OrbotStatusReceiver and handle Orbot foreground starts in onActivityResult

This commit is contained in:
vanitasvitae 2016-06-06 16:06:16 +02:00
parent 8bbd7b996e
commit da80c11b66
2 changed files with 42 additions and 17 deletions

View file

@ -40,6 +40,7 @@ import android.support.annotation.NonNull;
import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.NavigationView; import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
@ -51,6 +52,7 @@ import android.text.Html;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.text.util.Linkify; import android.text.util.Linkify;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -179,7 +181,7 @@ public class MainActivity extends AppCompatActivity
podUserProfile.setListener(this); podUserProfile.setListener(this);
//Orbot integration //Orbot integration
OrbotStatusReceiver.setMainActivity(this); LocalBroadcastManager.getInstance(this).registerReceiver(brRequestShowOrbot, new IntentFilter(OrbotStatusReceiver.REQUEST_SHOW_ORBOT));
OrbotHelper.requestStartTor(getApplicationContext()); OrbotHelper.requestStartTor(getApplicationContext());
if(appSettings.isProxyOrbot()) { if(appSettings.isProxyOrbot()) {
if(!OrbotHelper.isOrbotInstalled(getApplicationContext())) { if(!OrbotHelper.isOrbotInstalled(getApplicationContext())) {
@ -332,6 +334,7 @@ public class MainActivity extends AppCompatActivity
Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show();
} }
} }
LocalBroadcastManager.getInstance(this).unregisterReceiver(brRequestShowOrbot);
} }
private void setupNavigationSlider() { private void setupNavigationSlider() {
@ -384,6 +387,10 @@ public class MainActivity extends AppCompatActivity
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == OrbotHelper.START_TOR_RESULT) {
OrbotStatusReceiver.setProxy(this, data);
return;
}
if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) { if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
return; return;
@ -422,6 +429,7 @@ public class MainActivity extends AppCompatActivity
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
registerReceiver(brLoadUrl, new IntentFilter(URL_MESSAGE)); registerReceiver(brLoadUrl, new IntentFilter(URL_MESSAGE));
LocalBroadcastManager.getInstance(this).registerReceiver(brRequestShowOrbot, new IntentFilter(OrbotStatusReceiver.REQUEST_SHOW_ORBOT));
} }
@Override @Override
@ -455,9 +463,17 @@ public class MainActivity extends AppCompatActivity
} }
}; };
private final BroadcastReceiver brRequestShowOrbot = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
requestOrbotStart(intent.getBooleanExtra(OrbotStatusReceiver.EXTRA_BACKGROUND_STARTS_DISABLED, false));
}
};
@Override @Override
protected void onPause() { protected void onPause() {
unregisterReceiver(brLoadUrl); unregisterReceiver(brLoadUrl);
LocalBroadcastManager.getInstance(this).unregisterReceiver(brRequestShowOrbot);
super.onPause(); super.onPause();
} }
@ -1105,7 +1121,7 @@ public class MainActivity extends AppCompatActivity
startDialog.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { startDialog.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialogInterface, int i) { public void onClick(DialogInterface dialogInterface, int i) {
startActivityForResult(OrbotHelper.getShowOrbotStartIntent(), 1); startActivityForResult(OrbotHelper.getShowOrbotStartIntent(), OrbotHelper.START_TOR_RESULT);
} }
}).setNegativeButton(android.R.string.no, null).show(); }).setNegativeButton(android.R.string.no, null).show();
} }

View file

@ -24,6 +24,7 @@ import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log; import android.util.Log;
import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.App;
@ -42,14 +43,14 @@ public class OrbotStatusReceiver extends BroadcastReceiver {
public static final String EXTRA_HTTP_HOST = "org.torproject.android.intent.extra.HTTP_PROXY_HOST"; public static final String EXTRA_HTTP_HOST = "org.torproject.android.intent.extra.HTTP_PROXY_HOST";
public static final String EXTRA_HTTP_PORT = "org.torproject.android.intent.extra.HTTP_PROXY_PORT"; public static final String EXTRA_HTTP_PORT = "org.torproject.android.intent.extra.HTTP_PROXY_PORT";
public static final String REQUEST_SHOW_ORBOT = "com.github.dfa.diaspora.request_show_orbot";
public static final String EXTRA_BACKGROUND_STARTS_DISABLED = "com.github.dfa.diaspora.request_show_orbot.BG_START_DISABLED";
public static final String DEFAULT_HOST = "127.0.0.1"; public static final String DEFAULT_HOST = "127.0.0.1";
public static final int DEFAULT_PORT = 8118; public static final int DEFAULT_PORT = 8118;
private static String host = ""; private static String host = "";
private static int port = 0; private static int port = 0;
private Intent lastStatus; private static boolean promptOnBackgroundStart = true;
private Context lastContext;
private static MainActivity mainActivity;
private AppSettings appSettings; private AppSettings appSettings;
private static boolean proxySet = false; private static boolean proxySet = false;
@ -59,19 +60,29 @@ public class OrbotStatusReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if(OrbotHelper.ACTION_STATUS.equals(intent.getAction())) { if(OrbotHelper.ACTION_STATUS.equals(intent.getAction())) {
lastStatus = intent;
lastContext = context;
if(appSettings == null) appSettings = new AppSettings(context.getApplicationContext()); if(appSettings == null) appSettings = new AppSettings(context.getApplicationContext());
String orbotStatus = intent.getExtras().getString(OrbotHelper.EXTRA_STATUS); String orbotStatus = intent.getExtras().getString(OrbotHelper.EXTRA_STATUS);
if(appSettings.isProxyOrbot()) { if(appSettings.isProxyOrbot() && orbotStatus != null) {
//Status on
if (orbotStatus.equals(OrbotHelper.STATUS_ON)) { if (orbotStatus.equals(OrbotHelper.STATUS_ON)) {
setProxy(lastContext, lastStatus); setProxy(context, intent);
} else if(orbotStatus.equals(OrbotHelper.STATUS_OFF)) { return;
}
//Status off
if(orbotStatus.equals(OrbotHelper.STATUS_OFF)) {
Log.d(App.TAG, "Warning: Orbot reports status off."); Log.d(App.TAG, "Warning: Orbot reports status off.");
OrbotHelper.requestStartTor(context.getApplicationContext()); Intent orbotOffIntent = new Intent(REQUEST_SHOW_ORBOT);
} else if(orbotStatus.equals(OrbotHelper.STATUS_STARTS_DISABLED)) { orbotOffIntent.putExtra(EXTRA_BACKGROUND_STARTS_DISABLED, false);
LocalBroadcastManager.getInstance(context).sendBroadcast(orbotOffIntent);
return;
}
//Background starts disabled
if(orbotStatus.equals(OrbotHelper.STATUS_STARTS_DISABLED ) && promptOnBackgroundStart) {
promptOnBackgroundStart = false;
Log.d(App.TAG, "Warning: Orbot has background starts disabled."); Log.d(App.TAG, "Warning: Orbot has background starts disabled.");
if(mainActivity != null) mainActivity.requestOrbotStart(true); Intent backgroundStartsDisabledIntent = new Intent(REQUEST_SHOW_ORBOT);
backgroundStartsDisabledIntent.putExtra(EXTRA_BACKGROUND_STARTS_DISABLED, true);
LocalBroadcastManager.getInstance(context).sendBroadcast(backgroundStartsDisabledIntent);
} }
} }
} else { } else {
@ -95,7 +106,7 @@ public class OrbotStatusReceiver extends BroadcastReceiver {
} }
} }
} else { } else {
Log.e(App.TAG, "OrbotStatusReceiver: lastStatus intent is null. Cannot set Proxy."); setProxy(context, DEFAULT_HOST, DEFAULT_PORT);
} }
} }
@ -114,6 +125,7 @@ public class OrbotStatusReceiver extends BroadcastReceiver {
} }
public static void resetProxy(Context context) { public static void resetProxy(Context context) {
promptOnBackgroundStart = true;
try { try {
OrbotStatusReceiver.host = ""; OrbotStatusReceiver.host = "";
OrbotStatusReceiver.port = 0; OrbotStatusReceiver.port = 0;
@ -135,7 +147,4 @@ public class OrbotStatusReceiver extends BroadcastReceiver {
return proxySet; return proxySet;
} }
public static void setMainActivity(MainActivity main) {
mainActivity = main;
}
} }