package edu.cscie71.imm.slacker.plugin;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import edu.cscie71.imm.app.slacker.client.ISlackerClient;
import edu.cscie71.imm.app.slacker.client.SlackerClient;

public class Slacker extends CordovaPlugin {

    private ISlackerClient slackerClient = new SlackerClient();
    private String token = "xoxp-10020492535-10036686290-14227963249-1cb545e1ae";
    private String immTestChannel = "C0F6U0R5E";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("postMessage")) {
            String message = args.getString(0);
            this.postMessage(message, callbackContext);
        }
        return false;
    }

    private void postMessage(String message, CallbackContext callbackContext) {
        String response = slackerClient.postMessage(token, immTestChannel, message);
        if (response.contains("\"ok\":true"))
            callbackContext.success(message);
        else
            callbackContext.error("Error posting to the Slack channel.");
    }
}

@@ -34,16 +34,90 @@
 lugin;%0A%0A
+import android.content.Context;%0Aimport android.content.SharedPreferences;%0A
 import o
@@ -149,16 +149,16 @@
 Plugin;%0A
-
 import o
@@ -606,16 +606,67 @@
 6U0R5E%22;
+%0A    private static final String PREFS = %22Slacker%22;
 %0A%0A    @O
@@ -1244,16 +1244,16 @@
    else%0A
-
         
@@ -1323,11 +1323,544 @@
 );%0A    %7D
+%0A%0A    private void storeOAuthToken(String token) %7B%0A        Context context = cordova.getActivity();%0A    %09SharedPreferences.Editor editor = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit();%0A %09%09editor.putString(%22token%22, token);%0A %09%09editor.apply();%0A    %7D%0A%0A    private String getOAuthToken() %7B%0A        Context context = cordova.getActivity();%0A    %09SharedPreferences prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);%0A        // Return null as default value%0A    %09return prefs.getString(%22token%22, null);%0A    %7D
 %0A%7D%0A

Store OAuth token on Android