Commit ef3fc397 by wjg

修改推送逻辑

parent 5082450e
......@@ -2,19 +2,17 @@ package com.ihaoin.hooloo.device.component;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.fasterxml.jackson.databind.JsonNode;
import com.ihaoin.hooloo.device.HLApplication;
import com.ihaoin.hooloo.device.config.AppConfig;
import com.ihaoin.hooloo.device.network.HttpUtil;
import com.ihaoin.hooloo.device.util.JsonUtils;
import com.ihaoin.hooloo.device.util.StringUtils;
import com.ihaoin.hooloo.device.util.Utils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
......@@ -24,16 +22,6 @@ import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;
public class PushMessageReceiver extends JPushMessageReceiver {
private static Map<String, Handler> subscribers = new HashMap<>();
public static void subscribe(String key, Handler handler) {
subscribers.put(key, handler);
}
public static void unsubscribe(String key) {
subscribers.remove(key);
}
@Override
public void onMessage(Context context, CustomMessage customMessage) {
Log.e(AppConfig.TAG_PUSH, "[onMessage] " + customMessage);
......@@ -46,24 +34,14 @@ public class PushMessageReceiver extends JPushMessageReceiver {
}
private void dispatchMsg(CustomMessage customMessage) throws IOException {
// if (CollectionUtils.isEmpty(subscribers)) {
// Log.e(AppConfig.DEBUG_TAG_PUSH,"msg subscribers is empty");
// return;
// }
JsonNode jsonNode = JsonUtils.getMapper().readValue(customMessage.message, JsonNode.class);
String action = jsonNode.get("action").asText();
if (StringUtils.isEmpty(action)) {
Log.e(AppConfig.TAG_PUSH, "msg action is empty");
return;
}
Handler handler = subscribers.get(action);
if (handler == null) {
Log.e(AppConfig.TAG_PUSH, String.format("msg action[%s] subscribers is empty", action));
return;
}
JsonNode data = jsonNode.get("data");
Message msg = handler.obtainMessage(1, JsonUtils.getMapper().writeValueAsString(data));
handler.sendMessage(msg);
Utils.fastPushBroadcast(HLApplication.SELF, action, jsonNode.get("data") == null ? null : jsonNode.get("data").toString());
}
@Override
......
......@@ -2,7 +2,7 @@ package com.ihaoin.hooloo.device.config;
public class AppConfig {
public static Boolean DEBUG = false; // TODO false
public static Boolean DEBUG = true; // TODO false
public static String HL = "HL";
public static String TAG = HL + "_TEST";
public static String TAG_PUSH = HL + "_PUSH";
......
......@@ -4,6 +4,7 @@ public class Base {
public static final Integer TRUE = 1;
public static final Integer FALSE = 0;
public static final String PUSH_DEFAULT_EXTRA = "PUSH_DEFAULT_EXTRA";
public static final String PUSH_ACTION_ORDER_STATE_CHANGED = "ORDER_STATE_CHANGED";
public static final String PUSH_ACTION_SKU_STATE_CHANGED = "SKU_STATE_CHANGED";
public static final String PUSH_ACTION_QRCODE_VISITED = "QRCODE_VISITED";
......
......@@ -22,4 +22,15 @@ public class JsonUtils {
return null;
}
}
public static String toString(Object obj) {
try {
if (obj == null) {
return null;
}
return JsonUtils.getMapper().writeValueAsString(obj);
} catch (Exception e) {
return null;
}
}
}
......@@ -45,6 +45,13 @@ public class Utils {
context.sendBroadcast(intent);
}
public static void fastPushBroadcast(Context context, String action, String content) {
Intent intent = new Intent();
intent.setAction(action);
intent.putExtra(Base.PUSH_DEFAULT_EXTRA, content);
context.sendBroadcast(intent);
}
public static void sendAddGoodsBroadcast(Context context, Goods goods, Sku sku, Integer count) {
Intent intent = new Intent();
intent.setAction(Base.BROADCAST_ACTION_ADD_GOODS);
......
package com.ihaoin.hooloo.device.view;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
......@@ -23,7 +26,6 @@ import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.ihaoin.hooloo.device.R;
import com.ihaoin.hooloo.device.component.NetworkHandler;
import com.ihaoin.hooloo.device.component.PushMessageReceiver;
import com.ihaoin.hooloo.device.config.AppConfig;
import com.ihaoin.hooloo.device.config.Base;
import com.ihaoin.hooloo.device.data.TrolleyGoods;
......@@ -184,17 +186,13 @@ public class ConfirmOrderDialog extends Dialog {
confirmOrderHandler.removeCallbacksAndMessages(null);
confirmOrderHandler = null;
}
if (qrCodeVisitedHandler != null) {
qrCodeVisitedHandler.removeCallbacksAndMessages(null);
qrCodeVisitedHandler = null;
}
unregsitQRCodeVisitedListener();
unregsitQRCodeVisitedReceiver();
}
};
/** 等待扫码倒计时 */
private void startExpireThread() {
regsitQRCodeVisitedListener();
regsitQRCodeVisitedReceiver();
expireThread = new Thread(() -> {
try {
for (int i = EXPIRE_TIMES; i >= 0; i--) {
......@@ -308,28 +306,30 @@ public class ConfirmOrderDialog extends Dialog {
return bitmap;
}
private QRCodeVisitedHandler qrCodeVisitedHandler;
private QRCodeVisitedReceiver qrCodeVisitedReceiver;
class QRCodeVisitedHandler extends Handler {
class QRCodeVisitedReceiver extends BroadcastReceiver {
@Override
public void handleMessage(@NonNull Message msg) {
if (msg.what != 1) {
return;
}
public void onReceive(Context context, Intent intent) {
expireThread.interrupt();
showSucceedViews();
startReturnThread();
unregsitQRCodeVisitedListener();
unregsitQRCodeVisitedReceiver();
}
}
private void regsitQRCodeVisitedListener() {
qrCodeVisitedHandler = new QRCodeVisitedHandler();
PushMessageReceiver.subscribe(Base.PUSH_ACTION_QRCODE_VISITED, qrCodeVisitedHandler);
private void regsitQRCodeVisitedReceiver() {
qrCodeVisitedReceiver = new QRCodeVisitedReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Base.PUSH_ACTION_QRCODE_VISITED);
getContext().registerReceiver(qrCodeVisitedReceiver, filter);
}
private void unregsitQRCodeVisitedListener() {
PushMessageReceiver.unsubscribe(Base.PUSH_ACTION_QRCODE_VISITED);
private void unregsitQRCodeVisitedReceiver() {
if (qrCodeVisitedReceiver != null) {
getContext().unregisterReceiver(qrCodeVisitedReceiver);
qrCodeVisitedReceiver = null;
}
}
/** 将购物车列表按商品id分组保存 */
......
......@@ -26,12 +26,14 @@ import com.ihaoin.hooloo.device.R;
import com.ihaoin.hooloo.device.component.AutoWrapLayout;
import com.ihaoin.hooloo.device.component.IntroViewPager;
import com.ihaoin.hooloo.device.component.NetworkHandler;
import com.ihaoin.hooloo.device.config.AppConfig;
import com.ihaoin.hooloo.device.config.Base;
import com.ihaoin.hooloo.device.data.enums.SkuState;
import com.ihaoin.hooloo.device.data.vo.Goods;
import com.ihaoin.hooloo.device.data.vo.GoodsRule;
import com.ihaoin.hooloo.device.data.vo.Sku;
import com.ihaoin.hooloo.device.data.vo.SkuRule;
import com.ihaoin.hooloo.device.data.vo.SkuStateChangedVo;
import com.ihaoin.hooloo.device.network.HttpUtil;
import com.ihaoin.hooloo.device.util.CollectionUtils;
import com.ihaoin.hooloo.device.util.JsonUtils;
......@@ -327,6 +329,35 @@ public class GoodsDetailDialog extends Dialog {
private void setGoods(Goods rspGoods) {
this.mGoods = rspGoods;
if (this.mGoods != null) {
Map<Integer, List<Sku>> map = mGoods.getSkus().stream().collect(Collectors.groupingBy(Sku::getState));
if (CollectionUtils.isEmpty(map)) {
return;
}
// 售罄SKU列表
List<Sku> sellOuts = map.get(SkuState.SELLOUT.getCode());
// 未售罄SKU列表
List<Sku> sellIns = map.get(SkuState.SELLIN.getCode());
if (!CollectionUtils.isEmpty(sellOuts)) {
List<Integer> ids = sellOuts.stream().map(Sku::getSkuId).collect(Collectors.toList());
SkuStateChangedVo changedVo = new SkuStateChangedVo();
changedVo.setMachineCode(AppConfig.MACHINE_CODE);
changedVo.setGoodsId(mGoods.getGoodsId());
changedVo.setState(SkuState.SELLOUT.getCode());
changedVo.setSkuIds(ids);
Utils.fastPushBroadcast(getContext(), Base.PUSH_ACTION_SKU_STATE_CHANGED, JsonUtils.toString(changedVo));
}
if (!CollectionUtils.isEmpty(sellIns)) {
List<Integer> ids = sellIns.stream().map(Sku::getSkuId).collect(Collectors.toList());
SkuStateChangedVo changedVo = new SkuStateChangedVo();
changedVo.setMachineCode(AppConfig.MACHINE_CODE);
changedVo.setGoodsId(mGoods.getGoodsId());
changedVo.setState(SkuState.SELLIN.getCode());
changedVo.setSkuIds(ids);
Utils.fastPushBroadcast(getContext(), Base.PUSH_ACTION_SKU_STATE_CHANGED, JsonUtils.toString(changedVo));
}
}
processSpecRules();
buildSpecs();
}
......
......@@ -10,7 +10,6 @@ import android.graphics.Rect;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
......@@ -31,7 +30,6 @@ import com.ihaoin.hooloo.device.R;
import com.ihaoin.hooloo.device.adapter.GoodsAdapter;
import com.ihaoin.hooloo.device.adapter.RecommendAdapter;
import com.ihaoin.hooloo.device.component.NetworkHandler;
import com.ihaoin.hooloo.device.component.PushMessageReceiver;
import com.ihaoin.hooloo.device.component.SettingsQueue;
import com.ihaoin.hooloo.device.config.AppConfig;
import com.ihaoin.hooloo.device.config.Base;
......@@ -54,8 +52,8 @@ import java.util.ArrayList;
import java.util.List;
public class LauncherActivity extends Activity {
private SkuStateChangeHandler skuStateChangeHandler;
private GoodsChangeHandler goodsChangeHandler;
private SkuStateChangeReceiver skuChangeReceiver;
private GoodsChangeReceiver goodsChangeReceiver;
private RadioGroup groupCategory;
private RecyclerView recGoods;
......@@ -75,7 +73,6 @@ public class LauncherActivity extends Activity {
private List<Integer> tPosition = new ArrayList<>();
//记录商品当前可见的第一个item的position
private int first = 0;
private Context mContext;
private TrolleyView trolleyView;
private MenuPresentation menu;
......@@ -84,7 +81,6 @@ public class LauncherActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
mContext = this;
requestPermissions();
......@@ -108,8 +104,8 @@ public class LauncherActivity extends Activity {
registTrolleyStateChangedReceiver();
registSettingChangedReceiver();
prepareMenuView();
regsitSkuChangeListener();
regsitGoodsChangeListener();
regsitSkuChangeReceiver();
regsitGoodsChangeReceiver();
startLoadDataThread();
initSettingsQueue();
......@@ -117,7 +113,7 @@ public class LauncherActivity extends Activity {
private void initSettingsQueue() {
View imgLogo = findViewById(R.id.img_logo);
new SettingsQueue(mContext, imgLogo, txtCategory);
new SettingsQueue(this, imgLogo, txtCategory);
}
private void setMachineCode() {
......@@ -129,7 +125,7 @@ public class LauncherActivity extends Activity {
}
private void initRecommends() {
recommendManager = new GridLayoutManager(mContext, 2);
recommendManager = new GridLayoutManager(this, 2);
recommendManager.setSpanSizeLookup(new MySpanSizeLookup());
recRecommends.addItemDecoration(new MyItemDecoration());
......@@ -184,7 +180,7 @@ public class LauncherActivity extends Activity {
}
private void initGoods() {
goodsLayoutManager = new LinearLayoutManager(mContext);
goodsLayoutManager = new LinearLayoutManager(this);
if (goodsAdapter == null) {
goodsAdapter = new GoodsAdapter(this, R.layout.item_goods, R.layout.item_goods_category);
recGoods.setAdapter(goodsAdapter);
......@@ -227,15 +223,15 @@ public class LauncherActivity extends Activity {
int nextPosition = firstPosition + 1; // 当前显示的下一个索引
if (nextPosition < scrollGoods.size()) { // 已经是最后一个item
if (scrollGoods.get(nextPosition).isHeader) { // 下一个是否是标题
int paddingTop = Utils.getDimens(mContext, R.dimen.goods_padding_top);
int paddingTop = Utils.getDimens(LauncherActivity.this, R.dimen.goods_padding_top);
View nextView = goodsLayoutManager.findViewByPosition(nextPosition); // 下一个view
int[] location = new int[2];
nextView.getLocationInWindow(location);
int topMargin = location[1] -paddingTop;
int topMargin = location[1] - paddingTop;
int height = nextView.getHeight();
// Log.d(AppConfig.TAG, String.format("c:%s, n:%s, 0:%s, 1:%s", firstPosition, nextPosition, rect[0], rect[1]));
if (topMargin < height) {
lp.setMargins(0, -(height - topMargin ), 0, 0);
lp.setMargins(0, -(height - topMargin), 0, 0);
}
}
}
......@@ -406,8 +402,8 @@ public class LauncherActivity extends Activity {
this.unregistTrolleyGoodsChangedReceiver();
this.unregistTrolleyStateChangedReceiver();
this.unregistSettingChangedReceiver();
this.unregsitSkuChangeListener();
this.unregsitGoodsChangeListener();
this.unregsitSkuChangeReceiver();
this.unregsitGoodsChangeReceiver();
System.exit(0);
}
......@@ -467,7 +463,7 @@ public class LauncherActivity extends Activity {
private void unregistTrolleyStateChangedReceiver() {
if (trolleyStateChangedReceiver != null) {
mContext.unregisterReceiver(trolleyStateChangedReceiver);
this.unregisterReceiver(trolleyStateChangedReceiver);
trolleyStateChangedReceiver = null;
}
}
......@@ -490,22 +486,6 @@ public class LauncherActivity extends Activity {
}
}
private void registSettingChangedReceiver() {
settingChangedReceiver = new SettingChangedReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Base.BROADCAST_ACTION_MACHINE_CODE_CHANGED);
filter.addAction(Base.BROADCAST_ACTION_MENU_STATE_CHANGED);
filter.addAction(Base.BROADCAST_ACTION_LOGCAT_STATE_CHANGED);
this.registerReceiver(settingChangedReceiver, filter);
}
private void unregistSettingChangedReceiver() {
if (settingChangedReceiver != null) {
mContext.unregisterReceiver(settingChangedReceiver);
settingChangedReceiver = null;
}
}
private class SettingChangedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
......@@ -570,33 +550,55 @@ public class LauncherActivity extends Activity {
}
}
private void regsitSkuChangeListener() {
skuStateChangeHandler = new SkuStateChangeHandler();
PushMessageReceiver.subscribe(Base.PUSH_ACTION_SKU_STATE_CHANGED, skuStateChangeHandler);
private void registSettingChangedReceiver() {
settingChangedReceiver = new SettingChangedReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Base.BROADCAST_ACTION_MACHINE_CODE_CHANGED);
filter.addAction(Base.BROADCAST_ACTION_MENU_STATE_CHANGED);
filter.addAction(Base.BROADCAST_ACTION_LOGCAT_STATE_CHANGED);
this.registerReceiver(settingChangedReceiver, filter);
}
private void unregistSettingChangedReceiver() {
if (settingChangedReceiver != null) {
this.unregisterReceiver(settingChangedReceiver);
settingChangedReceiver = null;
}
}
private void regsitSkuChangeReceiver() {
skuChangeReceiver = new SkuStateChangeReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Base.PUSH_ACTION_SKU_STATE_CHANGED);
this.registerReceiver(skuChangeReceiver, filter);
}
private void unregsitSkuChangeListener() {
PushMessageReceiver.unsubscribe(Base.PUSH_ACTION_SKU_STATE_CHANGED);
private void unregsitSkuChangeReceiver() {
if (skuChangeReceiver != null) {
this.unregisterReceiver(skuChangeReceiver);
skuChangeReceiver = null;
}
}
private void regsitGoodsChangeListener() {
goodsChangeHandler = new GoodsChangeHandler();
PushMessageReceiver.subscribe(Base.PUSH_ACTION_GOODS_CHANGED, goodsChangeHandler);
private void regsitGoodsChangeReceiver() {
goodsChangeReceiver = new GoodsChangeReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Base.PUSH_ACTION_GOODS_CHANGED);
this.registerReceiver(goodsChangeReceiver, filter);
}
private void unregsitGoodsChangeListener() {
PushMessageReceiver.unsubscribe(Base.PUSH_ACTION_GOODS_CHANGED);
private void unregsitGoodsChangeReceiver() {
if (goodsChangeReceiver != null) {
this.unregisterReceiver(goodsChangeReceiver);
goodsChangeReceiver = null;
}
}
class SkuStateChangeHandler extends Handler {
class SkuStateChangeReceiver extends BroadcastReceiver {
@Override
public void handleMessage(@NonNull Message msg) {
public void onReceive(Context context, Intent intent) {
try {
super.handleMessage(msg);
if (msg.obj == null) {
return;
}
String msgBody = msg.obj.toString();
String msgBody = intent.getStringExtra(Base.PUSH_DEFAULT_EXTRA);
if (StringUtils.isEmpty(msgBody)) {
return;
}
......@@ -658,10 +660,9 @@ public class LauncherActivity extends Activity {
}
}
class GoodsChangeHandler extends Handler {
class GoodsChangeReceiver extends BroadcastReceiver {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
public void onReceive(Context context, Intent intent) {
loadData();
}
}
......
package com.ihaoin.hooloo.device.view;
import android.app.Presentation;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.widget.GridView;
......@@ -16,7 +18,6 @@ import com.ihaoin.hooloo.device.HLApplication;
import com.ihaoin.hooloo.device.R;
import com.ihaoin.hooloo.device.adapter.OrderAdapter;
import com.ihaoin.hooloo.device.component.NetworkHandler;
import com.ihaoin.hooloo.device.component.PushMessageReceiver;
import com.ihaoin.hooloo.device.config.Base;
import com.ihaoin.hooloo.device.data.MainData;
import com.ihaoin.hooloo.device.data.enums.ImageState;
......@@ -39,7 +40,7 @@ public class MenuPresentation extends Presentation {
private List<OrderPkg> pkgs;
private LoadOrdersHandler loadOrdersHandler;
private OrderStateChangeHandler orderStateChangeHandler;
private OrderStateChangeReceiver orderStateChangeReceiver;
private List<Order> cookingOrders;
private List<Order> waitingOrders;
......@@ -148,12 +149,14 @@ public class MenuPresentation extends Presentation {
completedAdapter.notifyDataSetChanged();
showImages();
regsitOrderChangeListener();
regsitOrderChangeReceiver();
}
private void regsitOrderChangeListener() {
orderStateChangeHandler = new OrderStateChangeHandler();
PushMessageReceiver.subscribe(Base.PUSH_ACTION_ORDER_STATE_CHANGED, orderStateChangeHandler);
private void regsitOrderChangeReceiver() {
orderStateChangeReceiver = new OrderStateChangeReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Base.PUSH_ACTION_ORDER_STATE_CHANGED);
getContext().registerReceiver(orderStateChangeReceiver, filter);
}
private List<Order> getOrders(Integer state) {
......@@ -190,15 +193,11 @@ public class MenuPresentation extends Presentation {
}
}
class OrderStateChangeHandler extends Handler {
class OrderStateChangeReceiver extends BroadcastReceiver {
@Override
public void handleMessage(@NonNull Message msg) {
public void onReceive(Context context, Intent intent) {
try {
super.handleMessage(msg);
if (msg.obj == null) {
return;
}
String msgBody = msg.obj.toString();
String msgBody = intent.getStringExtra(Base.PUSH_DEFAULT_EXTRA);
if (StringUtils.isEmpty(msgBody)) {
return;
}
......
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