Skip to content

Commit

Permalink
About dialog remains open on rotation. Added an android:id to its
Browse files Browse the repository at this point in the history
ScrollView so its position is retained too.
  • Loading branch information
bitmold committed May 10, 2020
1 parent 0a3a4f7 commit 872b524
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 175 deletions.
66 changes: 7 additions & 59 deletions app/src/main/java/org/torproject/android/OrbotMainActivity.java
Expand Up @@ -6,6 +6,7 @@
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.AlertDialog;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
Expand All @@ -17,14 +18,12 @@
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.net.Uri;
import android.net.VpnService;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
Expand Down Expand Up @@ -66,6 +65,7 @@
import org.torproject.android.settings.SettingsPreferences;
import org.torproject.android.ui.AppManagerActivity;
import org.torproject.android.ui.Rotate3dAnimation;
import org.torproject.android.ui.dialog.AboutDialogFragment;
import org.torproject.android.ui.hiddenservices.ClientCookiesActivity;
import org.torproject.android.ui.hiddenservices.HiddenServicesActivity;
import org.torproject.android.ui.hiddenservices.backup.BackupUtils;
Expand All @@ -74,10 +74,7 @@
import org.torproject.android.ui.onboarding.BridgeWizardActivity;
import org.torproject.android.ui.onboarding.OnboardingActivity;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
Expand Down Expand Up @@ -466,8 +463,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
doExit();

} else if (item.getItemId() == R.id.menu_about) {
showAbout();

new AboutDialogFragment().show(getSupportFragmentManager(), AboutDialogFragment.TAG);
} else if (item.getItemId() == R.id.menu_scan) {
IntentIntegrator integrator = new IntentIntegrator(OrbotMainActivity.this);
integrator.initiateScan();
Expand Down Expand Up @@ -496,51 +492,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

private void showAbout() {
View view = getLayoutInflater().inflate(R.layout.layout_about, null);
String version;

try {
version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName + " (Tor " + OrbotService.BINARY_TOR_VERSION + ")";
} catch (NameNotFoundException e) {
version = "Version Not Found";
}

TextView versionName = view.findViewById(R.id.versionName);
versionName.setText(version);

TextView aboutOther = view.findViewById(R.id.aboutother);

try {
String aboutText = readFromAssets(this, "LICENSE");
aboutText = aboutText.replace("\n", "<br/>");
aboutOther.setText(Html.fromHtml(aboutText));
} catch (IOException e) {
}


new AlertDialog.Builder(this)
.setTitle(getString(R.string.button_about))
.setView(view)
.show();
}

@SuppressWarnings("SameParameterValue")
private static String readFromAssets(Context context, String filename) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open(filename)));

// do reading, usually loop until end of file reading
StringBuilder sb = new StringBuilder();
String mLine = reader.readLine();
while (mLine != null) {
sb.append(mLine).append('\n'); // process line
mLine = reader.readLine();
}
reader.close();
return sb.toString();
}


/**
* This is our attempt to REALLY exit Orbot, and stop the background service
* However, Android doesn't like people "quitting" apps, and/or our code may
Expand Down Expand Up @@ -589,14 +540,13 @@ private void enableVPN(boolean enable) {

Intent intentVPN = VpnService.prepare(this);
if (intentVPN != null)
startActivityForResult(intentVPN,REQUEST_VPN);
startActivityForResult(intentVPN, REQUEST_VPN);
else {
sendIntentToService(ACTION_START);
sendIntentToService(ACTION_START_VPN);
}

} else
{
} else {
//stop the VPN here
sendIntentToService(ACTION_STOP_VPN);
}
Expand Down Expand Up @@ -879,11 +829,9 @@ public void run() {
torStatus.equals(TorServiceConstants.STATUS_ON))
refreshVPNApps();

}
else if (request == REQUEST_VPN && response == RESULT_OK) {
} else if (request == REQUEST_VPN && response == RESULT_OK) {
sendIntentToService(ACTION_START_VPN);
}
else if (request == REQUEST_VPN && response == RESULT_CANCELED) {
} else if (request == REQUEST_VPN && response == RESULT_CANCELED) {
mBtnVPN.setChecked(false);
Prefs.putUseVpn(false);
}
Expand Down
@@ -0,0 +1,90 @@
package org.torproject.android.ui.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import org.torproject.android.R;
import org.torproject.android.service.OrbotService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AboutDialogFragment extends DialogFragment {

public static final String TAG = AboutDialogFragment.class.getSimpleName();

private TextView tvAbout;
private static final String BUNDLE_KEY_TV_ABOUT_TEXT = "about_tv_txt";

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.layout_about, null);
String version;

try {
version = getContext().getPackageManager().getPackageInfo(
getContext().getPackageName(), 0).versionName + " (Tor " +
OrbotService.BINARY_TOR_VERSION + ")";
} catch (PackageManager.NameNotFoundException e) {
version = "Version Not Found";
}

TextView versionName = view.findViewById(R.id.versionName);
versionName.setText(version);

tvAbout = view.findViewById(R.id.aboutother);

boolean buildAboutText = true;

if (savedInstanceState != null) {
String tvAboutText = savedInstanceState.getParcelable(BUNDLE_KEY_TV_ABOUT_TEXT);
if (tvAboutText != null) {
buildAboutText = false;
tvAbout.setText(tvAboutText);
}
}

if (buildAboutText) {
try {
String aboutText = readFromAssets(getContext(), "LICENSE");
aboutText = aboutText.replace("\n", "<br/>");
tvAbout.setText(Html.fromHtml(aboutText));
} catch (IOException e) {
}
}
return new AlertDialog.Builder(getContext())
.setTitle(getString(R.string.button_about))
.setView(view)
.create();
}

@SuppressWarnings("SameParameterValue")
private static String readFromAssets(Context context, String filename) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open(filename)));

// do reading, usually loop until end of file reading
StringBuilder sb = new StringBuilder();
String mLine = reader.readLine();
while (mLine != null) {
sb.append(mLine).append('\n'); // process line
mLine = reader.readLine();
}
reader.close();
return sb.toString();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(BUNDLE_KEY_TV_ABOUT_TEXT, tvAbout.getText().toString());
}
}

0 comments on commit 872b524

Please sign in to comment.