Commit 3d717eeb authored by liuzhaoce's avatar liuzhaoce

scheduler msg report task

parent 78081c3c
......@@ -4,4 +4,6 @@ public class Constant {
public static final String TAG = "G4ProxyAgent";
public static final String sharedPreferenceFile = "config";
public static final String schedulerTAG = "SchedulerTask";
}
package com.virjar.g4proxy;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
......@@ -12,15 +11,12 @@ import android.widget.Button;
import android.widget.TextView;
import com.tencent.mm.R;
import com.virjar.g4proxy.bean.Const;
import com.virjar.g4proxy.service.SchedulerTaskService;
import com.virjar.g4proxy.utils.AccessAuthority;
import com.virjar.g4proxy.utils.BDLocationUtils;
import com.virjar.g4proxy.utils.DeviceInfoHolder;
import com.virjar.g4proxy.utils.DeviceMessageUtil;
import com.virjar.g4proxy.utils.HttpClientUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
......@@ -95,7 +91,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override
public void onResponse(@javax.annotation.Nullable Call call, @javax.annotation.Nullable Response response) {
try {
Log.e("report response", response.body().string());
if (response != null) {
Log.e("report response", response.body().string());
}
} catch (IOException e) {
e.printStackTrace();
}
......
......@@ -11,7 +11,6 @@ import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
......@@ -31,15 +30,20 @@ import okhttp3.Request;
import okhttp3.Response;
import top.wuhaojie.installerlibrary.AutoInstaller;
import static com.virjar.g4proxy.Constant.schedulerTAG;
public class SchedulerTaskService extends Service {
//查询更新的间隔 (单位:分钟)
public static final int updateInterval = 10;
//上传设备信息的间隔 (单位: 分钟)
public static final int reportInterval = 5;
public static final String getNewestModifiedApkUrl = "http://www.scumall.com:5597/hermes/ModifiedApp/getNewestModifiedApk?appPackageName=";
public static final String msgReportlUrl = "http://taskcenter.beta.qunar.com/api/phone/report";
private long lastCheckUpdateTimestamp = 0;
private long lastCheckReportTimestamp = 0;
@Nullable
......@@ -59,27 +63,51 @@ public class SchedulerTaskService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerAlarm();
checkAndRunTask();
return START_STICKY;
}
private void checkAndRunTask() {
if (System.currentTimeMillis() - lastCheckUpdateTimestamp < (updateInterval * 1000 * 60) / 3) {
return;
Log.e(schedulerTAG, "checkAndRunTask");
if (System.currentTimeMillis() - lastCheckUpdateTimestamp > (updateInterval * 1000 * 60) / 3) {
runAppInstallTask();
}else {
lastCheckUpdateTimestamp = System.currentTimeMillis();
}
if (System.currentTimeMillis() - lastCheckReportTimestamp > (reportInterval * 1000 * 60) / 3){
runReportMessageTask();
}else {
lastCheckReportTimestamp = System.currentTimeMillis();
}
lastCheckUpdateTimestamp = System.currentTimeMillis();
}
runAppInstallTask();
private void runReportMessageTask() {
Log.e(schedulerTAG, "runUploadMessageTask");
JSONObject deviceMessage = DeviceMessageUtil.getDeviceMessage(this);
runUploadMessageTask();
}
Request request = HttpClientUtils.postRequest(msgReportlUrl, deviceMessage);
HttpClientUtils.getClient().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@javax.annotation.Nullable Call call, @javax.annotation.Nullable IOException e) {
Log.e(Constant.TAG, "report device msg failed", e);
}
private void runUploadMessageTask() {
JSONObject deviceMessage = DeviceMessageUtil.getDeviceMessage(this);
@Override
public void onResponse(@javax.annotation.Nullable Call call, @javax.annotation.Nullable Response response) {
try {
if (response != null) {
Log.e("report response", response.body().string());
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
......@@ -160,7 +188,7 @@ public class SchedulerTaskService extends Service {
private void registerAlarm() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int time = 1000 * 60 * updateInterval;
int time = 1000 * 60 * Math.min(reportInterval, updateInterval);
//获取系统开机至今所经历的毫秒
long startTime = SystemClock.elapsedRealtime() + time;
Intent intent = new Intent(this, SchedulerTaskService.class);
......
......@@ -2,7 +2,6 @@ package com.virjar.g4proxy.utils;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
......@@ -18,11 +17,8 @@ import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.google.common.collect.Lists;
import com.virjar.g4proxy.bean.App;
import com.virjar.g4proxy.bean.Const;
......@@ -45,7 +41,6 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static android.content.Context.ACTIVITY_SERVICE;
......@@ -59,7 +54,8 @@ public class DeviceMessageUtil {
private static String longitude = "longitude";
private static String DeviceMsg = "DeviceMsg";
public static String slaveInfo = "/sdcard/slave_info.txt";
//0:UI 1:沙盒
private static int channel;
private static LocationListener locationListener = new LocationListener() {
......@@ -88,8 +84,25 @@ public class DeviceMessageUtil {
JSONObject devicesMsg = new JSONObject();
Map<String, String> latAndLon = DeviceMessageUtil.getLatAndLon(context);
String slaveInfo = getSlaveInfo(context);
String deviceId;
switch (channel){
case 0:
deviceId = getSerialNumber();
Log.e(DeviceMsg, "channel: " + channel);
break;
case 1:
deviceId = getSerialNumber() + "_" + getAndroidID(context);
Log.e(DeviceMsg, "channel: " + channel);
break;
default:
deviceId = null;
Log.e(DeviceMsg, "channel error!");
}
try {
devicesMsg.put("deviceId", getSerialNumber() + "_" + getAndroidID(context));
devicesMsg.put("deviceId", deviceId);
devicesMsg.put("brand", android.os.Build.BRAND);
devicesMsg.put("os", Build.VERSION.RELEASE);
devicesMsg.put("mac", getNewMac());
......@@ -97,7 +110,7 @@ public class DeviceMessageUtil {
devicesMsg.put("cpuUsage", getCPURateDesc());
devicesMsg.put("memoryTotal", getMemoryTotal());
devicesMsg.put("memoryUsage", getMemUsage(context));
devicesMsg.put("slaveInfo", getSlaveInfo(context));
devicesMsg.put("slaveInfo", slaveInfo);
devicesMsg.put("latitude", latAndLon.get(latitude));
devicesMsg.put("longitude", latAndLon.get(longitude));
devicesMsg.put("osType", 2);
......@@ -321,15 +334,18 @@ public class DeviceMessageUtil {
* 获取SlaveInfo
*/
private static String getSlaveInfo(Context context) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return null;
}
String path = slaveInfo;
Log.e(DeviceMsg, "slave_info.txt path: " + path);
String slaveInfoPath = "/sdcard/slave_info.txt";
Log.e(DeviceMsg, "slave_info.txt path: " + slaveInfoPath);
String firstLine = null;
try {
FileReader fileReader = new FileReader(path);
FileReader fileReader = new FileReader(slaveInfoPath);
BufferedReader br = new BufferedReader(fileReader, 8192);
firstLine = br.readLine();
br.close();
......@@ -337,7 +353,15 @@ public class DeviceMessageUtil {
Log.e(DeviceMsg, "Cannot find slave_info.txt !");
e.printStackTrace();
}
return firstLine;
String slaveInfo = null;
if (firstLine != null) {
slaveInfo = firstLine.split(",")[0];
channel = Integer.valueOf(firstLine.split(",")[1]);
}else{
Log.e(DeviceMsg, "slave_info.txt can not be parsed!");
}
return slaveInfo;
}
......@@ -376,6 +400,7 @@ public class DeviceMessageUtil {
/**
* 获取手机序列号
*/
@SuppressLint("HardwareIds")
private static String getSerialNumber() {
return android.os.Build.SERIAL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment