|  |  |  | 
|---|
|  |  |  | 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.OrderFragment; | 
|---|
|  |  |  | 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 | 
|---|
|  |  |  | 
|---|
|  |  |  | super.onCreate(savedInstanceState); | 
|---|
|  |  |  | binding = ActivityMainBinding.inflate(LayoutInflater.from(this)); | 
|---|
|  |  |  | setContentView(binding.getRoot()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | setupFragments(); | 
|---|
|  |  |  | initView(); | 
|---|
|  |  |  | initTab(); | 
|---|
|  |  |  | mqttManager = new MqttManager(this); | 
|---|
|  |  |  | mqttManager.connect(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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() { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void initTab() { | 
|---|
|  |  |  | TabAdapter adapter = new TabAdapter(getSupportFragmentManager(), fragments); | 
|---|
|  |  |  | TabAdapter adapter = new TabAdapter(this, fragments); | 
|---|
|  |  |  | binding.viewPager.setAdapter(adapter); | 
|---|
|  |  |  | binding.viewPager.setPagingEnabled(false); | 
|---|
|  |  |  | binding.viewPager.setCurrentItem(1, false); // 默认显示地图页 | 
|---|
|  |  |  | binding.viewPager.setOffscreenPageLimit(fragments.size()); | 
|---|
|  |  |  | binding.viewPager.setCurrentItem(1); // 默认显示地图页 | 
|---|
|  |  |  | binding.viewPager.setUserInputEnabled(false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | protected void onDestroy() { | 
|---|
|  |  |  | super.onDestroy(); | 
|---|
|  |  |  | //关闭MQ | 
|---|
|  |  |  | mqttManager.disconnect(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | //关闭MQ | 
|---|
|  |  |  | mqttManager.disconnect(); | 
|---|
|  |  |  | unregisterNetworkCallback(); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * 更新某个 Tab 的 UI 状态 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void updateTabUI(int position, int iconResId, int textColorResId) { | 
|---|
|  |  |  | binding.viewPager.setCurrentItem(position); | 
|---|
|  |  |  | 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.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); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|