package com.basic.security.manager;
|
|
import android.database.Cursor;
|
import android.text.TextUtils;
|
import android.widget.TextView;
|
|
import com.basic.security.activity.MainActivity;
|
import com.basic.security.base.BaseApplication;
|
import com.basic.security.dao.DatabaseManager;
|
import com.basic.security.dao.SqliteManager;
|
import com.basic.security.fragment.HomeFragment;
|
import com.basic.security.fragment.helper.HomeGridViewAdapter;
|
import com.basic.security.model.Alarm;
|
import com.basic.security.utils.Constants;
|
import com.basic.security.utils.FrameUtil;
|
import com.basic.security.utils.VideoPath;
|
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.HashSet;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.ReentrantLock;
|
|
public class AlarmManager extends SqliteManager {
|
|
public static List<Map<String, String>> sqliteAlarmList = new ArrayList<>();
|
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
public static Map<String, String> findById(String id) {
|
Cursor cursor = null;
|
try {
|
cursor = DatabaseManager.getDatabase().rawQuery("select * from alarm where id = '" + id + "'", null);
|
if (cursor.moveToFirst()) {
|
return cursorToModelAdapter(cursor, "alarm");
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (cursor != null)
|
cursor.close();
|
}
|
return null;
|
}
|
|
public static void renameKeys(List<Map<String, String>> listApiModel) {
|
for (Map<String, String> apiModel : listApiModel) {
|
for (Map.Entry<String, String> entry : apiModel.entrySet()) {
|
String value = entry.getValue();
|
if (value != null) {
|
if (value.startsWith("\"")) {
|
value = value.substring(1);
|
}
|
if (value.endsWith("\"")) {
|
value = value.substring(0, value.length() - 1);
|
}
|
apiModel.put(entry.getKey(), value);
|
}
|
}
|
}
|
for (Map<String, String> apiModel : listApiModel) {
|
apiModel.put("table", "alarm");
|
String picDate = apiModel.get("picDate");
|
String alarmTime = picDate;
|
if (alarmTime != null && alarmTime.length() > "2019-11-11 12:32:05".length()) {
|
alarmTime = alarmTime.substring(0, "2019-11-11 12:32:05".length());
|
}
|
apiModel.put("alarmTime", alarmTime);
|
apiModel.put("alarmPerson", "");
|
|
String sdkType = apiModel.get("sdkType");
|
if (sdkType != null) {
|
apiModel.put("alarmType", apiModel.remove("sdkType"));
|
apiModel.put("property", apiModel.remove("showLabels"));
|
apiModel.put("alarmPerson", apiModel.remove("tableName") + " " + apiModel.remove("idcard"));
|
}
|
apiModel.put("alarmVideo", apiModel.get("videoNum"));
|
if (picDate != null) {
|
apiModel.put("alarmPicture", apiModel.remove("picDate"));
|
}
|
apiModel.put("alarmPicture",
|
"http://" + apiModel.get("picSmUrl")
|
);
|
apiModel.put("alarmLargePicture",
|
"http://" + apiModel.get("picMaxUrl")
|
);
|
apiModel.put(Alarm.picDate, picDate);
|
String picAddress = apiModel.get("picAddress");
|
if (picAddress != null) {
|
apiModel.put("alarmAddress", apiModel.remove("picAddress"));
|
}
|
}
|
}
|
|
public static List<Map<String, String>> findAlarmList() {
|
List<Map<String, String>> dbSqliteAlarmList = findList("select * from alarm where 1=1 and closeAlarm='false' order by alarmTime desc ");
|
int maxAlarm = 1000;
|
sqliteAlarmList.clear();
|
if (dbSqliteAlarmList.size() > maxAlarm) {
|
for (int i = 0; i < dbSqliteAlarmList.size(); i++) {
|
if (i < maxAlarm) {
|
sqliteAlarmList.add(dbSqliteAlarmList.get(i));
|
} else {
|
try {
|
DatabaseManager.execSQL("delete from alarm where id='" + dbSqliteAlarmList.get(i).get("id") + "'");
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|
} else {
|
sqliteAlarmList.addAll(dbSqliteAlarmList);
|
}
|
long keepStartTime = SettingManager.getKeepStartTime();
|
for (Map<String, String> alarm : sqliteAlarmList) {
|
String alarmVideo = alarm.get("alarmVideo");
|
String alarmPicture = alarm.get("alarmPicture");
|
String alarmLargePicture = alarm.get("alarmLargePicture");
|
String alarmTimeStr = alarm.get(Alarm.alarmTime);
|
long alarmTime = new Date().getTime();
|
if (alarmTimeStr != null && alarmTimeStr.length() > "yyyy-MM-dd HH:mm:ss".length()) {
|
alarmTimeStr = alarmTimeStr.substring(0, "yyyy-MM-dd HH:mm:ss".length());
|
}
|
try {
|
alarmTime = sdf.parse(alarmTimeStr).getTime();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
// if (alarmVideo != null && alarmVideo.startsWith("http:")) {
|
if (alarmVideo != null) {
|
alarm.put("alarmVideoValid", "true");
|
} else {
|
alarm.put("alarmVideoValid", "false");
|
}
|
// if (alarmPicture != null && alarmPicture.startsWith("http:")) {
|
if (alarmPicture != null) {
|
alarm.put("alarmPictureValid", "true");
|
} else {
|
alarm.put("alarmPictureValid", "false");
|
}
|
// if (alarmLargePicture != null && alarmLargePicture.startsWith("http:")) {
|
if (alarmLargePicture != null) {
|
alarm.put("alarmLargePictureValid", "true");
|
} else {
|
alarm.put("alarmLargePictureValid", "false");
|
}
|
if (alarmTime < keepStartTime) {
|
alarm.put("canClose", "true");
|
} else {
|
alarm.put("canClose", "false");
|
}
|
// alarm.put("alarmVideo", "/sdcard/face1.mp4");
|
}
|
return sqliteAlarmList;
|
}
|
|
public static Set<String> newAlarmIdSet = new HashSet<>();
|
public static Lock newAlarmIdSetLock = new ReentrantLock();
|
|
public static void putNewAlarmIdSet(Map<String, String> newRemoteAlarm) {
|
newAlarmIdSetLock.lock();
|
try {
|
String id = newRemoteAlarm.get(Alarm.id);
|
if (!TextUtils.isEmpty(id)) {
|
newAlarmIdSet.add(id);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
newAlarmIdSetLock.unlock();
|
}
|
|
public static boolean removeNewAlarmIdSet(Map<String, String> alarm) {
|
newAlarmIdSetLock.lock();
|
boolean success = false;
|
try {
|
String id = alarm.get(Alarm.id);
|
if (!TextUtils.isEmpty(id)) {
|
if (newAlarmIdSet.contains(id)) {
|
success = true;
|
}
|
newAlarmIdSet.remove(id);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
newAlarmIdSetLock.unlock();
|
return success;
|
}
|
|
|
public static int saveRemoteAlarmListToSqlite(List<Map<String, String>> newRemoteAlarmList) {
|
int savedCount = 0;
|
for (Map<String, String> newRemoteAlarm : newRemoteAlarmList) {
|
Map<String, String> dbRemoteAlarm = findById(newRemoteAlarm.get("id"));
|
if (dbRemoteAlarm == null) {
|
newRemoteAlarm.put("table", "alarm");
|
newRemoteAlarm.put("closeAlarm", "false");
|
newRemoteAlarm.put("mute", "false");
|
newRemoteAlarm.put("createTime", new Date().getTime() + "");
|
save(newRemoteAlarm);
|
putNewAlarmIdSet(newRemoteAlarm);
|
savedCount++;
|
} else {
|
if (TextUtils.isEmpty(dbRemoteAlarm.get("alarmVideo"))) {
|
dbRemoteAlarm.put("table", "alarm");
|
dbRemoteAlarm.put("alarmVideo", newRemoteAlarm.get("alarmVideo"));
|
save(dbRemoteAlarm);
|
savedCount++;
|
}
|
}
|
}
|
return savedCount;
|
}
|
|
public static void deleteAlarm(Map<String, String> alarm) {
|
try {
|
DatabaseManager.execSQL("delete from alarm where id='" + alarm.get("id") + "'");
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public static List<Map<String, String>> newSqliteAlarmListBackup = new ArrayList<>();
|
|
public static void updateVideoUrl() {
|
HomeFragment homeFragment = ((MainActivity) BaseApplication.getApplication().activity).fragment_home;
|
if (homeFragment == null) {
|
return;
|
}
|
if (homeFragment.refreshGridViewLocker != null) {
|
try {
|
homeFragment.refreshGridViewLocker.lock();
|
if (homeFragment.newSqliteAlarmList != null) {
|
for (int i = 0; i < homeFragment.newSqliteAlarmList.size(); i++) {
|
Map<String, String> alarm = homeFragment.newSqliteAlarmList.get(i);
|
if (alarm.get(Alarm.alarmVideo).length() < 5) {
|
Map<String, String> newAlarm = new HashMap<>();
|
newAlarm.putAll(alarm);
|
newSqliteAlarmListBackup.add(newAlarm);
|
}
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
homeFragment.refreshGridViewLocker.unlock();
|
}
|
}
|
|
int videoUrlChangeCount = 0;
|
if (newSqliteAlarmListBackup.size() > 0) {
|
for (Map<String, String> alarm : newSqliteAlarmListBackup) {
|
String videoUrl = VideoPath.getVideoUrl(alarm);
|
if (!TextUtils.isEmpty(videoUrl)) {
|
alarm.put(Alarm.alarmVideo, videoUrl);
|
save(alarm);
|
videoUrlChangeCount++;
|
}
|
}
|
}
|
if (videoUrlChangeCount > 0) {
|
homeFragment.refreshGridView();
|
}
|
}
|
|
static Map<String, String> alarmPlayingMap = new HashMap<>();
|
static Lock alarmPlayingMapLock = new ReentrantLock();
|
|
public static void putAlarmPlaying(Map<String, String> alarm, boolean state) {
|
try {
|
String id = alarm.get(Alarm.id);
|
if (!TextUtils.isEmpty(id)) {
|
if (state) {
|
System.out.println("AlarmManager.putAlarmPlaying " + state + " " + id + FrameUtil.getFrames());
|
}
|
alarmPlayingMapLock.lock();
|
alarmPlayingMap.put(id, state ? Constants.TRUE : Constants.FALSE);
|
alarmPlayingMapLock.unlock();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public static boolean getAlarmPlaying(Map<String, String> alarm) {
|
boolean state = false;
|
try {
|
String id = alarm.get(Alarm.id);
|
if (!TextUtils.isEmpty(id)) {
|
alarmPlayingMapLock.lock();
|
if ("true".equals(alarmPlayingMap.get(id))) {
|
state = true;
|
}
|
alarmPlayingMapLock.unlock();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return state;
|
}
|
|
}
|