Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
Directory Picker
Browse files Browse the repository at this point in the history
Directory Picker

Fixed typos

fixed Null pointer exception

reformated code in xml

added /ODK to filePath

Changed method names

Added /odk

Deleted some unused changes

Made code Checkstyle

removed unused strings
  • Loading branch information
ajay-prabhakar committed Apr 29, 2019
1 parent 8922412 commit 5e36e79
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.odk.share.preferences;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -20,6 +21,7 @@

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import timber.log.Timber;


/**
Expand All @@ -28,10 +30,11 @@

public class SettingsPreference extends PreferenceActivity {

private static final int DIRECTORY_REQUEST_CODE = 9999;
EditTextPreference hotspotNamePreference;
Preference hotspotPasswordPreference;
CheckBoxPreference passwordRequirePreference;
EditTextPreference odkDestinationDirPreference;
SwitchPreference passwordRequirePreference;
Preference odkDestinationDirPreference;
private SharedPreferences prefs;

@Override
Expand All @@ -57,26 +60,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
private void addPreferences() {
hotspotNamePreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_NAME);
hotspotPasswordPreference = findPreference(PreferenceKeys.KEY_HOTSPOT_PASSWORD);
passwordRequirePreference = (CheckBoxPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE);
odkDestinationDirPreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR);
passwordRequirePreference = (SwitchPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE);
odkDestinationDirPreference = findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR);

prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
hotspotNamePreference.setSummary(prefs.getString(PreferenceKeys.KEY_HOTSPOT_NAME,
getString(R.string.default_hotspot_ssid)));
boolean isPasswordSet = prefs.getBoolean(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE, false);
odkDestinationDirPreference.setSummary(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR,
getString(R.string.default_odk_destination_dir)));

odkDestinationDirPreference.setSummary(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR, getString(R.string.default_odk_destination_dir)));
hotspotPasswordPreference.setEnabled(isPasswordSet);
passwordRequirePreference.setChecked(isPasswordSet);

hotspotNamePreference.setOnPreferenceChangeListener(preferenceChangeListener());
hotspotPasswordPreference.setOnPreferenceChangeListener(preferenceChangeListener());
passwordRequirePreference.setOnPreferenceChangeListener(preferenceChangeListener());
odkDestinationDirPreference.setOnPreferenceChangeListener(preferenceChangeListener());

hotspotPasswordPreference.setOnPreferenceClickListener(preferenceClickListener());
odkDestinationDirPreference.setOnPreferenceClickListener(preferenceClickListener());
}

private Preference.OnPreferenceClickListener preferenceClickListener() {
Expand All @@ -85,6 +87,9 @@ private Preference.OnPreferenceClickListener preferenceClickListener() {
case PreferenceKeys.KEY_HOTSPOT_PASSWORD:
showPasswordDialog();
break;
case PreferenceKeys.KEY_ODK_DESTINATION_DIR:
chooseDirectory();
break;
}
return false;
};
Expand Down Expand Up @@ -117,15 +122,6 @@ private Preference.OnPreferenceChangeListener preferenceChangeListener() {
hotspotPasswordPreference.setEnabled(false);
}
break;
case PreferenceKeys.KEY_ODK_DESTINATION_DIR:
String dir = newValue.toString();
if (dir.length() == 0) {
Toast.makeText(getApplicationContext(), getString(R.string.odk_destination_dir_error), Toast.LENGTH_LONG).show();
return false;
} else {
odkDestinationDirPreference.setSummary(dir);
}
break;
}
return true;
};
Expand Down Expand Up @@ -159,4 +155,25 @@ private void showPasswordDialog() {
AlertDialog alertDialog = builder.create();
alertDialog.show();
}

public void chooseDirectory() {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(Intent.createChooser(i, getString(R.string.choose_directory)), DIRECTORY_REQUEST_CODE);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case DIRECTORY_REQUEST_CODE:
try {
String filePath = (data.getData()).getPath();
filePath = filePath + getString(R.string.directory_odk);
prefs.edit().putString(PreferenceKeys.KEY_ODK_DESTINATION_DIR, filePath).apply();
odkDestinationDirPreference.setSummary(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR, getString(R.string.default_odk_destination_dir)));
} catch (Exception e) {
Timber.e("Can not choose the Directory");
}
break;
}
}
}
3 changes: 2 additions & 1 deletion skunkworks_crow/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@
<string name="no_forms">No Forms Found</string>

<string name="default_odk_destination_dir">\/sdcard\/odk</string>
<string name="directory_odk">/odk</string>
<string name="choose_directory">Choose Directory</string>
<string name="title_odk_destination_dir">ODK Destination Directory</string>
<string name="odk_destination_dir_error">The destination should not be empty</string>
<string name="location_permission_needed">Location Permission Needed</string>
<string name="location_settings_dialog">Enable location from the settings.</string>
</resources>
28 changes: 14 additions & 14 deletions skunkworks_crow/src/main/res/xml/preferences_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@

<EditTextPreference
android:defaultValue="@string/default_hotspot_ssid"
android:summary="@string/default_hotspot_ssid"
android:title="@string/title_hotspot_ssid"
android:icon="@drawable/ic_wifi_tethering_black_24dp"
android:key="hotspot_name"
android:icon="@drawable/ic_wifi_tethering_black_24dp"/>
android:summary="@string/default_hotspot_ssid"
android:title="@string/title_hotspot_ssid" />

<Preference
android:defaultValue="@string/default_hotspot_password"
android:summary="********"
android:title="@string/title_hotspot_password"
android:key="hotspot_password"
android:enabled="false"
android:icon="@drawable/ic_vpn_key_black_24dp"/>
android:icon="@drawable/ic_vpn_key_black_24dp"
android:key="hotspot_password"
android:summary="********"
android:title="@string/title_hotspot_password" />

<CheckBoxPreference
<SwitchPreference
android:defaultValue="false"
android:icon="@drawable/ic_lock_black_24dp"
android:key="hotspot_pwd_require"
android:summary="@string/password_hotspot"
android:title="@string/set_password"
android:icon="@drawable/ic_lock_black_24dp"/>
android:title="@string/set_password" />

<EditTextPreference
<Preference
android:defaultValue="@string/default_odk_destination_dir"
android:summary="@string/default_odk_destination_dir"
android:title="@string/title_odk_destination_dir"
android:icon="@drawable/ic_sd_storage_black_24dp"
android:key="odk_destination_dir"
android:icon="@drawable/ic_sd_storage_black_24dp"/>
android:summary="@string/default_odk_destination_dir"
android:title="@string/title_odk_destination_dir" />

</PreferenceCategory>
</PreferenceScreen>

0 comments on commit 5e36e79

Please sign in to comment.