From 2702ddab42dad49d8696f26530946e82dd0658bf Mon Sep 17 00:00:00 2001 From: fanhuitong <981964879@qq.com> Date: Tue, 11 May 2021 17:57:46 +0800 Subject: [PATCH] =?UTF-8?q?version:=20update:=E6=9E=81=E5=85=89=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E4=BC=98=E5=8C=96=20fix:=20add:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../myappstore/activity/MainActivity.java | 6 +- .../myappstore/activity/MainPresenter.java | 36 +- .../myappstore/base/BaseApplication.java | 687 ++++++------------ .../jpush/TagAliasOperatorHelper.java | 2 +- .../myappstore/receiver/BootReceiver.java | 75 +- .../myappstore/server/GuardService.java | 254 ++++++- .../myappstore/server/MainService.java | 15 +- .../myappstore/server/StepService.java | 6 +- .../mjsheng/myappstore/utils/JGYUtils.java | 56 +- .../com/mjsheng/myappstore/utils/Utils.java | 15 - 10 files changed, 606 insertions(+), 546 deletions(-) diff --git a/app/src/main/java/com/mjsheng/myappstore/activity/MainActivity.java b/app/src/main/java/com/mjsheng/myappstore/activity/MainActivity.java index f4ae678..61aa115 100644 --- a/app/src/main/java/com/mjsheng/myappstore/activity/MainActivity.java +++ b/app/src/main/java/com/mjsheng/myappstore/activity/MainActivity.java @@ -41,8 +41,11 @@ import io.reactivex.annotations.NonNull; import io.reactivex.disposables.Disposable; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; +import rx.functions.Action1; public class MainActivity extends BaseActivity implements MainContact.MainView { + private static final String TAG = MainActivity.class.getSimpleName(); + @BindView(R.id.imageView) ImageView imageView; @BindView(R.id.tv_devsn) @@ -76,11 +79,11 @@ public class MainActivity extends BaseActivity implements MainContact.MainView { @BindView(R.id.tv_customversion) TextView tv_customversion; - private static final String TAG = MainActivity.class.getSimpleName(); private MainPresenter mPresenter; private boolean netWorkIsRunning = false; private boolean updateApp = false; + //jiguang public static boolean isForeground = false; //for receive customer msg from jpush server @@ -133,6 +136,7 @@ public class MainActivity extends BaseActivity implements MainContact.MainView { builder.show(); } + //执行所有接口耗时 long timeMillis; @Override diff --git a/app/src/main/java/com/mjsheng/myappstore/activity/MainPresenter.java b/app/src/main/java/com/mjsheng/myappstore/activity/MainPresenter.java index f518fd4..e9e2d13 100644 --- a/app/src/main/java/com/mjsheng/myappstore/activity/MainPresenter.java +++ b/app/src/main/java/com/mjsheng/myappstore/activity/MainPresenter.java @@ -384,12 +384,7 @@ public class MainPresenter implements MainContact.Presenter { @Override public void setJpushAlias() { JPushInterface.init(mContext); - TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean(); - tagAliasBean.action = ACTION_SET; - sequence++; - tagAliasBean.alias = Utils.getSerial(); - tagAliasBean.isAliasAction = true; - TagAliasOperatorHelper.getInstance().handleAction(mContext, sequence, tagAliasBean); + SetAlias(); String rid = JPushInterface.getRegistrationID(mContext); if (!TextUtils.isEmpty(rid)) { Log.e("setJpushAlias", "RegId:" + rid); @@ -398,6 +393,15 @@ public class MainPresenter implements MainContact.Presenter { mView.setAliasFinished(); } + private void SetAlias(){ + TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean(); + tagAliasBean.action = ACTION_SET; + sequence++; + tagAliasBean.alias = Utils.getSerial(); + tagAliasBean.isAliasAction = true; + TagAliasOperatorHelper.getInstance().handleAction(mContext, sequence, tagAliasBean); + } + @Override public void setJpushTags() { NetInterfaceManager.getInstance() @@ -414,14 +418,7 @@ public class MainPresenter implements MainContact.Presenter { JsonObject jsonObject = JsonParser.parseString(new Gson().toJson(response.data)).getAsJsonObject(); String batch = jsonObject.get("batch").getAsString(); if (!TextUtils.isEmpty(batch)) { - Set set = new HashSet(); - set.add(batch); - TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean(); - tagAliasBean.action = ACTION_SET; - sequence++; - tagAliasBean.tags = set; - tagAliasBean.isAliasAction = false; - TagAliasOperatorHelper.getInstance().handleAction(mContext, sequence, tagAliasBean); + setTag(batch); } else { Log.e("setJpushTags", "onNext: " + "batch empty"); } @@ -444,6 +441,17 @@ public class MainPresenter implements MainContact.Presenter { }); } + private void setTag(String batch){ + Set set = new HashSet(); + set.add(batch); + TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean(); + tagAliasBean.action = ACTION_SET; + sequence++; + tagAliasBean.tags = set; + tagAliasBean.isAliasAction = false; + TagAliasOperatorHelper.getInstance().handleAction(mContext, sequence, tagAliasBean); + } + @Override public void checkStoreUpdate() { checkUpdateStore(); diff --git a/app/src/main/java/com/mjsheng/myappstore/base/BaseApplication.java b/app/src/main/java/com/mjsheng/myappstore/base/BaseApplication.java index 6302fa7..2fae674 100644 --- a/app/src/main/java/com/mjsheng/myappstore/base/BaseApplication.java +++ b/app/src/main/java/com/mjsheng/myappstore/base/BaseApplication.java @@ -1,20 +1,9 @@ package com.mjsheng.myappstore.base; -import android.content.BroadcastReceiver; -import android.content.ComponentName; import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.content.res.Configuration; -import android.net.Uri; -import android.net.wifi.WifiManager; -import android.os.BatteryManager; -import android.os.Build; import android.os.Handler; import android.os.Looper; -import android.os.PowerManager; import android.os.Process; -import android.provider.Settings; import android.util.Log; import androidx.multidex.MultiDexApplication; @@ -22,7 +11,6 @@ import androidx.multidex.MultiDexApplication; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; -import com.amap.api.location.AMapLocationClient; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; import com.lzy.okgo.OkGo; @@ -37,23 +25,14 @@ import com.mjsheng.myappstore.manager.FileManager; import com.mjsheng.myappstore.manager.NetInterfaceManager; import com.mjsheng.myappstore.network.HTTPInterface; import com.mjsheng.myappstore.network.URLAddress; -import com.mjsheng.myappstore.network.api.newapi.GetLockStateApi; -import com.mjsheng.myappstore.receiver.BootReceiver; -import com.mjsheng.myappstore.receiver.NewAppReceiver; -import com.mjsheng.myappstore.server.GuardService; -import com.mjsheng.myappstore.server.MainService; -import com.mjsheng.myappstore.server.StepService; import com.mjsheng.myappstore.statistics.AppInformation; import com.mjsheng.myappstore.statistics.StatisticsInfo; import com.mjsheng.myappstore.utils.JGYUtils; -import com.mjsheng.myappstore.utils.NetworkUtils; import com.mjsheng.myappstore.utils.SystemUtils; import com.mjsheng.myappstore.utils.ToastUtil; import com.mjsheng.myappstore.utils.Utils; import com.mjsheng.myappstore.utils.XAPKUtils; -import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -61,15 +40,13 @@ import java.util.logging.Level; import cn.jpush.android.api.JPushInterface; import cn.jpush.android.api.JPushMessage; +import io.reactivex.Observable; +import io.reactivex.ObservableEmitter; +import io.reactivex.ObservableOnSubscribe; import io.reactivex.Observer; -import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; -import io.reactivex.schedulers.Schedulers; import okhttp3.Call; import okhttp3.Response; -import okhttp3.ResponseBody; -import rx.Observable; -import rx.functions.Action1; /** * Created by Administrator on 2016/2/17 0017. @@ -77,12 +54,10 @@ import rx.functions.Action1; //public class MyApplication extends MultiDexApplication implements Thread.UncaughtExceptionHandler { public class BaseApplication extends MultiDexApplication { - private final String TAG = BaseApplication.class.getSimpleName(); + private static final String TAG = BaseApplication.class.getSimpleName(); public static Context context; private static BaseApplication instance; - private Handler mHandler = new Handler(); - public static Context getAppContext() { return context; @@ -100,6 +75,7 @@ public class BaseApplication extends MultiDexApplication { @Override public void onCreate() { super.onCreate(); + context = getApplicationContext(); if (SystemUtils.isMainProcessName(this, Process.myPid())) { //非主进程不初始化 init(); @@ -111,17 +87,10 @@ public class BaseApplication extends MultiDexApplication { JGYUtils.init(this); XAPKUtils.init(this); FileManager.init(this); - JGYUtils.getInstance().hookWebView(); - Configuration config = getResources().getConfiguration(); + AmapManager.init(this); + ToastUtil.init(this); -// int smallestScreenWidthDp = config.smallestScreenWidthDp; -// Log.e("mjsheng", "smallestScreenWidthDp=" + smallestScreenWidthDp); -// CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() -// .setDefaultFontPath("fonts/1234.ttf") -// .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf") -// .setFontAttrId(R.attr.fontPath) -// .build() -// ); + JGYUtils.getInstance().hookWebView(); // 设置开启日志,发布时请关闭日志 JPushInterface.setDebugMode(true); @@ -130,272 +99,222 @@ public class BaseApplication extends MultiDexApplication { //JPushInterface.init(this); //解锁设备不需要初始化 - context = getApplicationContext(); - initOKHttp(); Aria.init(this); Aria.get(this).getDownloadConfig().setMaxTaskNum(1); Aria.get(this).getDownloadConfig().setConvertSpeed(true); - ToastUtil.init(this); if (!BuildConfig.DEBUG) { catchException(); } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { -// 8.0之后需要在 - registmNewAppReceiver(); - - startService(new Intent(this, MainService.class)); - startService(new Intent(this, StepService.class)); - startService(new Intent(this, GuardService.class)); - } - registerTimeReceiver(); - registerBatteryReceiver(); - registerNetworkReceiver(); - //忽略电池优化 -// ignoreBatteryOptimization(this); - AmapManager.init(this); +// Configuration config = getResources().getConfiguration(); +// int smallestScreenWidthDp = config.smallestScreenWidthDp; +// Log.e("mjsheng", "smallestScreenWidthDp=" + smallestScreenWidthDp); +// CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() +// .setDefaultFontPath("fonts/1234.ttf") +// .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf") +// .setFontAttrId(R.attr.fontPath) +// .build() +// ); + initAliasObservable(); + initTagObservable(); } - - /** - * 忽略电池优化 - */ - private void ignoreBatteryOptimization(Context context) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - - PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); - - boolean hasIgnored = powerManager.isIgnoringBatteryOptimizations(context.getPackageName()); - // 判断当前APP是否有加入电池优化的白名单,如果没有,弹出加入电池优化的白名单的设置对话框。 - if (!hasIgnored) { - Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); - intent.setData(Uri.parse("package:" + context.getPackageName())); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); - } - } - } - - 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 ScreenReceiver screenReceiver; - - //监听时间和日期变化 - public void registerTimeReceiver() { - screenReceiver = new ScreenReceiver(); - 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("android.intent.action.FACTORY_RESET"); - filter.addAction("android.intent.action.MASTER_CLEAR"); - registerReceiver(screenReceiver, filter); - } - - - - Long time1 = 0L; - Long time2 = 0L; - - Long time3 = 0L; - Long time4 = 0L; - - public class ScreenReceiver extends BroadcastReceiver { - - @Override - public void onReceive(final Context context, Intent intent) { - String action = intent.getAction(); - Log.e(TAG, "action:" + action); - if (action.equals(Intent.ACTION_SCREEN_ON)) { - long time = System.currentTimeMillis(); - AMapLocationClient aMapLocationClient = AmapManager.getInstance().getLocationClient(); - aMapLocationClient.stopLocation(); - aMapLocationClient.startLocation(); - getLockState("2", String.valueOf(time)); - } else if (action.equals(Intent.ACTION_USER_PRESENT)) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - /*在8.0以上 除了开机广播其他广播基本上没有用 - *当静默安装升级时,app不会被唤醒,只有通过显示广播唤醒 - * */ - mHandler.postDelayed(new Runnable() { - @Override - public void run() { - Intent intent1 = new Intent(BootReceiver.BOOT_COMPLETED); - intent1.setComponent(new ComponentName("com.jiaoguanyi.store", "com.mjsheng.myappstore.receiver.BootReceiver")); - // 携带数据 -// intent1.putExtra("test", "我是来测 A 应用的Android 8.0 系统静态广播的测试数据"); - // 启动发送广播 - sendBroadcast(intent1); - - time2 = System.currentTimeMillis(); - if ((time2 - time1) > 60 * 1000) { - //可能会多次触发,1分钟之内不执行 - //application中启动服务,startcommand执行会两次 - startService(new Intent(context, MainService.class)); - startService(new Intent(context, StepService.class)); - startService(new Intent(context, GuardService.class)); - time1 = time2; - } - } - }, 5000); - - } - } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { - mHandler.postDelayed(new Runnable() { - @Override - public void run() { - long time = System.currentTimeMillis(); - getLockState("1", String.valueOf(time)); - if (time3 - System.currentTimeMillis() >= 10000) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - Intent intent1 = new Intent(BootReceiver.BOOT_COMPLETED); - intent1.setComponent(new ComponentName("com.jiaoguanyi.store", "com.mjsheng.myappstore.receiver.BootReceiver")); - sendBroadcast(intent1); - deleteScreenshots(); - } - } - time3 = System.currentTimeMillis(); - } - }, 10000); - } else if (action.equals("android.intent.action.FACTORY_RESET") - || action.equals("android.intent.action.MASTER_CLEAR") - //MASTER_CLEAR 7.0可以监听 - || action.equals("android.intent.action.MASTER_CLEAR_NOTIFICATION") - || action.equals("android.intent.action.FORCE_FACTORY_RESET") - ) { - Log.e("ScreenReceiver", "MASTER_CLEAR"); - android.os.Process.killProcess(android.os.Process.myPid()); - System.exit(0); - } - } - } - - 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) { - startService(new Intent(getBaseContext(), MainService.class)); - } - } else if (Intent.ACTION_POWER_CONNECTED.equals(action) - || Intent.ACTION_POWER_DISCONNECTED.equals(action) - || Intent.ACTION_BATTERY_LOW.equals(action) - || Intent.ACTION_BATTERY_OKAY.equals(action) - ) { - Log.e(TAG, action); - startService(new Intent(getBaseContext(), MainService.class)); - } - } - } - - private void registerNetworkReceiver() { - if (null == netReceiver) { - netReceiver = new NetworkStateReceiver(); - } - IntentFilter filter = new IntentFilter(); - filter.addAction("android.net.ethernet.ETHERNET_STATE_CHANGED"); - filter.addAction("android.net.ethernet.STATE_CHANGE"); - filter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); - filter.addAction("android.net.wifi.WIFI_STATE_CHANGED"); - filter.addAction("android.net.wifi.STATE_CHANGE"); - filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); - registerReceiver(netReceiver, filter); - } - - private NetworkStateReceiver netReceiver; - - - public class NetworkStateReceiver extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - if (intent == null || intent.getAction() == null) { - Log.e(TAG, "NetworkStateReceiver: 异常"); - return; - } - if (NetworkUtils.isConnected(getBaseContext())) { - Log.e(TAG, "NetworkStateReceiver: 网络连接成功"); - startService(new Intent(getBaseContext(), MainService.class)); - } else { - Log.e(TAG, "NetworkStateReceiver: 网络连接失败"); - } - } - } - - private void deleteScreenshots() { - Log.e("File", "deleteScreenshots"); - String path = getExternalFilesDir("db").getAbsolutePath(); - File file = new File(path); - File[] files = file.listFiles(); - for (File f : files) { - if (f.isFile()) { - Log.e("File", f.getAbsolutePath()); - f.delete(); - } - } - } - - private void getLockState(String status, String time) { - if (JGYUtils.isOfficialVersion()) { + public void onAliasOperatorResult(JPushMessage jPushMessage) { + if (jPushMessage == null) { return; } - GetLockStateApi getLockState = NetInterfaceManager.getLockState(); - getLockState.getLockState(Utils.getSerial(), status, time) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Observer() { + int errorCode = jPushMessage.getErrorCode(); + onAliasResult.onResult(errorCode); + } + + public static void setJpushAlias() { + Log.e("jiguangInterface", "30s后重新设置alias"); + JPushInterface.setAlias(context, TagAliasOperatorHelper.sequence++, Utils.getSerial()); + } + + public void onTagOperatorResult(JPushMessage jPushMessage) { + if (jPushMessage == null) { + return; + } + int errorCode = jPushMessage.getErrorCode(); + onTagResult.onResult(errorCode); + } + + public static void setJpushTags() { + Log.e(TAG, "30s后重新设置tags"); + HTTPInterface.setJpushTags(context); + } + + synchronized public static void clean() { + //alias的绑定的设备超过10个,但是alias应该是一个设备对应一个,在重置设备后jpush的regid会变动,所以需要清除 + //https://docs.jiguang.cn/jpush/server/push/rest_api_v3_device/#_5 + HTTPInterface.cleanJpushAlias(Utils.getSerial()); + } + + private static void initAliasObservable() { + Log.e(TAG, "initAliasObservable: " ); + Observable.create(new ObservableOnSubscribe() { + @Override + public void subscribe(ObservableEmitter emitter) throws Exception { + onAliasResult = new OnAliasResult() { + @Override + public void onResult(int code) { + Log.e("initAliasObservable", "onResult: " + code); + emitter.onNext(code); + } + }; + } + }).throttleLast(30, TimeUnit.SECONDS) + .subscribe(new Observer() { @Override public void onSubscribe(Disposable d) { } @Override - public void onNext(ResponseBody responseBody) { - try { - com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(responseBody.string()); - int code = jsonObject.getInteger("code"); + public void onNext(Integer integer) { + Log.e("initAliasObservable", "onNext: " + integer); + String s = "alias:\t"; + switch (integer) { + case 0: + Log.e("jiguangInterface", s + "Alias绑定成功"); + break; + case 6001: + Log.e("jiguangInterface", s + "无效的设置"); + break; + case 6011: + Log.e("jiguangInterface", s + "短时间内操作过于频繁"); + break; + case 6013: + Log.e("jiguangInterface", s + "用户设备时间轴异常"); + ToastUtil.show("用户设备时间轴异常,修改后重新登陆\t" + s + "code:6013"); + break; + case 6022: + Log.e("jiguangInterface", s + "alias 操作正在进行中"); + break; + //需要重新设置 + case 6002: + Log.e("jiguangInterface", s + "设置超时,请重试"); + setJpushAlias(); + break; + case 6014: + Log.e("jiguangInterface", s + "服务器繁忙,建议重试"); + setJpushAlias(); + break; + case 6020: + Log.e("jiguangInterface", s + "建议过一段时间再设置"); + setJpushAlias(); + break; + case 6024: + Log.e("jiguangInterface", s + "服务器内部错误"); + setJpushAlias(); + break; + case 6017: + case 6027: + Log.e("jiguangInterface", s + "别名绑定的设备数超过限制"); + clean(); + setJpushAlias(); + break; + default: + break; + } - } catch (IOException e) { - e.printStackTrace(); + } + + @Override + public void onError(Throwable e) { + + } + + @Override + public void onComplete() { + + } + }); + } + + private static void initTagObservable() { + Log.e(TAG, "initTagObservable: " ); + Observable.create(new ObservableOnSubscribe() { + @Override + public void subscribe(ObservableEmitter emitter) { + onTagResult = new OnTagResult() { + @Override + public void onResult(int code) { + Log.e("initTagObservable", "onResult: " + code); + emitter.onNext(code); + } + }; + } + }).throttleLast(30, TimeUnit.SECONDS) + .subscribe(new Observer() { + @Override + public void onSubscribe(Disposable d) { + + } + + @Override + public void onNext(Integer integer) { + Log.e("initTagObservable", "onNext: " + integer); + String s = "tags:\t"; + switch (integer) { + case 0: + Log.e("jiguangInterface", s + "Tag绑定成功"); + break; + case 6001: + Log.e("jiguangInterface", s + "无效的设置"); + break; + case 6005: + Log.e("jiguangInterface", s + "某一个 tag 字符串不合法"); + ToastUtil.show("设备标签不合法,联系管理员修改\t" + "code:6005"); + break; + case 6006: + Log.e("jiguangInterface", s + "某一个 tag 超长"); + ToastUtil.show("设备标签过长,联系管理员修改\t" + "code:6006"); + break; + case 6007: + Log.e("jiguangInterface", s + "tags 数量超出限制"); + ToastUtil.show("设备标签数量超出限制,联系管理员修改\t" + "code:6007"); + break; + case 6008: + Log.e("jiguangInterface", s + "tag 超出总长度限制"); + ToastUtil.show("设备标签超出总长度限制,联系管理员修改\t" + "code:6008"); + break; + case 6011: + Log.e("jiguangInterface", s + "短时间内操作过于频繁"); + break; + case 6013: + Log.e("jiguangInterface", s + "用户设备时间轴异常"); + ToastUtil.show("用户设备时间轴异常,修改后重新登陆\t" + "code:6013"); + break; + case 6018: + Log.e("jiguangInterface", s + "Tags 过多"); + ToastUtil.show("设备标签数量超出限制,联系管理员修改\t" + "code:6018"); + break; + case 6021: + Log.e("jiguangInterface", s + "tags 操作正在进行中"); + break; + //需要重新设置 + case 6002: + Log.e("jiguangInterface", s + "设置超时,请重试"); + setJpushTags(); + break; + case 6014: + Log.e("jiguangInterface", s + "服务器繁忙,建议重试"); + setJpushTags(); + break; + case 6020: + Log.e("jiguangInterface", s + "建议过一段时间再设置"); + setJpushTags(); + break; + case 6024: + Log.e("jiguangInterface", s + "服务器内部错误"); + setJpushTags(); + break; + default: + break; } } @@ -411,153 +330,44 @@ public class BaseApplication extends MultiDexApplication { }); } + private static OnAliasResult onAliasResult; - public void onTagOperatorResult(JPushMessage jPushMessage) { - if (jPushMessage == null) { - return; - } - String s = "tags:\t"; - int errorCode = jPushMessage.getErrorCode(); - switch (errorCode) { - case 0: - Log.e("jiguangInterface", s + "Tag绑定成功"); - break; - case 6001: - Log.e("jiguangInterface", s + "无效的设置"); - break; - case 6005: - Log.e("jiguangInterface", s + "某一个 tag 字符串不合法"); - ToastUtil.show("设备标签不合法,联系管理员修改\t" + "code:6005"); - break; - case 6006: - Log.e("jiguangInterface", s + "某一个 tag 超长"); - ToastUtil.show("设备标签过长,联系管理员修改\t" + "code:6006"); - break; - case 6007: - Log.e("jiguangInterface", s + "tags 数量超出限制"); - ToastUtil.show("设备标签数量超出限制,联系管理员修改\t" + "code:6007"); - break; - case 6008: - Log.e("jiguangInterface", s + "tag 超出总长度限制"); - ToastUtil.show("设备标签超出总长度限制,联系管理员修改\t" + "code:6008"); - break; - case 6011: - Log.e("jiguangInterface", s + "短时间内操作过于频繁"); - break; - case 6013: - Log.e("jiguangInterface", s + "用户设备时间轴异常"); - ToastUtil.show("用户设备时间轴异常,修改后重新登陆\t" + "code:6013"); - break; - case 6018: - Log.e("jiguangInterface", s + "Tags 过多"); - ToastUtil.show("设备标签数量超出限制,联系管理员修改\t" + "code:6018"); - break; - case 6021: - Log.e("jiguangInterface", s + "tags 操作正在进行中"); - break; - - //需要重新设置 - case 6002: - Log.e("jiguangInterface", s + "设置超时,请重试"); - setJpushTags(); - break; - case 6014: - Log.e("jiguangInterface", s + "服务器繁忙,建议重试"); - setJpushTags(); - break; - case 6020: - Log.e("jiguangInterface", s + "建议过一段时间再设置"); - setJpushTags(); - break; - case 6024: - Log.e("jiguangInterface", s + "服务器内部错误"); - setJpushTags(); - break; - } + interface OnAliasResult { + void onResult(int code); } - public static void setJpushTags() { - Log.e("jiguangInterface", "10s后重新设置tags"); - Observable.timer(10000, TimeUnit.MILLISECONDS) - .observeOn(rx.android.schedulers.AndroidSchedulers.mainThread()) - .subscribe(new Action1() { + private static OnTagResult onTagResult; + + interface OnTagResult { + void onResult(int code); + } + + private void catchException() { + Thread.setDefaultUncaughtExceptionHandler( + new Thread.UncaughtExceptionHandler() { @Override - public void call(Long aLong) { - HTTPInterface.setJpushTags(context); + public void uncaughtException(Thread t, Throwable e) { + Log.e("捕获异常子线程:", Thread.currentThread().getName() + + "在:" + e.getStackTrace()[0].getClassName()); } - }); - } - - - public void onAliasOperatorResult(JPushMessage jPushMessage) { - if (jPushMessage == null) { - return; - } - String s = "alias:\t"; - int errorCode = jPushMessage.getErrorCode(); - switch (errorCode) { - case 0: - Log.e("jiguangInterface", s + "Alias绑定成功"); - break; - case 6001: - Log.e("jiguangInterface", s + "无效的设置"); - break; - case 6011: - Log.e("jiguangInterface", s + "短时间内操作过于频繁"); - break; - case 6013: - Log.e("jiguangInterface", s + "用户设备时间轴异常"); - ToastUtil.show("用户设备时间轴异常,修改后重新登陆\t" + s + "code:6013"); - break; - case 6022: - Log.e("jiguangInterface", s + "alias 操作正在进行中"); - break; - - - //需要重新设置 - case 6002: - Log.e("jiguangInterface", s + "设置超时,请重试"); - setJpushAlias(); - break; - case 6014: - Log.e("jiguangInterface", s + "服务器繁忙,建议重试"); - setJpushAlias(); - break; - case 6020: - Log.e("jiguangInterface", s + "建议过一段时间再设置"); - setJpushAlias(); - break; - case 6024: - Log.e("jiguangInterface", s + "服务器内部错误"); - setJpushAlias(); - break; - - case 6017: - case 6027: - Log.e("jiguangInterface", s + "别名绑定的设备数超过限制"); - clean(); - setJpushAlias(); - break; - } - } - - synchronized public static void setJpushAlias() { - Log.e("jiguangInterface", "10s后重新设置alias"); - Observable.timer(10000, TimeUnit.MILLISECONDS) - .observeOn(rx.android.schedulers.AndroidSchedulers.mainThread()) - .subscribe(new Action1() { - @Override - public void call(Long aLong) { - JPushInterface.setAlias(context, TagAliasOperatorHelper.sequence++, Utils.getSerial()); + } + ); + //下面是新增方法! + new Handler(Looper.getMainLooper()).post(new Runnable() { + @Override + public void run() { + while (true) { + try { + Looper.loop(); //会先执行这个方法,然后在执行下面的异常捕获方法! + } catch (Exception e) { + Log.e("捕获异常主线程:", Thread.currentThread().getName() + "在:" + e.getStackTrace()[0].getClassName()); + e.printStackTrace(); } - }); + } + } + }); } - public void clean() { - //alias的绑定的设备超过10个,但是alias应该是一个设备对应一个,在重置设备后jpush的regid会变动,所以需要清除 - //https://docs.jiguang.cn/jpush/server/push/rest_api_v3_device/#_5 - HTTPInterface.cleanJpushAlias(Utils.getSerial()); - } private boolean finished = false; @@ -717,34 +527,6 @@ public class BaseApplication extends MultiDexApplication { }); } - - private void catchException() { - Thread.setDefaultUncaughtExceptionHandler( - new Thread.UncaughtExceptionHandler() { - @Override - public void uncaughtException(Thread t, Throwable e) { - Log.e("捕获异常子线程:", Thread.currentThread().getName() + - "在:" + e.getStackTrace()[0].getClassName()); - } - } - ); - //下面是新增方法! - new Handler(Looper.getMainLooper()).post(new Runnable() { - @Override - public void run() { - while (true) { - try { - Looper.loop(); //会先执行这个方法,然后在执行下面的异常捕获方法! - } catch (Exception e) { - Log.e("捕获异常主线程:", Thread.currentThread().getName() + "在:" + e.getStackTrace()[0].getClassName()); - e.printStackTrace(); - } - } - } - }); - } - - // @Override // public void uncaughtException(Thread thread, Throwable ex) { //// Log.e(TAG, ex.getMessage(), new Exception(ex)); @@ -764,7 +546,6 @@ public class BaseApplication extends MultiDexApplication { // // } - // static List fileList = new ArrayList<>(); // // diff --git a/app/src/main/java/com/mjsheng/myappstore/jpush/TagAliasOperatorHelper.java b/app/src/main/java/com/mjsheng/myappstore/jpush/TagAliasOperatorHelper.java index bbf7b03..4b700a3 100644 --- a/app/src/main/java/com/mjsheng/myappstore/jpush/TagAliasOperatorHelper.java +++ b/app/src/main/java/com/mjsheng/myappstore/jpush/TagAliasOperatorHelper.java @@ -294,7 +294,7 @@ public class TagAliasOperatorHelper { setActionCache.remove(sequence); String logs = getActionStr(tagAliasBean.action)+" alias success"; Logger.i(TAG,logs); - Log.e("fht","JPush绑定成功"); + Log.e("fht","Alias绑定成功"); ExampleUtil.showToast(logs, context); }else{ String logs = "Failed to " + getActionStr(tagAliasBean.action)+" alias, errorCode:" + jPushMessage.getErrorCode(); diff --git a/app/src/main/java/com/mjsheng/myappstore/receiver/BootReceiver.java b/app/src/main/java/com/mjsheng/myappstore/receiver/BootReceiver.java index 6b8e45b..670780c 100644 --- a/app/src/main/java/com/mjsheng/myappstore/receiver/BootReceiver.java +++ b/app/src/main/java/com/mjsheng/myappstore/receiver/BootReceiver.java @@ -13,51 +13,50 @@ import com.mjsheng.myappstore.server.MainService; import com.mjsheng.myappstore.server.StepService; public class BootReceiver extends BroadcastReceiver { - private String TAG = BootReceiver.class.getSimpleName() + "myapp"; + private String TAG = BootReceiver.class.getSimpleName() + ":myappstore"; public static final String BOOT_COMPLETED = "com.jiaoguanyi.appstore.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context context, Intent intent) { Log.e(TAG, "action:" + intent.getAction()); - if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { - String packageList = Settings.System.getString(context.getContentResolver(), "qch_launcher_icon_app"); - if (TextUtils.isEmpty(packageList)) { - Settings.System.putString(context.getContentResolver(), "qch_launcher_icon_app", ""); - } - HTTPInterface.updateDeviceInfo(context); - startService(context); - } else if ( - intent.getAction().equals(BOOT_COMPLETED) - || intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED) - || intent.getAction().equals(Intent.ACTION_BATTERY_LOW) - || intent.getAction().equals(Intent.ACTION_BATTERY_OKAY) - || intent.getAction().equals(Intent.ACTION_POWER_CONNECTED) - || intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED) - || intent.getAction().equals(Intent.ACTION_DATE_CHANGED) - || intent.getAction().equals(Intent.ACTION_TIME_TICK) - || intent.getAction().equals(Intent.ACTION_USER_PRESENT) - || intent.getAction().equals(Intent.ACTION_SCREEN_OFF) - || intent.getAction().equals(Intent.ACTION_SCREEN_ON) - //测试 -// || intent.getAction().equals("android.hardware.usb.action.USB_ACCESSORY_ATTACHED") -// || intent.getAction().equals("android.hardware.usb.action.USB_ACCESSORY_DETACHED") -// || intent.getAction().equals("android.hardware.usb.action.USB_DEVICE_ATTACHED") -// || intent.getAction().equals("android.hardware.usb.action.USB_DEVICE_DETACHED") -// || intent.getAction().equals("android.intent.action.MEDIA_MOUNTED") -// || intent.getAction().equals("android.intent.action.MEDIA_CHECKING") -// || intent.getAction().equals("android.intent.action.MEDIA_UNMOUNTED") -// || intent.getAction().equals("android.intent.action.MEDIA_EJECT") -// || intent.getAction().equals("android.intent.action.MEDIA_UNMOUNTABLE") -// || intent.getAction().equals("android.intent.action.MEDIA_REMOVED") -// || intent.getAction().equals("android.intent.action.MEDIA_BAD_REMOVAL") - - ) { - //除了USER_PRESENT应该都收不到 + switch (intent.getAction()) { + default: + break; + case Intent.ACTION_BOOT_COMPLETED: + String packageList = Settings.System.getString(context.getContentResolver(), "qch_launcher_icon_app"); + if (TextUtils.isEmpty(packageList)) { + Settings.System.putString(context.getContentResolver(), "qch_launcher_icon_app", ""); + } + HTTPInterface.updateDeviceInfo(context); + startService(context); + break; + case BOOT_COMPLETED: + case Intent.ACTION_BATTERY_CHANGED: + case Intent.ACTION_BATTERY_LOW: + case Intent.ACTION_BATTERY_OKAY: + case Intent.ACTION_POWER_CONNECTED: + case Intent.ACTION_POWER_DISCONNECTED: + case Intent.ACTION_DATE_CHANGED: + case Intent.ACTION_TIME_TICK: + case Intent.ACTION_USER_PRESENT: + case Intent.ACTION_SCREEN_OFF: + case Intent.ACTION_SCREEN_ON: +// case "android.hardware.usb.action.USB_ACCESSORY_ATTACHED": +// case "android.hardware.usb.action.USB_ACCESSORY_DETACHED": +// case "android.hardware.usb.action.USB_DEVICE_ATTACHED": +// case "android.hardware.usb.action.USB_DEVICE_DETACHED": +// case "android.intent.action.MEDIA_MOUNTED": +// case "android.intent.action.MEDIA_CHECKING": +// case "android.intent.action.MEDIA_UNMOUNTED": +// case "android.intent.action.MEDIA_EJECT": +// case "android.intent.action.MEDIA_UNMOUNTABLE": +// case "android.intent.action.MEDIA_REMOVED": +// case "android.intent.action.MEDIA_BAD_REMOVAL": + //除了USER_PRESENT应该都收不到 // Log.e("fht", "BootReceiver MSG:" + intent.getAction()); - startService(context); + startService(context); + break; } - - } private void startService(Context context) { diff --git a/app/src/main/java/com/mjsheng/myappstore/server/GuardService.java b/app/src/main/java/com/mjsheng/myappstore/server/GuardService.java index 3a38a41..75981a3 100644 --- a/app/src/main/java/com/mjsheng/myappstore/server/GuardService.java +++ b/app/src/main/java/com/mjsheng/myappstore/server/GuardService.java @@ -19,6 +19,8 @@ import android.net.Network; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; +import android.os.BatteryManager; +import android.os.Build; import android.os.IBinder; import android.text.format.Formatter; import android.util.Log; @@ -27,11 +29,17 @@ import androidx.annotation.Nullable; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.amap.api.location.AMapLocationClient; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.task.DownloadTask; +import com.mjsheng.myappstore.manager.AmapManager; +import com.mjsheng.myappstore.network.api.newapi.GetLockStateApi; +import com.mjsheng.myappstore.receiver.BootReceiver; +import com.mjsheng.myappstore.receiver.NewAppReceiver; import com.mjsheng.myappstore.utils.JGYUtils; +import com.mjsheng.myappstore.utils.NetworkUtils; import com.mjsheng.myappstore.utils.XAPKUtils; import com.lzy.okgo.OkGo; import com.lzy.okgo.callback.StringCallback; @@ -51,8 +59,13 @@ import java.io.IOException; import java.io.InputStream; import java.util.List; +import io.reactivex.Observer; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; +import io.reactivex.schedulers.Schedulers; import okhttp3.Call; import okhttp3.Response; +import okhttp3.ResponseBody; /** @@ -63,6 +76,7 @@ import okhttp3.Response; */ 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) { @@ -86,15 +100,10 @@ public class GuardService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { - registerNetworkReceiver(); return new KeepAliveConnection.Stub() { }; - } - private Process exec; - private LogThread logThread; - @Override public void onCreate() { // try { @@ -107,9 +116,23 @@ public class GuardService extends Service { //// e.printStackTrace(); // Log.e("第一个是Logcat", e.getMessage()); // } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { +// 8.0之后需要在 + registmNewAppReceiver(); + startService(new Intent(this, MainService.class)); + startService(new Intent(this, StepService.class)); + startService(new Intent(this, GuardService.class)); + } + registerTimeReceiver(); + registerBatteryReceiver(); + registerNetworkReceiver(); + registerNetworkConnectedReceiver(); super.onCreate(); } + private Process exec; + private LogThread logThread; + //第一个是Logcat ,也就是我们想要获取的log日志 //第二个是 -s 也就是表示过滤的意思 //第三个就是 我们要过滤的类型 W表示warm ,我们也可以换成 D :debug, I:info,E:error等等 @@ -150,7 +173,6 @@ public class GuardService extends Service { try { os.close(); os = null; - } catch (IOException e) { // Do nothing } @@ -161,10 +183,9 @@ public class GuardService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { + Log.e(TAG, "onStartCommand: "); Aria.init(this); - Aria.get(this).getDownloadConfig().setMaxTaskNum(1); Aria.download(this).register(); - // startForeground(1, new Notification()); // 绑定建立链接 bindService(new Intent(this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT); return START_STICKY; @@ -176,6 +197,18 @@ public class GuardService extends Service { if (null != mNetworkChangedRecceiver) { unregisterReceiver(mNetworkChangedRecceiver); } + if (null != mNewAppReceiver) { + unregisterReceiver(mNewAppReceiver); + } + if (null != screenReceiver) { + unregisterReceiver(screenReceiver); + } + if (null != batteryReceiver) { + unregisterReceiver(batteryReceiver); + } + if (null != netReceiver) { + unregisterReceiver(netReceiver); + } } private NetworkChangedRecceiver mNetworkChangedRecceiver; @@ -246,8 +279,8 @@ public class GuardService extends Service { WifiInfo wifiInfo = wifiManager.getConnectionInfo(); //获取当前wifi名称 Log.e("NetworkChangedRecceiver", "连接到网络 " + wifiInfo.getSSID()); - startService(new Intent(context, MainService.class)); - Aria.download(this).resumeAllTask(); + startService(new Intent(context, MainService.class)); + Aria.download(this).resumeAllTask(); } } //wifi打开与否 @@ -262,6 +295,205 @@ public class GuardService extends Service { } } + 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 ScreenReceiver screenReceiver; + + //监听时间和日期变化 + public void registerTimeReceiver() { + screenReceiver = new ScreenReceiver(); + 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("android.intent.action.FACTORY_RESET"); + filter.addAction("android.intent.action.MASTER_CLEAR"); + registerReceiver(screenReceiver, filter); + } + + public class ScreenReceiver 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("com.jiaoguanyi.store", "com.mjsheng.myappstore.receiver.BootReceiver")); + // 携带数据 + //intent1.putExtra("test", "我是来测 A 应用的Android 8.0 系统静态广播的测试数据"); + // 启动发送广播 + sendBroadcast(intent1); + startService(new Intent(context, MainService.class)); + startService(new Intent(context, StepService.class)); + startService(new Intent(context, GuardService.class)); + } + break; + case Intent.ACTION_SCREEN_ON: { + long time = System.currentTimeMillis(); + AMapLocationClient aMapLocationClient = AmapManager.getInstance().getLocationClient(); + aMapLocationClient.stopLocation(); + aMapLocationClient.startLocation(); + getLockState("2", String.valueOf(time)); + break; + } + case Intent.ACTION_SCREEN_OFF: { + long time = System.currentTimeMillis(); + getLockState("1", String.valueOf(time)); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Intent intent1 = new Intent(BootReceiver.BOOT_COMPLETED); + intent1.setComponent(new ComponentName("com.jiaoguanyi.store", "com.mjsheng.myappstore.receiver.BootReceiver")); + sendBroadcast(intent1); + JGYUtils.getInstance().deleteScreenshots(); + } + break; + } + case "android.intent.action.FACTORY_RESET": + case "android.intent.action.MASTER_CLEAR": + case "android.intent.action.MASTER_CLEAR_NOTIFICATION": + case "android.intent.action.FORCE_FACTORY_RESET": + Log.e("ScreenReceiver", "MASTER_CLEAR"); + android.os.Process.killProcess(android.os.Process.myPid()); + System.exit(0); + break; + } + } + } + + private void getLockState(String status, String time) { + if (JGYUtils.isOfficialVersion()) { + return; + } + GetLockStateApi getLockState = NetInterfaceManager.getLockState(); + getLockState.getLockState(Utils.getSerial(), status, time) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Observer() { + @Override + public void onSubscribe(Disposable d) { + + } + + @Override + public void onNext(ResponseBody responseBody) { + try { + com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(responseBody.string()); + int code = jsonObject.getInteger("code"); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void onError(Throwable e) { + + } + + @Override + public void 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) { + startService(new Intent(getBaseContext(), MainService.class)); + } + } else if (Intent.ACTION_POWER_CONNECTED.equals(action) + || Intent.ACTION_POWER_DISCONNECTED.equals(action) + || Intent.ACTION_BATTERY_LOW.equals(action) + || Intent.ACTION_BATTERY_OKAY.equals(action) + ) { + Log.e(TAG, action); + startService(new Intent(getBaseContext(), MainService.class)); + } + } + } + + private void registerNetworkConnectedReceiver() { + if (null == netReceiver) { + netReceiver = new NetworkStateReceiver(); + } + IntentFilter filter = new IntentFilter(); + filter.addAction("android.net.ethernet.ETHERNET_STATE_CHANGED"); + filter.addAction("android.net.ethernet.STATE_CHANGE"); + filter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); + filter.addAction("android.net.wifi.WIFI_STATE_CHANGED"); + filter.addAction("android.net.wifi.STATE_CHANGE"); + filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); + registerReceiver(netReceiver, filter); + } + + private NetworkStateReceiver netReceiver; + + + public class NetworkStateReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + if (intent == null || intent.getAction() == null) { + Log.e(TAG, "NetworkStateReceiver: 异常"); + return; + } + if (NetworkUtils.isConnected(getBaseContext())) { + Log.e(TAG, "NetworkStateReceiver: 网络连接成功"); + startService(new Intent(getBaseContext(), MainService.class)); + } else { + Log.e(TAG, "NetworkStateReceiver: 网络连接失败"); + } + } + } + + //在这里处理任务执行中的状态,如进度进度条的刷新 @Download.onTaskRunning protected void running(DownloadTask task) { @@ -309,7 +541,7 @@ public class GuardService extends Service { .execute(new StringCallback() { @Override public void onSuccess(String s, Call call, okhttp3.Response response) { - Log.e("taskComplete", "onSuccess: " ); + Log.e("taskComplete", "onSuccess: "); } @Override diff --git a/app/src/main/java/com/mjsheng/myappstore/server/MainService.java b/app/src/main/java/com/mjsheng/myappstore/server/MainService.java index c5f9a17..3277334 100644 --- a/app/src/main/java/com/mjsheng/myappstore/server/MainService.java +++ b/app/src/main/java/com/mjsheng/myappstore/server/MainService.java @@ -54,7 +54,6 @@ public class MainService extends Service implements MainContact.MainView { private Start start; - private ObservableOnSubscribe subscribe = new ObservableOnSubscribe() { @Override public void subscribe(ObservableEmitter emitter) throws Exception { @@ -67,7 +66,7 @@ public class MainService extends Service implements MainContact.MainView { } }; - private Observer TimeObserver = new Observer() { + private Observer timeObserver = new Observer() { @Override public void onSubscribe(Disposable d) { @@ -92,7 +91,6 @@ public class MainService extends Service implements MainContact.MainView { } }; - public MainService() { } @@ -111,7 +109,7 @@ public class MainService extends Service implements MainContact.MainView { addShortcut(); Observable.create(subscribe) .throttleLast(60, TimeUnit.SECONDS) - .subscribe(TimeObserver); + .subscribe(timeObserver); // cleanLauncherCache(); super.onCreate(); } @@ -123,11 +121,11 @@ public class MainService extends Service implements MainContact.MainView { mPresenter.detachView(); } - long runningTime; //执行所有请求的时间 + long runningTime; - long startCommandTime = 0; //MainService上次执行时间 + long startCommandTime = 0; @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -146,7 +144,7 @@ public class MainService extends Service implements MainContact.MainView { // Log.e(TAG, "onStartCommand: " + (SystemClock.elapsedRealtime() - startCommandTime)); // } start.onstar(SystemClock.elapsedRealtime()); - Log.e("TimeObserver", "onStartCommand: " + (SystemClock.elapsedRealtime() - startCommandTime) + "ms"); + Log.e("timeObserver", "onStartCommand: " + (SystemClock.elapsedRealtime() - startCommandTime) + "ms"); } return START_STICKY; } @@ -272,7 +270,7 @@ public class MainService extends Service implements MainContact.MainView { } } - + //锁屏管控广播 public class LockScreenReceiver extends BroadcastReceiver { public static final String action_lock = "LockScreenReceiver_lockscreen"; public static final String action_unlock = "LockScreenReceiver_unlockscreen"; @@ -280,6 +278,7 @@ public class MainService extends Service implements MainContact.MainView { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); + Log.e("LockScreenReceiver", "onReceive: " + action); if (TextUtils.isEmpty(action)) { return; } diff --git a/app/src/main/java/com/mjsheng/myappstore/server/StepService.java b/app/src/main/java/com/mjsheng/myappstore/server/StepService.java index 79cd361..ecf4172 100644 --- a/app/src/main/java/com/mjsheng/myappstore/server/StepService.java +++ b/app/src/main/java/com/mjsheng/myappstore/server/StepService.java @@ -35,6 +35,8 @@ import java.net.URI; * @time Created by 2018/8/17 11:26 */ public class StepService extends Service { + private final static String TAG = StepService.class.getSimpleName(); + public JWebSocketClient client; private JWebSocketClientBinder mBinder = new JWebSocketClientBinder(); @@ -45,7 +47,6 @@ public class StepService extends Service { } } - private final static String TAG = StepService.class.getSimpleName(); private ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { @@ -75,8 +76,6 @@ public class StepService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { -// Utils.KillOTA(StepService.this); -// Utils.openOTA(StepService.this); // startForeground(1, new Notification()); // 绑定建立链接 // try { @@ -90,6 +89,7 @@ public class StepService extends Service { // } catch (Exception e) { // Log.e("fht", e.getMessage()); // } + //初始化websocket Log.e(TAG, "onStartCommand: "); initSocketClient(); diff --git a/app/src/main/java/com/mjsheng/myappstore/utils/JGYUtils.java b/app/src/main/java/com/mjsheng/myappstore/utils/JGYUtils.java index 6fdf17f..b7c2459 100644 --- a/app/src/main/java/com/mjsheng/myappstore/utils/JGYUtils.java +++ b/app/src/main/java/com/mjsheng/myappstore/utils/JGYUtils.java @@ -1,6 +1,8 @@ package com.mjsheng.myappstore.utils; import android.annotation.SuppressLint; +import android.app.ActivityManager; +import android.content.ComponentName; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; @@ -8,6 +10,7 @@ import android.content.IntentFilter; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; +import android.net.Uri; import android.os.BatteryManager; import android.os.Build; import android.os.PowerManager; @@ -43,8 +46,11 @@ import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; + + public class JGYUtils { private static final String TAG = JGYUtils.class.getSimpleName(); + private static JGYUtils sInstance; private Context mContext; public static final String ROOT_URL = BuildConfig.ROOT_URL; @@ -586,11 +592,11 @@ public class JGYUtils { int versionCode = jsonObject.get("app_version_code").getAsInt(); String packageName = jsonObject.get("app_package").getAsString(); String app_name = jsonObject.get("app_name").getAsString(); - String app_id = jsonObject.get("app_id").getAsString(); +// String app_id = jsonObject.get("app_id").getAsString(); JSONObject object = new JSONObject(); object.put("app_name", app_name); object.put("app_package", packageName); - object.put("app_id", app_id); +// object.put("app_id", app_id); PackageManager pm = mContext.getPackageManager(); PackageInfo packageInfo = null; try { @@ -839,4 +845,50 @@ public class JGYUtils { } } + public void deleteScreenshots() { + Log.e("File", "deleteScreenshots"); + String path = mContext.getExternalFilesDir("db").getAbsolutePath(); + File file = new File(path); + File[] files = file.listFiles(); + for (File f : files) { + if (f.isFile()) { + Log.e("File", f.getAbsolutePath()); + f.delete(); + } + } + } + + /** + * 忽略电池优化 + */ + private void ignoreBatteryOptimization(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + + PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); + + boolean hasIgnored = powerManager.isIgnoringBatteryOptimizations(context.getPackageName()); + // 判断当前APP是否有加入电池优化的白名单,如果没有,弹出加入电池优化的白名单的设置对话框。 + if (!hasIgnored) { + Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); + intent.setData(Uri.parse("package:" + context.getPackageName())); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + mContext.startActivity(intent); + } + } + } + + public void KillOTA() { + ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); + manager.killBackgroundProcesses("com.adups.fota"); + CmdUtil.execute("am force-stop " + "com.adups.fota"); + } + + public void openOTA() { + Intent intent = new Intent(Intent.ACTION_MAIN); + /**知道要跳转应用的包命与目标Activity*/ + ComponentName componentName = new ComponentName("com.adups.fota", "com.adups.fota.GoogleOtaClient"); + intent.setComponent(componentName); + mContext.startActivity(intent); + } + } diff --git a/app/src/main/java/com/mjsheng/myappstore/utils/Utils.java b/app/src/main/java/com/mjsheng/myappstore/utils/Utils.java index 04c26ec..221019b 100644 --- a/app/src/main/java/com/mjsheng/myappstore/utils/Utils.java +++ b/app/src/main/java/com/mjsheng/myappstore/utils/Utils.java @@ -1155,21 +1155,6 @@ public class Utils { return result; } - - public static void KillOTA(Context context) { - ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); - manager.killBackgroundProcesses("com.adups.fota"); - CmdUtil.execute("am force-stop " + "com.adups.fota"); - } - - public static void openOTA(Context context) { - Intent intent = new Intent(Intent.ACTION_MAIN); - /**知道要跳转应用的包命与目标Activity*/ - ComponentName componentName = new ComponentName("com.adups.fota", "com.adups.fota.GoogleOtaClient"); - intent.setComponent(componentName); - context.startActivity(intent); - } - public static String getAndroiodScreenProperty(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics();