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);
|
|
}
|
|
}
|