Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Hevin committed Mar 31, 2016
1 parent 1eb4354 commit f18b026
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 83 deletions.
70 changes: 35 additions & 35 deletions Plugins/Android/JPushEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@
using System.Collections;

namespace JPush {

// internal event listener model
internal class EventListener {
public string name;
public GameObject listener;
public string function;
}

// Custom event class, extend when creating custom events
public class CustomEvent {

private string _type;
private Hashtable _arguments = new Hashtable();

// constructor
public CustomEvent(string eventType = "") {
_type = eventType;
}

// the type of event
public string type {
get { return _type; }
set { _type = value; }
}

// the arguments to pass with the event
public Hashtable arguments {
get { return _arguments; }
set { _arguments = value; }
}
}

public class JPushEventManager : MonoBehaviour {

// singleton instance
public static JPushEventManager instance;

// settings
public bool allowSingleton = true; // JPushEventManager class will transfer between scene changes.
public bool allowWarningOutputs = true;
public bool allowDebugOutputs = true;

private static bool _created = false;
private Hashtable _listeners = new Hashtable();

static JPushEventManager(){
instance = new JPushEventManager() ;
}
Expand All @@ -69,14 +69,15 @@ public void Awake() {
}
}
}

// clear events on quit
public void OnApplicationQuit() {
Debug.LogWarning("------Application Quit--------");
_listeners.Clear();
}

// PUBLIC *******************************

// Add event listener
public bool addEventListener(string eventType, GameObject listener, string function) {
if (listener == null || eventType == null) {
Expand All @@ -88,11 +89,11 @@ public bool addEventListener(string eventType, GameObject listener, string funct
recordEvent(eventType);
return recordListener(eventType, listener, function);
}

// Remove event listener
public bool removeEventListener(string eventType, GameObject listener) {
if (!checkForEvent(eventType)) return false;

ArrayList listenerList = _listeners[eventType] as ArrayList;
foreach (EventListener callback in listenerList) {
if (callback.name == listener.GetInstanceID().ToString()) {
Expand All @@ -102,19 +103,19 @@ public bool removeEventListener(string eventType, GameObject listener) {
}
return false;
}

// Remove all event listeners
public void removeAllEventListeners(GameObject listener) {
//print ("listener.name------" + listener.name + " ---" + listener.GetInstanceID().ToString()) ;
_listeners.Clear() ;
/*foreach (EventListener callback in _listeners) {

/*foreach (EventListener callback in _listeners) {
if (callback.listener.GetInstanceID().ToString() == listener.GetInstanceID().ToString()) {
_listeners.Remove(callback);
}
}*/
}

// Dispatch an event
public bool dispatchEvent(CustomEvent evt) {
string eventType = evt.type;
Expand All @@ -124,61 +125,61 @@ public bool dispatchEvent(CustomEvent evt) {
}
return false;
}

ArrayList listenerList = _listeners[eventType] as ArrayList;
if (allowDebugOutputs) {
Debug.Log("Event Manager: Event " + eventType + " dispatched to " + listenerList.Count + ((listenerList.Count == 1) ? " listener." : " listeners."));
}

foreach (EventListener callback in listenerList) {
//print ("function---------" + callback.function) ;
if (callback.listener && callback.listener.activeSelf) {//callback.listener.active
callback.listener.SendMessage(callback.function, evt, SendMessageOptions.DontRequireReceiver);
callback.listener.SendMessage(callback.function, evt, SendMessageOptions.DontRequireReceiver);
}
}
return false;
}

// PRIVATE *******************************

private void Setup() {
// TO DO: Self create GameObject if not already created
}

// see if event already exists
private bool checkForEvent(string eventType) {
if (_listeners.ContainsKey(eventType)) return true;
return false;
}

// record event, if it doesn't already exists
private bool recordEvent(string eventType) {
if (!checkForEvent(eventType)) {
_listeners.Add(eventType, new ArrayList());
}
return true;
}

// delete event, if not already removed
private bool deleteEvent(string eventType) {
private bool deleteEvent(string eventType) {
if (!checkForEvent(eventType)) return false;
_listeners.Remove(eventType);
return true;
}

// check if listener exists
private bool checkForListener(string eventType, GameObject listener) {
if (!checkForEvent(eventType)) {
recordEvent(eventType);
}

ArrayList listenerList = _listeners[eventType] as ArrayList;
foreach (EventListener callback in listenerList) {
if (callback.name == listener.GetInstanceID().ToString()) return true;
}
return false;
}

// record listener, if not already recorded
private bool recordListener(string eventType, GameObject listener, string function) {
if (!checkForListener(eventType, listener)) {
Expand All @@ -197,7 +198,6 @@ private bool recordListener(string eventType, GameObject listener, string functi
}
}
}


}


}
Binary file modified Plugins/Android/jpush_bringe.jar
Binary file not shown.
74 changes: 36 additions & 38 deletions Plugins/Android/src/JPushBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,72 +17,72 @@
import com.unity3d.player.UnityPlayer;


/**
/**
* Copyright © 2014 JPUSH. All rights reserved.
* @Title: JPushBridge.java
* @Prject: Unity_002
* @Package: com.example.unity_002
* @Description: TODO
* @author: zhangfl
* @author: zhangfl
* @date: 2014-4-16 上午9:48:20
* @version: V1.0
* @version: V1.0
*/
public class JPushBridge {
private static JPushBridge jpushBridge = new JPushBridge() ;
private Activity activity = null;
public static String gameObjectName = "" ;
public static String funcName = "" ;

public static boolean ISQUIT = true ;

private Activity getActivity(){
if(activity == null){
activity = UnityPlayer.currentActivity;
activity = UnityPlayer.currentActivity;
}

return activity;
}

public static JPushBridge getInstance() {
if(null == jpushBridge)
if(null == jpushBridge)
jpushBridge = new JPushBridge() ;
ISQUIT = false ;
return jpushBridge ;
}

public void isQuit() {
ISQUIT = true ;
}

public void setDebug(boolean enable) {
JPushInterface.setDebugMode(enable) ;
}


public void initJPush(String gameObject , String func) {
gameObjectName = gameObject ;
gameObjectName = gameObject ;
funcName = func ;

JPushInterface.init(getActivity()) ;
UnityPlayer.UnitySendMessage(gameObjectName ,funcName , "initJPush:" + gameObject + "---" + func );
}

public void stopJPush(String gameObject , String func) {
JPushInterface.stopPush(getActivity()) ;
UnityPlayer.UnitySendMessage(gameObject ,func , "stopJPush" );
}

public void resumeJPush(String gameObject , String func) {
JPushInterface.resumePush(getActivity()) ;
UnityPlayer.UnitySendMessage(gameObject ,func , "resumeJPush" );
}

public void setTags(String gameObject , String func ,String unity_tags ) {

if (TextUtils.isEmpty(unity_tags)) {
return;
}

String[] sArray = unity_tags.split(",");
final Set<String> tagSet = new LinkedHashSet<String>();
for (String sTagItme : sArray) {
Expand All @@ -91,21 +91,21 @@ public void setTags(String gameObject , String func ,String unity_tags ) {
}
tagSet.add(sTagItme);
}

UnityPlayer.UnitySendMessage(gameObject , func , "setTags:" + unity_tags);

getActivity().runOnUiThread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
JPushInterface.setTags(getActivity(), tagSet, mTagsCallback) ;
}
}) ;
}

public void setAlias(String gameObject , String func , String unity_alias) {

if (TextUtils.isEmpty(unity_alias)) {
return;
}
Expand All @@ -114,17 +114,17 @@ public void setAlias(String gameObject , String func , String unity_alias) {
}
final String alias = unity_alias ;
getActivity().runOnUiThread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
JPushInterface.setAlias(getActivity(), alias, mAliasCallback) ;
}
});

UnityPlayer.UnitySendMessage(gameObject , func , "setAlias:" + unity_alias );
}

private final TagAliasCallback mAliasCallback = new TagAliasCallback() {

@Override
Expand All @@ -137,18 +137,17 @@ public void gotResult(int code, String alias, Set<String> tags) {

case 6002:
logs = "Failed to set alias due to timeout. Try again after 60s.";

break;

default:
logs = "Failed with errorCode = " + code;
}

showToast(logs, getActivity());
Log.i("JPushBridge", logs);
}

};

private final TagAliasCallback mTagsCallback = new TagAliasCallback() {

@Override
Expand All @@ -166,12 +165,11 @@ public void gotResult(int code, String alias, Set<String> tags) {
default:
logs = "Failed with errorCode = " + code;
}

showToast(logs, getActivity());
Log.i("JPushBridge", logs);
}

};

public void setPushTime(String gameObject , String func , String _days, String _startime, String _endtime) {
//TODO check _days _starttime _endtime are vertify
if(!isNumeric(_startime) || !isNumeric(_endtime))
Expand All @@ -188,7 +186,7 @@ public void setPushTime(String gameObject , String func , String _days, String _
JPushInterface.setPushTime(getActivity(), days, starttime, endtime) ;
UnityPlayer.UnitySendMessage(gameObject ,func , "setPushTime" );
}

// 校验Tag Alias 只能是数字,英文字母和中文
private boolean isValidTagAndAlias(String s) {
Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_-]{0,}$");
Expand All @@ -212,6 +210,6 @@ private boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}


}
Loading

0 comments on commit f18b026

Please sign in to comment.