Skip to content

Commit

Permalink
Makes utility method static
Browse files Browse the repository at this point in the history
Uses ActionListener to get results from SharedPreferencesUtils
  • Loading branch information
shobhitagarwal1612 committed Mar 3, 2018
1 parent 3b03e8d commit 683c1fc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void onClick(View v) {
// Give JSON file preference
if (j.exists()) {
SharedPreferencesUtils sharedPrefs = new SharedPreferencesUtils();
boolean success = sharedPrefs.loadSharedPreferencesFromJSONFile(j);
boolean success = SharedPreferencesUtils.loadSharedPreferencesFromJSONFile(j);
if (success) {
ToastUtils.showLongToast(R.string.settings_successfully_loaded_file_notification);
j.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import org.json.JSONException;
import org.json.JSONObject;
import org.odk.collect.android.R;
import org.odk.collect.android.activities.CollectAbstractActivity;
import org.odk.collect.android.activities.ScannerWithFlashlightActivity;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.listeners.ActionListener;
import org.odk.collect.android.preferences.AdminPreferencesActivity;
import org.odk.collect.android.utilities.LocaleHelper;
import org.odk.collect.android.utilities.QRCodeUtils;
Expand Down Expand Up @@ -240,19 +239,21 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {


private void applySettings(String content) {
try {
JSONObject jsonObject = new JSONObject(content);
SharedPreferencesUtils prefUtils = new SharedPreferencesUtils();
prefUtils.savePreferencesFromJSON(jsonObject);
} catch (JSONException e) {
Timber.e(e);
return;
}
SharedPreferencesUtils.savePreferencesFromString(content, new ActionListener() {
@Override
public void onSuccess() {
ToastUtils.showLongToast(Collect.getInstance().getString(R.string.successfully_imported_settings));
getActivity().finish();
final LocaleHelper localeHelper = new LocaleHelper();
localeHelper.updateLocale(getActivity());
((CollectAbstractActivity) getActivity()).goToTheMainActivityAndCloseAllOthers();
}

getActivity().finish();
final LocaleHelper localeHelper = new LocaleHelper();
localeHelper.updateLocale(getActivity());
((CollectAbstractActivity) getActivity()).goToTheMainActivityAndCloseAllOthers();
@Override
public void onFailure(Exception exception) {
Timber.e(exception);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import org.json.JSONException;
import org.json.JSONObject;
import org.odk.collect.android.R;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.listeners.ActionListener;
import org.odk.collect.android.preferences.AdminSharedPreferences;
import org.odk.collect.android.preferences.AutoSendPreferenceMigrator;
import org.odk.collect.android.preferences.GeneralSharedPreferences;
Expand Down Expand Up @@ -96,39 +96,46 @@ private static JSONObject getModifiedPrefs(Collection<String> keys) throws JSONE
return prefs;
}

public void savePreferencesFromJSON(JSONObject settingsJson) throws JSONException {

JSONObject generalPrefsJson = settingsJson.getJSONObject("general");
JSONObject adminPrefsJson = settingsJson.getJSONObject("admin");
public static void savePreferencesFromString(String content, ActionListener listener) {
try {
JSONObject settingsJson = new JSONObject(content);
JSONObject generalPrefsJson = settingsJson.getJSONObject("general");
JSONObject adminPrefsJson = settingsJson.getJSONObject("admin");

for (String key : getAllGeneralKeys()) {
for (String key : getAllGeneralKeys()) {

if (generalPrefsJson.has(key)) {
Object value = generalPrefsJson.get(key);
GeneralSharedPreferences.getInstance().save(key, value);
} else {
GeneralSharedPreferences.getInstance().reset(key);
if (generalPrefsJson.has(key)) {
Object value = generalPrefsJson.get(key);
GeneralSharedPreferences.getInstance().save(key, value);
} else {
GeneralSharedPreferences.getInstance().reset(key);
}
}
}

for (String key : getAllAdminKeys()) {
for (String key : getAllAdminKeys()) {

if (adminPrefsJson.has(key)) {
Object value = adminPrefsJson.get(key);
AdminSharedPreferences.getInstance().save(key, value);
} else {
AdminSharedPreferences.getInstance().reset(key);
if (adminPrefsJson.has(key)) {
Object value = adminPrefsJson.get(key);
AdminSharedPreferences.getInstance().save(key, value);
} else {
AdminSharedPreferences.getInstance().reset(key);
}
}
}

AuthDialogUtility.setWebCredentialsFromPreferences();
AutoSendPreferenceMigrator.migrate(generalPrefsJson);
AuthDialogUtility.setWebCredentialsFromPreferences();
AutoSendPreferenceMigrator.migrate(generalPrefsJson);

//settings import confirmation toast
ToastUtils.showLongToast(context.getString(R.string.successfully_imported_settings));
if (listener != null) {
listener.onSuccess();
}
} catch (JSONException exception) {
if (listener != null) {
listener.onFailure(exception);
}
}
}

public boolean loadSharedPreferencesFromJSONFile(File src) {
public static boolean loadSharedPreferencesFromJSONFile(File src) {
boolean res = false;
BufferedReader br = null;

Expand All @@ -141,15 +148,11 @@ public boolean loadSharedPreferencesFromJSONFile(File src) {
builder.append(line);
}

JSONObject jo = new JSONObject(builder.toString());

this.savePreferencesFromJSON(jo);
savePreferencesFromString(builder.toString(), null);

res = true;
} catch (IOException e) {
Timber.e(e, "Exception while loading preferences from file due to : %s ", e.getMessage());
} catch (JSONException e) {
Timber.e(e, "Exception while converting file to JSON object due to : %s ", e.getMessage());
} finally {
try {
if (br != null) {
Expand Down

0 comments on commit 683c1fc

Please sign in to comment.