管灌系统巡查员智能手机App
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package com.dayu.pipirrapp.activity;
 
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
 
import com.dayu.pipirrapp.MyApplication;
import com.dayu.pipirrapp.R;
import com.dayu.pipirrapp.adapter.TabAdapter;
import com.dayu.pipirrapp.bean.db.TagBean;
import com.dayu.pipirrapp.bean.net.OrderDetailResult;
import com.dayu.pipirrapp.dao.DaoSingleton;
import com.dayu.pipirrapp.databinding.ActivityMainBinding;
import com.dayu.pipirrapp.fragment.MapFragment;
import com.dayu.pipirrapp.fragment.MyFragment;
import com.dayu.pipirrapp.fragment.OrderFragment;
import com.dayu.pipirrapp.net.MqttManager;
import com.dayu.pipirrapp.tool.InspectionUtils;
import com.dayu.pipirrapp.utils.CommonKeyName;
import com.dayu.pipirrapp.utils.MyLog;
import com.dayu.pipirrapp.utils.NetUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 首页
 */
public class MainActivity extends AppCompatActivity {
    private static final String CHANNEL_ID = "order_channel";
    private ActivityMainBinding binding;
    private List<Fragment> fragments = new ArrayList<>();
    private long mExitTime;
    MqttManager mqttManager;
    public volatile static Map<String, String> workerIddata = new HashMap<>();
 
 
    private enum Tab {
        ORDER, MAP, MY
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());
        setupFragments();
        initView();
        initTab();
 
        try {
            TagBean tagBean = DaoSingleton.getInstance(this).tagDao().findFirst();
            MyApplication.myApplication.myTag = tagBean.getTag();
        } catch (Exception e) {
            e.printStackTrace();
        }
        mqttManager = new MqttManager(this, this);
        LiveEventBus.get(CommonKeyName.NetworkCallback).observeForever(new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                switch ((int) o) {
                    case NetUtils.Available:
                        MyLog.i("MqttManager>>>Lost");
                        InspectionUtils.aginPutInspectionData(MainActivity.this);
                        break;
                    case NetUtils.Lost:
 
                        break;
                }
            }
        });
        LiveEventBus.get(CommonKeyName.RedLotRefresh).observeForever(new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                if (o instanceof Boolean) {
                    if ((boolean) o) {
                        binding.redDotImg.setVisibility(View.GONE);
                        workerIddata.clear();
                    }
                } else if (o instanceof String) {
                    workerIddata.remove(o);
                    if (workerIddata.size() == 0) {
                        binding.redDotImg.setVisibility(View.GONE);
                    }
                }
 
            }
        });
        LiveEventBus.get(CommonKeyName.CreateNotification).observeForever(new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                if (o instanceof OrderDetailResult) {
                    OrderDetailResult orderDetailResult = ((OrderDetailResult) o);
                    workerIddata.put(orderDetailResult.getWorkOrderId(), orderDetailResult.getWorkOrderId());
                    binding.redDotImg.setVisibility(View.VISIBLE);
                    creatOrderNotification(orderDetailResult.getWorkOrderId().hashCode(), orderDetailResult.getTaskType(), orderDetailResult.getWorkOrderId());
                }
 
 
            }
        });
        registNetCallBack();
    }
 
    @Override
    protected void onStart() {
        super.onStart();
 
    }
 
    private void setupFragments() {
        fragments.add(new OrderFragment());
        fragments.add(new MapFragment());
        fragments.add(new MyFragment());
    }
 
    private void initView() {
        binding.orderLL.setOnClickListener(v -> changeBottomState(Tab.ORDER));
        binding.mapLL.setOnClickListener(v -> changeBottomState(Tab.MAP));
        binding.myLL.setOnClickListener(v -> changeBottomState(Tab.MY));
    }
 
    private void initTab() {
        TabAdapter adapter = new TabAdapter(this, fragments);
        binding.viewPager.setAdapter(adapter);
        binding.viewPager.setCurrentItem(1, false); // 默认显示地图页
        binding.viewPager.setOffscreenPageLimit(fragments.size());
        binding.viewPager.setUserInputEnabled(false);
    }
 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
 
        try {
            //关闭MQ
            mqttManager.disconnect();
            unregisterNetworkCallback();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 修改底部状态
     */
    private void changeBottomState(Tab tab) {
        resetTabState();
        switch (tab) {
            case ORDER:
                updateTabUI(0, R.drawable.bottom_order_white, R.color.white);
                break;
            case MAP:
                updateTabUI(1, R.drawable.bottom_map_white, R.color.white);
                break;
            case MY:
                updateTabUI(2, R.drawable.bottom_my_white, R.color.white);
                break;
        }
    }
 
    /**
     * 更新某个 Tab 的 UI 状态
     */
    private void updateTabUI(int position, int iconResId, int textColorResId) {
        if (position == 1) {
            binding.viewPager.setCurrentItem(position, true);
        } else {
            binding.viewPager.setCurrentItem(position, false);
        }
 
        switch (position) {
            case 0:
                binding.orderImg.setImageDrawable(ContextCompat.getDrawable(this, iconResId));
                binding.orderText.setTextColor(ContextCompat.getColor(this, textColorResId));
                break;
            case 1:
                binding.mapImg.setImageDrawable(ContextCompat.getDrawable(this, iconResId));
                binding.mapText.setTextColor(ContextCompat.getColor(this, textColorResId));
                break;
            case 2:
                binding.myImg.setImageDrawable(ContextCompat.getDrawable(this, iconResId));
                binding.myText.setTextColor(ContextCompat.getColor(this, textColorResId));
                break;
        }
    }
 
    /**
     * 重置所有 Tab 的默认状态
     */
    private void resetTabState() {
        binding.orderImg.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.bottom_order_black));
        binding.orderText.setTextColor(ContextCompat.getColor(this, R.color.black));
 
        binding.mapImg.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.bottom_map_black));
        binding.mapText.setTextColor(ContextCompat.getColor(this, R.color.black));
 
        binding.myImg.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.bottom_my_black));
        binding.myText.setTextColor(ContextCompat.getColor(this, R.color.black));
    }
 
    //    注册网络监控
    private void registNetCallBack() {
        NetUtils.registerNetCallBack(this);
    }
 
    private void unregisterNetworkCallback() {
        NetUtils.unregisterReceiver(this);
    }
 
    /**
     * 创建工单提示通知
     *
     * @param notifucId
     * @param data
     */
    private void creatOrderNotification(int notifucId, String data, String workOrderId) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 创建NotificationChannel(仅针对Android O及以上版本)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // 注册通道
            notificationManager.createNotificationChannel(channel);
        }
 
        Intent notificationIntent = new Intent(this, OrderDetailActivity.class);
        notificationIntent.putExtra("workOrderId", workOrderId);
        int requestCode = workOrderId.hashCode(); // 使用workOrderId的哈希码作为requestCode
        PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, notificationIntent, PendingIntent.FLAG_MUTABLE);
 
        // 创建震动效果
        // 获取 Vibrator 系统服务
        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        long[] vibrationPattern = {0, 1000, 500, 1000}; // 震动模式:等待0ms,震动1000ms,等待500ms,震动1000ms
        int repeat = -1; // 不重复
 
        // 对于Android 26及以上版本,使用VibrationEffect
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            VibrationEffect effect = VibrationEffect.createWaveform(vibrationPattern, repeat);
            vibrator.vibrate(effect);
        } else {
            // 对于Android 25及以下版本,使用旧的vibrate方法
            vibrator.vibrate(vibrationPattern, repeat);
        }
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("新工单")
                .setContentText(data)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true) // 点击通知后自动消失
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)// 使用默认的音效和震动
                .setVibrate(vibrationPattern)
                .build();
        notificationManager.notify(notifucId, notification);
 
    }
 
}