Android address book import/export

This commit is contained in:
str4d
2017-02-12 23:06:50 +00:00
parent ee783dc31b
commit de9c8f9036
10 changed files with 177 additions and 6 deletions

View File

@ -1,3 +1,6 @@
0.7
* Address book import/export
0.6 / 2015-06-21 / 77d67b4f2d465a5c528fb31af5785d176276b94b
* Identity switcher
* Language can be configured

View File

@ -96,6 +96,13 @@
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>
</activity>
<activity
android:name=".addressbook.AddressBookShipActivity"
android:parentActivityName=".addressbook.AddressBookActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="i2p.bote.android.addressbook.AddressBookActivity"/>
</activity>
<activity
android:name=".NetworkInfoActivity"
android:label="@string/network_status"

View File

@ -11,6 +11,7 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
@ -125,15 +126,36 @@ public class AddressBookFragment extends AuthenticatedFragment implements
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.address_book, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
boolean passwordRequired = I2PBote.getInstance().isPasswordRequired();
menu.findItem(R.id.export_address_book).setVisible(!passwordRequired);
menu.findItem(R.id.import_address_book).setVisible(!passwordRequired);
mPromotedActions.setVisibility(passwordRequired ? View.GONE : View.VISIBLE);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.export_address_book:
Intent ei = new Intent(getActivity(), AddressBookShipActivity.class);
ei.putExtra(AddressBookShipActivity.EXPORTING, true);
startActivity(ei);
return true;
case R.id.import_address_book:
Intent ii = new Intent(getActivity(), AddressBookShipActivity.class);
ii.putExtra(AddressBookShipActivity.EXPORTING, false);
startActivity(ii);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void startNewContact() {
Intent nci = new Intent(getActivity(), EditContactActivity.class);
getActivity().startActivityForResult(nci, AddressBookActivity.ALTER_CONTACT_LIST);

View File

@ -0,0 +1,26 @@
package i2p.bote.android.addressbook;
import android.widget.Toast;
import i2p.bote.android.R;
import i2p.bote.android.util.DataShipActivity;
import i2p.bote.android.util.DataShipFragment;
public class AddressBookShipActivity extends DataShipActivity {
@Override
protected DataShipFragment getDataShipFragment() {
return AddressBookShipFragment.newInstance(mExporting);
}
// DataShipFragment.Callbacks
public void onTaskFinished() {
Toast.makeText(this,
mExporting ?
R.string.address_book_exported:
R.string.address_book_imported,
Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
}

View File

@ -0,0 +1,95 @@
package i2p.bote.android.addressbook;
import android.os.Bundle;
import android.view.View;
import java.io.File;
import java.io.FileDescriptor;
import i2p.bote.I2PBote;
import i2p.bote.android.R;
import i2p.bote.android.util.DataShipFragment;
import i2p.bote.android.util.RobustAsyncTask;
import i2p.bote.fileencryption.PasswordException;
public abstract class AddressBookShipFragment extends DataShipFragment {
public static DataShipFragment newInstance(boolean exporting) {
return exporting ?
new ExportAddressBookFragment() :
new ImportAddressBookFragment();
}
public static class ExportAddressBookFragment extends ExportDataFragment {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mExportFilename.setText("addressBook");
}
@Override
protected RobustAsyncTask<Object, String, String> getExportWaiter() {
return new ExportWaiter();
}
@Override
protected int getTitle() {
return R.string.export_address_book;
}
private class ExportWaiter extends RobustAsyncTask<Object, String, String> {
@Override
protected String doInBackground(Object... params) {
try {
publishProgress(getResources().getString(R.string.exporting_address_book));
I2PBote.getInstance().getAddressBook().export(
(File) params[0],
(String) params[1]);
return null;
} catch (Throwable e) {
cancel(false);
return e.getMessage();
}
}
}
}
public static class ImportAddressBookFragment extends ImportDataFragment {
@Override
protected RobustAsyncTask<Object, String, String> getImportWaiter() {
return new ImportWaiter();
}
@Override
protected int getTitle() {
return R.string.import_address_book;
}
private class ImportWaiter extends RobustAsyncTask<Object, String, String> {
@Override
protected String doInBackground(Object... params) {
try {
publishProgress(getResources().getString(R.string.importing_address_book));
boolean success = I2PBote.getInstance().getAddressBook().importFromFileDescriptor(
(FileDescriptor) params[0],
(String) params[1],
(Boolean) params[2],
(Boolean) params[3]);
if (success)
return null;
else {
cancel(false);
return (params[1] == null) ?
getResources().getString(R.string.no_contacts_found_maybe_encrypted) :
getResources().getString(R.string.no_contacts_found);
}
} catch (Throwable e) {
e.printStackTrace();
cancel(false);
if (e instanceof PasswordException)
return getResources().getString(R.string.password_incorrect);
return e.getLocalizedMessage();
}
}
}
}
}

View File

@ -118,7 +118,7 @@ public class IdentityListFragment extends AuthenticatedFragment implements
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.settings, menu);
inflater.inflate(R.menu.identity_list, menu);
}
@Override

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:i2pandroid="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/export_address_book"
android:title="@string/export_address_book"
i2pandroid:showAsAction="never" />
<item
android:id="@+id/import_address_book"
android:title="@string/import_address_book"
i2pandroid:showAsAction="never" />
</menu>

View File

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:i2pandroid="http://schemas.android.com/apk/res-auto" >
</menu>

View File

@ -248,6 +248,14 @@
<string name="no_identities_found_maybe_encrypted">No identities were found. Is the file encrypted?</string>
<string name="identities_imported">Identities imported</string>
<string name="this_field_is_required">This field is required</string>
<string name="export_address_book">Export address book</string>
<string name="exporting_address_book">Exporting address book</string>
<string name="address_book_exported">Address book exported to Downloads folder</string>
<string name="import_address_book">Import address book</string>
<string name="importing_address_book">Importing address book</string>
<string name="no_contacts_found">No contacts were found.</string>
<string name="no_contacts_found_maybe_encrypted">No contacts were found. Is the file encrypted?</string>
<string name="address_book_imported">Address book imported</string>
<string name="bote_dest_for">Bote address for %s</string>
<string name="copied_to_clipboard">Copied to clipboard</string>
<string name="help">Help</string>