Commit 1dee9de5 by pye52

wifi及shell指令执行失败后不再重试,并且都返回Resul.Success

parent a41816e3
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "com.bgycc.smartcanteen"
minSdkVersion 22
targetSdkVersion 29
versionCode 140
versionName "1.4.0"
targetSdkVersion 30
versionCode 141
versionName "1.4.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
......@@ -109,7 +109,7 @@ dependencies {
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
def work_version = "2.3.4"
def work_version = "2.4.0"
implementation "androidx.work:work-runtime:$work_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
......@@ -121,7 +121,7 @@ dependencies {
def okhttp_version = "4.7.2"
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"
def retrofit_version = "2.8.1"
def retrofit_version = '2.9.0'
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation "com.squareup.retrofit2:converter-scalars:$retrofit_version"
......
......@@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.bgycc.smartcanteen">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
......
......@@ -4,6 +4,7 @@ import androidx.work.BackoffPolicy;
import androidx.work.Data;
import androidx.work.ListenableWorker;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkRequest;
import com.bgycc.smartcanteen.entity.Command;
import com.google.gson.Gson;
......@@ -66,6 +67,7 @@ public class CommandHelper {
public static OneTimeWorkRequest createWorker(Gson gson, Command command, String deviceSN) {
OneTimeWorkRequest worker = null;
Class<? extends ListenableWorker> workerClass = null;
boolean needRetry = true;
switch (command.getAction()) {
case LOG_UPLOAD:
workerClass = LogCommandWorker.class;
......@@ -75,22 +77,30 @@ public class CommandHelper {
break;
case CONFIG_WIFI:
workerClass = WifiConfigCommandWorker.class;
// wifi配置任务不应该重试
needRetry = false;
break;
case EXEC_SHELL:
workerClass = ShellCommandWorker.class;
// shell任务也不应该重试
needRetry = false;
break;
}
if (workerClass != null) {
Data inputData = CommandWorker.createBaseData(gson.toJson(command), deviceSN);
worker = new OneTimeWorkRequest.Builder(workerClass)
WorkRequest.Builder<OneTimeWorkRequest.Builder, OneTimeWorkRequest> builder;
builder = new OneTimeWorkRequest.Builder(workerClass)
.setInputData(inputData)
.addTag(command.getAction())
.setBackoffCriteria(
.addTag(command.getAction());
if (needRetry) {
worker = builder.setBackoffCriteria(
BackoffPolicy.LINEAR,
OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
TimeUnit.MILLISECONDS
)
TimeUnit.MILLISECONDS)
.build();
} else {
worker = builder.build();
}
}
return worker;
}
......
......@@ -35,7 +35,7 @@ public class ShellCommandWorker extends CommandWorker {
@Override
public Result doWork() {
if (commandShell.getData() == null) {
return failed("shell指令非法");
return success("shell指令非法");
}
CommandShell.CommandShellData data = commandShell.getData();
byte[] decryptAES = EncryptUtils.decryptHexStringAES(data.getShell(), KEY.getBytes(), TRANSFORMATION, IV.getBytes());
......
......@@ -26,7 +26,7 @@ public class WifiConfigCommandWorker extends CommandWorker {
public Result doWork() {
CommandWifiConfig.CommandWifiConfigData data = wifiConfig.getData();
if (data == null) {
return failed();
return success();
}
if (!NetworkUtils.isWifiEnabled()) {
......@@ -39,7 +39,7 @@ public class WifiConfigCommandWorker extends CommandWorker {
Thread.sleep(DEFAULT_DELAY);
} catch (Exception ignored) {
}
return failed(failedMessage);
return success(failedMessage);
}
}
......@@ -73,8 +73,8 @@ public class WifiConfigCommandWorker extends CommandWorker {
}
} catch (Exception e) {
LogUtils.e(TAG, "链接wifi过程出错: " + e.getMessage(), e);
return failed(e.getMessage());
return success(e.getMessage());
}
return failed("无法连接Wifi");
return success("无法连接Wifi");
}
}
package com.bgycc.smartcanteen.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
......@@ -196,6 +197,7 @@ public class NetworkUtils {
}
}
// 未做版本适配工作,目前使用机型都为6.0
public static void switchNetwork(ConnectivityManager.NetworkCallback callback) {
if (lastTryNetwork == NETWORK_ETHERNET) {
lastTryNetwork = NETWORK_WIFI;
......@@ -244,13 +246,14 @@ public class NetworkUtils {
return connect(createWifiConfiguration(ssid, null, pwd, null));
}
@SuppressLint("MissingPermission")
public static boolean connect(WifiConfiguration config) {
int netId = wifiManager.addNetwork(config);
if (!wifiManager.enableNetwork(netId, true)) {
wifiManager.addNetwork(config);
if (!wifiManager.enableNetwork(config.networkId, true)) {
return false;
}
wifiManager.reconnect();
return true;
return false;
}
public static boolean disconnect(int netId) {
......
......@@ -9,7 +9,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.android.tools.build:gradle:4.0.1'
}
}
......
......@@ -2,13 +2,13 @@ apply plugin: 'com.android.application'
def signed = "Daemon.apk"
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "com.bgycc.smartcanteen.daemon"
minSdkVersion 22
targetSdkVersion 29
targetSdkVersion 30
versionCode rootProject.ext.daemon_verson_code
versionName rootProject.ext.daemon_verson_name
......@@ -96,9 +96,14 @@ private String findAppAssetsDir() {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.blankj:utilcodex:1.29.0'
}
repositories {
mavenCentral()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment