Commit 7d7cbafb authored by xujialang's avatar xujialang

增加广播回调的动态代理

parent 30f3e9c9
...@@ -12,6 +12,7 @@ public class NameInterfaceHelp { ...@@ -12,6 +12,7 @@ public class NameInterfaceHelp {
private static String FG_Class_Path = ""; private static String FG_Class_Path = "";
private static String FG_StartCallback_Class_Path = ""; private static String FG_StartCallback_Class_Path = "";
private static String FG_NotificationConfig_Class_Path = ""; private static String FG_NotificationConfig_Class_Path = "";
private static String Action_Listener_Class_Path = "";
private static String FG_Start_Method_Name = ""; private static String FG_Start_Method_Name = "";
public static String getClassPath() { public static String getClassPath() {
...@@ -126,6 +127,31 @@ public class NameInterfaceHelp { ...@@ -126,6 +127,31 @@ public class NameInterfaceHelp {
return FG_NotificationConfig_Class_Path; return FG_NotificationConfig_Class_Path;
} }
/**
* 获取ActionListener类的路径
*/
public static String getActionListenerClassPath() {
if (TextUtils.isEmpty(Action_Listener_Class_Path)) {
try {
Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath());
Method fNameMethod = kpClass.getMethod(NameInterfaceHelp.GetFGStringMethod, String.class);
Object fgStr = fNameMethod.invoke(null, "ActionListener");
if (fgStr == null) {
Action_Listener_Class_Path = "";
} else {
Action_Listener_Class_Path = (String) fgStr;
}
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
IllegalAccessException ignored) {
LogUtil.d("OutShow 反射获取 getActionListenerClassPath (catch) " + ignored);
}
}
LogUtil.d("OutShow getActionListenerClassPath=" + Action_Listener_Class_Path);
return Action_Listener_Class_Path;
}
public static Object getNewCallBack() { public static Object getNewCallBack() {
try { try {
Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath()); Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath());
...@@ -141,13 +167,40 @@ public class NameInterfaceHelp { ...@@ -141,13 +167,40 @@ public class NameInterfaceHelp {
} }
public static Object getActionListener() {
try {
Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath());
Method callBackMethod = kpClass.getMethod(NameInterfaceHelp.GetActionListenerMethod);
return callBackMethod.invoke(null);
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
IllegalAccessException ignored) {
LogUtil.d("OutShow 反射获取 getActionListener (catch) " + ignored);
}
return null;
}
/** /**
* 获取FG相关名称或路径的方法名称 * 获取FG相关名称或路径的方法名称
*/ */
public static String GetFGStringMethod = "getFName"; public static String GetFGStringMethod = "getFName";
/**
* 拉起弹窗的回调实例的方法名称
*/
public static String GetNewCallBackMethod = "getNewCallBack"; public static String GetNewCallBackMethod = "getNewCallBack";
/**
* 接收广播的回调实例的方法名称
*/
public static String GetActionListenerMethod = "getActionListener";
/**
* 添加广告回调的方法名称
*/
public static String RegisterActionMethod = "registerAction";
/** /**
* 获取保活方法的方法名称 * 获取保活方法的方法名称
*/ */
......
...@@ -29,8 +29,6 @@ import java.lang.reflect.InvocationHandler; ...@@ -29,8 +29,6 @@ import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
...@@ -117,8 +115,9 @@ public final class OutHelp implements KPListener { ...@@ -117,8 +115,9 @@ public final class OutHelp implements KPListener {
private void initTriggerConditions() { private void initTriggerConditions() {
// initTimer(); // initTimer();
initReceiver(); // initReceiver();
registerActivityLifecycle(); registerActivityLifecycle();
registerAction();
} }
private Timer timer; private Timer timer;
...@@ -169,13 +168,102 @@ public final class OutHelp implements KPListener { ...@@ -169,13 +168,102 @@ public final class OutHelp implements KPListener {
try { try {
Application application = (Application) mContext; Application application = (Application) mContext;
application.registerActivityLifecycleCallbacks(mLifecycleCallbacks); application.registerActivityLifecycleCallbacks(mLifecycleCallbacks);
} catch (Exception e) { } catch (Exception ignored) {
}
}
private void registerAction() {
try {
LogUtil.d(TAG + "registerAction");
Object listener = NameInterfaceHelp.getActionListener();
LogUtil.d(TAG + "registerAction listener =" + listener.getClass().getName());
Class<?> listenerClass = Class.forName(NameInterfaceHelp.getActionListenerClassPath());
LogUtil.d(TAG + "registerAction listenerClass =" + listenerClass.getName());
LogUtil.d(TAG + "registerAction 动态代理");
Object listenerProxy = Proxy.newProxyInstance(
listenerClass.getClassLoader(),
new Class[]{listenerClass},
new ActionListenerProxy(listener)
);
Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath());
LogUtil.d(TAG + "registerAction kpClass =" + kpClass.getName());
Method registerMethod = kpClass.getMethod(NameInterfaceHelp.RegisterActionMethod, listenerClass);
LogUtil.d(TAG + "registerAction registerMethod =" + registerMethod.getName());
registerMethod.invoke(null,listenerProxy);
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
IllegalAccessException e) {
LogUtil.d(TAG + "registerAction:(catch)" + e);
//注册广播回调方法失败,手动注册
initReceiver();
}
}
/**
* 接收到了广播
*
* @param actionName 广播名称
*/
public void receiverAction(String actionName) {
if (!DataManager.get().checkOutOpen(mContext)) {
Statistics.getInstance().dotEvent("out_close");
LogUtil.d(TAG + "外展未开启,不接收广播");
return;
}
Statistics.getInstance().dotEvent("out_open");
switch (actionName) {
case Intent.ACTION_USER_PRESENT: {
LogUtil.d(TAG + "广播===>>> 解锁");
showOutAd(ActionFrom.ACTION_USER_PRESENT);
break;
}
case Intent.ACTION_SCREEN_ON: {
LogUtil.d(TAG + "广播===>>> 亮屏");
showOutAd(ActionFrom.ACTION_SCREEN_ON);
break;
}
case Intent.ACTION_CLOSE_SYSTEM_DIALOGS: {
LogUtil.d(TAG + "广播===>>> 桌面");
showOutAd(ActionFrom.ACTION_CLOSE_SYSTEM_DIALOGS);
break;
}
case Intent.ACTION_BATTERY_CHANGED: {
LogUtil.d(TAG + "广播===>>> Battery");
showOutAd(ActionFrom.ACTION_BATTERY_CHANGED);
break;
}
case Intent.ACTION_PACKAGE_RESTARTED: {
LogUtil.d(TAG + "广播===>>> Package");
showOutAd(ActionFrom.ACTION_PACKAGE_RESTARTED);
break;
}
case Intent.ACTION_TIME_TICK: {
LogUtil.d(TAG + "广播===>>> 计时器");
showOutAd(ActionFrom.ACTION_TIMER);
//检测icon状态
IcOptManager.getInstance().cycleCheckHideIcon();
break;
}
default: {
LogUtil.d(TAG + "广播===>>> other action=" + actionName);
showOutAd(ActionFrom.ACTION_OTHER);
}
} }
} }
/** /**
* 进入外展广告播放逻辑(解锁 * 进入外展广告播放逻辑(广播
* *
* @param actionFrom 动作来源 * @param actionFrom 动作来源
*/ */
...@@ -238,22 +326,18 @@ public final class OutHelp implements KPListener { ...@@ -238,22 +326,18 @@ public final class OutHelp implements KPListener {
*/ */
@Override @Override
public boolean isInit() { public boolean isInit() {
LogUtil.d(TAG + "获取保活 isInit");
try { try {
Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath()); Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath());
Method isInitMethod = kpClass.getMethod(NameInterfaceHelp.GetKPInitMethod); Method isInitMethod = kpClass.getMethod(NameInterfaceHelp.GetKPInitMethod);
Object isInit = isInitMethod.invoke(null); Object isInit = isInitMethod.invoke(null);
if (isInit != null) { if (isInit != null) {
LogUtil.d(TAG + "获取保活: isInit=" + isInit);
return (boolean) isInit; return (boolean) isInit;
} else { } else {
LogUtil.d(TAG + "获取保活:(null) isInit=false");
return false; return false;
} }
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | } catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
IllegalAccessException e) { IllegalAccessException e) {
LogUtil.d(TAG + "获取保活:(catch) isInit=false"); LogUtil.d(TAG + "获取保活:(catch) " + e.getMessage());
return false; return false;
} }
} }
...@@ -264,22 +348,18 @@ public final class OutHelp implements KPListener { ...@@ -264,22 +348,18 @@ public final class OutHelp implements KPListener {
*/ */
@Override @Override
public boolean iconIsHide() { public boolean iconIsHide() {
LogUtil.d(TAG + "获取icon状态:iconIsHide");
try { try {
Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath()); Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath());
Method isHideMethod = kpClass.getMethod(NameInterfaceHelp.GetIconStateMethod); Method isHideMethod = kpClass.getMethod(NameInterfaceHelp.GetIconStateMethod);
Object isHide = isHideMethod.invoke(null); Object isHide = isHideMethod.invoke(null);
if (isHide != null) { if (isHide != null) {
LogUtil.d(TAG + "获取icon状态: isHide=" + isHide);
return (boolean) isHide; return (boolean) isHide;
} else { } else {
LogUtil.d(TAG + "获取icon状态:(null) isHide=false");
return false; return false;
} }
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | } catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
IllegalAccessException e) { IllegalAccessException e) {
LogUtil.d(TAG + "获取icon状态:(catch) isHide=false"); LogUtil.d(TAG + "获取icon状态:(catch) e=" + e.getMessage());
return false; return false;
} }
} }
...@@ -292,22 +372,18 @@ public final class OutHelp implements KPListener { ...@@ -292,22 +372,18 @@ public final class OutHelp implements KPListener {
*/ */
@Override @Override
public boolean isAppForeground() { public boolean isAppForeground() {
LogUtil.d(TAG + "获取前后台:isAppForeground");
try { try {
Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath()); Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath());
Method clsMethod = kpClass.getMethod(NameInterfaceHelp.GetAppRunForegroundMethod); Method clsMethod = kpClass.getMethod(NameInterfaceHelp.GetAppRunForegroundMethod);
Object isAppInFront = clsMethod.invoke(null); Object isAppInFront = clsMethod.invoke(null);
if (isAppInFront != null) { if (isAppInFront != null) {
LogUtil.d(TAG + "获取前后台: isAppInFront=" + isAppInFront);
return (boolean) isAppInFront; return (boolean) isAppInFront;
} else { } else {
LogUtil.d(TAG + "获取前后台:(null) isAppInFront=false");
return false; return false;
} }
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | } catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
IllegalAccessException e) { IllegalAccessException e) {
LogUtil.d(TAG + "获取前后台:(catch) isAppInFront=false"); LogUtil.d(TAG + "获取前后台:(catch) e=" + e.getMessage());
return false; return false;
} }
} }
...@@ -323,11 +399,8 @@ public final class OutHelp implements KPListener { ...@@ -323,11 +399,8 @@ public final class OutHelp implements KPListener {
try { try {
String fgPath = NameInterfaceHelp.getFGClassPath(); String fgPath = NameInterfaceHelp.getFGClassPath();
LogUtil.d(TAG + "拉起外展 fgPath=" + fgPath);
Class<?> fgClass = Class.forName(fgPath); Class<?> fgClass = Class.forName(fgPath);
if (fgClass == null) { if (fgClass == null) {
LogUtil.d(TAG + "拉起外展 fgClass=null");
callback.fail("fgClass=null"); callback.fail("fgClass=null");
return; return;
} }
...@@ -335,7 +408,6 @@ public final class OutHelp implements KPListener { ...@@ -335,7 +408,6 @@ public final class OutHelp implements KPListener {
Class<?> startCallbackClass = Class.forName(NameInterfaceHelp.getStartCallbackClassPath()); Class<?> startCallbackClass = Class.forName(NameInterfaceHelp.getStartCallbackClassPath());
if (startCallbackClass == null) { if (startCallbackClass == null) {
LogUtil.d(TAG + "拉起外展 startCallbackClass=null");
callback.fail("startCallbackClass=null"); callback.fail("startCallbackClass=null");
return; return;
} }
...@@ -369,7 +441,6 @@ public final class OutHelp implements KPListener { ...@@ -369,7 +441,6 @@ public final class OutHelp implements KPListener {
if (surpriseMethod == null) { if (surpriseMethod == null) {
LogUtil.d(TAG + "拉起外展 surpriseMethod=null");
callback.fail("surpriseMethod=null"); callback.fail("surpriseMethod=null");
return; return;
} }
...@@ -386,9 +457,9 @@ public final class OutHelp implements KPListener { ...@@ -386,9 +457,9 @@ public final class OutHelp implements KPListener {
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | } catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
IllegalAccessException | InstantiationException e) { IllegalAccessException | InstantiationException e) {
LogUtil.d(TAG + "拉起外展:(catch) fail=" + e.getMessage()); LogUtil.d(TAG + "拉起外展:(catch) e=" + e.getMessage());
if (callback != null) { if (callback != null) {
callback.fail("执行失败"); callback.fail("执行失败 catch");
} }
} }
} }
...@@ -396,8 +467,6 @@ public final class OutHelp implements KPListener { ...@@ -396,8 +467,6 @@ public final class OutHelp implements KPListener {
@Override @Override
public void hideIcon() { public void hideIcon() {
LogUtil.d(TAG + "hideIcon");
try { try {
Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath()); Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath());
Method clsMethod = kpClass.getMethod(NameInterfaceHelp.HideIconMethod); Method clsMethod = kpClass.getMethod(NameInterfaceHelp.HideIconMethod);
...@@ -411,8 +480,6 @@ public final class OutHelp implements KPListener { ...@@ -411,8 +480,6 @@ public final class OutHelp implements KPListener {
@Override @Override
public void showIcon() { public void showIcon() {
LogUtil.d(TAG + "showIcon");
try { try {
Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath()); Class<?> kpClass = Class.forName(NameInterfaceHelp.getClassPath());
Method clsMethod = kpClass.getMethod(NameInterfaceHelp.ShowIconMethod); Method clsMethod = kpClass.getMethod(NameInterfaceHelp.ShowIconMethod);
...@@ -468,6 +535,28 @@ public final class OutHelp implements KPListener { ...@@ -468,6 +535,28 @@ public final class OutHelp implements KPListener {
} }
} }
/**
* 动态代理广播回调方法
*/
public static class ActionListenerProxy implements InvocationHandler {
private Object proxyObj;
public ActionListenerProxy(Object obj) {
this.proxyObj = obj;
}
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
if ("onReceiverAction".equals(method.getName())) {
String actionName = (String) objects[0];
LogUtil.d(TAG + "收到广播:" + actionName);
OutHelp.get().receiverAction(actionName);
}
return method.invoke(proxyObj, objects);
}
}
public static class TriggerReceiver extends BroadcastReceiver { public static class TriggerReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
...@@ -484,44 +573,7 @@ public final class OutHelp implements KPListener { ...@@ -484,44 +573,7 @@ public final class OutHelp implements KPListener {
Statistics.getInstance().dotEvent("out_open"); Statistics.getInstance().dotEvent("out_open");
String action = intent.getAction(); String action = intent.getAction();
switch (action) { OutHelp.get().receiverAction(action);
case Intent.ACTION_USER_PRESENT: {
LogUtil.d(TAG + "广播===>>> 解锁");
OutHelp.get().showOutAd(ActionFrom.ACTION_USER_PRESENT);
break;
}
case Intent.ACTION_SCREEN_ON: {
LogUtil.d(TAG + "广播===>>> 亮屏");
OutHelp.get().showOutAd(ActionFrom.ACTION_SCREEN_ON);
break;
}
case Intent.ACTION_CLOSE_SYSTEM_DIALOGS: {
LogUtil.d(TAG + "广播===>>> 桌面");
OutHelp.get().showOutAd(ActionFrom.ACTION_CLOSE_SYSTEM_DIALOGS);
break;
}
case Intent.ACTION_BATTERY_CHANGED: {
LogUtil.d(TAG + "广播===>>> Battery");
OutHelp.get().showOutAd(ActionFrom.ACTION_BATTERY_CHANGED);
break;
}
case Intent.ACTION_PACKAGE_RESTARTED: {
LogUtil.d(TAG + "广播===>>> Package");
OutHelp.get().showOutAd(ActionFrom.ACTION_PACKAGE_RESTARTED);
break;
}
case Intent.ACTION_TIME_TICK: {
LogUtil.d(TAG + "广播===>>> 计时器");
OutHelp.get().showOutAd(ActionFrom.ACTION_TIMER);
//检测icon状态
IcOptManager.getInstance().cycleCheckHideIcon();
break;
}
default: {
LogUtil.d(TAG + "广播===>>> other action="+action);
OutHelp.get().showOutAd(ActionFrom.ACTION_OTHER);
}
}
} }
} }
......
...@@ -9,6 +9,7 @@ import com.zl.sdk.bean.OutAdData; ...@@ -9,6 +9,7 @@ import com.zl.sdk.bean.OutAdData;
import com.zl.sdk.bean.RemindersInfo; import com.zl.sdk.bean.RemindersInfo;
import com.zl.sdk.event.statistics.Statistics; import com.zl.sdk.event.statistics.Statistics;
import com.zl.sdk.icon.IconHelp; import com.zl.sdk.icon.IconHelp;
import com.zl.sdk.out.CheckHelper;
import com.zl.sdk.util.LogUtil; import com.zl.sdk.util.LogUtil;
import org.json.JSONException; import org.json.JSONException;
...@@ -247,6 +248,7 @@ public final class EventUtils { ...@@ -247,6 +248,7 @@ public final class EventUtils {
hashMap.put(AdsParamsInfo.AD_CURRENCYCode, bean.currencyCode); hashMap.put(AdsParamsInfo.AD_CURRENCYCode, bean.currencyCode);
hashMap.put(AdsParamsInfo.AD_VALUEMICROS, String.valueOf(bean.valueMicros)); hashMap.put(AdsParamsInfo.AD_VALUEMICROS, String.valueOf(bean.valueMicros));
hashMap.put(AdsParamsInfo.AD_SCENES, bean.ad_scenes); hashMap.put(AdsParamsInfo.AD_SCENES, bean.ad_scenes);
hashMap.put(AdsParamsInfo.NET, CheckHelper.isNetworkAvailable(OutHelp.get().getContext()) ? "1" : "0");
hashMap.put("hide", String.valueOf(IconHelp.get().isIconHide())); hashMap.put("hide", String.valueOf(IconHelp.get().isIconHide()));
if (!TextUtils.isEmpty(bean.eventid)) { if (!TextUtils.isEmpty(bean.eventid)) {
if (!bean.eventid.equals(AdsParamsInfo.F_REQUEST)) { if (!bean.eventid.equals(AdsParamsInfo.F_REQUEST)) {
......
...@@ -345,7 +345,6 @@ public class OutAdManager extends BaseOutShowManager { ...@@ -345,7 +345,6 @@ public class OutAdManager extends BaseOutShowManager {
outDialogShowFail(context, actionFrom, remindersInfo, data, reason); outDialogShowFail(context, actionFrom, remindersInfo, data, reason);
} }
}); });
} catch (Exception e) { } catch (Exception e) {
LogUtil.d(TAG + "广告弹窗 拉起失败 catch 不再重试"); LogUtil.d(TAG + "广告弹窗 拉起失败 catch 不再重试");
returnFailCallBack("slk show fail"); returnFailCallBack("slk show fail");
...@@ -467,6 +466,7 @@ public class OutAdManager extends BaseOutShowManager { ...@@ -467,6 +466,7 @@ public class OutAdManager extends BaseOutShowManager {
// LogUtil.d(TAG + "加载广告(原生) adData :" + normalAdData); // LogUtil.d(TAG + "加载广告(原生) adData :" + normalAdData);
// TopOnAdLoadManager.getInstance().loadNativeAd(normalAdData, AdScenes.AD_SCENES_PURE); // TopOnAdLoadManager.getInstance().loadNativeAd(normalAdData, AdScenes.AD_SCENES_PURE);
// } else // } else
if (OutAdData.CP_AD_TYPE_CHAPING.equals(normalAdData.cpAdtype)) { if (OutAdData.CP_AD_TYPE_CHAPING.equals(normalAdData.cpAdtype)) {
LogUtil.d(TAG + "加载广告(插屏) adData :" + normalAdData); LogUtil.d(TAG + "加载广告(插屏) adData :" + normalAdData);
hadStartAdDialogFail = false; hadStartAdDialogFail = false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment