commit 929407d4c673f6f955fe8e6940de921ca1cb1b19 Author: bim dsnake@protonmail.com Date: Tue Sep 29 19:14:02 2020 -0400
finishing touches (also use bundle keys in backup flows) --- .../ui/hiddenservices/ClientCookiesActivity.java | 61 +++++++++------------- .../ui/hiddenservices/HiddenServicesActivity.java | 15 ++++-- .../dialogs/CookieActionsDialog.java | 40 +++++++++----- .../hiddenservices/dialogs/CookieDeleteDialog.java | 3 +- .../ui/hiddenservices/dialogs/HSActionsDialog.java | 47 +++++++++-------- .../ui/hiddenservices/dialogs/HSCookieDialog.java | 7 ++- .../ui/hiddenservices/dialogs/HSDeleteDialog.java | 9 ++-- 7 files changed, 95 insertions(+), 87 deletions(-)
diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/ClientCookiesActivity.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/ClientCookiesActivity.java index 86b7b5ac..d221bd21 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/ClientCookiesActivity.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/ClientCookiesActivity.java @@ -34,6 +34,10 @@ import org.torproject.android.ui.hiddenservices.providers.CookieContentProvider; import java.io.File;
public class ClientCookiesActivity extends AppCompatActivity { + public static final String BUNDLE_KEY_ID = "_id", + BUNDLE_KEY_DOMAIN = "domain", + BUNDLE_KEY_COOKIE = "auth_cookie_value", + BUNDLE_KEY_ENABLED = "enabled"; private static final int REQUEST_CODE_READ_COOKIE = 54; private ContentResolver mResolver; private ClientCookiesAdapter mAdapter; @@ -65,11 +69,10 @@ public class ClientCookiesActivity extends AppCompatActivity { Cursor item = (Cursor) parent.getItemAtPosition(position);
Bundle arguments = new Bundle(); - arguments.putInt("_id", item.getInt(item.getColumnIndex(CookieContentProvider.ClientCookie._ID))); - - arguments.putString("domain", item.getString(item.getColumnIndex(CookieContentProvider.ClientCookie.DOMAIN))); - arguments.putString("auth_cookie_value", item.getString(item.getColumnIndex(CookieContentProvider.ClientCookie.AUTH_COOKIE_VALUE))); - arguments.putInt("enabled", item.getInt(item.getColumnIndex(CookieContentProvider.ClientCookie.ENABLED))); + arguments.putInt(BUNDLE_KEY_ID, item.getInt(item.getColumnIndex(CookieContentProvider.ClientCookie._ID))); + arguments.putString(BUNDLE_KEY_DOMAIN, item.getString(item.getColumnIndex(CookieContentProvider.ClientCookie.DOMAIN))); + arguments.putString(BUNDLE_KEY_COOKIE, item.getString(item.getColumnIndex(CookieContentProvider.ClientCookie.AUTH_COOKIE_VALUE))); + arguments.putInt(BUNDLE_KEY_ENABLED, item.getInt(item.getColumnIndex(CookieContentProvider.ClientCookie.ENABLED)));
CookieActionsDialog dialog = new CookieActionsDialog(); dialog.setArguments(arguments); @@ -138,39 +141,25 @@ public class ClientCookiesActivity extends AppCompatActivity { @Override protected void onActivityResult(int request, int response, Intent data) { super.onActivityResult(request, response, data); - - if (request == REQUEST_CODE_READ_COOKIE) { - if (response != RESULT_OK) return; + if (request == REQUEST_CODE_READ_COOKIE && response == RESULT_OK) { String cookieStr = DiskUtils.readFileFromInputStream(mResolver, data.getData()); new BackupUtils(this).restoreCookieBackup(cookieStr); - return; - } - - IntentResult scanResult = IntentIntegrator.parseActivityResult(request, response, data); - - if (scanResult == null) return; - - String results = scanResult.getContents(); - - if (results == null || results.length() < 1) return; - - try { - JSONObject savedValues = new JSONObject(results); - ContentValues fields = new ContentValues(); - - fields.put( - CookieContentProvider.ClientCookie.DOMAIN, - savedValues.getString(CookieContentProvider.ClientCookie.DOMAIN)); - - fields.put( - CookieContentProvider.ClientCookie.AUTH_COOKIE_VALUE, - savedValues.getString(CookieContentProvider.ClientCookie.AUTH_COOKIE_VALUE)); - - mResolver.insert(CookieContentProvider.CONTENT_URI, fields); - - } catch (JSONException e) { - e.printStackTrace(); - Toast.makeText(this, R.string.error, Toast.LENGTH_LONG).show(); + } else { + IntentResult scanResult = IntentIntegrator.parseActivityResult(request, response, data); + if (scanResult == null) return; + + String results = scanResult.getContents(); + if (results == null || results.length() < 1) return; + try { + JSONObject savedValues = new JSONObject(results); + ContentValues fields = new ContentValues(); + fields.put(CookieContentProvider.ClientCookie.DOMAIN, savedValues.getString(CookieContentProvider.ClientCookie.DOMAIN)); + fields.put(CookieContentProvider.ClientCookie.AUTH_COOKIE_VALUE, savedValues.getString(CookieContentProvider.ClientCookie.AUTH_COOKIE_VALUE)); + mResolver.insert(CookieContentProvider.CONTENT_URI, fields); + } catch (JSONException e) { + e.printStackTrace(); + Toast.makeText(this, R.string.error, Toast.LENGTH_LONG).show(); + } } }
diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java index f3efa689..2bf3eeda 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java @@ -33,6 +33,11 @@ import org.torproject.android.ui.hiddenservices.providers.HSContentProvider; import java.io.File;
public class HiddenServicesActivity extends AppCompatActivity { + public static final String BUNDLE_KEY_ID = "_id", + BUNDLE_KEY_PORT = "port", + BUNDLE_KEY_ONION = "onion", + BUNDLE_KEY_AUTH_COOKIE = "auth_cookie", + BUNDLE_KEY_AUTH_COOKIE_VALUE = "auth_cookie_value"; private static final int REQUEST_CODE_READ_ZIP_BACKUP = 125; private static final String BUNDLE_KEY_SHOW_USER_SERVICES = "show_user_services"; private ContentResolver mResolver; @@ -75,11 +80,11 @@ public class HiddenServicesActivity extends AppCompatActivity { Cursor item = (Cursor) parent.getItemAtPosition(position);
Bundle arguments = new Bundle(); - arguments.putInt("_id", item.getInt(item.getColumnIndex(HSContentProvider.HiddenService._ID))); - arguments.putString("port", item.getString(item.getColumnIndex(HSContentProvider.HiddenService.PORT))); - arguments.putString("onion", item.getString(item.getColumnIndex(HSContentProvider.HiddenService.DOMAIN))); - arguments.putInt("auth_cookie", item.getInt(item.getColumnIndex(HSContentProvider.HiddenService.AUTH_COOKIE))); - arguments.putString("auth_cookie_value", item.getString(item.getColumnIndex(HSContentProvider.HiddenService.AUTH_COOKIE_VALUE))); + arguments.putInt(BUNDLE_KEY_ID, item.getInt(item.getColumnIndex(HSContentProvider.HiddenService._ID))); + arguments.putString(BUNDLE_KEY_PORT, item.getString(item.getColumnIndex(HSContentProvider.HiddenService.PORT))); + arguments.putString(BUNDLE_KEY_ONION, item.getString(item.getColumnIndex(HSContentProvider.HiddenService.DOMAIN))); + arguments.putInt(BUNDLE_KEY_AUTH_COOKIE, item.getInt(item.getColumnIndex(HSContentProvider.HiddenService.AUTH_COOKIE))); + arguments.putString(BUNDLE_KEY_AUTH_COOKIE_VALUE, item.getString(item.getColumnIndex(HSContentProvider.HiddenService.AUTH_COOKIE_VALUE)));
HSActionsDialog dialog = new HSActionsDialog(); dialog.setArguments(arguments); diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/CookieActionsDialog.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/CookieActionsDialog.java index fb1d1212..8c5ef304 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/CookieActionsDialog.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/CookieActionsDialog.java @@ -16,6 +16,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.torproject.android.R; import org.torproject.android.core.DiskUtils; +import org.torproject.android.ui.hiddenservices.ClientCookiesActivity; import org.torproject.android.ui.hiddenservices.providers.CookieContentProvider;
import java.io.File; @@ -33,27 +34,35 @@ public class CookieActionsDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Bundle arguments = getArguments(); - domain = arguments.getString("domain"); - cookie = arguments.getString("auth_cookie_value"); - enabled = arguments.getInt("enabled"); + domain = arguments.getString(ClientCookiesActivity.BUNDLE_KEY_DOMAIN); + cookie = arguments.getString(ClientCookiesActivity.BUNDLE_KEY_COOKIE); + enabled = arguments.getInt(ClientCookiesActivity.BUNDLE_KEY_ENABLED);
- return new AlertDialog.Builder(getActivity()) + AlertDialog ad = new AlertDialog.Builder(getActivity()) .setTitle(R.string.client_cookies) .setItems(new CharSequence[]{ getString(R.string.backup_cookie), getString(R.string.delete_cookie) - }, (dialog, which) -> { - if (which == 0) doBackup(); - else doDelete(arguments); - }) + }, null) .setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss()) .create(); + + ad.getListView().setOnItemClickListener((parent, view, position, id) -> { + if (position == 0) doBackup(); + else { + doDelete(arguments); + ad.dismiss(); + } + }); + + return ad; }
private void doDelete(Bundle arguments) { CookieDeleteDialog dialog = new CookieDeleteDialog(); dialog.setArguments(arguments); dialog.show(getFragmentManager(), "CookieDeleteDialog"); + dismiss(); }
private void doBackup() { @@ -62,6 +71,7 @@ public class CookieActionsDialog extends DialogFragment { Intent createFile = DiskUtils.createWriteFileIntent(filename, "application/json"); startActivityForResult(createFile, REQUEST_CODE_WRITE_FILE); } else { // API 16, 17, and 18 + int msg = R.string.backup_saved_at_external_storage; try { File externalStorage = DiskUtils.getOrCreateLegacyBackupDir(); String backupFile = externalStorage.getAbsolutePath() + "/" + filename; @@ -70,8 +80,10 @@ public class CookieActionsDialog extends DialogFragment { writer.write(data); writer.close(); } catch (JSONException | IOException e) { - Toast.makeText(getContext(), R.string.error, Toast.LENGTH_LONG).show(); + msg = R.string.error; } + Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show(); + dismiss(); } }
@@ -87,20 +99,20 @@ public class CookieActionsDialog extends DialogFragment { public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_WRITE_FILE && resultCode == Activity.RESULT_OK) { if (data != null) { + int msg = R.string.backup_saved_at_external_storage; Uri file = data.getData(); try { ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(file, "w"); FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor()); fileOutputStream.write(createBackupData().getBytes()); - // Let the document provider know you're done by closing the stream. - fileOutputStream.close(); + fileOutputStream.close(); // Let the document provider know you're done by closing the stream pfd.close(); } catch (JSONException | IOException e) { e.printStackTrace(); - Toast.makeText(getContext(), R.string.error, Toast.LENGTH_LONG).show(); - return; + msg = R.string.error; } - Toast.makeText(getContext(), R.string.backup_saved_at_external_storage, Toast.LENGTH_LONG).show(); + dismiss(); + Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show(); } } } diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/CookieDeleteDialog.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/CookieDeleteDialog.java index d5f094c6..fd596578 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/CookieDeleteDialog.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/CookieDeleteDialog.java @@ -9,6 +9,7 @@ import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.DialogFragment;
import org.torproject.android.R; +import org.torproject.android.ui.hiddenservices.ClientCookiesActivity; import org.torproject.android.ui.hiddenservices.providers.CookieContentProvider;
public class CookieDeleteDialog extends DialogFragment { @@ -29,7 +30,7 @@ public class CookieDeleteDialog extends DialogFragment { private void doDelete(Bundle arguments, Context context) { context.getContentResolver().delete( // delete from db CookieContentProvider.CONTENT_URI, - CookieContentProvider.ClientCookie._ID + "=" + arguments.getInt("_id"), + CookieContentProvider.ClientCookie._ID + "=" + arguments.getInt(ClientCookiesActivity.BUNDLE_KEY_ID), null ); } diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSActionsDialog.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSActionsDialog.java index 688b5efc..76932c88 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSActionsDialog.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSActionsDialog.java @@ -15,6 +15,7 @@ import androidx.fragment.app.DialogFragment; import org.torproject.android.R; import org.torproject.android.core.ClipboardUtils; import org.torproject.android.core.DiskUtils; +import org.torproject.android.ui.hiddenservices.HiddenServicesActivity; import org.torproject.android.ui.hiddenservices.backup.BackupUtils;
import java.io.File; @@ -27,22 +28,27 @@ public class HSActionsDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Bundle arguments = getArguments(); - port = Integer.parseInt(arguments.getString("port")); - return new AlertDialog.Builder(getActivity()) + port = Integer.parseInt(arguments.getString(HiddenServicesActivity.BUNDLE_KEY_PORT)); + AlertDialog ad = new AlertDialog.Builder(getActivity()) .setItems(new CharSequence[]{ - getString(R.string.copy_address_to_clipboard), - getString(R.string.show_auth_cookie), - getString(R.string.backup_service), - getString(R.string.delete_service)}, - (dialog, which) -> { - if (which == 0) doCopy(arguments, getContext()); - else if (which == 1) doShowAuthCookie(arguments, getContext()); - else if (which == 2) doBackup(arguments, getContext()); - else if (which == 3) doDelete(arguments); - }) + getString(R.string.copy_address_to_clipboard), + getString(R.string.show_auth_cookie), + getString(R.string.backup_service), + getString(R.string.delete_service)}, null) .setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss()) .setTitle(R.string.hidden_services) .create(); + + // done this way so we can startActivityForResult on backup without the dialog vanishing + ad.getListView().setOnItemClickListener((parent, view, position, id) -> { + if (position == 0) doCopy(arguments, getContext()); + else if (position == 1) doShowAuthCookie(arguments, getContext()); + else if (position == 2) doBackup(arguments, getContext()); + else if (position == 3) doDelete(arguments); + if (position != 2) dismiss(); + }); + + return ad; }
private void doDelete(Bundle arguments) { @@ -52,9 +58,9 @@ public class HSActionsDialog extends DialogFragment { }
private void doShowAuthCookie(Bundle arguments, Context context) { - String auth_cookie_value = arguments.getString("auth_cookie_value"); + String auth_cookie_value = arguments.getString(HiddenServicesActivity.BUNDLE_KEY_AUTH_COOKIE_VALUE);
- if (arguments.getInt("auth_cookie") == 1) { + if (arguments.getInt(HiddenServicesActivity.BUNDLE_KEY_AUTH_COOKIE) == 1) { if (auth_cookie_value == null || auth_cookie_value.length() < 1) { Toast.makeText(context, R.string.please_restart_Orbot_to_enable_the_changes, Toast.LENGTH_LONG).show(); } else { @@ -68,16 +74,16 @@ public class HSActionsDialog extends DialogFragment { }
private void doCopy(Bundle arguments, Context context) { - String onion = arguments.getString("onion"); + String onion = arguments.getString(HiddenServicesActivity.BUNDLE_KEY_ONION); if (onion == null) Toast.makeText(context, R.string.please_restart_Orbot_to_enable_the_changes, Toast.LENGTH_LONG).show(); else - ClipboardUtils.copyToClipboard("onion", arguments.getString("onion"), getString(R.string.done), context); + ClipboardUtils.copyToClipboard("onion", onion, getString(R.string.done), context); }
private void doBackup(Bundle arguments, Context context) { String filename = "hs" + port + ".zip"; - String onion = arguments.getString("onion"); + String onion = arguments.getString(HiddenServicesActivity.BUNDLE_KEY_ONION); if (onion == null) { Toast.makeText(context, R.string.please_restart_Orbot_to_enable_the_changes, Toast.LENGTH_LONG).show(); return; @@ -102,11 +108,8 @@ public class HSActionsDialog extends DialogFragment { private void attemptToWriteBackup(Uri outputFile) { BackupUtils backupUtils = new BackupUtils(getContext()); String backup = backupUtils.createZipBackup(port, outputFile); - if (backup != null) { - Toast.makeText(getContext(), R.string.backup_saved_at_external_storage, Toast.LENGTH_LONG).show(); - } else { - Toast.makeText(getContext(), R.string.error, Toast.LENGTH_LONG).show(); - } + Toast.makeText(getContext(), backup != null ? R.string.backup_saved_at_external_storage : R.string.error, Toast.LENGTH_LONG).show(); + dismiss(); }
} diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSCookieDialog.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSCookieDialog.java index 2875fbc4..2bc29e59 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSCookieDialog.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSCookieDialog.java @@ -13,6 +13,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.torproject.android.R; import org.torproject.android.core.ClipboardUtils; +import org.torproject.android.ui.hiddenservices.HiddenServicesActivity; import org.torproject.android.ui.hiddenservices.providers.CookieContentProvider;
public class HSCookieDialog extends DialogFragment { @@ -22,8 +23,8 @@ public class HSCookieDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Bundle arguments = getArguments(); - authCookieValue = arguments.getString("auth_cookie_value"); - onion = arguments.getString("onion"); + authCookieValue = arguments.getString(HiddenServicesActivity.BUNDLE_KEY_AUTH_COOKIE_VALUE); + onion = arguments.getString(HiddenServicesActivity.BUNDLE_KEY_ONION); return new AlertDialog.Builder(getActivity()) .setTitle(authCookieValue) .setItems(new CharSequence[]{ @@ -42,9 +43,7 @@ public class HSCookieDialog extends DialogFragment { JSONObject backup = new JSONObject(); backup.put(CookieContentProvider.ClientCookie.DOMAIN, onion); backup.put(CookieContentProvider.ClientCookie.AUTH_COOKIE_VALUE, authCookieValue); - new IntentIntegrator(getActivity()).shareText(backup.toString()); - } catch (JSONException e) { e.printStackTrace(); } diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSDeleteDialog.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSDeleteDialog.java index 9182f9c4..d26141a0 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSDeleteDialog.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSDeleteDialog.java @@ -10,6 +10,7 @@ import androidx.fragment.app.DialogFragment;
import org.torproject.android.R; import org.torproject.android.service.TorServiceConstants; +import org.torproject.android.ui.hiddenservices.HiddenServicesActivity; import org.torproject.android.ui.hiddenservices.providers.HSContentProvider;
import java.io.File; @@ -29,14 +30,12 @@ public class HSDeleteDialog extends DialogFragment { }
private void doDelete(Bundle arguments, Context context) { - context.getContentResolver().delete( // Delete from db - HSContentProvider.CONTENT_URI, - HSContentProvider.HiddenService._ID + "=" + arguments.getInt("_id"), - null); + context.getContentResolver().delete(HSContentProvider.CONTENT_URI, + HSContentProvider.HiddenService._ID + "=" + arguments.getInt(HiddenServicesActivity.BUNDLE_KEY_ID), null);
// Delete from internal storage String base = context.getFilesDir().getAbsolutePath() + "/" + TorServiceConstants.HIDDEN_SERVICES_DIR; - File dir = new File(base, "hs" + arguments.getString("port")); + File dir = new File(base, "hs" + arguments.getString(HiddenServicesActivity.BUNDLE_KEY_PORT));
if (dir.isDirectory()) { String[] children = dir.list();
tor-commits@lists.torproject.org