version:1.1
fix: add:应用使用数据统计
This commit is contained in:
414
app/src/main/java/com/uiui/sn/service/MainService.java
Normal file
414
app/src/main/java/com/uiui/sn/service/MainService.java
Normal file
@@ -0,0 +1,414 @@
|
||||
package com.uiui.sn.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.IBinder;
|
||||
import android.os.SystemClock;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.blankj.utilcode.util.NetworkUtils;
|
||||
import com.uiui.sn.activity.main.MainActivity;
|
||||
import com.uiui.sn.activity.main.MainContact;
|
||||
import com.uiui.sn.activity.main.MainPresenter;
|
||||
import com.uiui.sn.bean.zuoye.BaseResponse;
|
||||
import com.uiui.sn.bean.zuoye.UserInfo;
|
||||
import com.uiui.sn.bean.gankao.AvailableProduct;
|
||||
import com.uiui.sn.config.Configs;
|
||||
import com.uiui.sn.jpush.TagAliasOperatorHelper;
|
||||
import com.uiui.sn.manager.ControlManager;
|
||||
import com.uiui.sn.network.HTTPInterface;
|
||||
import com.uiui.sn.utils.SPUtils;
|
||||
import com.uiui.sn.utils.ToastUtil;
|
||||
import com.uiui.sn.utils.Utils;
|
||||
import com.trello.rxlifecycle2.LifecycleProvider;
|
||||
import com.trello.rxlifecycle2.LifecycleTransformer;
|
||||
import com.trello.rxlifecycle2.RxLifecycle;
|
||||
import com.trello.rxlifecycle2.android.ActivityEvent;
|
||||
import com.trello.rxlifecycle2.android.RxLifecycleAndroid;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import cn.jpush.android.api.JPushInterface;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.ObservableEmitter;
|
||||
import io.reactivex.ObservableOnSubscribe;
|
||||
import io.reactivex.Observer;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.subjects.BehaviorSubject;
|
||||
|
||||
import static com.uiui.sn.jpush.TagAliasOperatorHelper.ACTION_SET;
|
||||
|
||||
/**
|
||||
* @author jgy02
|
||||
*/
|
||||
public class MainService extends Service implements MainContact.MainView, NetworkUtils.OnNetworkStatusChangedListener, LifecycleProvider<ActivityEvent> {
|
||||
private String TAG = MainService.class.getSimpleName();
|
||||
public static MainPresenter mPresenter;
|
||||
|
||||
//执行所有请求的时间
|
||||
long runningTime = 0;
|
||||
|
||||
//MainService上次执行时间
|
||||
long startCommandTime = 0;
|
||||
|
||||
private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Observable<ActivityEvent> lifecycle() {
|
||||
return lifecycleSubject.hide();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <T> LifecycleTransformer<T> bindUntilEvent(@NotNull ActivityEvent event) {
|
||||
return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <T> LifecycleTransformer<T> bindToLifecycle() {
|
||||
return RxLifecycleAndroid.bindActivity(lifecycleSubject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnected() {
|
||||
Log.e(TAG, "网络未连接");
|
||||
ToastUtil.show("网络未连接");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(NetworkUtils.NetworkType networkType) {
|
||||
mPresenter.getUserInfo();
|
||||
HTTPInterface.checkUpdate(this);
|
||||
Log.e(TAG, "网络已连接");
|
||||
ToastUtil.show("网络已连接");
|
||||
Log.e(TAG, "onConnected: wifi ssid = " + Utils.getWifiSSID(this));
|
||||
SPUtils.put(this, "wifi_last_connect_time", System.currentTimeMillis());
|
||||
mPresenter.checkUpdate();
|
||||
}
|
||||
|
||||
private interface Start {
|
||||
void onstar(long time);
|
||||
}
|
||||
|
||||
private Start start;
|
||||
|
||||
private final ObservableOnSubscribe<Long> subscribe = new ObservableOnSubscribe<Long>() {
|
||||
@Override
|
||||
public void subscribe(ObservableEmitter emitter) throws Exception {
|
||||
start = new Start() {
|
||||
@Override
|
||||
public void onstar(long time) {
|
||||
emitter.onNext(time);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
private Observer<Long> timeObserver = new Observer<Long>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(Long aLong) {
|
||||
Log.e("TimeObserver", "onNext: $aLong");
|
||||
startCommandTime = runningTime = SystemClock.elapsedRealtime();
|
||||
mPresenter.checkUpdate();
|
||||
mPresenter.initAmap();
|
||||
mPresenter.getUserInfo();
|
||||
HTTPInterface.sendAppUsed(MainService.this);
|
||||
// HTTPInterface.sendRunningApp(MainService.this);
|
||||
HTTPInterface.getAPPinfo(MainService.this);
|
||||
startService();
|
||||
mPresenter.setAlias();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
lifecycleSubject.onNext(ActivityEvent.CREATE);
|
||||
JPushInterface.init(this);
|
||||
mPresenter = new MainPresenter(this);
|
||||
mPresenter.setProvider(this);
|
||||
mPresenter.attachView(this);
|
||||
NetworkUtils.registerNetworkStatusChangedListener(this);
|
||||
Observable.create(subscribe)
|
||||
.throttleFirst(60, TimeUnit.SECONDS)
|
||||
.subscribe(timeObserver);
|
||||
PackageManager packageManager = getPackageManager();
|
||||
try {
|
||||
// packageManager.setApplicationEnabledSetting("com.jiaoguanyi.appstore", PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0)
|
||||
// packageManager.setApplicationEnabledSetting("com.jiaoguanyi.store", PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0)
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "onCreate: " + e.getMessage());
|
||||
}
|
||||
ControlManager.getInstance().setDefaultUSBstate();
|
||||
mPresenter.setAlias();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
JPushInterface.init(this);
|
||||
if (MainActivity.isForeground) {
|
||||
Log.e(TAG, "onStartCommand: MainActivity: isForeground: " + MainActivity.isForeground);
|
||||
} else {
|
||||
JPushInterface.init(this);
|
||||
start.onstar(startCommandTime);
|
||||
Log.e(TAG, "onStartCommand: " + (SystemClock.elapsedRealtime() - startCommandTime) + "ms");
|
||||
}
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
lifecycleSubject.onNext(ActivityEvent.DESTROY);
|
||||
NetworkUtils.unregisterNetworkStatusChangedListener(this);
|
||||
mPresenter.detachView();
|
||||
}
|
||||
|
||||
private void startService() {
|
||||
startService(new Intent(this, GuardService.class));
|
||||
startService(new Intent(this, StepService.class));
|
||||
startService(new Intent(this, DownloadService.class));
|
||||
}
|
||||
|
||||
public static MainPresenter getPresenter() {
|
||||
return mPresenter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPowerUserList(String date) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkSNFinish(int code) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGankaoUID(String gankaoUID) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAvailableProduct(List<AvailableProduct> availableProductList, String gankaoUID) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void activeUserFinish(int code) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAlias() {
|
||||
setJpushAlias();
|
||||
Log.e(TAG, "setAlias: " + "finish");
|
||||
mPresenter.getBatch();
|
||||
}
|
||||
|
||||
private void setJpushAlias() {
|
||||
String alias = Utils.getSerial();
|
||||
TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
|
||||
tagAliasBean.action = ACTION_SET;
|
||||
TagAliasOperatorHelper.sequence++;
|
||||
tagAliasBean.alias = alias;
|
||||
tagAliasBean.isAliasAction = true;
|
||||
TagAliasOperatorHelper.getInstance().handleAction(this, TagAliasOperatorHelper.sequence, tagAliasBean);
|
||||
}
|
||||
|
||||
private void setTag(Set set) {
|
||||
TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
|
||||
tagAliasBean.action = ACTION_SET;
|
||||
TagAliasOperatorHelper.sequence++;
|
||||
tagAliasBean.tags = set;
|
||||
tagAliasBean.isAliasAction = false;
|
||||
TagAliasOperatorHelper.getInstance().handleAction(this, TagAliasOperatorHelper.sequence, tagAliasBean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBatch(String batch) {
|
||||
HashSet hashSet = new HashSet();
|
||||
hashSet.add(batch);
|
||||
hashSet.removeIf(o -> TextUtils.isEmpty(o.toString()));
|
||||
if (hashSet.size() != 0) {
|
||||
setTag(hashSet);
|
||||
} else {
|
||||
Log.e(TAG, "setBatch: " + "hashSet size is 0");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setQRCode(Bitmap bitmap) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSnInfo(BaseResponse<UserInfo> response) {
|
||||
if (response != null) {
|
||||
int code = response.code;
|
||||
//设备已经绑定
|
||||
if (code == 200) {
|
||||
UserInfo userInfo = response.data;
|
||||
boolean username = Settings.System.putString(getContentResolver(), "UserInfo_username", userInfo.getSn_name());
|
||||
boolean gread = Settings.System.putString(getContentResolver(), "UserInfo_grade", userInfo.getGrade());
|
||||
SPUtils.put(this, Configs.isLogined, 1);
|
||||
SPUtils.put(this, "member_id", userInfo.getMember_id());
|
||||
SPUtils.put(this, "sn_id", userInfo.getId());
|
||||
if (!TextUtils.isEmpty(userInfo.getSn_name())) {
|
||||
SPUtils.put(this, "USERINFO_NAME", userInfo.getSn_name());
|
||||
}
|
||||
if (!TextUtils.isEmpty(userInfo.getSchool())) {
|
||||
SPUtils.put(this, "USERINFO_SCHOOL", userInfo.getSchool());
|
||||
}
|
||||
if (!TextUtils.isEmpty(userInfo.getGrade())) {
|
||||
SPUtils.put(this, "USERINFO_GRADE", userInfo.getGrade());
|
||||
}
|
||||
|
||||
mPresenter.getLocked();
|
||||
}
|
||||
//设备没有绑定
|
||||
else if (code == 300) {
|
||||
ControlManager.getInstance().setDisableSetting();
|
||||
}
|
||||
//没有授权的设备
|
||||
else if (code == 400) {
|
||||
ControlManager.getInstance().setDisableSetting();
|
||||
}
|
||||
}
|
||||
mPresenter.getSnUid();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSnUid(JSONObject jsonObject) {
|
||||
if (jsonObject != null) {
|
||||
String uid = jsonObject.getString("uid");
|
||||
if (!TextUtils.isEmpty(uid)) {
|
||||
Settings.System.putString(getContentResolver(), "gankaoUID", uid);
|
||||
}
|
||||
// String uid = jsonObject.getString("uid");
|
||||
// long activate_time = jsonObject.getLong("activate_time");
|
||||
// String card_info = jsonObject.getString("card_info");
|
||||
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
// Date d1 = new Date(activate_time * 1000);
|
||||
// int grade = jsonObject.getIntValue("grade");
|
||||
// SPUtils.put(MainService.this, "int_grade", grade);
|
||||
// String t1 = format.format(d1);
|
||||
} else {
|
||||
mPresenter.registerGankao();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGankao(String gankaoUID) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUserInfo() {
|
||||
mPresenter.getSnInfo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLocked(int lockedStatus) {
|
||||
switch (lockedStatus) {
|
||||
case 0:
|
||||
ControlManager.getInstance().setDisableSetting();
|
||||
break;
|
||||
case 1:
|
||||
break;
|
||||
case 2:
|
||||
ControlManager.getInstance().setDisableSetting();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
mPresenter.checkUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged() {
|
||||
HTTPInterface.updateAdminInfo(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkUpdateFinish() {
|
||||
mPresenter.getAllApp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getAllAppFinish() {
|
||||
mPresenter.getAppInside();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAppInside() {
|
||||
mPresenter.getForceInstall();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setForceInstall() {
|
||||
mPresenter.getSystemSettings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSystemSettings() {
|
||||
mPresenter.getBrowserLabel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBrowserLabel() {
|
||||
mPresenter.getBrowserWhiteList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBrowserWhiteList() {
|
||||
mPresenter.getAppStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAppStart() {
|
||||
mPresenter.getTimeControl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTimeControl() {
|
||||
mPresenter.getScreenLock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScreenLock() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user