Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 9798eaa

Browse files
committed
add preference option to export all passwords to an external dir
1 parent d0ca596 commit 9798eaa

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

app/src/main/java/com/zeapo/pwdstore/UserPreference.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.support.v4.content.ContextCompat;
2424
import android.support.v7.app.AlertDialog;
2525
import android.support.v7.app.AppCompatActivity;
26+
import android.util.Log;
2627
import android.view.MenuItem;
2728
import android.view.View;
2829
import android.view.accessibility.AccessibilityManager;
@@ -45,15 +46,19 @@
4546
import java.io.File;
4647
import java.io.IOException;
4748
import java.io.InputStream;
49+
import java.text.SimpleDateFormat;
50+
import java.util.Date;
4851
import java.util.HashSet;
4952
import java.util.List;
53+
import java.util.Locale;
5054
import java.util.Set;
5155

5256
public class UserPreference extends AppCompatActivity {
5357
private final static int IMPORT_SSH_KEY = 1;
5458
private final static int IMPORT_PGP_KEY = 2;
5559
private final static int EDIT_GIT_INFO = 3;
5660
private final static int SELECT_GIT_DIRECTORY = 4;
61+
private final static int EXPORT_PASSWORDS = 5;
5762
private final static int REQUEST_EXTERNAL_STORAGE = 50;
5863
private PrefsFragment prefsFragment;
5964

@@ -205,6 +210,14 @@ public void onDismiss(DialogInterface dialog) {
205210
return true;
206211
}
207212
});
213+
214+
findPreference("export_passwords").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
215+
@Override
216+
public boolean onPreferenceClick(Preference preference) {
217+
callingActivity.exportPasswordsWithPermissions();
218+
return true;
219+
}
220+
});
208221
}
209222

210223
@Override
@@ -354,6 +367,42 @@ public void getSshKey() {
354367
startActivityForResult(i, IMPORT_SSH_KEY);
355368
}
356369

370+
public void exportPasswordsWithPermissions() {
371+
final Activity activity = this;
372+
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
373+
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
374+
Snackbar snack = Snackbar.make(prefsFragment.getView(),
375+
"We need access to the sd-card to export the passwords",
376+
Snackbar.LENGTH_INDEFINITE)
377+
.setAction(R.string.dialog_ok, new View.OnClickListener() {
378+
@Override
379+
public void onClick(View view) {
380+
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);
381+
}
382+
});
383+
snack.show();
384+
View view = snack.getView();
385+
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
386+
tv.setTextColor(Color.WHITE);
387+
tv.setMaxLines(10);
388+
} else {
389+
// No explanation needed, we can request the permission.
390+
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);
391+
}
392+
} else {
393+
Intent i = new Intent(getApplicationContext(), FilePickerActivity.class);
394+
395+
// Set these depending on your use case. These are the defaults.
396+
i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
397+
i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true);
398+
i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR);
399+
400+
i.putExtra(FilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath());
401+
402+
startActivityForResult(i, EXPORT_PASSWORDS);
403+
}
404+
}
405+
357406
/**
358407
* Opens a key generator to generate a public/private key pair
359408
*/
@@ -457,6 +506,22 @@ public void onClick(DialogInterface dialog, int which) {
457506
}
458507
}
459508
break;
509+
case EXPORT_PASSWORDS: {
510+
final Uri uri = data.getData();
511+
final File repositoryDirectory = PasswordRepository.getRepositoryDirectory(getApplicationContext());
512+
SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
513+
Date date = new Date();
514+
String password_now = "/password_store_" + fmtOut.format(date);
515+
final File targetDirectory = new File(uri.getPath() + password_now);
516+
if (repositoryDirectory != null) {
517+
try {
518+
FileUtils.copyDirectory(repositoryDirectory, targetDirectory, true);
519+
} catch (IOException e) {
520+
Log.d("PWD_EXPORT", "Exception happened : " + e.getMessage());
521+
}
522+
}
523+
}
524+
break;
460525
default:
461526
break;
462527
}

app/src/main/res/xml/preference.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@
9090
</PreferenceCategory>
9191

9292
<PreferenceCategory android:title="@string/pref_misc_title">
93+
<Preference
94+
android:key="export_passwords"
95+
android:title="Export Passwords"
96+
android:summary="Exports the encrypted passwords to an external directory"/>
97+
9398
<CheckBoxPreference
9499
android:defaultValue="false"
95100
android:key="clear_clipboard_20x"

0 commit comments

Comments
 (0)