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

feat: Made Directory Picker instead of edittext #251

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public boolean isSupported() {
public WifiConfiguration getWifiConfig() {
Object obj = null;
try {
obj = getWifiApConfig.invoke(wifiManager, null);
obj = getWifiApConfig.invoke(wifiManager, obj);
if (obj != null) {
return (WifiConfiguration) obj;
}
Expand Down
12 changes: 10 additions & 2 deletions skunkworks_crow/src/main/java/org/odk/share/tasks/DownloadJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.preference.PreferenceManager;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -93,6 +94,7 @@ public class DownloadJob extends Job {

private int total;
private int progress;
private String directoryPath = null;
private DataInputStream dis;
private DataOutputStream dos;
public static final String RESULT_DIVIDER = "---------------\n";
Expand Down Expand Up @@ -368,9 +370,15 @@ private String getInstancesPath() {

private String getOdkDestinationDir() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
directoryPath = prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER,
getContext().getString(R.string.default_odk_destination_dir));
} else {
directoryPath = prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR_EDIT_TEXT,
getContext().getString(R.string.default_odk_destination_dir));

return prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR,
getContext().getString(R.string.default_odk_destination_dir));
}
return directoryPath;
}

private void readInstances(String formId, String formVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ public class PreferenceKeys {
public static final String KEY_HOTSPOT_NAME = "hotspot_name";
public static final String KEY_HOTSPOT_PASSWORD = "hotspot_password";
public static final String KEY_HOTSPOT_PWD_REQUIRE = "hotspot_pwd_require";
public static final String KEY_ODK_DESTINATION_DIR = "odk_destination_dir";
public static final String KEY_ODK_DESTINATION_DIR_EDIT_TEXT = "odk_destination_dir_edit_text";
public static final String KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER = "odk_destination_dir_directory_picker";
public static final String KEY_DEFAULT_TRANSFER_METHOD = "default_transfer_method";
public static final String KEY_BLUETOOTH_NAME = "bluetooth_name";
public static final String KEY_BLUETOOTH_SECURE_MODE = "bluetooth_secure_mode";
public static final String KEY_ODK_SETTINGS = "hotspot_setting";


private PreferenceKeys() {

}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.odk.share.views.ui.settings;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -9,6 +10,7 @@
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
Expand All @@ -25,6 +27,8 @@

import org.odk.share.R;

import timber.log.Timber;


/**
* Created by laksh on 5/27/2018.
Expand All @@ -37,10 +41,13 @@ public class SettingsActivity extends PreferenceActivity {
Preference hotspotPasswordPreference;
CheckBoxPreference passwordRequirePreference;
CheckBoxPreference btSecureModePreference;
EditTextPreference odkDestinationDirPreference;
EditTextPreference odkDestinationDirPreferenceEditText;
ListPreference defaultMethodPreference;
Preference odkDestinationDirPreferenceDirectoryPicker;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating two different preferences is fine, but the values that we get from these two different preferences on different versions stored separately. Ideally, whatever value that we receive from these UI Objects should be saved to the same preference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that but I am getting preference cast exception

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give it a shot then will suggest the solution

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah than you

private SharedPreferences prefs;

private static final int DIRECTORY_REQUEST_CODE = 9999;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -51,6 +58,13 @@ protected void onCreate(Bundle savedInstanceState) {

addPreferencesFromResource(R.xml.preferences_menu);
addPreferences();

PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference(PreferenceKeys.KEY_ODK_SETTINGS);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
preferenceCategory.removePreference(odkDestinationDirPreferenceEditText);
} else {
preferenceCategory.removePreference(odkDestinationDirPreferenceDirectoryPicker);
}
}

@Override
Expand All @@ -68,7 +82,8 @@ private void addPreferences() {
hotspotPasswordPreference = findPreference(PreferenceKeys.KEY_HOTSPOT_PASSWORD);
passwordRequirePreference = (CheckBoxPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE);
btSecureModePreference = (CheckBoxPreference) findPreference(PreferenceKeys.KEY_BLUETOOTH_SECURE_MODE);
odkDestinationDirPreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR);
odkDestinationDirPreferenceEditText = (EditTextPreference) findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR_EDIT_TEXT);
odkDestinationDirPreferenceDirectoryPicker = (Preference) findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER);

prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

Expand All @@ -81,7 +96,10 @@ private void addPreferences() {
bluetoothNamePreference.setDefaultValue(defaultBluetoothName);
bluetoothNamePreference.setSummary(prefs.getString(PreferenceKeys.KEY_BLUETOOTH_NAME, defaultBluetoothName));
boolean isPasswordSet = prefs.getBoolean(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE, false);
odkDestinationDirPreference.setSummary(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR,


odkDestinationDirPreferenceDirectoryPicker.setSummary(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER, getString(R.string.default_odk_destination_dir)));
odkDestinationDirPreferenceEditText.setSummary(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR_EDIT_TEXT,
getString(R.string.default_odk_destination_dir)));
boolean isSecureMode = prefs.getBoolean(PreferenceKeys.KEY_BLUETOOTH_SECURE_MODE, true);

Expand All @@ -93,7 +111,8 @@ private void addPreferences() {
bluetoothNamePreference.setOnPreferenceChangeListener(preferenceChangeListener());
hotspotPasswordPreference.setOnPreferenceChangeListener(preferenceChangeListener());
passwordRequirePreference.setOnPreferenceChangeListener(preferenceChangeListener());
odkDestinationDirPreference.setOnPreferenceChangeListener(preferenceChangeListener());
odkDestinationDirPreferenceEditText.setOnPreferenceChangeListener(preferenceChangeListener());
odkDestinationDirPreferenceDirectoryPicker.setOnPreferenceClickListener(preferenceClickListener());
defaultMethodPreference.setOnPreferenceChangeListener(preferenceChangeListener());

hotspotPasswordPreference.setOnPreferenceClickListener(preferenceClickListener());
Expand All @@ -105,6 +124,9 @@ private Preference.OnPreferenceClickListener preferenceClickListener() {
case PreferenceKeys.KEY_HOTSPOT_PASSWORD:
showPasswordDialog();
break;
case PreferenceKeys.KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER:
chooseDirectory();
break;
}
return false;
};
Expand Down Expand Up @@ -148,13 +170,14 @@ private Preference.OnPreferenceChangeListener preferenceChangeListener() {
hotspotPasswordPreference.setEnabled(false);
}
break;
case PreferenceKeys.KEY_ODK_DESTINATION_DIR:
case PreferenceKeys.KEY_ODK_DESTINATION_DIR_EDIT_TEXT:
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);
odkDestinationDirPreferenceEditText.setSummary(dir);
prefs.edit().putString(PreferenceKeys.KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER, null).apply();
}
break;
case PreferenceKeys.KEY_DEFAULT_TRANSFER_METHOD:
Expand Down Expand Up @@ -198,4 +221,26 @@ private void showPasswordDialog() {
alertDialog.setCancelable(true);
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

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() + getString(R.string.directory_odk);
prefs.edit().putString(PreferenceKeys.KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER, filePath).apply();
prefs.edit().putString(PreferenceKeys.KEY_ODK_DESTINATION_DIR_EDIT_TEXT, null).apply();
odkDestinationDirPreferenceDirectoryPicker.setSummary(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER, getString(R.string.default_odk_destination_dir)));
} catch (Exception e) {
Timber.e("Can not choose the Directory");
}
break;
}
}
}

2 changes: 2 additions & 0 deletions skunkworks_crow/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@

<string name="default_odk_destination_dir">\/sdcard\/odk</string>
<string name="title_odk_destination_dir">ODK Destination Directory</string>
<string name="directory_odk">/odk</string>
<string name="choose_directory">Choose 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>
Expand Down
15 changes: 12 additions & 3 deletions skunkworks_crow/src/main/res/xml/preferences_menu.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General Settings">
<PreferenceCategory
android:key="hotspot_setting"
android:title="General Settings">

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

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

Expand Down Expand Up @@ -61,4 +70,4 @@
android:title="@string/secure_mode" />

</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.bluetooth.BluetoothAdapter;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
Expand All @@ -19,24 +20,28 @@
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = Build.VERSION_CODES.O_MR1)
public class SettingsActivityTest {

private SettingsActivity settingsActivity;
private SharedPreferences prefs;
private Preference hotspotPasswordPreference;
private CheckBoxPreference passwordRequirePreference;
private EditTextPreference hotspotNamePreference;
private EditTextPreference odkDestinationDirPreference;
private EditTextPreference odkDestinationDirPreferenceEditText;
private CheckBoxPreference btSecureModePreference;
private ListPreference defaultMethodPreference;
private EditTextPreference bluetoothNamePreference;
private Preference odkDestinationDirDirectoryPicker;


@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -75,8 +80,10 @@ public void preferenceMenuTest() {
assertNotNull(settingsActivity.findPreference(PreferenceKeys.KEY_HOTSPOT_PASSWORD));
//test for passwordRequirePreference
assertNotNull(settingsActivity.findPreference(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE));
//test for odkDestinationDirPreference
assertNotNull(settingsActivity.findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR));
//test for odkDestinationDirPreferenceEditText
assertNotNull(settingsActivity.findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR_EDIT_TEXT));
//test for odkDestinationDirDirectoryPicker
assertNotNull(settingsActivity.findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER));
//test for KEY_BLUETOOTH_SECURE_MODE
assertNotNull(settingsActivity.findPreference(PreferenceKeys.KEY_BLUETOOTH_SECURE_MODE));
//test for KEY_BLUETOOTH_NAME
Expand All @@ -91,15 +98,20 @@ public void preferenceMenuTest() {
@Test
public void preferenceSummaryTest() {
hotspotNamePreference = (EditTextPreference) settingsActivity.findPreference(PreferenceKeys.KEY_HOTSPOT_NAME);
odkDestinationDirPreference = (EditTextPreference) settingsActivity.findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR);
odkDestinationDirPreferenceEditText = (EditTextPreference) settingsActivity.findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR_EDIT_TEXT);
odkDestinationDirDirectoryPicker = settingsActivity.findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER);
bluetoothNamePreference = (EditTextPreference) settingsActivity.findPreference(PreferenceKeys.KEY_BLUETOOTH_NAME);

//test the summary
assertEquals(prefs.getString(PreferenceKeys.KEY_HOTSPOT_NAME,
settingsActivity.getString(R.string.default_hotspot_ssid)), hotspotNamePreference.getSummary());

assertEquals(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR,
settingsActivity.getString(R.string.default_odk_destination_dir)), odkDestinationDirPreference.getSummary());
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
assertEquals(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR_DIRECTORY_PICKER,
settingsActivity.getString(R.string.default_odk_destination_dir)), odkDestinationDirDirectoryPicker.getSummary());
} else {
assertEquals(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR_EDIT_TEXT,
settingsActivity.getString(R.string.default_odk_destination_dir)), odkDestinationDirPreferenceEditText.getSummary());
}

assertEquals(prefs.getString(PreferenceKeys.KEY_BLUETOOTH_NAME,
BluetoothAdapter.getDefaultAdapter().getName()), bluetoothNamePreference.getSummary());
Expand Down Expand Up @@ -142,4 +154,4 @@ public void listPreferenceTest() {
assertEquals(settingsActivity.getString(R.string.bluetooth), defaultMethodPreference.getValue());
assertEquals(defaultMethodPreference.getEntries().length, settingsActivity.getResources().getStringArray(R.array.methods_array).length);
}
}
}