|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package com.xinyingpower.microphoto;
|
|
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import android.app.AlarmManager;
|
|
|
|
|
import android.app.Notification;
|
|
|
|
|
import android.app.NotificationChannel;
|
|
|
|
@ -10,11 +11,23 @@ import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
|
import android.os.Environment;
|
|
|
|
|
import android.os.IBinder;
|
|
|
|
|
import android.os.SystemClock;
|
|
|
|
|
|
|
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
|
|
|
|
|
|
import android.telephony.CellLocation;
|
|
|
|
|
import android.telephony.PhoneStateListener;
|
|
|
|
|
import android.telephony.ServiceState;
|
|
|
|
|
import android.telephony.SignalStrength;
|
|
|
|
|
import android.telephony.TelephonyManager;
|
|
|
|
|
import android.telephony.cdma.CdmaCellLocation;
|
|
|
|
|
import android.telephony.gsm.GsmCellLocation;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.text.format.DateFormat;
|
|
|
|
|
import android.util.Log;
|
|
|
|
@ -85,6 +98,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
|
|
|
|
|
private Map<Long, PendingIntent> mTimers = new HashMap<>();
|
|
|
|
|
private static int stateService = STATE_SERVICE.NOT_CONNECTED;
|
|
|
|
|
|
|
|
|
|
public MicroPhotoService() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -101,6 +115,8 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
|
|
|
stateService = STATE_SERVICE.NOT_CONNECTED;
|
|
|
|
|
|
|
|
|
|
getPhoneState(this.getApplicationContext());
|
|
|
|
|
|
|
|
|
|
alarmReceiver = new AlarmReceiver(this);
|
|
|
|
|
// 注册广播接受者
|
|
|
|
|
IntentFilter intentFilter = new IntentFilter(ACTION_HEARTBEAT);
|
|
|
|
@ -143,8 +159,10 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
// File path = getApplicationContext().getFilesDir();
|
|
|
|
|
String appPath = path.getAbsolutePath();
|
|
|
|
|
Log.i(TAG, "AppPath=" + appPath);
|
|
|
|
|
String ip = "180.166.218.222";
|
|
|
|
|
int port = 40032;
|
|
|
|
|
// String ip = "180.166.218.222";
|
|
|
|
|
// int port = 40032;
|
|
|
|
|
String ip = "192.168.137.102";
|
|
|
|
|
int port = 6891;
|
|
|
|
|
String cmdid = "XYDEV100230100012";
|
|
|
|
|
init(appPath, ip, port, cmdid);
|
|
|
|
|
|
|
|
|
@ -167,17 +185,18 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
public static class AlarmReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
private MicroPhotoService mService;
|
|
|
|
|
|
|
|
|
|
public AlarmReceiver(MicroPhotoService service) {
|
|
|
|
|
mService = service;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
if (TextUtils.equals(ACTION_HEARTBEAT, action)) {
|
|
|
|
|
Log.i(TAG, "receiver ACTION=" + action);
|
|
|
|
|
mService.sendHeartbeat();
|
|
|
|
|
mService.registerHeartbeatTimer();
|
|
|
|
|
}
|
|
|
|
|
else if(TextUtils.equals(ACTION_TAKE_PHOTO, action)) {
|
|
|
|
|
} else if (TextUtils.equals(ACTION_TAKE_PHOTO, action)) {
|
|
|
|
|
long ts = intent.getLongExtra(EXTRA_PARAM_TIME, 0);
|
|
|
|
|
int cnt = intent.getIntExtra(EXTRA_PARAM_SCHEDULES, 0);
|
|
|
|
|
if (cnt > 0) {
|
|
|
|
@ -201,8 +220,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
long baseTime = nowTs - startTime;
|
|
|
|
|
|
|
|
|
|
mService.registerCaptureSchedule(startTime, baseTime);
|
|
|
|
|
}
|
|
|
|
|
else if(TextUtils.equals(ACTION_TAKE_PHOTO_MANUALLY, action)) {
|
|
|
|
|
} else if (TextUtils.equals(ACTION_TAKE_PHOTO_MANUALLY, action)) {
|
|
|
|
|
int channel = intent.getIntExtra(EXTRA_PARAM_CHANNEL, 0);
|
|
|
|
|
int preset = intent.getIntExtra(EXTRA_PARAM_PRESET, 0);
|
|
|
|
|
// long ts = intent.getLongExtra(EXTRA_PARAM_TIME, 0);
|
|
|
|
@ -210,8 +228,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
long ts = System.currentTimeMillis() / 1000;
|
|
|
|
|
|
|
|
|
|
mService.notifyToTakePhoto(channel, preset, ts, mService.buildPhotoDir(channel), mService.buildPhotoFileName(channel, preset, ts), photoOrVideo);
|
|
|
|
|
}
|
|
|
|
|
else if(TextUtils.equals(ACTION_TIMEOUT, action)) {
|
|
|
|
|
} else if (TextUtils.equals(ACTION_TIMEOUT, action)) {
|
|
|
|
|
long uid = intent.getLongExtra(EXTRA_PARAM_TIMER_UID, 0);
|
|
|
|
|
Log.i(TAG, "Timeout:" + uid);
|
|
|
|
|
mService.fireTimeout(uid);
|
|
|
|
@ -235,6 +252,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void registerHeartbeatTimer() {
|
|
|
|
|
|
|
|
|
|
// 创建延迟意图
|
|
|
|
@ -248,6 +266,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
mNextHeartbeatTime = System.currentTimeMillis() + mHeartbeatDuration;
|
|
|
|
|
// alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeout, pendingIntent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void registerPhotoTimer(int channel, int preset, long ts, long timeout, List<Long> schedules) {
|
|
|
|
|
|
|
|
|
|
// 创建延迟意图
|
|
|
|
@ -316,6 +335,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean registerCaptureSchedule(long startTime, long baseTime) {
|
|
|
|
|
|
|
|
|
|
long[] photoTimeData = getPhotoTimeData();
|
|
|
|
@ -416,6 +436,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
|
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void connect() {
|
|
|
|
|
// after 10 seconds its connected
|
|
|
|
|
new android.os.Handler().postDelayed(
|
|
|
|
@ -494,6 +515,124 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
return notificationBuilder.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected String getNetworkType() {
|
|
|
|
|
|
|
|
|
|
// ConnectionManager instance
|
|
|
|
|
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
|
|
|
|
|
|
|
|
|
|
// If not connected, "-" will be displayed
|
|
|
|
|
if (ni == null || !ni.isConnected()) return "";
|
|
|
|
|
|
|
|
|
|
// If Connected to Wifi
|
|
|
|
|
if (ni.getType() == ConnectivityManager.TYPE_WIFI) return "WIFI";
|
|
|
|
|
|
|
|
|
|
// If Connected to Mobile
|
|
|
|
|
String subTypeName = "";
|
|
|
|
|
if (ni.getType() == ConnectivityManager.TYPE_MOBILE) {
|
|
|
|
|
int subType = ni.getSubtype();
|
|
|
|
|
switch (subType) {
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_GPRS:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_EDGE:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_CDMA:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_1xRTT:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_IDEN:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_GSM:
|
|
|
|
|
subTypeName = "2G";
|
|
|
|
|
break;
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_UMTS:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_EVDO_0:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_EVDO_A:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_HSDPA:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_HSUPA:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_HSPA:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_EVDO_B:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_EHRPD:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_HSPAP:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
|
|
|
|
|
subTypeName = "3G";
|
|
|
|
|
break;
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_LTE:
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_IWLAN:
|
|
|
|
|
case 19:
|
|
|
|
|
subTypeName = "4G";
|
|
|
|
|
break;
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_NR:
|
|
|
|
|
subTypeName = "5G";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
subTypeName = "";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return subTypeName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void getPhoneState(Context context) {
|
|
|
|
|
final TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
|
PhoneStateListener MyPhoneListener = new PhoneStateListener() {
|
|
|
|
|
@Override
|
|
|
|
|
//获取对应网络的ID,这个方法在这个程序中没什么用处
|
|
|
|
|
public void onCellLocationChanged(CellLocation location) {
|
|
|
|
|
if (location instanceof GsmCellLocation) {
|
|
|
|
|
int CID = ((GsmCellLocation) location).getCid();
|
|
|
|
|
} else if (location instanceof CdmaCellLocation) {
|
|
|
|
|
int ID = ((CdmaCellLocation) location).getBaseStationId();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//系统自带的服务监听器,实时监听网络状态
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceStateChanged(ServiceState serviceState) {
|
|
|
|
|
super.onServiceStateChanged(serviceState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//这个是我们的主角,就是获取对应网络信号强度
|
|
|
|
|
@Override
|
|
|
|
|
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
|
|
|
|
|
//这个ltedbm 是4G信号的值
|
|
|
|
|
String signalinfo = signalStrength.toString();
|
|
|
|
|
String[] parts = signalinfo.split(" ");
|
|
|
|
|
String ltedbm = parts[9];
|
|
|
|
|
//这个dbm 是2G和3G信号的值
|
|
|
|
|
int asu = signalStrength.getGsmSignalStrength();
|
|
|
|
|
int dbm = -113 + 2 * asu;
|
|
|
|
|
|
|
|
|
|
if (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE) {
|
|
|
|
|
Log.i("NetWorkUtil", "网络:LTE 信号强度:" + ltedbm + "======Detail:" + signalinfo);
|
|
|
|
|
} else if (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSDPA ||
|
|
|
|
|
telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSPA ||
|
|
|
|
|
telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSUPA ||
|
|
|
|
|
telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) {
|
|
|
|
|
String bin;
|
|
|
|
|
if (dbm > -75) {
|
|
|
|
|
bin = "网络很好";
|
|
|
|
|
} else if (dbm > -85) {
|
|
|
|
|
bin = "网络不错";
|
|
|
|
|
} else if (dbm > -95) {
|
|
|
|
|
bin = "网络还行";
|
|
|
|
|
} else if (dbm > -100) {
|
|
|
|
|
bin = "网络很差";
|
|
|
|
|
} else {
|
|
|
|
|
bin = "网络错误";
|
|
|
|
|
}
|
|
|
|
|
Log.i("NetWorkUtil", "网络:WCDMA 信号值:" + dbm + "========强度:" + bin + "======Detail:" + signalinfo);
|
|
|
|
|
} else {
|
|
|
|
|
String bin;
|
|
|
|
|
if (asu < 0 || asu >= 99) bin = "网络错误";
|
|
|
|
|
else if (asu >= 16) bin = "网络很好";
|
|
|
|
|
else if (asu >= 8) bin = "网络不错";
|
|
|
|
|
else if (asu >= 4) bin = "网络还行";
|
|
|
|
|
else bin = "网络很差";
|
|
|
|
|
Log.i("NetWorkUtil", "网络:GSM 信号值:" + dbm + "========强度:" + bin + "======Detail:" + signalinfo);
|
|
|
|
|
}
|
|
|
|
|
super.onSignalStrengthsChanged(signalStrength);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
telephonyManager.listen(MyPhoneListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
public boolean takePhoto(short channel, short preset, String path) {
|
|
|
|
|
|
|
|
|
|