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

View File

@ -37,7 +37,8 @@ public class HiddenServiceUtils {
return zip_path; 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.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
public class ZipIt { public class ZipIt {
@ -47,4 +51,48 @@ public class ZipIt {
return true; 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(); 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 intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder"); intent.setDataAndType(selectedUri, "resource/folder");
if (intent.resolveActivityInfo(mContext.getPackageManager(), 0) != null) { if (intent.resolveActivityInfo(mContext.getPackageManager(), 0) != null) {
startActivity(intent); startActivity(intent);
} else { } else {