commit ec11d9db387c19c7ca6b385d44a5fcc1fed79659 Author: bim dsnake@protonmail.com Date: Sat Nov 28 12:35:03 2020 -0500
V3 UI Cleanup, Deprecated Stuff Under Menu + filtering of v3 user/app services --- .../ui/hiddenservices/HiddenServicesActivity.java | 14 +++-- .../ui/hiddenservices/backup/BackupUtils.java | 30 ++++------- .../backup/{ZipIt.java => ZipUtilities.java} | 14 +++-- .../ui/v3onionservice/OnionServicesActivity.java | 52 ++++++++++++++++--- app/src/main/res/menu/orbot_main.xml | 59 +++++++--------------- app/src/main/res/values/strings.xml | 1 + .../java/org/torproject/android/core/DiskUtils.kt | 1 - 7 files changed, 89 insertions(+), 82 deletions(-)
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 886bd27d..7436eb91 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 @@ -25,6 +25,7 @@ import org.torproject.android.core.DiskUtils; import org.torproject.android.core.LocaleHelper; import org.torproject.android.ui.hiddenservices.adapters.OnionListAdapter; import org.torproject.android.ui.hiddenservices.backup.BackupUtils; +import org.torproject.android.ui.hiddenservices.backup.ZipUtilities; import org.torproject.android.ui.hiddenservices.dialogs.HSActionsDialog; import org.torproject.android.ui.hiddenservices.dialogs.HSDataDialog; import org.torproject.android.ui.hiddenservices.permissions.PermissionManager; @@ -43,7 +44,7 @@ public class HiddenServicesActivity extends AppCompatActivity { private static final String BUNDLE_KEY_SHOW_USER_SERVICES = "show_user_services"; private ContentResolver mResolver; private OnionListAdapter mAdapter; - private RadioButton radioShowUserServices, radioShowAppServices; + private RadioButton radioShowUserServices; private FloatingActionButton fab; private String mWhere = HSContentProvider.HiddenService.CREATED_BY_USER + "=1";
@@ -52,7 +53,7 @@ public class HiddenServicesActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.layout_hs_list_view); radioShowUserServices = findViewById(R.id.radioUserServices); - radioShowAppServices = findViewById(R.id.radioAppServices); + RadioButton radioShowAppServices = findViewById(R.id.radioAppServices);
Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); @@ -61,7 +62,7 @@ public class HiddenServicesActivity extends AppCompatActivity { mResolver = getContentResolver();
fab = findViewById(R.id.fab); - fab.setOnClickListener(view -> new HSDataDialog().show(getSupportFragmentManager(), "HSDataDialog")); + fab.setOnClickListener(view -> new HSDataDialog().show(getSupportFragmentManager(), HSDataDialog.class.getSimpleName()));
mAdapter = new OnionListAdapter(this, mResolver.query(HSContentProvider.CONTENT_URI, HSContentProvider.PROJECTION, mWhere, null, null), 0); mResolver.registerContentObserver(HSContentProvider.CONTENT_URI, true, new HSObserver(new Handler())); @@ -108,7 +109,7 @@ public class HiddenServicesActivity extends AppCompatActivity {
private void doRestoreLegacy() { // API 16, 17, 18 File backupDir = DiskUtils.getOrCreateLegacyBackupDir(getString(R.string.app_name)); - File[] files = backupDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".zip")); + File[] files = backupDir.listFiles(ZipUtilities.FILTER_ZIP_FILES); if (files != null) { if (files.length == 0) { Toast.makeText(this, R.string.create_a_backup_first, Toast.LENGTH_LONG).show(); @@ -140,7 +141,7 @@ public class HiddenServicesActivity extends AppCompatActivity { public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_restore_backup) { if (DiskUtils.supportsStorageAccessFramework()) { - Intent readFile = DiskUtils.createReadFileIntent("application/zip"); + Intent readFile = DiskUtils.createReadFileIntent(ZipUtilities.ZIP_MIME_TYPE); startActivityForResult(readFile, REQUEST_CODE_READ_ZIP_BACKUP); } else { // API 16, 17, 18 doRestoreLegacy(); @@ -177,12 +178,9 @@ public class HiddenServicesActivity extends AppCompatActivity { @Override public void onChange(boolean selfChange) { mAdapter.changeCursor(mResolver.query(HSContentProvider.CONTENT_URI, HSContentProvider.PROJECTION, mWhere, null, null)); - if (!PermissionManager.isLollipopOrHigher()) return; Cursor active = mResolver.query(HSContentProvider.CONTENT_URI, HSContentProvider.PROJECTION, HSContentProvider.HiddenService.ENABLED + "=1", null, null); - if (active == null) return; - if (active.getCount() > 0) // Call only if there running services PermissionManager.requestBatteryPermissions(HiddenServicesActivity.this, getApplicationContext()); else // Drop whe not needed diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/BackupUtils.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/BackupUtils.java index e81f73a9..1dbad3cb 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/BackupUtils.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/BackupUtils.java @@ -30,8 +30,8 @@ import java.nio.charset.Charset;
public class BackupUtils { private static final String configFileName = "config.json"; - private Context mContext; - private ContentResolver mResolver; + private final Context mContext; + private final ContentResolver mResolver;
public BackupUtils(Context context) { mContext = context; @@ -44,14 +44,14 @@ public class BackupUtils {
public String createV3ZipBackup(String port, Uri zipFile) { String[] files = createFilesForZippingV3(port); - ZipIt zip = new ZipIt(files, zipFile, mResolver); + ZipUtilities zip = new ZipUtilities(files, zipFile, mResolver); if (!zip.zip()) return null; return zipFile.getPath(); }
public String createV2ZipBackup(int port, Uri zipFile) { String[] files = createFilesForZippingV2(port); - ZipIt zip = new ZipIt(files, zipFile, mResolver); + ZipUtilities zip = new ZipUtilities(files, zipFile, mResolver);
if (!zip.zip()) return null; @@ -84,17 +84,12 @@ public class BackupUtils { config.put(OnionServiceContentProvider.OnionService.CREATED_BY_USER, portData.getString(portData.getColumnIndex(OnionServiceContentProvider.OnionService.CREATED_BY_USER))); config.put(OnionServiceContentProvider.OnionService.ENABLED, portData.getString(portData.getColumnIndex(OnionServiceContentProvider.OnionService.ENABLED)));
- } catch (JSONException jsone) { - jsone.printStackTrace(); - return null; - } - portData.close(); + portData.close();
- try { FileWriter fileWriter = new FileWriter(configFilePath); fileWriter.write(config.toString()); fileWriter.close(); - } catch (IOException ioe) { + } catch (JSONException | IOException ioe) { ioe.printStackTrace(); return null; } @@ -131,10 +126,7 @@ public class BackupUtils { config.put(HSContentProvider.HiddenService.AUTH_COOKIE_VALUE, portData.getString(portData.getColumnIndex(HSContentProvider.HiddenService.AUTH_COOKIE_VALUE))); config.put(HSContentProvider.HiddenService.CREATED_BY_USER, portData.getInt(portData.getColumnIndex(HSContentProvider.HiddenService.CREATED_BY_USER))); config.put(HSContentProvider.HiddenService.ENABLED, portData.getInt(portData.getColumnIndex(HSContentProvider.HiddenService.ENABLED))); - } catch (JSONException e) { - e.printStackTrace(); - return null; - } catch (NullPointerException e) { + } catch (JSONException | NullPointerException e) { e.printStackTrace(); return null; } @@ -261,7 +253,7 @@ public class BackupUtils {
public void restoreZipBackupV2Legacy(File zipFile) { String backupName = zipFile.getName(); - ZipIt zip = new ZipIt(null, null, mResolver); + ZipUtilities zip = new ZipUtilities(null, null, mResolver); String hsDir = backupName.substring(0, backupName.lastIndexOf('.')); File hsPath = new File(getHSBasePath().getAbsolutePath(), hsDir); if (zip.unzipLegacy(hsPath.getAbsolutePath(), zipFile)) @@ -272,7 +264,7 @@ public class BackupUtils {
public void restoreZipBackupV3Legacy(File zipFile) { String backupName = zipFile.getName(); - ZipIt zip = new ZipIt(null, null, mResolver); + ZipUtilities zip = new ZipUtilities(null, null, mResolver); String v3Dir = backupName.substring(0, backupName.lastIndexOf('.')); File v3Path = new File(getV3BasePath().getAbsolutePath(), v3Dir); if (zip.unzipLegacy(v3Path.getAbsolutePath(), zipFile)) @@ -291,7 +283,7 @@ public class BackupUtils {
String hsDir = backupName.substring(0, backupName.lastIndexOf('.')); File hsPath = new File(getHSBasePath().getAbsolutePath(), hsDir); - if (new ZipIt(null, zipUri, mResolver).unzip(hsPath.getAbsolutePath())) + if (new ZipUtilities(null, zipUri, mResolver).unzip(hsPath.getAbsolutePath())) extractConfigFromUnzippedBackupV2(backupName); else Toast.makeText(mContext, R.string.error, Toast.LENGTH_LONG).show(); @@ -306,7 +298,7 @@ public class BackupUtils {
String v3Dir = backupName.substring(0, backupName.lastIndexOf('.')); File v3Paath = new File(getV3BasePath().getAbsolutePath(), v3Dir); - if (new ZipIt(null, zipUri, mResolver).unzip(v3Paath.getAbsolutePath())) + if (new ZipUtilities(null, zipUri, mResolver).unzip(v3Paath.getAbsolutePath())) extractConfigFromUnzippedBackupV3(backupName); else Toast.makeText(mContext, R.string.error, Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/ZipIt.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/ZipUtilities.java similarity index 88% rename from app/src/main/java/org/torproject/android/ui/hiddenservices/backup/ZipIt.java rename to app/src/main/java/org/torproject/android/ui/hiddenservices/backup/ZipUtilities.java index 5c264b8e..11fd1743 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/ZipIt.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/ZipUtilities.java @@ -10,22 +10,26 @@ import androidx.annotation.Nullable; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream;
-public class ZipIt { +public class ZipUtilities { private static final int BUFFER = 2048; + public static final String ZIP_MIME_TYPE = "application/zip"; + public static final FilenameFilter FILTER_ZIP_FILES = (dir, name) -> name.toLowerCase().endsWith(".zip");
- private String[] files; - private Uri zipFile; - private ContentResolver contentResolver; + private final String[] files; + private final Uri zipFile; + private final ContentResolver contentResolver;
- public ZipIt(@Nullable String[] files, @NonNull Uri zipFile, @NonNull ContentResolver contentResolver) { + public ZipUtilities(@Nullable String[] files, @NonNull Uri zipFile, @NonNull ContentResolver contentResolver) { this.files = files; this.zipFile = zipFile; this.contentResolver = contentResolver; diff --git a/app/src/main/java/org/torproject/android/ui/v3onionservice/OnionServicesActivity.java b/app/src/main/java/org/torproject/android/ui/v3onionservice/OnionServicesActivity.java index a51ef37c..ba06b7c0 100644 --- a/app/src/main/java/org/torproject/android/ui/v3onionservice/OnionServicesActivity.java +++ b/app/src/main/java/org/torproject/android/ui/v3onionservice/OnionServicesActivity.java @@ -9,10 +9,12 @@ import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.MenuItem; +import android.view.View; import android.widget.ListView; import android.widget.RadioButton; import android.widget.Toast;
+import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; @@ -23,15 +25,17 @@ import org.torproject.android.R; import org.torproject.android.core.DiskUtils; import org.torproject.android.core.LocaleHelper; import org.torproject.android.ui.hiddenservices.backup.BackupUtils; +import org.torproject.android.ui.hiddenservices.backup.ZipUtilities;
import java.io.File;
public class OnionServicesActivity extends AppCompatActivity {
static final String BUNDLE_KEY_ID = "id", BUNDLE_KEY_PORT = "port", BUNDLE_KEY_DOMAIN = "domain"; - private static final String WHERE_SELECTION_CLAUSE = OnionServiceContentProvider.OnionService.CREATED_BY_USER + "=1"; + private static final String BASE_WHERE_SELECTION_CLAUSE = OnionServiceContentProvider.OnionService.CREATED_BY_USER + "="; + private static final String BUNDLE_KEY_SHOW_USER_SERVICES = "show_user_key"; private static final int REQUEST_CODE_READ_ZIP_BACKUP = 347; - private RadioButton radioShowUserServices, radioShowAppServices; + private RadioButton radioShowUserServices; private FloatingActionButton fab; private ContentResolver mContentResolver; private OnionV3ListAdapter mAdapter; @@ -46,13 +50,20 @@ public class OnionServicesActivity extends AppCompatActivity { getSupportActionBar().setDisplayHomeAsUpEnabled(true);
fab = findViewById(R.id.fab); - fab.setOnClickListener(v -> new NewOnionServiceDialogFragment().show(getSupportFragmentManager(), "NewOnionServiceDialogFragment")); + fab.setOnClickListener(v -> new NewOnionServiceDialogFragment().show(getSupportFragmentManager(), NewOnionServiceDialogFragment.class.getSimpleName()));
mContentResolver = getContentResolver(); - mAdapter = new OnionV3ListAdapter(this, mContentResolver.query(OnionServiceContentProvider.CONTENT_URI, OnionServiceContentProvider.PROJECTION, WHERE_SELECTION_CLAUSE, null, null), 0); + mAdapter = new OnionV3ListAdapter(this, mContentResolver.query(OnionServiceContentProvider.CONTENT_URI, OnionServiceContentProvider.PROJECTION, BASE_WHERE_SELECTION_CLAUSE + '1', null, null), 0); mContentResolver.registerContentObserver(OnionServiceContentProvider.CONTENT_URI, true, new OnionServiceObserver(new Handler()));
ListView onionList = findViewById(R.id.onion_list); + + radioShowUserServices = findViewById(R.id.radioUserServices); + RadioButton radioShowAppServices = findViewById(R.id.radioAppServices); + boolean showUserServices = radioShowAppServices.isChecked() || bundle == null || bundle.getBoolean(BUNDLE_KEY_SHOW_USER_SERVICES, false); + if (showUserServices) radioShowUserServices.setChecked(true); + else radioShowAppServices.setChecked(true); + filterServices(showUserServices); onionList.setAdapter(mAdapter); onionList.setOnItemClickListener((parent, view, position, id) -> { Cursor item = (Cursor) parent.getItemAtPosition(position); @@ -65,6 +76,19 @@ public class OnionServicesActivity extends AppCompatActivity { }); }
+ private void filterServices(boolean showUserServices) { + String predicate; + if (showUserServices) { + predicate = "1"; + fab.show(); + } else { + predicate = "0"; + fab.hide(); + } + mAdapter.changeCursor(mContentResolver.query(OnionServiceContentProvider.CONTENT_URI, OnionServiceContentProvider.PROJECTION, + BASE_WHERE_SELECTION_CLAUSE + predicate, null, null)); + } + @Override protected void attachBaseContext(Context base) { super.attachBaseContext(LocaleHelper.onAttach(base)); @@ -76,11 +100,17 @@ public class OnionServicesActivity extends AppCompatActivity { return true; }
+ @Override + protected void onSaveInstanceState(@NonNull Bundle icicle) { + super.onSaveInstanceState(icicle); + icicle.putBoolean(BUNDLE_KEY_SHOW_USER_SERVICES, radioShowUserServices.isChecked()); + } + @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_restore_backup) { if (DiskUtils.supportsStorageAccessFramework()) { - Intent readFileIntent = DiskUtils.createReadFileIntent("application/zip"); + Intent readFileIntent = DiskUtils.createReadFileIntent(ZipUtilities.ZIP_MIME_TYPE); startActivityForResult(readFileIntent, REQUEST_CODE_READ_ZIP_BACKUP); } else { // APIs 16, 17, 18 doRestoreLegacy(); @@ -91,7 +121,7 @@ public class OnionServicesActivity extends AppCompatActivity {
private void doRestoreLegacy() { // APIs 16, 17, 18 File backupDir = DiskUtils.getOrCreateLegacyBackupDir(getString(R.string.app_name)); - File[] files = backupDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".zip")); + File[] files = backupDir.listFiles(ZipUtilities.FILTER_ZIP_FILES); if (files == null) return; if (files.length == 0) { Toast.makeText(this, R.string.create_a_backup_first, Toast.LENGTH_LONG).show(); @@ -115,6 +145,14 @@ public class OnionServicesActivity extends AppCompatActivity { } }
+ public void onRadioButtonClick(View view) { + int id = view.getId(); + if (id == R.id.radioUserServices) + filterServices(true); + else if (id == R.id.radioAppServices) + filterServices(false); + } + private class OnionServiceObserver extends ContentObserver {
OnionServiceObserver(Handler handler) { @@ -123,7 +161,7 @@ public class OnionServicesActivity extends AppCompatActivity {
@Override public void onChange(boolean selfChange) { - mAdapter.changeCursor(mContentResolver.query(OnionServiceContentProvider.CONTENT_URI, OnionServiceContentProvider.PROJECTION, WHERE_SELECTION_CLAUSE, null, null)); + mAdapter.changeCursor(mContentResolver.query(OnionServiceContentProvider.CONTENT_URI, OnionServiceContentProvider.PROJECTION, BASE_WHERE_SELECTION_CLAUSE + '1', null, null)); // todo battery optimization stuff if lollipop or higher and running onion services } } diff --git a/app/src/main/res/menu/orbot_main.xml b/app/src/main/res/menu/orbot_main.xml index 2fb0ad4e..c0133d44 100644 --- a/app/src/main/res/menu/orbot_main.xml +++ b/app/src/main/res/menu/orbot_main.xml @@ -1,25 +1,7 @@ -<?xml version="1.0" encoding="utf-8"?><!-- -/* - * Copyright (C) 2008 Esmertec AG. - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ ---> +<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto">
- <item android:id="@+id/menu_newnym" android:icon="@drawable/ic_refresh_white_24dp" @@ -56,44 +38,37 @@
<item android:id="@+id/menu_v3_onion_services" - android:title="@string/hidden_services" - yourapp:showAsAction="never" /> - - <item - android:id="@+id/menu_hidden_services" android:title="@string/hosted_services" yourapp:showAsAction="never" />
<item - android:id="@+id/menu_client_cookies" - android:title="@string/client_cookies" - yourapp:showAsAction="never" /> + android:title="@string/v2_hidden_services" + yourapp:showAsAction="never"> + <menu> + <item + android:id="@+id/menu_hidden_services" + android:title="@string/hosted_services" + yourapp:showAsAction="never" /> + + <item + android:id="@+id/menu_client_cookies" + android:title="@string/client_cookies" + yourapp:showAsAction="never" /> + </menu> + </item> </menu> </item>
- <!-- - <item android:id="@+id/menu_promo_apps" - android:title="@string/menu_promo_apps" - android:icon="@drawable/ic_menu_goto" - yourapp:showAsAction="never" - - /> - --> - <item android:id="@+id/menu_about" android:icon="@drawable/ic_menu_about" android:title="@string/menu_about" - yourapp:showAsAction="never" - - /> + yourapp:showAsAction="never" />
<item android:id="@+id/menu_exit" android:icon="@drawable/ic_menu_exit" android:title="@string/menu_exit" - yourapp:showAsAction="never" - - /> + yourapp:showAsAction="never" />
</menu> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0501adf8..ef8d918f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -169,6 +169,7 @@
<string name="vpn_default_world">Global (Auto)</string> <string name="hidden_services">Onion Services</string> + <string name="v2_hidden_services">V2 Onion Services (Deprecated)</string> <string name="title_activity_hidden_services">Onion Services</string> <string name="menu_hidden_services">Onion Services</string> <string name="save">Save</string> diff --git a/appcore/src/main/java/org/torproject/android/core/DiskUtils.kt b/appcore/src/main/java/org/torproject/android/core/DiskUtils.kt index 1a9551b5..8ac067b7 100644 --- a/appcore/src/main/java/org/torproject/android/core/DiskUtils.kt +++ b/appcore/src/main/java/org/torproject/android/core/DiskUtils.kt @@ -21,7 +21,6 @@ object DiskUtils { @Throws(IOException::class) fun readFileFromAssets(assetFilename: String, context: Context): String { val reader = BufferedReader(InputStreamReader(context.assets.open(assetFilename))) - // do reading, usually loop until end of file reading val sb = StringBuilder() var mLine = reader.readLine() while (mLine != null) {
tor-commits@lists.torproject.org