commit f030963c9e9b8e349db5ba959dddb7e0089be7f7 Author: arrase arrase@gmail.com Date: Tue Nov 22 22:37:34 2016 +0100
refactor --- .../org/torproject/android/OrbotMainActivity.java | 4 +- .../org/torproject/android/backup/BackupUtils.java | 45 ++++++++++ .../java/org/torproject/android/backup/ZipIt.java | 98 ++++++++++++++++++++++ .../android/hsutils/HiddenServiceUtils.java | 46 ---------- .../android/ui/hs/HiddenServicesActivity.java | 4 +- .../android/ui/hs/dialogs/HSActionsDialog.java | 4 +- .../java/org/torproject/android/zip/ZipIt.java | 98 ---------------------- 7 files changed, 148 insertions(+), 151 deletions(-)
diff --git a/app/src/main/java/org/torproject/android/OrbotMainActivity.java b/app/src/main/java/org/torproject/android/OrbotMainActivity.java index 6d58d0e..e0fdbb0 100644 --- a/app/src/main/java/org/torproject/android/OrbotMainActivity.java +++ b/app/src/main/java/org/torproject/android/OrbotMainActivity.java @@ -29,7 +29,7 @@ import org.torproject.android.ui.PromoAppsActivity; import org.torproject.android.ui.Rotate3dAnimation; import org.torproject.android.ui.hs.providers.HSContentProvider; import org.torproject.android.vpn.VPNEnableActivity; -import org.torproject.android.hsutils.HiddenServiceUtils; +import org.torproject.android.backup.BackupUtils;
import android.annotation.SuppressLint; import android.app.ActivityManager; @@ -646,7 +646,7 @@ public class OrbotMainActivity extends AppCompatActivity Cursor onion = getContentResolver().query(HSContentProvider.CONTENT_URI, mProjection, "port=" + hsPort, null, null); if(onion != null) { hostname = onion.getString(onion.getColumnIndex(HSContentProvider.HiddenService.NAME)); - HiddenServiceUtils hsutils = new HiddenServiceUtils(getApplicationContext()); + BackupUtils hsutils = new BackupUtils(getApplicationContext()); if(keyZipPath != null && keyZipPath.length() > 0) { hsutils.restoreOnionBackup(hsPort, keyZipPath); diff --git a/app/src/main/java/org/torproject/android/backup/BackupUtils.java b/app/src/main/java/org/torproject/android/backup/BackupUtils.java new file mode 100644 index 0000000..42c98f8 --- /dev/null +++ b/app/src/main/java/org/torproject/android/backup/BackupUtils.java @@ -0,0 +1,45 @@ +package org.torproject.android.backup; + +import android.app.Application; +import android.content.Context; + +import org.torproject.android.service.TorServiceConstants; +import org.torproject.android.storage.ExternalStorage; + +import java.io.File; + +public class BackupUtils { + private static File appCacheHome; + + public BackupUtils(Context context) { + appCacheHome = context.getDir(TorServiceConstants.DIRECTORY_TOR_DATA, Application.MODE_PRIVATE); + } + + public String createOnionBackup(Integer port) { + + ExternalStorage storage = new ExternalStorage(); + String storage_path = storage.createBackupDir(); + if (storage_path == null) { + return null; + } + + String zip_path = storage_path + "/hs" + port + ".zip"; + String files[] = { + appCacheHome + "/hs" + port + "/hostname", + appCacheHome + "/hs" + port + "/private_key" + }; + + ZipIt zip = new ZipIt(files, zip_path); + + if (!zip.zip()) { + return null; + } + + return zip_path; + } + + public void restoreOnionBackup(Integer port, String path) { + ZipIt zip = new ZipIt(null, path); + zip.unzip(appCacheHome + "/hs" + port); + } +} diff --git a/app/src/main/java/org/torproject/android/backup/ZipIt.java b/app/src/main/java/org/torproject/android/backup/ZipIt.java new file mode 100644 index 0000000..bde29f0 --- /dev/null +++ b/app/src/main/java/org/torproject/android/backup/ZipIt.java @@ -0,0 +1,98 @@ +package org.torproject.android.backup; + + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +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 { + private static final int BUFFER = 2048; + + private String[] _files; + private String _zipFile; + + public ZipIt(String[] files, String zipFile) { + _files = files; + _zipFile = zipFile; + } + + public boolean zip() { + try { + BufferedInputStream origin = null; + FileOutputStream dest = new FileOutputStream(_zipFile); + + ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); + + byte data[] = new byte[BUFFER]; + + for (String _file : _files) { + FileInputStream fi = new FileInputStream(_file); + origin = new BufferedInputStream(fi, BUFFER); + ZipEntry entry = new ZipEntry(_file.substring(_file.lastIndexOf("/") + 1)); + out.putNextEntry(entry); + int count; + while ((count = origin.read(data, 0, BUFFER)) != -1) { + out.write(data, 0, count); + } + origin.close(); + } + + out.close(); + } catch (Exception e) { + return false; + } + + return true; + } + + public boolean unzip(String output_path) { + InputStream is; + ZipInputStream zis; + + try { + String filename; + is = new FileInputStream(_zipFile); + zis = new ZipInputStream(new BufferedInputStream(is)); + ZipEntry ze; + byte[] buffer = new byte[1024]; + int count; + + while ((ze = zis.getNextEntry()) != null) { + // zapis do souboru + filename = ze.getName(); + + // Need to create directories if not exists, or + // it will generate an Exception... + if (ze.isDirectory()) { + File fmd = new File(output_path + filename); + fmd.mkdirs(); + continue; + } + + FileOutputStream fout = new FileOutputStream(output_path + filename); + + // cteni zipu a zapis + while ((count = zis.read(buffer)) != -1) { + fout.write(buffer, 0, count); + } + + fout.close(); + zis.closeEntry(); + } + + zis.close(); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/app/src/main/java/org/torproject/android/hsutils/HiddenServiceUtils.java b/app/src/main/java/org/torproject/android/hsutils/HiddenServiceUtils.java deleted file mode 100644 index 0cf359b..0000000 --- a/app/src/main/java/org/torproject/android/hsutils/HiddenServiceUtils.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.torproject.android.hsutils; - -import android.app.Application; -import android.content.Context; - -import org.torproject.android.service.TorServiceConstants; -import org.torproject.android.storage.ExternalStorage; -import org.torproject.android.zip.ZipIt; - -import java.io.File; - -public class HiddenServiceUtils { - private static File appCacheHome; - - public HiddenServiceUtils(Context context) { - appCacheHome = context.getDir(TorServiceConstants.DIRECTORY_TOR_DATA, Application.MODE_PRIVATE); - } - - public String createOnionBackup(Integer port) { - - ExternalStorage storage = new ExternalStorage(); - String storage_path = storage.createBackupDir(); - if (storage_path == null) { - return null; - } - - String zip_path = storage_path + "/hs" + port + ".zip"; - String files[] = { - appCacheHome + "/hs" + port + "/hostname", - appCacheHome + "/hs" + port + "/private_key" - }; - - ZipIt zip = new ZipIt(files, zip_path); - - if (!zip.zip()) { - return null; - } - - return zip_path; - } - - public void restoreOnionBackup(Integer port, String path) { - ZipIt zip = new ZipIt(null, path); - zip.unzip(appCacheHome + "/hs" + port); - } -} diff --git a/app/src/main/java/org/torproject/android/ui/hs/HiddenServicesActivity.java b/app/src/main/java/org/torproject/android/ui/hs/HiddenServicesActivity.java index a10c3e2..52103dc 100644 --- a/app/src/main/java/org/torproject/android/ui/hs/HiddenServicesActivity.java +++ b/app/src/main/java/org/torproject/android/ui/hs/HiddenServicesActivity.java @@ -58,9 +58,7 @@ public class HiddenServicesActivity extends AppCompatActivity {
mAdapter = new OnionListAdapter( this, - mCR.query( - HSContentProvider.CONTENT_URI, mProjection, mWhere, null, null - ), + mCR.query(HSContentProvider.CONTENT_URI, mProjection, mWhere, null, null), 0 );
diff --git a/app/src/main/java/org/torproject/android/ui/hs/dialogs/HSActionsDialog.java b/app/src/main/java/org/torproject/android/ui/hs/dialogs/HSActionsDialog.java index 52ea6c5..f432a01 100644 --- a/app/src/main/java/org/torproject/android/ui/hs/dialogs/HSActionsDialog.java +++ b/app/src/main/java/org/torproject/android/ui/hs/dialogs/HSActionsDialog.java @@ -22,7 +22,7 @@ import android.widget.Button; import android.widget.Toast;
import org.torproject.android.R; -import org.torproject.android.hsutils.HiddenServiceUtils; +import org.torproject.android.backup.BackupUtils; import org.torproject.android.ui.hs.providers.HSContentProvider;
public class HSActionsDialog extends DialogFragment { @@ -49,7 +49,7 @@ public class HSActionsDialog extends DialogFragment { return; }
- HiddenServiceUtils hsutils = new HiddenServiceUtils(mContext); + BackupUtils hsutils = new BackupUtils(mContext); String backupPath = hsutils.createOnionBackup(Integer.parseInt(arguments.getString("port")));
if (backupPath == null || backupPath.length() < 1) { diff --git a/app/src/main/java/org/torproject/android/zip/ZipIt.java b/app/src/main/java/org/torproject/android/zip/ZipIt.java deleted file mode 100644 index 84a0b7a..0000000 --- a/app/src/main/java/org/torproject/android/zip/ZipIt.java +++ /dev/null @@ -1,98 +0,0 @@ -package org.torproject.android.zip; - - -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -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 { - private static final int BUFFER = 2048; - - private String[] _files; - private String _zipFile; - - public ZipIt(String[] files, String zipFile) { - _files = files; - _zipFile = zipFile; - } - - public boolean zip() { - try { - BufferedInputStream origin = null; - FileOutputStream dest = new FileOutputStream(_zipFile); - - ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); - - byte data[] = new byte[BUFFER]; - - for (String _file : _files) { - FileInputStream fi = new FileInputStream(_file); - origin = new BufferedInputStream(fi, BUFFER); - ZipEntry entry = new ZipEntry(_file.substring(_file.lastIndexOf("/") + 1)); - out.putNextEntry(entry); - int count; - while ((count = origin.read(data, 0, BUFFER)) != -1) { - out.write(data, 0, count); - } - origin.close(); - } - - out.close(); - } catch (Exception e) { - return false; - } - - return true; - } - - public boolean unzip(String output_path) { - InputStream is; - ZipInputStream zis; - - try { - String filename; - is = new FileInputStream(_zipFile); - zis = new ZipInputStream(new BufferedInputStream(is)); - ZipEntry ze; - byte[] buffer = new byte[1024]; - int count; - - while ((ze = zis.getNextEntry()) != null) { - // zapis do souboru - filename = ze.getName(); - - // Need to create directories if not exists, or - // it will generate an Exception... - if (ze.isDirectory()) { - File fmd = new File(output_path + filename); - fmd.mkdirs(); - continue; - } - - FileOutputStream fout = new FileOutputStream(output_path + filename); - - // cteni zipu a zapis - while ((count = zis.read(buffer)) != -1) { - fout.write(buffer, 0, count); - } - - fout.close(); - zis.closeEntry(); - } - - zis.close(); - } catch (IOException e) { - e.printStackTrace(); - return false; - } - - return true; - } -} \ No newline at end of file