Import identities UI
This commit is contained in:
@@ -2,19 +2,23 @@ package i2p.bote.android.config;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileDescriptor;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import i2p.bote.I2PBote;
|
||||
import i2p.bote.android.R;
|
||||
@@ -55,7 +59,9 @@ public abstract class IdentityShipFragment extends Fragment {
|
||||
TextView mError;
|
||||
|
||||
public static IdentityShipFragment newInstance(boolean exporting) {
|
||||
return new ExportIdentitiesFragment(); // TODO implement importing
|
||||
return exporting ?
|
||||
new ExportIdentitiesFragment() :
|
||||
new ImportIdentitiesFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -91,21 +97,9 @@ public abstract class IdentityShipFragment extends Fragment {
|
||||
protected abstract void setInterfaceEnabled(boolean enabled);
|
||||
|
||||
public static class ShipWaiterFrag extends TaskFragment<Object, String, String> {
|
||||
static final String SHIP_FILE = "shipFile";
|
||||
static final String PASSWORD = "password";
|
||||
|
||||
String currentStatus;
|
||||
TextView mStatus;
|
||||
|
||||
public static ShipWaiterFrag newInstance(File shipFile, String password) {
|
||||
ShipWaiterFrag f = new ShipWaiterFrag();
|
||||
Bundle args = new Bundle();
|
||||
args.putSerializable(SHIP_FILE, shipFile);
|
||||
args.putString(PASSWORD, password);
|
||||
f.setArguments(args);
|
||||
return f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
@@ -118,15 +112,6 @@ public abstract class IdentityShipFragment extends Fragment {
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] getParams() {
|
||||
Bundle args = getArguments();
|
||||
return new Object[]{
|
||||
(File) args.getSerializable(SHIP_FILE),
|
||||
args.getString(PASSWORD),
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProgress(String... values) {
|
||||
currentStatus = values[0];
|
||||
@@ -217,7 +202,6 @@ public abstract class IdentityShipFragment extends Fragment {
|
||||
if (exportFile.exists()) {
|
||||
// TODO ask to rename or overwrite
|
||||
mError.setText(R.string.file_exists);
|
||||
return;
|
||||
} else
|
||||
exportIdentities(exportFile, password);
|
||||
}
|
||||
@@ -228,7 +212,7 @@ public abstract class IdentityShipFragment extends Fragment {
|
||||
setInterfaceEnabled(false);
|
||||
mError.setText("");
|
||||
|
||||
ShipWaiterFrag f = ShipWaiterFrag.newInstance(exportFile, password);
|
||||
ExportWaiterFrag f = ExportWaiterFrag.newInstance(exportFile, password);
|
||||
f.setTask(new ExportWaiter());
|
||||
f.setTargetFragment(ExportIdentitiesFragment.this, SHIP_WAITER);
|
||||
getFragmentManager().beginTransaction()
|
||||
@@ -244,6 +228,29 @@ public abstract class IdentityShipFragment extends Fragment {
|
||||
mConfirmPassword.setEnabled(enabled);
|
||||
}
|
||||
|
||||
public static class ExportWaiterFrag extends ShipWaiterFrag {
|
||||
static final String SHIP_FILE = "shipFile";
|
||||
static final String PASSWORD = "password";
|
||||
|
||||
public static ExportWaiterFrag newInstance(File shipFile, String password) {
|
||||
ExportWaiterFrag f = new ExportWaiterFrag();
|
||||
Bundle args = new Bundle();
|
||||
args.putSerializable(SHIP_FILE, shipFile);
|
||||
args.putString(PASSWORD, password);
|
||||
f.setArguments(args);
|
||||
return f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] getParams() {
|
||||
Bundle args = getArguments();
|
||||
return new Object[]{
|
||||
args.getSerializable(SHIP_FILE),
|
||||
args.getString(PASSWORD),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private class ExportWaiter extends RobustAsyncTask<Object, String, String> {
|
||||
@Override
|
||||
protected String doInBackground(Object... params) {
|
||||
@@ -260,4 +267,124 @@ public abstract class IdentityShipFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ImportIdentitiesFragment extends IdentityShipFragment {
|
||||
static final int SELECT_IMPORT_FILE = 1;
|
||||
|
||||
EditText mPassword;
|
||||
CheckBox mOverwrite;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_import_identities, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
mPassword = (EditText) view.findViewById(R.id.password);
|
||||
mOverwrite = (CheckBox) view.findViewById(R.id.overwrite);
|
||||
|
||||
view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
i.setType("text/plain");
|
||||
i.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
try {
|
||||
startActivityForResult(i, SELECT_IMPORT_FILE);
|
||||
} catch (android.content.ActivityNotFoundException ex) {
|
||||
Toast.makeText(getActivity(), "Please install a File Manager.",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == SELECT_IMPORT_FILE) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
Uri result = data.getData();
|
||||
try {
|
||||
ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(result, "r");
|
||||
String password = mPassword.getText().toString();
|
||||
if (password.isEmpty())
|
||||
password = null;
|
||||
importIdentities(pfd, password, !mOverwrite.isChecked());
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
mError.setText(e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
}
|
||||
|
||||
private void importIdentities(ParcelFileDescriptor importFile, String password, boolean append) {
|
||||
setInterfaceEnabled(false);
|
||||
mError.setText("");
|
||||
|
||||
ImportWaiterFrag f = ImportWaiterFrag.newInstance(importFile, password, append);
|
||||
f.setTask(new ImportWaiter());
|
||||
f.setTargetFragment(ImportIdentitiesFragment.this, SHIP_WAITER);
|
||||
getFragmentManager().beginTransaction()
|
||||
.replace(R.id.waiter_frag, f, SHIP_WAITER_TAG)
|
||||
.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setInterfaceEnabled(boolean enabled) {
|
||||
mPassword.setEnabled(enabled);
|
||||
mOverwrite.setEnabled(enabled);
|
||||
}
|
||||
|
||||
public static class ImportWaiterFrag extends ShipWaiterFrag {
|
||||
static final String SHIP_FILE_DESCRIPTOR = "shipFile";
|
||||
static final String PASSWORD = "password";
|
||||
static final String APPEND = "append";
|
||||
|
||||
public static ImportWaiterFrag newInstance(ParcelFileDescriptor shipFile, String password, boolean append) {
|
||||
ImportWaiterFrag f = new ImportWaiterFrag();
|
||||
Bundle args = new Bundle();
|
||||
args.putParcelable(SHIP_FILE_DESCRIPTOR, shipFile);
|
||||
args.putString(PASSWORD, password);
|
||||
args.putBoolean(APPEND, append);
|
||||
f.setArguments(args);
|
||||
return f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] getParams() {
|
||||
Bundle args = getArguments();
|
||||
return new Object[]{
|
||||
((ParcelFileDescriptor) args.getParcelable(SHIP_FILE_DESCRIPTOR))
|
||||
.getFileDescriptor(),
|
||||
args.getString(PASSWORD),
|
||||
args.getBoolean(APPEND),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private class ImportWaiter extends RobustAsyncTask<Object, String, String> {
|
||||
@Override
|
||||
protected String doInBackground(Object... params) {
|
||||
try {
|
||||
publishProgress("Importing identities");
|
||||
I2PBote.getInstance().getIdentities().importFromFileDescriptor(
|
||||
(FileDescriptor) params[0],
|
||||
(String) params[1],
|
||||
(Boolean) params[2]);
|
||||
return null;
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
cancel(false);
|
||||
return e.getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -140,6 +140,11 @@ public class SettingsActivity extends PreferenceActivity {
|
||||
ei.putExtra(IdentityShipActivity.EXPORTING, true);
|
||||
startActivity(ei);
|
||||
return true;
|
||||
case R.id.import_identities:
|
||||
Intent ii = new Intent(this, IdentityShipActivity.class);
|
||||
ii.putExtra(IdentityShipActivity.EXPORTING, false);
|
||||
startActivity(ii);
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
tools:context="i2p.bote.android.config.IdentityShipFragment">
|
||||
tools:context="i2p.bote.android.config.IdentityShipFragment$ExportIdentitiesFragment">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
44
app/src/main/res/layout/fragment_import_identities.xml
Normal file
44
app/src/main/res/layout/fragment_import_identities.xml
Normal file
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="vertical" android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
tools:context="i2p.bote.android.config.IdentityShipFragment$ImportIdentitiesFragment">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Password (leave blank if file is not encrypted)" />
|
||||
|
||||
<EditText
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="textPassword"
|
||||
android:ems="10"
|
||||
android:id="@+id/password" />
|
||||
|
||||
<CheckBox
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Overwrite existing identities"
|
||||
android:id="@+id/overwrite"
|
||||
android:checked="false" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/error"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/default_identity"
|
||||
android:textColor="@color/error_color" />
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Select file to import"
|
||||
android:id="@+id/button" />
|
||||
</LinearLayout>
|
||||
@@ -7,4 +7,9 @@
|
||||
android:id="@+id/export_identities"
|
||||
android:title="@string/export_identities"
|
||||
i2pandroid:showAsAction="never" />
|
||||
|
||||
<item
|
||||
android:id="@+id/import_identities"
|
||||
android:title="@string/import_identities"
|
||||
i2pandroid:showAsAction="never" />
|
||||
</menu>
|
||||
@@ -152,6 +152,7 @@
|
||||
<string name="passwords_do_not_match">Passwords do not match</string>
|
||||
<string name="file_exists">File exists</string>
|
||||
<string name="identities_exported">Identities exported to Documents folder</string>
|
||||
<string name="import_identities">Import identities</string>
|
||||
<string name="identities_imported">Identities imported</string>
|
||||
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user