|
|
@@ -7,24 +7,18 @@ import android.app.PendingIntent;
|
|
|
import android.content.BroadcastReceiver;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
-import android.os.Handler;
|
|
|
-import android.os.Message;
|
|
|
import android.support.v7.app.NotificationCompat;
|
|
|
+import android.util.Log;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
import com.xzjmyk.pm.activity.R;
|
|
|
import com.xzjmyk.pm.activity.bean.oa.Signin;
|
|
|
import com.xzjmyk.pm.activity.db.dao.SigninDao;
|
|
|
import com.xzjmyk.pm.activity.ui.erp.util.CommonUtil;
|
|
|
-import com.xzjmyk.pm.activity.ui.erp.util.ViewUtil;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.util.StringUtils;
|
|
|
import com.xzjmyk.pm.activity.util.TimeUtils;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Calendar;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.Map;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
@@ -32,90 +26,23 @@ import java.util.regex.Pattern;
|
|
|
* AlarmReceiver
|
|
|
*/
|
|
|
public class AlarmReceiver extends BroadcastReceiver {
|
|
|
-
|
|
|
- private Context ct;
|
|
|
- private boolean isU;
|
|
|
-
|
|
|
- private Handler handler = new Handler() {
|
|
|
- @Override
|
|
|
- public void handleMessage(Message msg) {
|
|
|
- if (msg.what == 0x11) {
|
|
|
- String message = msg.getData().getString("result");
|
|
|
- if (JSON.parseObject(message).containsKey("listdata")) {
|
|
|
- JSONArray array = JSON.parseObject(message).getJSONArray("listdata");
|
|
|
- String[] time = new String[array.size()];
|
|
|
- for (int i = 0; i < array.size(); i++) {
|
|
|
- time[i] = array.getJSONObject(i).containsKey("cl_time") ? array.getJSONObject(i).getString("cl_time") : "--";
|
|
|
- }
|
|
|
- //判断是否已经签到过了
|
|
|
- ArrayList<Signin> signins = SigninDao.getInstance(ct).query();
|
|
|
- String str = TimeUtils.long2str(System.currentTimeMillis(), "HH:mm");
|
|
|
- //判断当前时间在哪个区间
|
|
|
- for (Signin e : signins) {
|
|
|
- if (str.compareTo(e.getUsigmin()) > 0 && str.compareTo(e.getDsigmin()) < 0) {
|
|
|
- //当前时间在这个打卡时间之内,判断是否已经打卡
|
|
|
- if (e.getUtime() == null || e.getDtime() == null) {
|
|
|
- //提醒
|
|
|
- sendNotification(ct, isU);
|
|
|
- return;
|
|
|
- }
|
|
|
- if (isU && time.length > 0 && time[0].compareTo(e.getUtime()) < 0) {
|
|
|
- //已经打卡到上班卡了
|
|
|
-
|
|
|
- } else if (!isU && time.length > 0 && time[time.length - 1].compareTo(e.getDtime()) > 0) {
|
|
|
- //已经打卡到下班卡了
|
|
|
- } else {
|
|
|
- //提醒
|
|
|
- sendNotification(ct, isU);
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- };
|
|
|
+ private long newTime = System.currentTimeMillis();//当前时间(先取网络时间在取本地时间)
|
|
|
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
- this.ct = context;
|
|
|
- if (intent.getAction().equals(AlarmaActivity.KEY_ALAR_ACTION)) {//上班签到提醒
|
|
|
- isU = true;
|
|
|
- } else {//下班签到提醒
|
|
|
- isU = true;
|
|
|
+ if (intent.getAction() != null && "ALARMA_ACTION".equals(intent.getAction())) {
|
|
|
+ if (CommonUtil.getSharedPreferencesBoolean(context, "ALARMA_CLICK", false))
|
|
|
+ sendNotification(context);
|
|
|
}
|
|
|
- loadLog(context);
|
|
|
}
|
|
|
|
|
|
|
|
|
- //获取打卡记录 date:yyyy-MM-dd
|
|
|
- private void loadLog(Context ct) {
|
|
|
- //获取网络数据
|
|
|
- String code = CommonUtil.getSharedPreferences(ct, "erp_username");
|
|
|
- String date = TimeUtils.s_long_2_str(System.currentTimeMillis());
|
|
|
- String url = "mobile/common/list.action";
|
|
|
- final Map<String, Object> param = new HashMap<>();
|
|
|
- param.put("currentMaster", CommonUtil.getSharedPreferences(ct, "erp_master"));
|
|
|
- param.put("page", 1);
|
|
|
- param.put("pageSize", 1000);
|
|
|
- param.put("condition", "cl_emcode='" + code + "' and to_char(cl_time,'yyyy-MM-dd')='" + date + "'");
|
|
|
- param.put("caller", "CardLog");
|
|
|
- param.put("emcode", code);
|
|
|
- param.put("sessionId", CommonUtil.getSharedPreferences(ct, "sessionId"));
|
|
|
- LinkedHashMap<String, Object> headers = new LinkedHashMap<>();
|
|
|
- headers.put("Cookie", "JSESSIONID=" + CommonUtil.getSharedPreferences(ct, "sessionId"));
|
|
|
- ViewUtil.httpSendRequest(ct, url, param, handler, headers, 0x11, null, null, "get");
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 签到提醒弹出通知栏
|
|
|
*
|
|
|
* @param context
|
|
|
- * @param isAlar 下一次提醒为上班还是下班(前一次为上班 下一次为下班)
|
|
|
*/
|
|
|
- private void sendNotification(Context context, boolean isAlar) {
|
|
|
+ private void sendNotification(Context context) {
|
|
|
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(
|
|
|
context.getApplicationContext());
|
|
|
@@ -123,46 +50,28 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|
|
builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
|
|
|
builder.setAutoCancel(true);
|
|
|
builder.setContentTitle("签到提醒");
|
|
|
- CharSequence stringExtra = "";
|
|
|
- if (isAlar) {
|
|
|
- stringExtra = context.getResources().getString(R.string.alar_content);
|
|
|
- } else {
|
|
|
- stringExtra = context.getResources().getString(R.string.ualar_content);
|
|
|
- }
|
|
|
+ CharSequence stringExtra = "注意按时打卡";
|
|
|
builder.setContentText(stringExtra);
|
|
|
Intent intent = new Intent(context, SigninActivity.class);
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
builder.setContentIntent(pendingIntent);
|
|
|
mNotificationManager.notify(1000, builder.build());
|
|
|
//设置签到提醒
|
|
|
- initArmmanager(context, isAlar);
|
|
|
+ initArmmanager(context);
|
|
|
}
|
|
|
|
|
|
- private void setArmmanager(Context context, Calendar calendar, boolean isAlar) {
|
|
|
- //操作:发送一个广播,广播接收后Toast提示定时操作完成
|
|
|
- Intent intent = new Intent(context, AlarmReceiver.class);
|
|
|
- String actionArg;
|
|
|
- if (isAlar)
|
|
|
- actionArg = AlarmaActivity.KEY_ALAR_ACTION;
|
|
|
- else
|
|
|
- actionArg = AlarmaActivity.KEY_UALAR_ACTION;
|
|
|
- intent.setAction(actionArg);
|
|
|
- AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
- c.setTimeInMillis(System.currentTimeMillis());
|
|
|
- alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), getPendingIntent(context, intent));
|
|
|
- }
|
|
|
-
|
|
|
- private PendingIntent getPendingIntent(Context context, Intent intent) {
|
|
|
- return PendingIntent.getBroadcast(context, 0, intent, 0);
|
|
|
+ private int getMin(String chche) {
|
|
|
+ if (StringUtils.isEmpty(chche)) return 5;
|
|
|
+ Pattern p = Pattern.compile("(\\d+)");
|
|
|
+ Matcher m = p.matcher(chche);
|
|
|
+ if (m.find()) {
|
|
|
+ return Integer.parseInt(m.group(0));
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 设置下一次提醒
|
|
|
- *
|
|
|
- * @param isAlar 需要提醒为上班还是下班,如果前一次为上班提醒,则为下班,如果前一次为下班,则为下班
|
|
|
- */
|
|
|
- private void initArmmanager(Context ct, boolean isAlar) {
|
|
|
+ //初始化提醒
|
|
|
+ private void initArmmanager(Context ct) {
|
|
|
//1.获取打卡时间
|
|
|
ArrayList<Signin> signins = SigninDao.getInstance(ct).query();
|
|
|
if (signins == null) return;
|
|
|
@@ -170,68 +79,52 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|
|
//获取提前和往后提醒时间
|
|
|
int uTime = getMin(CommonUtil.getSharedPreferences(ct, AlarmaActivity.KEY_ALAR_TIME));
|
|
|
int dTime = getMin(CommonUtil.getSharedPreferences(ct, AlarmaActivity.KEY_UALAR_TIME));
|
|
|
- long utime = System.currentTimeMillis();
|
|
|
- long dtime = System.currentTimeMillis();
|
|
|
- if (uTime != 0) {
|
|
|
- utime = System.currentTimeMillis() - (uTime * 60 * 1000) + (30 * 60 * 1000);
|
|
|
- }
|
|
|
- if (dTime != 0) {
|
|
|
- dtime = System.currentTimeMillis() + (dTime * 60 * 1000) + (30 * 60 * 1000);
|
|
|
- }
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
+ Log.i("gongpengming", "uTime=" + uTime);
|
|
|
+ Log.i("gongpengming", "dTime=" + dTime);
|
|
|
+ long utime = (uTime * 60 * 1000) + (10 * 60 * 1000);//与上班时间提前的时间
|
|
|
+ long dtime = (dTime * 60 * 1000) + (10 * 60 * 1000);//与下班时间延后的时间
|
|
|
long chche = 0;//时间毫秒数
|
|
|
boolean isOk = false;
|
|
|
+ long chchelong = -1;
|
|
|
for (Signin e : signins) {
|
|
|
- if (e.getUtime() != null && e.getUtime().length() > 0)
|
|
|
- chche = TimeUtils.f_str_2_long(TimeUtils.s_long_2_str(System.currentTimeMillis()) + " " + e.getUtime() + ":00");
|
|
|
- if (chche > utime) {//当前时间大于选定的班次的上班时间
|
|
|
+ Log.i("gongpengming", "e.getDtime()=" + e.getDtime() + " e.getUtime()" + e.getUtime());
|
|
|
+ //如果上班时间-提前时间小于当前时间 说明已经过期
|
|
|
+ chche = TimeUtils.f_str_2_long(TimeUtils.s_long_2_str(System.currentTimeMillis()) + " " + e.getUtime() + ":00") - utime;
|
|
|
+ if (newTime < chche) {
|
|
|
+ if (chchelong != -1 && chchelong > chche) {
|
|
|
+ chchelong = chche;
|
|
|
+ }
|
|
|
+ //符合提醒
|
|
|
isOk = true;
|
|
|
- //TODO
|
|
|
- c.setTimeInMillis(chche);
|
|
|
+ //TODO 提醒该时间
|
|
|
+ setArmmanager(ct, chche);
|
|
|
break;
|
|
|
}
|
|
|
- if (e.getDtime() != null && e.getDtime().length() > 0)
|
|
|
- chche = TimeUtils.f_str_2_long(TimeUtils.s_long_2_str(System.currentTimeMillis()) + " " + e.getUtime() + ":00");
|
|
|
- if (chche > dtime) {//当前时间大于选定的班次的下班时间
|
|
|
+ chche = TimeUtils.f_str_2_long(TimeUtils.s_long_2_str(System.currentTimeMillis()) + " " + e.getDtime() + ":00") + dtime;
|
|
|
+ if (newTime < chche) {
|
|
|
+ //TODO 提醒该时间
|
|
|
isOk = true;
|
|
|
- //TODO
|
|
|
- c.setTimeInMillis(chche);
|
|
|
+ setArmmanager(ct, chche);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if (!isOk) {//在班次时间内
|
|
|
- chche = TimeUtils.f_str_2_long(TimeUtils.s_long_2_str(System.currentTimeMillis()) + " " + signins.get(0).getUtime() + ":00");
|
|
|
- chche += 24 * 60 * 60 * 1000;
|
|
|
- c.setTimeInMillis(chche);
|
|
|
- }
|
|
|
- setArmmanager(ct, c, isAlar);
|
|
|
- }
|
|
|
|
|
|
- private int getMin(String chche) {
|
|
|
- if (chche == null || chche.length() <= 0) return 0;
|
|
|
- Pattern p = Pattern.compile("(\\d+)");
|
|
|
- Matcher m = p.matcher(chche);
|
|
|
- if (m.find()) {
|
|
|
- return Integer.parseInt(m.group(0));
|
|
|
+ if (!isOk) {//不在当天允许提醒范围内
|
|
|
+ if (chchelong != -1) {
|
|
|
+ chchelong += 24 * 60 * 60 * 1000;
|
|
|
+ setArmmanager(ct, chchelong);
|
|
|
+ }
|
|
|
}
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
- private int[] getHourAndM(String temp) {
|
|
|
- int[] time = new int[2];
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
- c.setTimeInMillis(TimeUtils.f_str_2_long(temp));
|
|
|
- time[0] = c.get(Calendar.HOUR_OF_DAY);
|
|
|
- time[1] = c.get(Calendar.MINUTE);
|
|
|
- return time;
|
|
|
}
|
|
|
|
|
|
- private String getTimeFormat(int hh, int mm) {
|
|
|
- String h = (hh < 10 ? "0" : "") + hh;
|
|
|
- String m = (mm < 10 ? "0" : "") + mm;
|
|
|
- String temp = TimeUtils.s_long_2_str(System.currentTimeMillis()) + " " + h + ":" + m + ":" + "00";
|
|
|
- return temp;
|
|
|
+ private void setArmmanager(Context ct, long time) {
|
|
|
+ //操作:发送一个广播,广播接收后Toast提示定时操作完成
|
|
|
+ Intent intent = new Intent(ct, AlarmReceiver.class);
|
|
|
+ intent.setAction("ALARMA_ACTION");
|
|
|
+ AlarmManager alarm = (AlarmManager) ct.getSystemService(ct.ALARM_SERVICE);
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTimeInMillis(time);
|
|
|
+ alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), PendingIntent.getBroadcast(ct, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
}
|