Commit 2ee4e7a9 authored by rahadi's avatar rahadi

Preferences modifications

parent 86045cd4
......@@ -378,7 +378,8 @@ the specific language governing permissions and limitations under the License.
<!-- android:name="id.ac.stis.capi.pemutakhiran.activities.DsrtActivity" -->
<!-- android:theme="@style/VsenAppTheme" /> -->
<activity android:name=".lessthink.activities.ListingInstanceActivity"></activity>
<activity android:name=".lessthink.activities.InstanceActivity"></activity>
<activity android:name=".lessthink.activities.InstancePreferenceActivity" />
</application>
</manifest>
\ No newline at end of file
......@@ -88,8 +88,6 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
private static final String FORM_VERSION_KEY = "formversion";
private static final String HAS_BEEN_DOWNLOADED = "hasbeendownloaded";
private static final String FORM_TYPE_KEY = "formtype";
private static final boolean EXIT = true;
private static final boolean DO_NOT_EXIT = false;
......@@ -210,12 +208,6 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
return v;
}
@Override
public void onResume() {
super.onResume();
onRefreshPage();
}
public void refreshAfterSync() {
setUpDownloadedFormsOffline();
}
......@@ -241,13 +233,11 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
String formName = c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.DISPLAY_NAME));
String formVersion = c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.JR_FORM_ID));
String formId = c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.JR_FORM_ID));
String formType = c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE));
item.put(FORMNAME, formName);
item.put(FORM_ID_KEY, formId);
item.put(FORM_VERSION_KEY, formVersion);
item.put(HAS_BEEN_DOWNLOADED, "true");
item.put(FORM_TYPE_KEY, formType);
downloadedFormsBefore.add(item);
......@@ -451,8 +441,6 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
if (item.get(HAS_BEEN_DOWNLOADED) != null && item.get(HAS_BEEN_DOWNLOADED).equals("true")) {
if (isFormInActiveSurvey(item)) {
item.put(FORM_TYPE_KEY, c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE)));
downloadedFilteredFormList.add(item);
}
} else {
......@@ -463,8 +451,6 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
} else {
//has been downloaded
item.put(HAS_BEEN_DOWNLOADED, "true");
item.put(FORM_TYPE_KEY, c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE)));
if (isFormInActiveSurvey(item)) downloadedFilteredFormList.add(item);
}
}
......@@ -529,11 +515,6 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
item.put(FORM_ID_KEY, details.formID);
item.put(FORM_VERSION_KEY, details.formVersion);
// FormsDao formsDao = new FormsDao();
// Cursor c = formsDao.getFormsCursor(FormsProviderAPI.FormsColumns.JR_FORM_ID + "=?",
// new String[]{details.formID});
// item.put(FORM_TYPE_KEY, c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE)));
if (!mFormListContain(details.formName)) {
// Insert the new form in alphabetical order.
if (mFormList.size() == 0) {
......
......@@ -54,7 +54,6 @@ public class CapiInstancePreferences extends AppCompatActivity
private View constraintOption;
private View advancedSetting;
private View petaWbSetting;
private View pemutakhiranSetting;
private TextView primaryTextSummary, secondaryTextSummary, constraintValue;
private Switch showTableSwitch, showGroupInOneScreenSwitch;
private String formIds = "";
......@@ -81,7 +80,6 @@ public class CapiInstancePreferences extends AppCompatActivity
showGroupInOneScreenSwitch = findViewById(R.id.switch_show_onscreen);
advancedSetting = findViewById(R.id.advanced_setting);
petaWbSetting = findViewById(R.id.wb_peta_setting);
pemutakhiranSetting = findViewById(R.id.updating_setting);
constraintOption = findViewById(R.id.constraint_option);
constraintValue = findViewById(R.id.constraint_value);
......@@ -99,7 +97,6 @@ public class CapiInstancePreferences extends AppCompatActivity
constraintOption.setOnClickListener(this);
advancedSetting.setOnClickListener(this);
petaWbSetting.setOnClickListener(this);
pemutakhiranSetting.setOnClickListener(this);
primaryHolder.setOnClickListener(this);
secondaryHolder.setOnClickListener(this);
showTableSwitch.setOnCheckedChangeListener(this);
......@@ -157,13 +154,6 @@ public class CapiInstancePreferences extends AppCompatActivity
startActivity(new Intent(this, KonfigurasiPeta.class));
finish();
break;
// case R.id.updating_setting:
// Intent i = new Intent(this, VsenPreferenceActivity.class);
// i.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, VsenPreferenceFragment.class.getName());
// i.putExtra(PreferenceActivity.EXTRA_NO_HEADERS, true);
// startActivity(i);
// finish();
// break;
}
}
......
package id.ac.stis.capi.lessthink.activities;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import id.ac.stis.capi.R;
import id.ac.stis.capi.collectiva.activity.CapiInstanceActivity;
import id.ac.stis.capi.lessthink.preferences.PreferencesAPI;
import id.ac.stis.capi.lessthink.preferences.PreferencesManager;
import id.ac.stis.capi.odk.utilities.ApplicationConstants;
import timber.log.Timber;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class InstancePreferenceActivity extends PreferenceActivity {
public static final String KEY_FORM_ID = "formId";
public static final String KEY_FORM_NAME = "formName";
protected Method mLoadHeaders = null;
protected Method mHasHeaders = null;
private PreferencesManager preferencesManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewGroup root = getRootView();
root.setPadding(0, 0, 0, 0);
Toolbar toolbar = (Toolbar) View.inflate(this, R.layout.toolbar, null);
toolbar.setTitle(R.string.general_preferences);
View shadow = View.inflate(this, R.layout.toolbar_action_bar_shadow, null);
root.addView(toolbar, 0);
root.addView(shadow, 1);
preferencesManager = PreferencesManager.getInstance();
}
@Override
public void onBuildHeaders(List<Header> target) {
super.onBuildHeaders(target);
loadHeadersFromResource(R.xml.instance_preferences_header, target);
}
@Override
public boolean isValidFragment(String fragmentName) {
return true;
}
@Override
public void onBackPressed() {
// super.onBackPressed();
String formId = null;
String formName = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
formId = getIntent().getExtras().getString(KEY_FORM_ID);
formName = getIntent().getExtras().getString(KEY_FORM_NAME);
}
Intent intent;
String viewMode = (String) PreferencesManager.getInstance().getByFormId(PreferencesAPI.KEY_VIEW_MODE, formId);
if (viewMode.equalsIgnoreCase(PreferencesAPI.VIEW_MODE_ROSTER)) {
intent = new Intent(this, InstanceActivity.class);
} else {
intent = new Intent(this, CapiInstanceActivity.class);
}
intent.putExtra(ApplicationConstants.BundleKeys.FORM_MODE, ApplicationConstants.FormModes.VIEW_SENT);
intent.putExtra(CapiInstanceActivity.FORM_ID_KEY, formId);
intent.putExtra(CapiInstanceActivity.FORMNAME, formName);
startActivity(intent);
finish();
}
/**
* Checks to see if using new v11+ way of handling PrefsFragments.
*
* @return Returns false pre-v11, else checks to see if using headers.
*/
private boolean isNewV11Prefs() {
if (mHasHeaders != null && mLoadHeaders != null) {
try {
return (Boolean) mHasHeaders.invoke(this);
} catch (IllegalArgumentException e) {
Timber.e(e);
} catch (IllegalAccessException e) {
Timber.e(e);
} catch (InvocationTargetException e) {
Timber.e(e);
}
}
return false;
}
private ViewGroup getRootView() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return (ViewGroup) findViewById(android.R.id.list).getParent().getParent().getParent();
} else {
return (ViewGroup) findViewById(android.R.id.list).getParent();
}
}
}
package id.ac.stis.capi.lessthink.fragments;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import id.ac.stis.capi.R;
import id.ac.stis.capi.odk.preferences.PreferencesActivity;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class BasePreferenceFragment extends PreferenceFragment {
protected Toolbar toolbar;
private LinearLayout root;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
initToolbar(getPreferenceScreen(), view);
super.onViewCreated(view, savedInstanceState);
}
// inflates toolbar in the preference fragments
public void initToolbar(PreferenceScreen preferenceScreen, View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (getActivity() instanceof PreferencesActivity) {
root = (LinearLayout) ((ViewGroup) view.findViewById(android.R.id.list).getRootView()).getChildAt(0);
toolbar = (Toolbar) root.findViewById(R.id.toolbar);
} else {
root = (LinearLayout) view.findViewById(android.R.id.list).getParent().getParent();
toolbar = (Toolbar) LayoutInflater.from(getActivity()).inflate(R.layout.toolbar, root, false);
inflateToolbar(preferenceScreen.getTitle());
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
root = (LinearLayout) view.findViewById(android.R.id.list).getParent();
toolbar = (Toolbar) LayoutInflater.from(getActivity()).inflate(R.layout.toolbar, root, false);
inflateToolbar(preferenceScreen.getTitle());
}
}
private void inflateToolbar(CharSequence title) {
toolbar.setTitle(title);
root.addView(toolbar, 0);
View shadow = LayoutInflater.from(getActivity()).inflate(R.layout.toolbar_action_bar_shadow, root, false);
root.addView(shadow, 1);
}
}
......@@ -117,29 +117,19 @@ import static id.ac.stis.capi.odk.activities.FormEntryActivity.VIDEO_CHOOSER;
public class FormEntryDialogFragment extends DialogFragment implements FormLoaderListener, OnAnswerChangedListener,
OnButtonHierarchyClickListener, FormSavedListener {
public static final int MODE_EDIT = 0;
public static final int MODE_INSERT = 1;
private static final int EVALUATE_CONSTRAINTS = 1;
private static final int DO_NOT_EVALUATE_CONSTRAINTS = 2;
private static final int EVALUATE_CONSTRAINTS_BUT_CAN_SKIP = 3;
private static final int PROGRESS_DIALOG = 1;
private static final int SAVING_DIALOG = 2;
private static final int SAVING_IMAGE_DIALOG = 3;
private static final String INSTANCE_UUID = "instance_uuid";
private static final String FLAG_FROM_DETAIL = "from_detail";
private static final String VIEW_MODE = "viewMode";
private static final String FORM_ID = "form_id";
private final Object saveDialogLock = new Object();
private String instanceName;
private Uri instanceUri;
private SaveToDiskTask saveToDiskTask;
private boolean fromDetail;
private int viewMode, locationState;
private String instanceUuid, formPath;
private String instanceUuid, formId, formPath;
private OnEntrySavedListener onEntrySavedListener;
private FormLoaderTask formLoaderTask;
private FormsDao formsDao;
......@@ -158,12 +148,11 @@ public class FormEntryDialogFragment extends DialogFragment implements FormLoade
// Required empty public constructor
}
public static FormEntryDialogFragment newInstance(String uuid, int mode, boolean fromDetail) {
public static FormEntryDialogFragment newInstance(String instanceUuid, String formId) {
FormEntryDialogFragment fragment = new FormEntryDialogFragment();
Bundle args = new Bundle();
args.putString(INSTANCE_UUID, uuid);
args.putBoolean(FLAG_FROM_DETAIL, fromDetail);
args.putInt(VIEW_MODE, mode);
args.putString(INSTANCE_UUID, instanceUuid);
args.putString(FORM_ID, formId);
fragment.setArguments(args);
return fragment;
}
......@@ -178,31 +167,33 @@ public class FormEntryDialogFragment extends DialogFragment implements FormLoade
if (getArguments() != null) {
instanceUuid = getArguments().getString(INSTANCE_UUID);
fromDetail = getArguments().getBoolean(FLAG_FROM_DETAIL);
viewMode = getArguments().getInt(VIEW_MODE);
formId = getArguments().getString(FORM_ID);
}
Collect.getInstance().setFormController(null);
instancesDao = new InstancesDao();
Cursor c = instancesDao.getInstancesCursor(InstanceProviderAPI.InstanceColumns.INSTANCE_UUID + "=?",
new String[]{instanceUuid});
String instancePath = null;
// should be unique
Instance instance = instancesDao.getInstancesFromCursor(c).get(0);
String instancePath = instance.getInstanceFilePath();
String formId = instance.getJrFormId();
if (instanceUuid != null) {
instancesDao = new InstancesDao();
Cursor c = instancesDao.getInstancesCursor(InstanceProviderAPI.InstanceColumns.INSTANCE_UUID + "=?",
new String[]{instanceUuid});
instanceUri = ContentUris.withAppendedId(InstanceProviderAPI.InstanceColumns.CONTENT_URI,
instance.getIdInstance());
// should be unique
Instance instance = instancesDao.getInstancesFromCursor(c).get(0);
instancePath = instance.getInstanceFilePath();
instanceName = instance.getDisplayName();
instanceUri = ContentUris.withAppendedId(InstanceProviderAPI.InstanceColumns.CONTENT_URI,
instance.getIdInstance());
instanceName = instance.getDisplayName();
}
formsDao = new FormsDao();
Cursor c1 = formsDao.getFormsCursorForFormId(formId);
Form form = formsDao.getFormsFromCursor(c1).get(0);
String formPath = form.getFormFilePath();
formPath = form.getFormFilePath();
formLoaderTask = new FormLoaderTask(instancePath, null, null);
formLoaderTask.setFormLoaderListener(this);
......@@ -252,7 +243,7 @@ public class FormEntryDialogFragment extends DialogFragment implements FormLoade
builder.setView(v).setPositiveButton(getString(R.string.save_all_answers), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
saveDataToDisk();
saveDataToDisk();
}
});
......
package id.ac.stis.capi.lessthink.models;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
......
package id.ac.stis.capi.lessthink.preferences;
import java.util.HashMap;
import java.util.HashSet;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class PreferencesAPI {
public static final String KEY_CATEGORY_APPEARANCE = "appearance_category";
public static final String KEY_RESPONSE_APPEARANCE = "response_category";
public static final String KEY_CONSTRAINT_APPEARANCE = "constraint_category";
public static final String KEY_VIEW_MODE = "view_mode";
public static final String KEY_TITLE = "title";
public static final String KEY_SUBTITLE = "subtitle";
public static final String KEY_CONSTRAINT = "constraint";
public static final String TITLE_DEFAULT = "default";
public static final String VIEW_MODE_ROSTER = "roster";
public static final String VIEW_MODE_CARDS = "cards";
public static final String CONSTRAINT_STRICT = "strict";
public static final String CONSTRAINT_PROMPT = "prompt";
public static final String CONSTRAINT_ALLOW = "allow";
public static final HashMap<String, Object> DEFAULT_VALUE = getDefaultHashMap();
private static HashMap<String, Object> getDefaultHashMap() {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put(KEY_VIEW_MODE, VIEW_MODE_CARDS);
hashMap.put(KEY_TITLE, TITLE_DEFAULT);
hashMap.put(KEY_SUBTITLE, new HashSet<String>());
hashMap.put(KEY_CONSTRAINT, CONSTRAINT_STRICT);
return hashMap;
}
}
package id.ac.stis.capi.lessthink.preferences;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import java.util.Map;
import java.util.Set;
import id.ac.stis.capi.odk.application.Collect;
import timber.log.Timber;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class PreferencesManager {
private static PreferencesManager instance;
private SharedPreferences preferences;
private PreferencesManager() {
preferences = PreferenceManager.getDefaultSharedPreferences(Collect.getInstance());
}
public static PreferencesManager getInstance() {
if (instance == null) {
instance = new PreferencesManager();
}
return instance;
}
public Object getByFormId(String key, String formId) {
Object defaultValue = null;
Object value = null;
try {
defaultValue = PreferencesAPI.DEFAULT_VALUE.get(key);
} catch (Exception e) {
Timber.e(e);
}
String appendedKey = formId + "_" + key;
if (defaultValue == null || defaultValue instanceof String) {
value = preferences.getString(appendedKey, (String) defaultValue);
} else if (defaultValue instanceof Boolean) {
value = preferences.getBoolean(appendedKey, (Boolean) defaultValue);
} else if (defaultValue instanceof Long) {
value = preferences.getLong(appendedKey, (Long) defaultValue);
} else if (defaultValue instanceof Integer) {
value = preferences.getInt(appendedKey, (Integer) defaultValue);
} else if (defaultValue instanceof Float) {
value = preferences.getFloat(appendedKey, (Float) defaultValue);
} else if (defaultValue instanceof Set<?>) {
value = preferences.getStringSet(appendedKey, (Set<String>) defaultValue);
}
return value;
}
public String getKey(String key, String formId) {
return formId + "_" + key;
}
}
......@@ -52,6 +52,13 @@ import timber.log.Timber;
*/
public class XmlUtils {
/**
* Set values of an xml file by a map with key of xpath(s).
*
* @param file the XML file
* @param values the map of xpath and value
* @return true if the operation succeed
*/
public static boolean setValuesByMap(File file, Map<String, String> values) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
......@@ -83,6 +90,13 @@ public class XmlUtils {
return true;
}
/**
* Get init value rules supplied in the "initvalue" attribute inside a bind tag in a file.
*
* @param file the XML file
* @param rootElement the root element of the XML xpath
* @return Map of init value rule defined by the key of xpath from "nodeset" attribute
*/
public static Map<String, String> getInitValueRules(File file, String rootElement) {
Timber.d("Parsing dependencies from %s file", file.getAbsolutePath());
final InputStream is;
......@@ -145,6 +159,13 @@ public class XmlUtils {
return result;
}
/**
* Get instance values map of a file.
*
* @param file the XML file
* @param xPaths the xpath to be evaluated
* @return Map of instance value defined by the key of xpath
*/
public static Map<String, String> getInstanceValuesMap(File file, Map<String, String> xPaths) {
Map<String, String> values = new LinkedHashMap<>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
......@@ -172,6 +193,13 @@ public class XmlUtils {
return values;
}
/**
* Get instance value of a file.
*
* @param file the XML file
* @param xpath the xpath to be evaluated
* @return Instance value
*/
public static String getInstanceValue(File file, String xpath) {
String value = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
......@@ -195,6 +223,12 @@ public class XmlUtils {
return value;
}
/**
* Get list of xpaths from a file
*
* @param file the XML file
* @return list of xpaths
*/
public static List<String> getInstanceXPathList(File file) {
InputStream is;
......
......@@ -30,7 +30,6 @@ import android.view.Window;
import id.ac.stis.capi.BuildConfig;
import id.ac.stis.capi.R;
import id.ac.stis.capi.lessthink.activities.ListingInstanceActivity;
import id.ac.stis.capi.odk.application.Collect;
import id.ac.stis.capi.collectiva.activity.CapiLoginActivity;
import id.ac.stis.capi.collectiva.activity.CapiMainActivity;
......
......@@ -29,7 +29,9 @@ import id.ac.stis.capi.R;
import id.ac.stis.capi.collectiva.activity.CapiInstanceActivity;
import id.ac.stis.capi.collectiva.fragment.CapiFormFragment;
import id.ac.stis.capi.collectiva.preferences.Constants;
import id.ac.stis.capi.lessthink.activities.ListingInstanceActivity;
import id.ac.stis.capi.lessthink.activities.InstanceActivity;
import id.ac.stis.capi.lessthink.preferences.PreferencesAPI;
import id.ac.stis.capi.lessthink.preferences.PreferencesManager;
import id.ac.stis.capi.odk.dao.FormsDao;
import id.ac.stis.capi.odk.provider.FormsProviderAPI;
import id.ac.stis.capi.odk.utilities.ApplicationConstants;
......@@ -43,7 +45,6 @@ public class CapiFormAdapter extends RecyclerView.Adapter<CapiFormAdapter.ViewHo
private static final String FORM_ID_KEY = "formid";
private static final String FORM_VERSION_KEY = "formversion";
private static final String HAS_BEEN_DOWNLOADED = "hasbeendownloaded";
private static final String FORM_TYPE_KEY = "formtype";
private Activity context;
private Fragment fragment;
private ArrayList<HashMap<String, String>> dataSet;
......@@ -123,8 +124,9 @@ public class CapiFormAdapter extends RecyclerView.Adapter<CapiFormAdapter.ViewHo
private void seeListRespons(int position) {
Intent intent;
if (dataSet.get(position).get(FORM_TYPE_KEY).equalsIgnoreCase(FormsProviderAPI.FORM_TYPE_UPDATING)) {
intent = new Intent(context, ListingInstanceActivity.class);
String viewMode = (String) PreferencesManager.getInstance().getByFormId(PreferencesAPI.KEY_VIEW_MODE, dataSet.get(position).get(FORM_ID_KEY));
if (viewMode.equalsIgnoreCase(PreferencesAPI.VIEW_MODE_ROSTER)) {
intent = new Intent(context, InstanceActivity.class);
} else {
intent = new Intent(context, CapiInstanceActivity.class);
}
......
......@@ -130,7 +130,6 @@ public class FormsDao {
//RAHADI
int emptyInstanceFilePath = cursor.getColumnIndex(FormsProviderAPI.FormsColumns.EMPTY_INSTANCE_FILE_PATH);
int formType = cursor.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE);
Form form = new Form.Builder()
.displayName(cursor.getString(displayNameColumnIndex))
......@@ -147,7 +146,6 @@ public class FormsDao {
.formMediaPath(cursor.getString(formMediaPathColumnIndex))
.language(cursor.getString(languageColumnIndex))
.emptyInstanceFilePath(cursor.getString(emptyInstanceFilePath))
.formType(cursor.getString(formType))
.build();
forms.add(form);
......@@ -177,7 +175,6 @@ public class FormsDao {