restore backup from intent

This commit is contained in:
arrase 2016-11-20 23:56:17 +01:00
parent 11e663be67
commit 0fd59bc78d
4 changed files with 92 additions and 41 deletions

View File

@ -525,7 +525,7 @@ public class OrbotMainActivity extends AppCompatActivity
}
else if (item.getItemId() == R.id.menu_hidden_services)
{
if(usesRuntimePermissions()){
if(usesRuntimePermissions() && !hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
postPermissionsAction = new GrantedPermissionsAction() {
@Override
public void run(Context context, boolean granted) {
@ -615,7 +615,7 @@ public class OrbotMainActivity extends AppCompatActivity
stopVpnService();
}
private void enableHiddenServicePort (String hsName, final int hsPort, int hsRemotePort, final boolean doBackup) throws RemoteException, InterruptedException
private void enableHiddenServicePort (String hsName, final int hsPort, int hsRemotePort, final boolean doBackup, final String keyZipPath) throws RemoteException, InterruptedException
{
String onionHostname = null;
@ -664,8 +664,15 @@ 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));
if(doBackup) {
HiddenServiceUtils hsutils = new HiddenServiceUtils(getApplicationContext());
if(keyZipPath != null && keyZipPath.length() > 0)
{
hsutils.restoreOnionBackup(hsPort, keyZipPath);
requestTorRereadConfig();
}
if(doBackup)
{
backupPath = hsutils.createOnionBackup(hsPort);
}
onion.close();
@ -686,7 +693,6 @@ public class OrbotMainActivity extends AppCompatActivity
Intent nResult = new Intent();
nResult.putExtra("hs_host", onionHostname);
// TODO: Add key
setResult(RESULT_OK, nResult);
finish();
@ -713,23 +719,22 @@ public class OrbotMainActivity extends AppCompatActivity
final int hiddenServiceRemotePort = intent.getIntExtra("hs_onion_port", -1);
final String hiddenServiceName = intent.getStringExtra("hs_name");
final Boolean createBackup = intent.getBooleanExtra("hs_backup",false);
if(createBackup && usesRuntimePermissions())
checkPermissions();
final String keyZipPath = intent.getStringExtra("hs_key_zip_path");
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
if(createBackup && usesRuntimePermissions()){
if(createBackup && usesRuntimePermissions()
&& !hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
postPermissionsAction = new GrantedPermissionsAction() {
@Override
public void run(Context context, boolean granted) {
try {
enableHiddenServicePort (
hiddenServiceName, hiddenServicePort,
hiddenServiceRemotePort, createBackup
hiddenServiceRemotePort, createBackup, keyZipPath
);
} catch (RemoteException e) {
// TODO Auto-generated catch block
@ -745,7 +750,7 @@ public class OrbotMainActivity extends AppCompatActivity
try {
enableHiddenServicePort (
hiddenServiceName, hiddenServicePort,
hiddenServiceRemotePort, createBackup
hiddenServiceRemotePort, createBackup, keyZipPath
);
} catch (RemoteException e) {
// TODO Auto-generated catch block
@ -1605,7 +1610,6 @@ public class OrbotMainActivity extends AppCompatActivity
}
private void checkPermissions() {
if (!hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (ActivityCompat.shouldShowRequestPermissionRationale
(OrbotMainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Snackbar.make(findViewById(android.R.id.content),
@ -1625,7 +1629,6 @@ public class OrbotMainActivity extends AppCompatActivity
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
}
@SuppressLint("NewApi")
@Override

View File

@ -37,7 +37,8 @@ public class HiddenServiceUtils {
return zip_path;
}
public void restoreOnionBackup(String path) {
public void restoreOnionBackup(Integer port, String path) {
ZipIt zip = new ZipIt(null, path);
zip.unzip(appCacheHome + "/hs" + port);
}
}

View File

@ -3,9 +3,13 @@ package org.torproject.android.hsutils;
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 {
@ -47,4 +51,48 @@ public class ZipIt {
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;
}
}

View File

@ -53,11 +53,10 @@ public class HSActionsDialog extends DialogFragment {
Toast.makeText(mContext, R.string.done, Toast.LENGTH_LONG).show();
Uri selectedUri = Uri.parse(backupPath);
Uri selectedUri = Uri.parse(backupPath.substring(0, backupPath.lastIndexOf("/")));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
if (intent.resolveActivityInfo(mContext.getPackageManager(), 0) != null) {
startActivity(intent);
} else {