Skip to content

java.lang.Long cannot be cast to java.lang.String #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
124 changes: 62 additions & 62 deletions src/android/AppPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class AppPreferences extends CordovaPlugin {
private static final String LOG_TAG = "AppPrefs";
private static final int NO_PROPERTY = 0;
private static final int NO_PREFERENCE_ACTIVITY = 1;
private static final int SHOW_PREFERENCEACTIVITY_INTENT = 1;

private static final int SHOW_PREFERENCEACTIVITY_INTENT = 1;
private CallbackContext auxCtx;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
PluginResult.Status status = PluginResult.Status.OK;
Expand All @@ -48,35 +48,35 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
}
} else if (action.equals("set")) {
String key = args.getString(0);
String value = args.getString(1);
String value = args.getString(1);
Editor editor = sharedPrefs.edit();
if ("true".equals(value.toLowerCase()) || "false".equals(value.toLowerCase())) {
editor.putBoolean(key, Boolean.parseBoolean(value));
} else {
editor.putString(key, value);
}
callbackContext.sendPluginResult(new PluginResult(status, editor.commit()));
callbackContext.sendPluginResult(new PluginResult(status, editor.commit()));
} else if (action.equals("load")) {

this.load(callbackContext);

} else if (action.equals("show")) {
auxCtx = callbackContext;
this.load(callbackContext);

} else if (action.equals("show")) {
auxCtx = callbackContext;
String activityName = args.getString(0);
this.show(activityName, callbackContext);

this.show(activityName, callbackContext);

} else if (action.equals("clear")) {
Editor editor = sharedPrefs.edit();
editor.clear();
callbackContext.sendPluginResult(new PluginResult(status, editor.commit()));
Editor editor = sharedPrefs.edit();
editor.clear();
callbackContext.sendPluginResult(new PluginResult(status, editor.commit()));
} else if (action.equals("remove")) {
String key = args.getString(0);
if (sharedPrefs.contains(key)) {
Editor editor = sharedPrefs.edit();
editor.remove(key);
editor.commit();
callbackContext.sendPluginResult(new PluginResult(status, editor.commit()));
String key = args.getString(0);
if (sharedPrefs.contains(key)) {
Editor editor = sharedPrefs.edit();
editor.remove(key);
editor.commit();
callbackContext.sendPluginResult(new PluginResult(status, editor.commit()));
} else {
callbackContext.sendPluginResult(createErrorObj(NO_PROPERTY, "No such property called " + key));
}
Expand All @@ -89,52 +89,52 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return false;
}

private void load(CallbackContext callbackContext) {

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.cordova.getActivity());
Map map = sharedPrefs.getAll();
Iterator entries = map.entrySet().iterator();
JSONObject response = new JSONObject();
while(entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
try {
response.put(key, value);
} catch (JSONException e) {
continue;
}
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, response));
}
private void show(String prefActivity, CallbackContext callbackContext) {
String activityClass = prefActivity;
auxCtx = callbackContext;
try {
Class a = Class.forName(activityClass);
Intent intent = new Intent(this.cordova.getActivity(), a);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
this.cordova.startActivityForResult(this, intent, SHOW_PREFERENCEACTIVITY_INTENT);
} catch( ClassNotFoundException e ) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "No preferences activity called " + prefActivity));
}
private void load(CallbackContext callbackContext) {

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.cordova.getActivity());
Map map = sharedPrefs.getAll();
Iterator entries = map.entrySet().iterator();
JSONObject response = new JSONObject();
while(entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());

try {
response.put(key, value);
} catch (JSONException e) {
continue;
}
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, response));
}

private void show(String prefActivity, CallbackContext callbackContext) {

String activityClass = prefActivity;
auxCtx = callbackContext;

try {
Class a = Class.forName(activityClass);

Intent intent = new Intent(this.cordova.getActivity(), a);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

this.cordova.startActivityForResult(this, intent, SHOW_PREFERENCEACTIVITY_INTENT);

} catch( ClassNotFoundException e ) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "No preferences activity called " + prefActivity));
}

}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == SHOW_PREFERENCEACTIVITY_INTENT) {
this.load(auxCtx);
}

if(requestCode == SHOW_PREFERENCEACTIVITY_INTENT) {
this.load(auxCtx);
}
}

private PluginResult createErrorObj(int code, String message) throws JSONException {
JSONObject errorObj = new JSONObject();
errorObj.put("code", code);
Expand Down