Created
July 18, 2013 01:51
-
-
Save acidjazz/6026120 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.plugin.drawer; | |
import org.apache.cordova.api.CallbackContext; | |
import org.apache.cordova.api.CordovaPlugin; | |
import org.apache.cordova.api.PluginResult; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import android.support.v4.widget.DrawerLayout.DrawerListener; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import com.samsung.gea.NavDrawerDroidGap; | |
public class NavDrawerPlugin extends CordovaPlugin implements DrawerListener { | |
protected CallbackContext context; | |
public NavDrawerDroidGap getNavDrawerActivity() { | |
return (NavDrawerDroidGap) cordova.getActivity(); | |
} | |
private JSONObject jsonResults(String action, String param) { | |
JSONObject json = new JSONObject(); | |
try { | |
json.put("action", action); | |
json.put("param", param); | |
} catch (JSONException e) { | |
Log.w("Drawer Exception", e.getMessage()); | |
} | |
return json; | |
} | |
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { | |
if (action.equals("init")) { | |
getNavDrawerActivity().setDrawerListener(this); | |
getNavDrawerActivity().getLeftLayout().setOnItemClickListener(new DrawerItemClickListener()); | |
this.context = callbackContext; | |
JSONObject json = jsonResults("init",""); | |
PluginResult result = new PluginResult(PluginResult.Status.OK, json); | |
result.setKeepCallback(true); | |
this.context.sendPluginResult(result); | |
Log.w("DRAWER", "DRAWER INIT COMPLETE"); | |
return true; | |
} | |
if (action.equals("openDrawer")) { | |
NavDrawerDroidGap drawer = getNavDrawerActivity(); | |
drawer.getDrawerLayout().openDrawer(drawer.getLeftLayout()); | |
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK)); | |
} | |
if (action.equals("closeDrawer")) { | |
NavDrawerDroidGap drawer = getNavDrawerActivity(); | |
drawer.getDrawerLayout().closeDrawer(drawer.getLeftLayout()); | |
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK)); | |
} | |
return true; | |
} | |
@Override | |
public void onDrawerClosed(View arg0) { | |
Log.w("DRAWER", "CLOSE HANDLER"); | |
JSONObject json = jsonResults("close",""); | |
PluginResult result = new PluginResult(PluginResult.Status.OK, json); | |
result.setKeepCallback(true); | |
this.context.sendPluginResult(result); | |
} | |
@Override | |
public void onDrawerOpened(View arg0) { | |
Log.w("DRAWER", "OPEN HANDLER"); | |
JSONObject json = jsonResults("open",""); | |
PluginResult result = new PluginResult(PluginResult.Status.OK, json); | |
result.setKeepCallback(true); | |
this.context.sendPluginResult(result); | |
} | |
@Override | |
public void onDrawerSlide(View arg0, float arg1) { | |
// TODO Auto-generated method stub | |
//this.context.success("onDrawerSlide"); | |
} | |
@Override | |
public void onDrawerStateChanged(int arg0) { | |
// TODO Auto-generated method stub | |
//this.context.success("onDrawerStateChanged"); | |
} | |
private class DrawerItemClickListener implements ListView.OnItemClickListener { | |
@Override | |
public void onItemClick(AdapterView parent, View view, int position, long id) { | |
Log.w("DRAWER", "ITEM SELECTED HANDLER"); | |
JSONObject json = jsonResults("select",(String) parent.getAdapter().getItem(position)); | |
PluginResult result = new PluginResult(PluginResult.Status.OK, json); | |
result.setKeepCallback(true); | |
NavDrawerPlugin.this.context.sendPluginResult(result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment