915 lines
36 KiB
Java
915 lines
36 KiB
Java
package com.aoleyun.sn.service;
|
||
|
||
import android.app.Service;
|
||
import android.content.BroadcastReceiver;
|
||
import android.content.ComponentName;
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.IntentFilter;
|
||
import android.content.ServiceConnection;
|
||
import android.os.BatteryManager;
|
||
import android.os.Build;
|
||
import android.os.Environment;
|
||
import android.os.IBinder;
|
||
import android.os.SystemClock;
|
||
import android.provider.Settings;
|
||
import android.text.TextUtils;
|
||
import android.text.format.Formatter;
|
||
import android.util.Log;
|
||
|
||
import androidx.annotation.Nullable;
|
||
|
||
import com.aoleyun.sn.BuildConfig;
|
||
import com.aoleyun.sn.KeepAliveConnection;
|
||
import com.aoleyun.sn.R;
|
||
import com.aoleyun.sn.bean.BaseResponse;
|
||
import com.aoleyun.sn.bean.DownloadTaskInfo;
|
||
import com.aoleyun.sn.comm.CommonConfig;
|
||
import com.aoleyun.sn.comm.PackageNames;
|
||
import com.aoleyun.sn.gson.GsonUtils;
|
||
import com.aoleyun.sn.manager.MapManager;
|
||
import com.aoleyun.sn.network.NetInterfaceManager;
|
||
import com.aoleyun.sn.network.api.post.SendScreenStatusApi;
|
||
import com.aoleyun.sn.receiver.BootReceiver;
|
||
import com.aoleyun.sn.receiver.NewAppReceiver;
|
||
import com.aoleyun.sn.rlog.LogDBManager;
|
||
import com.aoleyun.sn.service.main.MainService;
|
||
import com.aoleyun.sn.utils.ApkUtils;
|
||
import com.aoleyun.sn.utils.CmdUtil;
|
||
import com.aoleyun.sn.utils.JGYUtils;
|
||
import com.aoleyun.sn.utils.SPUtils;
|
||
import com.aoleyun.sn.utils.ServiceAliveUtils;
|
||
import com.aoleyun.sn.utils.TimeUtils;
|
||
import com.aoleyun.sn.utils.Utils;
|
||
import com.aoleyun.sn.utils.XAPKUtils;
|
||
import com.arialyy.annotations.Download;
|
||
import com.arialyy.aria.core.Aria;
|
||
import com.arialyy.aria.core.task.DownloadTask;
|
||
import com.baidu.location.LocationClient;
|
||
import com.google.gson.Gson;
|
||
import com.google.gson.JsonObject;
|
||
import com.google.gson.reflect.TypeToken;
|
||
import com.hjq.toast.Toaster;
|
||
|
||
import java.io.BufferedReader;
|
||
import java.io.File;
|
||
import java.io.FileNotFoundException;
|
||
import java.io.FileReader;
|
||
import java.io.FileWriter;
|
||
import java.io.IOException;
|
||
import java.lang.reflect.Type;
|
||
import java.util.concurrent.TimeUnit;
|
||
|
||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||
import io.reactivex.rxjava3.annotations.NonNull;
|
||
import io.reactivex.rxjava3.core.Observable;
|
||
import io.reactivex.rxjava3.core.ObservableEmitter;
|
||
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
|
||
import io.reactivex.rxjava3.core.Observer;
|
||
import io.reactivex.rxjava3.disposables.Disposable;
|
||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||
import okhttp3.ResponseBody;
|
||
import retrofit2.Retrofit;
|
||
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
|
||
import retrofit2.converter.gson.GsonConverterFactory;
|
||
|
||
import static android.os.BatteryManager.BATTERY_HEALTH_COLD;
|
||
import static android.os.BatteryManager.BATTERY_HEALTH_DEAD;
|
||
import static android.os.BatteryManager.BATTERY_HEALTH_GOOD;
|
||
import static android.os.BatteryManager.BATTERY_HEALTH_OVERHEAT;
|
||
import static android.os.BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE;
|
||
import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN;
|
||
import static android.os.BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE;
|
||
import static android.os.BatteryManager.BATTERY_PLUGGED_AC;
|
||
import static android.os.BatteryManager.BATTERY_PLUGGED_ANY;
|
||
import static android.os.BatteryManager.BATTERY_PLUGGED_USB;
|
||
import static android.os.BatteryManager.BATTERY_PLUGGED_WIRELESS;
|
||
import static android.os.BatteryManager.BATTERY_STATUS_CHARGING;
|
||
import static android.os.BatteryManager.BATTERY_STATUS_DISCHARGING;
|
||
import static android.os.BatteryManager.BATTERY_STATUS_FULL;
|
||
import static android.os.BatteryManager.BATTERY_STATUS_NOT_CHARGING;
|
||
import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
|
||
import static android.os.BatteryManager.EXTRA_HEALTH;
|
||
import static android.os.BatteryManager.EXTRA_LEVEL;
|
||
import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT;
|
||
import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE;
|
||
import static android.os.BatteryManager.EXTRA_PLUGGED;
|
||
import static android.os.BatteryManager.EXTRA_SCALE;
|
||
import static android.os.BatteryManager.EXTRA_STATUS;
|
||
import static android.os.BatteryManager.EXTRA_TECHNOLOGY;
|
||
import static android.os.BatteryManager.EXTRA_TEMPERATURE;
|
||
import static android.os.BatteryManager.EXTRA_VOLTAGE;
|
||
|
||
|
||
/**
|
||
* 守护进程 双进程通讯
|
||
*
|
||
* @author LiGuangMin
|
||
* @time Created by 2018/8/17 11:27
|
||
*/
|
||
public class GuardService extends Service {
|
||
private final static String TAG = GuardService.class.getSimpleName();
|
||
|
||
private ServiceConnection mServiceConnection = new ServiceConnection() {
|
||
@Override
|
||
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
||
Log.w(TAG, "GuardService:建立链接");
|
||
boolean isServiceRunning = ServiceAliveUtils.isServiceAlive(GuardService.this, StepService.class.getName());
|
||
if (!isServiceRunning) {
|
||
startService(new Intent(GuardService.this, StepService.class));
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onServiceDisconnected(ComponentName componentName) {
|
||
// 断开链接
|
||
startService(new Intent(GuardService.this, StepService.class));
|
||
// 重新绑定
|
||
bindService(new Intent(GuardService.this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||
}
|
||
};
|
||
|
||
@Nullable
|
||
@Override
|
||
public IBinder onBind(Intent intent) {
|
||
return new KeepAliveConnection.Stub() {
|
||
};
|
||
}
|
||
|
||
private interface Start {
|
||
void onstar(long time);
|
||
}
|
||
|
||
private Start start;
|
||
|
||
private ObservableOnSubscribe<Long> subscribe = new ObservableOnSubscribe<Long>() {
|
||
@Override
|
||
public void subscribe(ObservableEmitter<Long> emitter) throws Exception {
|
||
start = emitter::onNext;
|
||
}
|
||
};
|
||
|
||
private Observer<Long> timeObserver = new Observer<Long>() {
|
||
@Override
|
||
public void onSubscribe(Disposable d) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onNext(Long aLong) {
|
||
Log.e(TAG, "timeObserver: onNext: " + aLong);
|
||
startService(new Intent(GuardService.this, MainService.class));
|
||
}
|
||
|
||
@Override
|
||
public void onError(Throwable e) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
|
||
}
|
||
};
|
||
|
||
private interface ResetStart {
|
||
void onstar(long time);
|
||
}
|
||
|
||
private ResetStart resetStart;
|
||
|
||
private ObservableOnSubscribe<Long> restSubscribe = new ObservableOnSubscribe<Long>() {
|
||
@Override
|
||
public void subscribe(ObservableEmitter<Long> emitter) throws Exception {
|
||
resetStart = emitter::onNext;
|
||
}
|
||
};
|
||
|
||
private Observer<Long> resetObserver = new Observer<Long>() {
|
||
@Override
|
||
public void onSubscribe(Disposable d) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onNext(Long aLong) {
|
||
Log.e("restSubscribe", "onNext: " + aLong);
|
||
sendRestoreTimes();
|
||
}
|
||
|
||
@Override
|
||
public void onError(Throwable e) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
|
||
}
|
||
};
|
||
|
||
@Override
|
||
public void onCreate() {
|
||
super.onCreate();
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||
// 8.0之后需要在
|
||
registmNewAppReceiver();
|
||
JGYUtils.startServices(GuardService.this);
|
||
}
|
||
registerPresentReceiver();
|
||
registerBatteryReceiver();
|
||
Observable.create(subscribe)
|
||
.throttleFirst(10, TimeUnit.MINUTES)
|
||
.subscribe(timeObserver);
|
||
|
||
Observable.create(restSubscribe)
|
||
.throttleFirst(1, TimeUnit.MINUTES)
|
||
.subscribe(resetObserver);
|
||
}
|
||
|
||
@Override
|
||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||
Log.e(TAG, "onStartCommand: ");
|
||
Aria.download(this).register();
|
||
Aria.download(this).resumeAllTask();
|
||
// 绑定建立链接
|
||
bindService(new Intent(this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||
return START_STICKY;
|
||
}
|
||
|
||
@Override
|
||
public void onDestroy() {
|
||
super.onDestroy();
|
||
if (null != mNewAppReceiver) {
|
||
unregisterReceiver(mNewAppReceiver);
|
||
}
|
||
if (null != presentReceiver) {
|
||
unregisterReceiver(presentReceiver);
|
||
}
|
||
if (null != batteryReceiver) {
|
||
unregisterReceiver(batteryReceiver);
|
||
}
|
||
}
|
||
|
||
private NewAppReceiver mNewAppReceiver;
|
||
|
||
private void registmNewAppReceiver() {
|
||
mNewAppReceiver = new NewAppReceiver();
|
||
IntentFilter filter = new IntentFilter();
|
||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
|
||
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
|
||
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
|
||
filter.addDataScheme("package");
|
||
registerReceiver(mNewAppReceiver, filter);
|
||
}
|
||
|
||
private PresentReceiver presentReceiver;
|
||
|
||
//监听亮屏息屏
|
||
public void registerPresentReceiver() {
|
||
presentReceiver = new PresentReceiver();
|
||
IntentFilter filter = new IntentFilter();
|
||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||
filter.addAction(Intent.ACTION_USER_PRESENT);
|
||
filter.addAction(Intent.ACTION_SCREEN_ON);
|
||
filter.addAction(Intent.ACTION_SCREEN_OFF);
|
||
filter.addAction(Intent.ACTION_USER_UNLOCKED);
|
||
filter.addAction(Intent.ACTION_FACTORY_RESET);
|
||
filter.addAction(Intent.ACTION_MASTER_CLEAR);
|
||
filter.addAction(Intent.ACTION_MASTER_CLEAR_NOTIFICATION);
|
||
filter.addAction("android.intent.action.FORCE_FACTORY_RESET");
|
||
filter.addAction(Intent.ACTION_SHUTDOWN);
|
||
filter.addAction(Intent.ACTION_REBOOT);
|
||
registerReceiver(presentReceiver, filter);
|
||
}
|
||
|
||
public class PresentReceiver extends BroadcastReceiver {
|
||
@Override
|
||
public void onReceive(final Context context, Intent intent) {
|
||
String action = intent.getAction();
|
||
Log.e(TAG, "action:" + action);
|
||
switch (action) {
|
||
default:
|
||
break;
|
||
case Intent.ACTION_USER_PRESENT:
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||
/*在8.0以上 除了开机广播其他广播基本上没有用
|
||
*当静默安装升级时,app不会被唤醒,只有通过显示广播唤醒
|
||
* */
|
||
// Intent intent1 = new Intent(BootReceiver.BOOT_COMPLETED);
|
||
// intent1.setComponent(new ComponentName(PackageNames.APPSTORE, PackageNames.APP_STORE_BOOTRECEIVER));
|
||
// // 携带数据
|
||
// //intent1.putExtra("test", "我是来测 A 应用的Android 8.0 系统静态广播的测试数据");
|
||
// // 启动发送广播
|
||
// sendBroadcast(intent1);
|
||
|
||
JGYUtils.startServices(GuardService.this);
|
||
if (JGYUtils.getInstance().checkAppPlatform() == JGYUtils.ZhanruiPlatform) {
|
||
int setting_statusbar = Settings.System.getInt(context.getContentResolver(), CommonConfig.AOLE_ACTION_HIDE_STATUS_BAR, 0);
|
||
String statusbarStatus = "";
|
||
if (setting_statusbar == 0) {
|
||
statusbarStatus = CommonConfig.AOLE_ACTION_SHOW_STATUS_BAR;
|
||
} else if (setting_statusbar == 1) {
|
||
statusbarStatus = CommonConfig.AOLE_ACTION_HIDE_STATUS_BAR;
|
||
}
|
||
Intent navIntent = new Intent(statusbarStatus).setPackage("com.android.systemui");
|
||
context.sendBroadcast(navIntent);
|
||
}
|
||
}
|
||
break;
|
||
case Intent.ACTION_SCREEN_ON: {
|
||
long time = System.currentTimeMillis();
|
||
LocationClient locationClient = MapManager.getInstance().getLocationClient();
|
||
locationClient.stop();
|
||
locationClient.start();
|
||
getLockState("2", String.valueOf(time));
|
||
// sendScreenStatus(1);
|
||
}
|
||
break;
|
||
case Intent.ACTION_SCREEN_OFF: {
|
||
long time = System.currentTimeMillis();
|
||
getLockState("1", String.valueOf(time));
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||
// JGYUtils.getInstance().wakeUpAoleyunAPP();
|
||
JGYUtils.getInstance().deleteScreenshots();
|
||
}
|
||
// sendScreenStatus(2);
|
||
}
|
||
break;
|
||
case Intent.ACTION_SHUTDOWN:
|
||
case Intent.ACTION_REBOOT:
|
||
int restart_count = (int) SPUtils.get(context, "restart_count", 0);
|
||
SPUtils.put(context, "restart_count", restart_count + 1);
|
||
// android.os.Process.killProcess(android.os.Process.myPid());
|
||
break;
|
||
case Intent.ACTION_FACTORY_RESET:
|
||
case Intent.ACTION_MASTER_CLEAR:
|
||
case Intent.ACTION_MASTER_CLEAR_NOTIFICATION:
|
||
case "android.intent.action.FORCE_FACTORY_RESET":
|
||
resetStart.onstar(System.currentTimeMillis());
|
||
Log.e("PresentReceiver", "MASTER_CLEAR");
|
||
// android.os.Process.killProcess(android.os.Process.myPid());
|
||
// System.exit(0);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void sendRestoreTimes() {
|
||
NetInterfaceManager.getInstance().getRestoreTimesApi()
|
||
.subscribe(new Observer<BaseResponse>() {
|
||
@Override
|
||
public void onSubscribe(Disposable d) {
|
||
Log.e("sendRestoreTimes", "onSubscribe: ");
|
||
}
|
||
|
||
@Override
|
||
public void onNext(BaseResponse baseResponse) {
|
||
Log.e("sendRestoreTimes", "onNext: ");
|
||
}
|
||
|
||
@Override
|
||
public void onError(Throwable e) {
|
||
Log.e("sendRestoreTimes", "onError: " + e.getMessage());
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
Log.e("sendRestoreTimes", "onComplete: ");
|
||
}
|
||
});
|
||
}
|
||
|
||
private void sendScreenStatus(int status) {
|
||
Retrofit retrofit = new Retrofit.Builder()
|
||
.client(NetInterfaceManager.getInstance().getOkHttpClient())
|
||
.baseUrl(BuildConfig.SCREEN_BASE_URL)
|
||
.addConverterFactory(GsonConverterFactory.create())
|
||
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
|
||
.build();
|
||
SendScreenStatusApi sendScreenStatusApi = retrofit.create(SendScreenStatusApi.class);
|
||
sendScreenStatusApi.sendScreenStatus(NetInterfaceManager.HTTP_KEY, Utils.getSerial(this), status)
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Observer<BaseResponse>() {
|
||
@Override
|
||
public void onSubscribe(Disposable d) {
|
||
Log.e("sendScreenStatus", "onSubscribe: ");
|
||
}
|
||
|
||
@Override
|
||
public void onNext(BaseResponse baseResponse) {
|
||
Log.e("sendScreenStatus", "onNext: " + baseResponse.msg);
|
||
}
|
||
|
||
@Override
|
||
public void onError(Throwable e) {
|
||
Log.e("sendScreenStatus", "onError: " + e.getMessage());
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
Log.e("sendScreenStatus", "onComplete: ");
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @param status
|
||
* @param time 和锁屏状态无关,记录设备使用时长的
|
||
*/
|
||
private void getLockState(String status, String time) {
|
||
if (JGYUtils.isOfficialVersion() || JGYUtils.getInstance().tagEmpty()) {
|
||
return;
|
||
}
|
||
NetInterfaceManager.getInstance()
|
||
.getLockState()
|
||
.getLockState(Utils.getSerial(this), status, time)
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Observer<ResponseBody>() {
|
||
@Override
|
||
public void onSubscribe(Disposable d) {
|
||
Log.e("getLockState", "onSubscribe: ");
|
||
}
|
||
|
||
@Override
|
||
public void onNext(ResponseBody responseBody) {
|
||
try {
|
||
JsonObject jsonObject = GsonUtils.getJsonObject(responseBody.string());
|
||
Log.e("getLockState", "onNext: " + jsonObject);
|
||
int code = jsonObject.get("code").getAsInt();
|
||
Log.e("getLockState", "onNext: code: " + code);
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onError(Throwable e) {
|
||
Log.e("getLockState", "onError: " + e.getMessage());
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
Log.e("getLockState", "onComplete: ");
|
||
}
|
||
});
|
||
}
|
||
|
||
private void registerBatteryReceiver() {
|
||
if (null == batteryReceiver) {
|
||
batteryReceiver = new BatteryReceiver();
|
||
}
|
||
IntentFilter filter = new IntentFilter();
|
||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
|
||
filter.addAction(Intent.ACTION_BATTERY_LOW);
|
||
filter.addAction(Intent.ACTION_BATTERY_OKAY);
|
||
filter.addAction(Intent.ACTION_POWER_CONNECTED);
|
||
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
|
||
registerReceiver(batteryReceiver, filter);
|
||
}
|
||
|
||
BatteryReceiver batteryReceiver;
|
||
|
||
private class BatteryReceiver extends BroadcastReceiver {
|
||
|
||
@Override
|
||
public void onReceive(Context context, Intent intent) {
|
||
String action = intent.getAction();
|
||
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
|
||
// 当前电量
|
||
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
|
||
// 最大电量
|
||
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
|
||
int elec = (level * 100) / scale;
|
||
// Log.e(TAG, "electricity:=" + elec + "%");
|
||
if (elec == 50) {
|
||
start.onstar(SystemClock.elapsedRealtime());
|
||
}
|
||
writeLog2File("batteryInfo.txt", recordBatteryInfo(intent));
|
||
// dumpBatterystats();
|
||
} else {
|
||
if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
|
||
LogDBManager.getInstance().creatOtherLog(LogDBManager.LOG_EVENT_BATTERY,
|
||
"开始充电", "electricity: " + JGYUtils.getInstance().getBatteryLevel() + "%");
|
||
}
|
||
if (Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
|
||
LogDBManager.getInstance().creatOtherLog(LogDBManager.LOG_EVENT_BATTERY,
|
||
"停止充电", "electricity: " + JGYUtils.getInstance().getBatteryLevel() + "%");
|
||
}
|
||
if (Intent.ACTION_BATTERY_LOW.equals(action)) {
|
||
LogDBManager.getInstance().creatOtherLog(LogDBManager.LOG_EVENT_BATTERY,
|
||
"电量偏低", "electricity: " + JGYUtils.getInstance().getBatteryLevel() + "%");
|
||
}
|
||
if (Intent.ACTION_BATTERY_OKAY.equals(action)) {
|
||
LogDBManager.getInstance().creatOtherLog(LogDBManager.LOG_EVENT_BATTERY,
|
||
"电量充足", "electricity: " + JGYUtils.getInstance().getBatteryLevel() + "%");
|
||
}
|
||
recordBattery("电量充足\t" + "electricity: " + JGYUtils.getInstance().getBatteryLevel() + "%");
|
||
setDefaultUSBStatus();
|
||
Log.e(TAG, action);
|
||
start.onstar(SystemClock.elapsedRealtime());
|
||
}
|
||
}
|
||
}
|
||
|
||
private void recordBattery(String s) {
|
||
Log.e(TAG, "recordBattery: " + writeLog2File("battery.txt", s));
|
||
}
|
||
|
||
private void dumpBatterystats() {
|
||
Observable.create(new ObservableOnSubscribe<StringBuffer>() {
|
||
@Override
|
||
public void subscribe(@NonNull ObservableEmitter<StringBuffer> emitter) throws Throwable {
|
||
StringBuffer shellExec = CmdUtil.shellExec(" dumpsys batterystats");
|
||
emitter.onNext(shellExec);
|
||
}
|
||
}).subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Observer<StringBuffer>() {
|
||
@Override
|
||
public void onSubscribe(@NonNull Disposable d) {
|
||
Log.e("debugTest", "onSubscribe: ");
|
||
}
|
||
|
||
@Override
|
||
public void onNext(StringBuffer result) {
|
||
Log.e("debugTest", "onNext: " + result.toString());
|
||
Log.e("debugTest", "writeLog2File: " + writeLog2File(result));
|
||
}
|
||
|
||
@Override
|
||
public void onError(@NonNull Throwable e) {
|
||
Log.e("debugTest", "onError: " + e.getMessage());
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
Log.e("debugTest", "onComplete: ");
|
||
}
|
||
});
|
||
}
|
||
|
||
private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000;
|
||
|
||
private String recordBatteryInfo(Intent intent) {
|
||
int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
|
||
int plugged = intent.getIntExtra(EXTRA_PLUGGED, BATTERY_PLUGGED_ANY);
|
||
int level = intent.getIntExtra(EXTRA_LEVEL, 0);
|
||
int scale = intent.getIntExtra(EXTRA_SCALE, 0);
|
||
int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
|
||
|
||
int maxChargingMicroAmp = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
|
||
int maxChargingMicroVolt = intent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
|
||
int batteryVolt = intent.getIntExtra(EXTRA_VOLTAGE, -1);
|
||
int temperature = intent.getIntExtra(EXTRA_TEMPERATURE, -1);
|
||
String batteryTechnologyDescript = intent.getStringExtra(EXTRA_TECHNOLOGY);
|
||
|
||
final int maxChargingMicroWatt;
|
||
|
||
if (maxChargingMicroVolt <= 0) {
|
||
maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
|
||
}
|
||
if (maxChargingMicroAmp > 0) {
|
||
// Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor
|
||
// to maintain precision equally on both factors.
|
||
maxChargingMicroWatt = (maxChargingMicroAmp / 1000)
|
||
* (maxChargingMicroVolt / 1000);
|
||
} else {
|
||
maxChargingMicroWatt = -1;
|
||
}
|
||
|
||
|
||
String statusStr = getStatus(status);
|
||
String healthStr = getHealth(health);
|
||
String pluggedStr = getPlugged(plugged);
|
||
String levelStr = getLevel(level);
|
||
String scaleStr = getLevel(scale);
|
||
String temperatureStr = getTemperature(temperature);
|
||
String batteryVoltStr = getBatteryVolt(batteryVolt);
|
||
String maxChargingMicroAmpStr = getMaxChargingMicroAmp(maxChargingMicroAmp);
|
||
String maxChargingMicroVoltStr = getMaxChargingMicroVolt(maxChargingMicroVolt);
|
||
|
||
int currentChargingCurrent = getCurrentChargingCurrent();
|
||
String currentChargingCurrentStr = getCurrentChargingCurrentStr(currentChargingCurrent);
|
||
int currentChargingVoltage = getCurrentChargingVoltage();
|
||
String currentChargingVoltageStr = getCurrentChargingVoltageStr(currentChargingVoltage);
|
||
|
||
|
||
String result = "\n" + getString(R.string.battery_current_level) + levelStr
|
||
+ "\n" + getString(R.string.battery_current_temperature) + temperatureStr
|
||
+ "\n" + getString(R.string.battery_current_volt) + batteryVoltStr
|
||
+ "\n" + getString(R.string.battery_current_charging_current) + currentChargingCurrentStr
|
||
+ "\n" + getString(R.string.battery_current_charging_voltage) + currentChargingVoltageStr
|
||
+ "\n" + getString(R.string.battery_status_titls) + statusStr
|
||
+ "\n" + getString(R.string.battery_plugged_titls) + pluggedStr
|
||
+ "\n" + getString(R.string.battery_max_charging_current) + maxChargingMicroAmpStr
|
||
+ "\n" + getString(R.string.battery_max_charging_voltage) + maxChargingMicroVoltStr
|
||
+ "\n" + getString(R.string.battery_health_titls) + healthStr
|
||
+ "\n" + getString(R.string.battery_max_level) + scaleStr
|
||
+ "\n" + getString(R.string.battery_technology_describing) + batteryTechnologyDescript
|
||
+ "\n充电速度 = " + maxChargingMicroWatt
|
||
+ "\n";
|
||
return result;
|
||
}
|
||
|
||
private String getCurrentChargingVoltageStr(int currentChargingVoltage) {
|
||
return String.format("%.3f V", currentChargingVoltage / 1000000.0);
|
||
}
|
||
|
||
private String getCurrentChargingCurrentStr(int currentChargingCurrent) {
|
||
return String.format("%.3f A", currentChargingCurrent / 1000.0);
|
||
}
|
||
|
||
/**
|
||
* 当前充电电流 mA
|
||
* <p>
|
||
* adb shell "cat /sys/class/power_supply/battery/BatteryAverageCurrent"
|
||
*/
|
||
private int getCurrentChargingCurrent() {
|
||
int result = 0;
|
||
BufferedReader br = null;
|
||
try {
|
||
String line;
|
||
br = new BufferedReader(new FileReader("/sys/class/power_supply/battery/BatteryAverageCurrent"));
|
||
if ((line = br.readLine()) != null) {
|
||
result = Integer.parseInt(line);
|
||
}
|
||
|
||
br.close();
|
||
} catch (FileNotFoundException e) {
|
||
e.printStackTrace();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
} finally {
|
||
if (br != null) {
|
||
try {
|
||
br.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 当前充电电压 uV
|
||
* <p>
|
||
* adb shell "cat /sys/class/power_supply/battery/batt_vol"
|
||
*/
|
||
private int getCurrentChargingVoltage() {
|
||
int result = 0;
|
||
BufferedReader br = null;
|
||
try {
|
||
String line;
|
||
br = new BufferedReader(new FileReader("/sys/class/power_supply/battery/batt_vol"));
|
||
if ((line = br.readLine()) != null) {
|
||
result = Integer.parseInt(line);
|
||
}
|
||
|
||
br.close();
|
||
} catch (FileNotFoundException e) {
|
||
e.printStackTrace();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
} finally {
|
||
if (br != null) {
|
||
try {
|
||
br.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
private String getMaxChargingMicroVolt(int maxChargingMicroVolt) {
|
||
return String.format("%.1f V", maxChargingMicroVolt / 1000000.0);
|
||
}
|
||
|
||
private String getMaxChargingMicroAmp(int maxChargingMicroAmp) {
|
||
return String.format("%.1f A", maxChargingMicroAmp / 1000000.0);
|
||
}
|
||
|
||
private String getBatteryVolt(int batteryVolt) {
|
||
return String.format("%.3f V", batteryVolt / 1000.0);
|
||
}
|
||
|
||
|
||
private String getTemperature(int temperature) {
|
||
return String.format("%.1f ℃", temperature / 10.0);
|
||
}
|
||
|
||
private String getLevel(int level) {
|
||
return String.format("%d %%", level);
|
||
}
|
||
|
||
private String getPlugged(int plugged) {
|
||
String result = getString(R.string.battery_plugged_any);
|
||
|
||
switch (plugged) {
|
||
case BATTERY_PLUGGED_ANY:
|
||
break;
|
||
case BATTERY_PLUGGED_AC:
|
||
result = getString(R.string.battery_plugged_ac);
|
||
break;
|
||
case BATTERY_PLUGGED_USB:
|
||
result = getString(R.string.battery_plugged_usb);
|
||
break;
|
||
case BATTERY_PLUGGED_WIRELESS:
|
||
result = getString(R.string.battery_plugged_wireless);
|
||
break;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
|
||
private String getHealth(int health) {
|
||
String result = getString(R.string.battery_health_unknow);
|
||
|
||
switch (health) {
|
||
case BATTERY_HEALTH_UNKNOWN:
|
||
break;
|
||
case BATTERY_HEALTH_GOOD:
|
||
result = getString(R.string.battery_health_good);
|
||
break;
|
||
case BATTERY_HEALTH_OVERHEAT:
|
||
result = getString(R.string.battery_health_overheat);
|
||
break;
|
||
case BATTERY_HEALTH_DEAD:
|
||
result = getString(R.string.battery_health_dead);
|
||
break;
|
||
case BATTERY_HEALTH_UNSPECIFIED_FAILURE:
|
||
result = getString(R.string.battery_health_unspecified_failure);
|
||
break;
|
||
case BATTERY_HEALTH_OVER_VOLTAGE:
|
||
result = getString(R.string.battery_health_over_voltage);
|
||
break;
|
||
case BATTERY_HEALTH_COLD:
|
||
result = getString(R.string.battery_health_cold);
|
||
break;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
|
||
private String getStatus(int status) {
|
||
String result = getString(R.string.battery_status_unknown);
|
||
|
||
switch (status) {
|
||
case BATTERY_STATUS_FULL:
|
||
result = getString(R.string.battery_status_full);
|
||
break;
|
||
case BATTERY_STATUS_NOT_CHARGING:
|
||
result = getString(R.string.battery_status_not_charging);
|
||
break;
|
||
case BATTERY_STATUS_DISCHARGING:
|
||
result = getString(R.string.battery_status_discharging);
|
||
break;
|
||
case BATTERY_STATUS_CHARGING:
|
||
result = getString(R.string.battery_status_charging);
|
||
break;
|
||
case BATTERY_STATUS_UNKNOWN:
|
||
break;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
private boolean writeLog2File(StringBuffer stringBuffer) {
|
||
String path = Environment.getExternalStorageDirectory() + File.separator + "aoleyun";
|
||
File file = new File(path);
|
||
if (!file.exists()) {
|
||
file.mkdirs();
|
||
}
|
||
try {
|
||
FileWriter fw = new FileWriter(path + File.separator + "log.txt", true);
|
||
fw.write(TimeUtils.transferLongToDate(System.currentTimeMillis()) + "\n" + stringBuffer.toString() + "\n");
|
||
fw.close();
|
||
return true;
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private boolean writeLog2File(String fileName, String string) {
|
||
String path = Environment.getExternalStorageDirectory() + File.separator + "aoleyun";
|
||
File file = new File(path);
|
||
if (!file.exists()) {
|
||
file.mkdirs();
|
||
}
|
||
try {
|
||
FileWriter fw = new FileWriter(path + File.separator + fileName, true);
|
||
fw.write(TimeUtils.transferLongToDate(System.currentTimeMillis()) + "\n" + string + "\n");
|
||
fw.close();
|
||
return true;
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private void setDefaultUSBStatus() {
|
||
String setting_usb = Settings.System.getString(getContentResolver(), "aole_usb_choose");
|
||
Log.e("setDefaultUSBStatus", "setting_usb---------" + setting_usb);
|
||
String usbStatus = "";
|
||
if (TextUtils.isEmpty(setting_usb)) {
|
||
usbStatus = CommonConfig.AOLE_ACTION_USB_USB_CHARGE;
|
||
} else {
|
||
switch (setting_usb) {
|
||
default:
|
||
case "usb_charge":
|
||
usbStatus = CommonConfig.AOLE_ACTION_USB_USB_CHARGE;
|
||
break;
|
||
case "usb_mtp":
|
||
usbStatus = CommonConfig.AOLE_ACTION_USB_USB_MTP;
|
||
break;
|
||
case "usb_midi":
|
||
usbStatus = CommonConfig.AOLE_ACTION_USB_USB_MIDI;
|
||
break;
|
||
}
|
||
}
|
||
Intent usbIntent = new Intent(usbStatus);
|
||
if (JGYUtils.getInstance().checkAppPlatform() == JGYUtils.TeclastP20sPlatform) {
|
||
usbIntent.setPackage("com.android.settings");
|
||
}
|
||
sendBroadcast(usbIntent);
|
||
}
|
||
|
||
//在这里处理任务执行中的状态,如进度进度条的刷新
|
||
@Download.onTaskRunning
|
||
protected void running(DownloadTask task) {
|
||
String filepath = task.getFilePath();
|
||
if (filepath.endsWith("apk")) {
|
||
String jsonString = task.getExtendField();
|
||
Gson gson = new Gson();
|
||
Type listType = new TypeToken<DownloadTaskInfo>() {
|
||
}.getType();
|
||
DownloadTaskInfo downloadTaskInfo = gson.fromJson(jsonString, listType);
|
||
String app_name = downloadTaskInfo.getApp_name();
|
||
String app_package = downloadTaskInfo.getApp_package();
|
||
Log.e("aria", "正在下载:" + task.getPercent() + ":" + task.getExtendField());
|
||
Toaster.show("下载启动:" + app_name + "\t" + task.getPercent() + "%");
|
||
} else if (filepath.endsWith(".zip")) {
|
||
Toaster.show("下载开机动画启动:" + "\t" + task.getPercent() + " % ");
|
||
}
|
||
}
|
||
|
||
@Download.onTaskComplete
|
||
void taskComplete(DownloadTask task) {
|
||
//在这里处理任务完成的状态
|
||
String filepath = task.getFilePath();
|
||
String extendField = task.getExtendField();
|
||
Log.e("taskComplete", "downloadPath::" + filepath);
|
||
Log.e("taskComplete", "extendField::" + extendField);
|
||
|
||
if (filepath.endsWith("apk")) {
|
||
String jsonString = task.getExtendField();
|
||
Gson gson = new Gson();
|
||
Type listType = new TypeToken<DownloadTaskInfo>() {
|
||
}.getType();
|
||
DownloadTaskInfo downloadTaskInfo = gson.fromJson(jsonString, listType);
|
||
String app_name = downloadTaskInfo.getApp_name();
|
||
String app_package = downloadTaskInfo.getApp_package();
|
||
Toaster.show(app_name + "\t:下载完成");
|
||
if (filepath.endsWith(".xapk")) {
|
||
XAPKUtils.getInstance().installXAPK(filepath);
|
||
Log.e(TAG, "taskComplete: " + filepath);
|
||
} else if (filepath.endsWith(".apk")) {
|
||
new Thread(() -> ApkUtils.installApp(GuardService.this, filepath)).start();
|
||
}
|
||
} else if (filepath.endsWith(".zip")) {
|
||
Log.e("taskComplete", "开机动画下载完成:" + task.getPercent() + ":" + task.getExtendField());
|
||
JGYUtils.getInstance().setBootanimationG10J(task.getFilePath());
|
||
}
|
||
}
|
||
|
||
@Download.onTaskFail
|
||
void taskFail(DownloadTask task, Exception e) {
|
||
try {
|
||
Aria.download(this).resumeAllTask();
|
||
final String filepath = task.getFilePath();
|
||
final String packageName = task.getExtendField();
|
||
JsonObject jsonObject = GsonUtils.getJsonObject(task.getExtendField());
|
||
String app_name = jsonObject.get("app_name").getAsString();
|
||
String app_package = jsonObject.get("app_package").getAsString();
|
||
Log.e("aria", "taskFail: " + packageName + "filepath: " + filepath + "Exception: " + e.getMessage());
|
||
// Toaster.show(app_name + "\t:下载失败");
|
||
} catch (Exception e1) {
|
||
Log.e("aria", "taskFail: " + e1.getMessage());
|
||
}
|
||
}
|
||
}
|
||
|