add default android file picker for selecting ssh-key

This commit is contained in:
zeapo 2017-06-24 13:10:17 +02:00
parent 2c076fb0eb
commit 2788786f65
2 changed files with 28 additions and 17 deletions

View file

@ -87,7 +87,7 @@ public class UserPreference extends AppCompatActivity {
findPreference("ssh_key").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { findPreference("ssh_key").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
callingActivity.getSshKeyWithPermissions(); callingActivity.getSshKeyWithPermissions(sharedPreferences.getBoolean("use_android_file_picker", false));
return true; return true;
} }
}); });
@ -264,12 +264,13 @@ public class UserPreference extends AppCompatActivity {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (getIntent() != null) { if (getIntent() != null) {
if (getIntent().getStringExtra("operation") != null) { if (getIntent().getStringExtra("operation") != null) {
switch (getIntent().getStringExtra("operation")) { switch (getIntent().getStringExtra("operation")) {
case "get_ssh_key": case "get_ssh_key":
getSshKeyWithPermissions(); getSshKeyWithPermissions(sharedPreferences.getBoolean("use_android_file_picker", false));
break; break;
case "make_ssh_key": case "make_ssh_key":
makeSshKey(false); makeSshKey(false);
@ -334,7 +335,7 @@ public class UserPreference extends AppCompatActivity {
/** /**
* Opens a file explorer to import the private key * Opens a file explorer to import the private key
*/ */
public void getSshKeyWithPermissions() { public void getSshKeyWithPermissions(boolean useDefaultPicker) {
final Activity activity = this; final Activity activity = this;
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_EXTERNAL_STORAGE)) { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_EXTERNAL_STORAGE)) {
@ -357,14 +358,19 @@ public class UserPreference extends AppCompatActivity {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE); ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);
} }
} else { } else {
getSshKey(); getSshKey(useDefaultPicker);
} }
} }
/** /**
* Opens a file explorer to import the private key * Opens a file explorer to import the private key
*/ */
public void getSshKey() { public void getSshKey(boolean useDefaultPicker) {
if (useDefaultPicker) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, IMPORT_SSH_KEY);
} else {
// This always works // This always works
Intent i = new Intent(getApplicationContext(), FilePickerActivity.class); Intent i = new Intent(getApplicationContext(), FilePickerActivity.class);
// This works if you defined the intent filter // This works if you defined the intent filter
@ -376,8 +382,7 @@ public class UserPreference extends AppCompatActivity {
i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_FILE); i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_FILE);
i.putExtra(FilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath()); i.putExtra(FilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath());
}
startActivityForResult(i, IMPORT_SSH_KEY);
} }
public void exportPasswordsWithPermissions() { public void exportPasswordsWithPermissions() {
@ -543,11 +548,12 @@ public class UserPreference extends AppCompatActivity {
@Override @Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
switch (requestCode) { switch (requestCode) {
case REQUEST_EXTERNAL_STORAGE: { case REQUEST_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty. // If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getSshKey(); getSshKey(sharedPreferences.getBoolean("use_android_file_picker", false));
} }
} }
} }

View file

@ -103,6 +103,11 @@
android:key="clear_clipboard_20x" android:key="clear_clipboard_20x"
android:summary="@string/pref_clear_clipboard_hint" android:summary="@string/pref_clear_clipboard_hint"
android:title="@string/pref_clear_clipboard_title" /> android:title="@string/pref_clear_clipboard_title" />
<CheckBoxPreference
android:defaultValue="false"
android:key="use_android_file_picker"
android:title="Use default file picker" />
</PreferenceCategory> </PreferenceCategory>
<Preference <Preference