private MiuiAdsCell getAdsCellByType(int showType) { MiuiAdsCell cell = new MiuiAdsCell(); switch (showType) { // 每次接受到广告的时候,我们需要进行判断,是否系统禁用了本app 的通知功能 case NotifyAdsDef.ADS_TYPE_BUBBLE: cell = new BubbleAdsCell(); break; case NotifyAdsDef.ADS_TYPE_NOTIFY: cell = new NotifyAdsCell(); break; default: break; } cell.failedCount = mFailedCount; cell.adsJsonString = mAdsJsonString; return cell; }
/** * 匹配广告的分发策略,看是采用主要广告还是候选广告 * * @param adsJsonObject * @return */ private int passAdsDistributePolicy(JSONObject adsJsonObject) { int showType = getShowTypeByJSON(adsJsonObject); // 如果notify 被禁用, 而且首选的是Notify 的话,那么设置这个值 boolean canOnlyBubble = false; boolean mainAdsReachUpperLimit = false; try { Class policyClass = Class.forName("miui.util.NotificationFilterHelper"); Method method = policyClass.getMethod("canSendNotifications", new Class[] {Context.class, String.class}); NotifyAdsManager.showLog(mAppPackageName); // 判断该app 是否收到了通知栏发布限制 canOnlyBubble = (Boolean) method.invoke(null, mContext, mAppPackageName); canOnlyBubble = !canOnlyBubble; } catch (Exception e) { Log.d("NotifyAdsDownloader", "reflect errors!"); e.printStackTrace(); } NotifyAdsManager.showLog("是否禁用了通知栏广告 " + canOnlyBubble); int upperBound = adsJsonObject.optInt(NotifyAdsDef.JSON_TAG_UPPERBOUND); // 如果是在白名单里面的用户,那么upperBound 将会设置为0 , if (upperBound > 0) { mainAdsReachUpperLimit = !passLimitConstrain(upperBound, showType); } else { mainAdsReachUpperLimit = false; } NotifyAdsManager.showLog("是否达到上限 " + mainAdsReachUpperLimit); try { if (mainAdsReachUpperLimit || (showType == NotifyAdsDef.ADS_TYPE_NOTIFY && canOnlyBubble)) { // 否则,我们需要得到 subAdInfo 的信息 NotifyAdsManager.showLog("使用候选广告 "); long subAdId = adsJsonObject.optLong(NotifyAdsDef.JSON_TAG_SUBADID); if (subAdId <= 0) { NotifyAdsManager.showLog("没有候选广告 "); return NotifyAdsDef.RET_NOTMATCHED; } String subAdInfoString = adsJsonObject.optString(NotifyAdsDef.JSON_TAG_SUBADINFO); JSONObject subAdJson = new JSONObject(subAdInfoString); int subShowType = getShowTypeByJSON(subAdJson); if (subShowType == NotifyAdsDef.ADS_TYPE_NOTIFY && canOnlyBubble) { return NotifyAdsDef.RET_NOT_PERMITTED; } int ret = passFilterRules(subAdJson); NotifyAdsManager.showLog("候选广告解析参数并检查: " + ret); if (ret != NotifyAdsDef.RET_OK) return ret; mAdsCell = getAdsCellByType(subShowType); mAdsCell.setValuesByJson(subAdJson); } else { NotifyAdsManager.showLog("使用主广告 "); mAdsCell = getAdsCellByType(showType); mAdsCell.setValuesByJson(adsJsonObject); } return NotifyAdsDef.RET_OK; } catch (JSONException e) { return NotifyAdsDef.RET_ERROR; } }