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

Commit

Permalink
add preference option to export all passwords to an external dir
Browse files Browse the repository at this point in the history
  • Loading branch information
zeapo committed Jan 24, 2017
1 parent d0ca596 commit 9798eaa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
65 changes: 65 additions & 0 deletions app/src/main/java/com/zeapo/pwdstore/UserPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
Expand All @@ -45,15 +46,19 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

public class UserPreference extends AppCompatActivity {
private final static int IMPORT_SSH_KEY = 1;
private final static int IMPORT_PGP_KEY = 2;
private final static int EDIT_GIT_INFO = 3;
private final static int SELECT_GIT_DIRECTORY = 4;
private final static int EXPORT_PASSWORDS = 5;
private final static int REQUEST_EXTERNAL_STORAGE = 50;
private PrefsFragment prefsFragment;

Expand Down Expand Up @@ -205,6 +210,14 @@ public void onDismiss(DialogInterface dialog) {
return true;
}
});

findPreference("export_passwords").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
callingActivity.exportPasswordsWithPermissions();
return true;
}
});
}

@Override
Expand Down Expand Up @@ -354,6 +367,42 @@ public void getSshKey() {
startActivityForResult(i, IMPORT_SSH_KEY);
}

public void exportPasswordsWithPermissions() {
final Activity activity = this;
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Snackbar snack = Snackbar.make(prefsFragment.getView(),
"We need access to the sd-card to export the passwords",
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.dialog_ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);
}
});
snack.show();
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
tv.setMaxLines(10);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);
}
} else {
Intent i = new Intent(getApplicationContext(), FilePickerActivity.class);

// Set these depending on your use case. These are the defaults.
i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true);
i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR);

i.putExtra(FilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath());

startActivityForResult(i, EXPORT_PASSWORDS);
}
}

/**
* Opens a key generator to generate a public/private key pair
*/
Expand Down Expand Up @@ -457,6 +506,22 @@ public void onClick(DialogInterface dialog, int which) {
}
}
break;
case EXPORT_PASSWORDS: {
final Uri uri = data.getData();
final File repositoryDirectory = PasswordRepository.getRepositoryDirectory(getApplicationContext());
SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
Date date = new Date();
String password_now = "/password_store_" + fmtOut.format(date);
final File targetDirectory = new File(uri.getPath() + password_now);
if (repositoryDirectory != null) {
try {
FileUtils.copyDirectory(repositoryDirectory, targetDirectory, true);
} catch (IOException e) {
Log.d("PWD_EXPORT", "Exception happened : " + e.getMessage());
}
}
}
break;
default:
break;
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
</PreferenceCategory>

<PreferenceCategory android:title="@string/pref_misc_title">
<Preference
android:key="export_passwords"
android:title="Export Passwords"
android:summary="Exports the encrypted passwords to an external directory"/>

<CheckBoxPreference
android:defaultValue="false"
android:key="clear_clipboard_20x"
Expand Down

0 comments on commit 9798eaa

Please sign in to comment.