首次提交

This commit is contained in:
2025-09-28 17:54:32 +08:00
commit 1d5c848f75
109 changed files with 15027 additions and 0 deletions

View File

@@ -0,0 +1,405 @@
package com.android.systemss;
import static com.android.api.encrytion.UtilEncrypt.encrypt;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import com.android.api.encrytion.TimeUtil;
import com.android.nebulasdk.api.ServerInterface;
import com.android.api.encrytion.UtilEncrypt;
import com.android.database.lib.DownLoadTaskBean;
import com.android.domain.rk.R;
import com.android.download.DownLoadManeger;
import com.android.nebulasdk.NetStateChangeObserver;
import com.android.nebulasdk.NetStateChangeReceiver;
import com.android.nebulasdk.bean.AppInfoBean;
import com.android.nebulasdk.bean.DomainBean;
import com.android.systemss.presenter.AppnetPresenter;
import com.android.systemss.presenter.callback.AppnetCallback;
import com.android.util.DeviceUtil;
import com.android.util.FileUtil;
import com.android.util.NetUtil;
import com.android.util.NetworkType;
import com.android.util.PakageInstallUtil;
import com.android.util.SharedPreferencesUtil;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
* 资源同步后台服务
*/
public class SystemService extends Service implements AppnetCallback, NetStateChangeObserver, DownLoadManeger.DownloadListener {
private static final String TAG = SystemService.class.getName();
private AppnetPresenter appnetPresenter;
String deviceId = "";
/**
* launcher 资源更新通知
*/
public static final String NOTIFY_LAUNCHER_UPDATE = "com.ik.launcher.res.notify";
/**
* Advert 资源更新通知
*/
public static final String NOTIFY_ADVERT_UPDATE = "com.ik.Advert.res.notify";
// HeartBean heartResponse = null;
private static boolean isRunning = false;
//开启下载任务
private static final int WHAT_START_DOWNLOADTASK = 0;
//提交日志数据
private static final int WHAT_SUBMIT_LOGINFO = 1;
//开启心跳
private static final int WHAT_INIT_HEARTBEAT = 2;
//激活设备
private static final int WHAT_ACTIVITE_DEVICE = 3;
private static final long INTERVAL_TIME = 1 * 60 * 1000;
// private static final long INTERVAL_TIME = 0;
private static final long INTERVAL_TIME2 = 30 * 60 * 1000;
// private static final long INTERVAL_TIME2 = 1 * 60 * 1000;
private static final long PERIOD_TIME_VALUE = 10 * 60 * 1000; // 每间隔10分钟验证是否达到间隔时间
private static final long PERIOD_TIME_VALUE2 = 16 * 60 * 60 * 1000; // 间隔时间16小时
private int index = -1;
private Timer mTimer = null;
private TimerTask mTimerTask = null;
private static final int NOTIFICATION_ID = 1001;
private static final String CHANNEL_ID = "SystemService"; // 必须与创建的ID一致
private Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case WHAT_START_DOWNLOADTASK:
// TaskManager.getInstance().starLocalDownloadTask();
handler.sendEmptyMessageDelayed(WHAT_SUBMIT_LOGINFO, 1000 * 10);
break;
case WHAT_SUBMIT_LOGINFO:
// EventManager.getInstance().submit();
handler.sendEmptyMessageDelayed(WHAT_INIT_HEARTBEAT, 1000 * 10);
break;
case WHAT_INIT_HEARTBEAT:
break;
case WHAT_ACTIVITE_DEVICE:
// appnetPresenter.postVerification(SystemService.this,deviceId, Config.getAppVersion());
break;
}
}
};
@Override
public void onCreate() {
super.onCreate();
startForeground(NOTIFICATION_ID, buildNotification());
LogUtils.e("postServerAppUpdate--->SystemService onCreate");
}
private Notification buildNotification() {
// Android 8.0+ 必须指定通知通道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return new NotificationCompat.Builder(this, CHANNEL_ID).setContentTitle("服务运行中").setContentText("系统服务正在后台运行").setSmallIcon(R.mipmap.icon_service_notification).setPriority(NotificationCompat.PRIORITY_LOW).setCategory(Notification.CATEGORY_SERVICE).setVisibility(NotificationCompat.VISIBILITY_PRIVATE).build();
} else {
// 低版本不需要通道
return new NotificationCompat.Builder(this).setContentTitle("服务运行中").setSmallIcon(R.mipmap.icon_service_notification).build();
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) { //判断是否属于开机启动
Bundle bundle = intent.getExtras();
if (bundle != null) {
boolean flag = bundle.getBoolean("boot_complleted", true);
LogUtils.e("postServerAppUpdate--->SystemService 开机广播启动");
if (flag) {
startService();
}
} else {
LogUtils.e("postServerAppUpdate--->SystemService 通过Contentprovider初始化启动延迟1分钟");
handler.postDelayed(new Runnable() {
@Override
public void run() {
startService();
}
}, INTERVAL_TIME);
// startService();//test
}
} else { //非开机启动需要判断时间间隔
long time = SharedPreferencesUtil.getSharePrefrencesLong(this, "start_time");
LogUtils.e("postServerAppUpdate--->SystemService 非开机启动间隔30分钟启动");
if ((System.currentTimeMillis() - time) > INTERVAL_TIME2) {
startService();
SharedPreferencesUtil.setSharePrefrencesLong(this, "start_time", System.currentTimeMillis());
}
}
return START_STICKY;
}
private void startService() {
if (!isRunning) {
isRunning = true;
deviceId = DeviceUtil.getEthernetMac();
DownLoadManeger.getInstance().registerDownloadListener(SystemService.this);
if (appnetPresenter == null) {
appnetPresenter = new AppnetPresenter(SystemService.this, this);
}
int netWorkState = NetUtil.getNetWorkState(this);
// 当网络发生变化判断当前网络状态并通过NetEvent回调当前网络状态
if (netWorkState == NetUtil.NETWORK_MOBILE || netWorkState == NetUtil.NETWORK_WIFI || netWorkState == NetUtil.NETWORK_ETHERNET) {
// EventManager.getInstance().recordEvent(EventManager.EVENT_DEVICE_ACTIVITY_CODE);
} else {
}
NetStateChangeReceiver.registerReceiver(this);
NetStateChangeReceiver.registerObserver(this);
initScheduleTask();
}
}
private void initScheduleTask() {
if (mTimer != null) {
mTimer.cancel();
mTimer.purge();
mTimer = null;
}
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
try {
long lastTime = SharedPreferencesUtil.getSharePrefrencesLong(SystemService.this, SharedPreferencesUtil.CONFIG_PARAM_LAST_TIME);
long currentTimeValue = System.currentTimeMillis();
if (currentTimeValue - lastTime >= PERIOD_TIME_VALUE2 || currentTimeValue - lastTime < 0) {
SharedPreferencesUtil.setSharePrefrencesLong(SystemService.this, SharedPreferencesUtil.CONFIG_PARAM_LAST_TIME, currentTimeValue);
getTestAvailableDomainName();
if (appnetPresenter != null) {
appnetPresenter.createNewInstance(SystemService.this);
appnetPresenter.getServerTime();
}
LogUtils.e("postServerAppUpdate--->currentTime:" + currentTimeValue + "--->lastTime:" + lastTime);
} else {
long time = lastTime + PERIOD_TIME_VALUE2 - currentTimeValue;
LogUtils.e("postServerAppUpdate--->interval time until next request " + TimeUtil.convertToHMS(time));
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
//设备未注册,请联网注册
mTimer.schedule(mTimerTask, 0, PERIOD_TIME_VALUE);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
LogUtils.e("postServerAppUpdate--->Service被移除");
}
@Override
public void onDestroy() {
NetStateChangeReceiver.unRegisterObserver(this);
NetStateChangeReceiver.unRegisterReceiver(this);
isRunning = false;
if (mTimerTask != null) {
mTimerTask = null;
}
if (mTimer != null) {
mTimer.cancel();
mTimer.purge();
mTimer = null;
}
super.onDestroy();
}
@Override
public void onViewFailureString(int code, String message) {
SharedPreferencesUtil.setSharePrefrencesString(SystemService.this, SharedPreferencesUtil.CONFIG_DOMAINNAME, "");
LogUtils.e("postServerAppUpdate--->getTestAvailableDomainName onViewFailureString(domain not available)" + message);
}
@Override
public void onExceptionFailure(String message) {
SharedPreferencesUtil.setSharePrefrencesString(SystemService.this, SharedPreferencesUtil.CONFIG_DOMAINNAME, "");
LogUtils.e("postServerAppUpdate--->getTestAvailableDomainName onExceptionFailure(domain not available)" + message);
}
@Override
public void onServerFailure(int code, String message) {
SharedPreferencesUtil.setSharePrefrencesString(SystemService.this, SharedPreferencesUtil.CONFIG_DOMAINNAME, "");
LogUtils.e("postServerAppUpdate--->getTestAvailableDomainName onServerFailure(domain not available)" + message);
}
@Override
public void onNetDisconnected() {
// TaskManager.getInstance().onDestroy();
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onNetConnected(NetworkType networkType) {
int netWorkState = NetUtil.getNetWorkState(this);
LogUtils.e("postServerAppUpdate--->networkType:" + networkType.toString());
if (netWorkState == NetUtil.NETWORK_MOBILE || netWorkState == NetUtil.NETWORK_WIFI || netWorkState == NetUtil.NETWORK_ETHERNET) {
// if (mTimerTask != null) {
// mTimerTask.cancel();
// mTimer.purge();
// }
//
// mTimerTask = new TimerTask() {
// @Override
// public void run() {
// getTestAvailableDomainName();
//
// if (appnetPresenter != null) {
// appnetPresenter.createNewInstance(SystemService.this);
// appnetPresenter.getServerTime();
// }
// }
// };
// //设备未注册,请联网注册
// mTimer.schedule(mTimerTask, 0, PERIOD_TIME_VALUE);
} else {
ToastUtils.showShort("网络未连接,请联网后重试!");
}
}
@Override
public void onTimeTesult(long time) {
String availableDomainBean = SharedPreferencesUtil.getSharePrefrencesString(SystemService.this, SharedPreferencesUtil.CONFIG_TEST_DOMAINNAME);
SharedPreferencesUtil.setSharePrefrencesString(SystemService.this, SharedPreferencesUtil.CONFIG_DOMAINNAME, availableDomainBean);
LogUtils.e("postServerAppUpdate--->getTestAvailableDomainName onTimeTesult(domain available):" + availableDomainBean);
SharedPreferencesUtil.setSharePrefrencesLong(SystemService.this, SharedPreferencesUtil.CONFIG_PARAM_TIME, time);
try {
appnetPresenter.postServerAppUpdate();
} catch (Exception e) {
LogUtils.e("postServerAppUpdate--->" + e.getMessage());
}
LogUtils.e("postServerAppUpdate--->onTimeTesult" + time);
}
@Override
public void onAppUpdate(List<AppInfoBean> appInfoBeanList, List<DomainBean> domainBeanList) {
LogUtils.e("postServerAppUpdate--->appInfoBeanList size:" + appInfoBeanList.size() + "domainBean size:" + domainBeanList.size());
if (appInfoBeanList != null && appInfoBeanList.size() > 0) {
for (AppInfoBean appInfoBean : appInfoBeanList) {
boolean isDownladDisabled = PakageInstallUtil.checkAppInstall(getApplicationContext(), appInfoBean.getPackageName(), appInfoBean.getVersionCode());
LogUtils.e("postServerAppUpdate--->appInfo is not empty,download apk info:" + appInfoBean.getPackageName() + "--->versionName:" + appInfoBean.getVersionName() + "--->versionCode:" + appInfoBean.getVersionCode() + "--->isDownladDisabled:" + isDownladDisabled);
if (!isDownladDisabled) {
addDownloadTask(appInfoBean.getUrl(), 1, appInfoBean.getSize());
}
}
}
if (domainBeanList != null && domainBeanList.size() > 0) {
DomainPool.getInstance().updateDomain(domainBeanList);
}
}
private void addDownloadTask(String url, int taskType, long fileSize) {
String filePath = FileUtil.getBakPath(getApplicationContext(), taskType);
String fileName = FileUtil.getFileName(url);
DownLoadManeger.getInstance().addDownloadTask(new DownLoadManeger.DownloadBuilder().url(url).fileName(fileName).filePath(filePath).taskType(taskType).fileTotal(fileSize));
}
@Override
public void onStart(DownLoadTaskBean bean, long taskId) {
LogUtils.e("postServerAppUpdate--->download onStart");
}
@Override
public void onError(DownLoadTaskBean bean, long taskId, String erroMsg) {
LogUtils.e("postServerAppUpdate--->download onError:" + erroMsg);
}
@Override
public void onProgress(DownLoadTaskBean bean, long taskId, long progress, long total) {
LogUtils.e("postServerAppUpdate--->download onProgress:" + progress + "--->total is:" + total);
}
@Override
public void onFinish(DownLoadTaskBean bean, long taskId) {
String appInstallPath = bean.getPath() + bean.getFileName();
LogUtils.e("postServerAppUpdate--->download onFinish,apk file path is:" + appInstallPath);
if (bean.getTaskType() == 1) {
boolean flag = PakageInstallUtil.silentInstall(getApplicationContext(), appInstallPath);
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (flag) {
LogUtils.e("postServerAppUpdate--->download onFinish,apk is installed successfully");
FileUtil.deleteFile(appInstallPath); //安装成功后删除文件
}
}
}, 1000 * 5);
}
}
private void getTestAvailableDomainName() {
LogUtils.e("postServerAppUpdate--->getTestAvailableDomainName isNeedContinueTest:" + TextUtils.isEmpty(DomainPool.getInstance().getAvailableDomainName()));
if (TextUtils.isEmpty(DomainPool.getInstance().getAvailableDomainName())) {
index++;
List<String> domainNames = new ArrayList<>();
List<String> tempDomainNames = DomainPool.getInstance().getDefaultDomain(getApplicationContext());
domainNames.add(UtilEncrypt.decrypt(ServerInterface.BUSINESS_IP_ADDRESS)); // 默认域名
domainNames.addAll(tempDomainNames);
SharedPreferencesUtil.setSharePrefrencesString(SystemService.this, SharedPreferencesUtil.CONFIG_TEST_DOMAINNAME, domainNames.get(index % domainNames.size()));
LogUtils.e("postServerAppUpdate--->getTestAvailableDomainName index:" + index + "--->" + domainNames.get(index % domainNames.size()) + "--->" + domainNames.toString());
} else {
LogUtils.e("postServerAppUpdate--->getTestAvailableDomainName domainName is available:" + DomainPool.getInstance().getAvailableDomainName());
}
}
}