Commit 4a2f1ade by patpat

增加扫码更新和远程更新功能

parent 9b6dfd52
......@@ -10,8 +10,8 @@ android {
applicationId "com.bgycc.smartcanteen"
minSdkVersion 21
targetSdkVersion 22
versionCode 2
versionName "1.0.2"
versionCode 5
versionName "1.0.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
......@@ -20,6 +20,22 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
signingConfigs {
canteen {
keyAlias 'town'
keyPassword 'maxrocky'
storeFile file('./canteen.jks')
storePassword 'maxrocky'
}
buildTypes {
debug {
signingConfig signingConfigs.canteen
}
release {
signingConfig signingConfigs.canteen
}
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
......
File added
......@@ -7,16 +7,18 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".App"
android:allowBackup="true"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<activity android:name=".activity.MainActivity" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
......@@ -41,6 +43,16 @@
<!--<category android:name="android.intent.category.DEFAULT" />-->
<!--</intent-filter>-->
<!--</receiver>-->
<!--<receiver android:name=".receiver.UpdateReceiver"-->
<!--android:label="@string/app_name">-->
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.PACKAGE_ADDED" />-->
<!--<action android:name="android.intent.action.PACKAGE_REPLACED" />-->
<!--<data android:scheme="package" />-->
<!--</intent-filter>-->
<!--</receiver>-->
</application>
</manifest>
\ No newline at end of file
package com.bgycc.smartcanteen
import android.Manifest
import android.app.ActivityManager
import android.app.Application
import android.content.Context
import com.bgycc.smartcanteen.Storage.PayStorage
import com.bgycc.smartcanteen.action.UpdateAction
import com.bgycc.smartcanteen.helper.TimerHelper
import com.bgycc.smartcanteen.helper.WifiHelpler
import com.bgycc.smartcanteen.util.LogUtil
import com.bgycc.smartcanteen.module.Device
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.PermissionUtils
import java.lang.Exception
import kotlin.system.exitProcess
......@@ -52,6 +56,8 @@ class App : Application() {
WifiHelpler.initialize(this)
PayStorage.initialize(this)
Device.initialize(this)
PermissionUtils.permission(PermissionConstants.STORAGE).request()
UpdateAction.getDefault().initialize()
}
fun isMainProcess(): Boolean {
......
......@@ -16,6 +16,7 @@ public enum ActionEnum {
CONFIG_LOG,
CONFIG_POWER,
CONFIG_WIFI,
CONFIG_UPDATE,
CONFIG_SERVER,
CONFIG_PAYCODE_RULE
}
package com.bgycc.smartcanteen.action
import com.bgycc.smartcanteen.event.SettingStateEvent
import com.bgycc.smartcanteen.helper.TimerHelper
import com.blankj.utilcode.util.*
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject
import java.io.BufferedInputStream
import java.io.File
import java.io.IOException
import java.util.concurrent.TimeUnit
class UpdateAction private constructor() : Action(ActionEnum.CONFIG_UPDATE.name) {
companion object {
private val TAG = UpdateAction::class.java.simpleName
private val FILE_UPDATE_APK = File(PathUtils.getExternalStoragePath(), "SmartCanteen-update.apk")
private var sDefault: UpdateAction? = null
@Synchronized fun getDefault(): UpdateAction {
if (sDefault == null) {
sDefault = UpdateAction()
}
return sDefault!!
}
}
private val mHttp = OkHttpClient.Builder()
.connectTimeout(6, TimeUnit.SECONDS)
.readTimeout(6, TimeUnit.SECONDS)
.writeTimeout(6, TimeUnit.SECONDS)
.build()
private fun finish() {
TimerHelper.timeout({
state = State.INITED
EventBus.getDefault().post(SettingStateEvent(-1))
}, 3000)
}
fun initialize() {
FileUtils.delete(FILE_UPDATE_APK)
}
fun exec(data: JSONObject) {
if (state != State.INITED) return
val url: String
try {
url = data.getString("url")
if (url.isEmpty()) return
FILE_UPDATE_APK.delete()
FILE_UPDATE_APK.parentFile.mkdirs()
} catch (e: IOException) {
return
}
EventBus.getDefault().post(SettingStateEvent(1, "正在下载更新包"))
state = State.STARTED
val request = Request.Builder().url(url).build()
mHttp.newCall(request).enqueue(object : okhttp3.Callback {
override fun onFailure(call: Call, e: IOException) {
EventBus.getDefault().post(SettingStateEvent(1, "更新包下载失败"))
finish()
}
override fun onResponse(call: Call, response: Response) {
try {
val body = response.body()!!
val success = FileIOUtils.writeFileFromIS(FILE_UPDATE_APK, BufferedInputStream(body.byteStream()))
if (success) {
EventBus.getDefault().post(SettingStateEvent(99, "安装更新包"))
AppUtils.installApp(FILE_UPDATE_APK)
finish()
}
} catch (e: Exception) {}
EventBus.getDefault().post(SettingStateEvent(99, "更新包安装失败"))
finish()
}
})
}
}
\ No newline at end of file
package com.bgycc.smartcanteen.action
import android.util.Log
import com.bgycc.smartcanteen.event.WifiStateEvent
import com.bgycc.smartcanteen.event.SettingStateEvent
import com.bgycc.smartcanteen.helper.TimerHelper
import com.bgycc.smartcanteen.helper.WifiHelpler
import org.greenrobot.eventbus.EventBus
......@@ -26,7 +26,7 @@ class WifiAction private constructor() : Action(ActionEnum.CONFIG_WIFI.name) {
fail("")
TimerHelper.timeout({
state = State.INITED
EventBus.getDefault().post(WifiStateEvent(-1))
EventBus.getDefault().post(SettingStateEvent(-1))
}, 3000)
}
......@@ -41,23 +41,23 @@ class WifiAction private constructor() : Action(ActionEnum.CONFIG_WIFI.name) {
val identity = data.optString("identity")
WifiHelpler.connect(ssid, identity, pwd, type)
EventBus.getDefault().post(WifiStateEvent(1, "正在启动Wifi"))
EventBus.getDefault().post(SettingStateEvent(1, "正在启动Wifi"))
if (!WifiHelpler.setEnable(true)) {
EventBus.getDefault().post(WifiStateEvent(1, "无法启动Wifi"))
EventBus.getDefault().post(SettingStateEvent(1, "无法启动Wifi"))
TimerHelper.timeout({
state = State.INITED
EventBus.getDefault().post(WifiStateEvent(-1))
EventBus.getDefault().post(SettingStateEvent(-1))
}, 3000)
return
}
EventBus.getDefault().post(WifiStateEvent(10, "正在配置Wifi"))
EventBus.getDefault().post(SettingStateEvent(10, "正在配置Wifi"))
val config = WifiHelpler.createWifiConfiguration(ssid, identity, pwd, type)
if (config == null) {
EventBus.getDefault().post(WifiStateEvent(10, "Wifi参数有误"))
EventBus.getDefault().post(SettingStateEvent(10, "Wifi参数有误"))
TimerHelper.timeout({
state = State.INITED
EventBus.getDefault().post(WifiStateEvent(-1))
EventBus.getDefault().post(SettingStateEvent(-1))
}, 3000)
return
}
......@@ -67,23 +67,23 @@ class WifiAction private constructor() : Action(ActionEnum.CONFIG_WIFI.name) {
val netId = WifiHelpler.addNetwork(config)
if (!WifiHelpler.enableNetwork(netId)) throw Exception("无法应用Wifi配置")
} catch (e: Exception) {
EventBus.getDefault().post(WifiStateEvent(20, e.message))
EventBus.getDefault().post(SettingStateEvent(20, e.message))
TimerHelper.timeout({
state = State.INITED
EventBus.getDefault().post(WifiStateEvent(-1))
EventBus.getDefault().post(SettingStateEvent(-1))
}, 3000)
return
}
EventBus.getDefault().post(WifiStateEvent(30, "正在连接Wifi"))
EventBus.getDefault().post(SettingStateEvent(30, "正在连接Wifi"))
TimerHelper.loop({ id, isLastTime ->
if (isLastTime) {
if (state == State.STARTED) {
state = State.FAIL
EventBus.getDefault().post(WifiStateEvent(30, "无法连接Wifi"))
EventBus.getDefault().post(SettingStateEvent(30, "无法连接Wifi"))
TimerHelper.timeout({
state = State.INITED
EventBus.getDefault().post(WifiStateEvent(-1))
EventBus.getDefault().post(SettingStateEvent(-1))
}, 3000)
}
} else {
......@@ -92,10 +92,10 @@ class WifiAction private constructor() : Action(ActionEnum.CONFIG_WIFI.name) {
Log.i(TAG, "ip: " + info.ipAddress)
if (info.ipAddress != 0) {
state = State.SUCCESS
EventBus.getDefault().post(WifiStateEvent(30, "已连接Wifi"))
EventBus.getDefault().post(SettingStateEvent(30, "已连接Wifi"))
TimerHelper.timeout({
state = State.INITED
EventBus.getDefault().post(WifiStateEvent(-1))
EventBus.getDefault().post(SettingStateEvent(-1))
}, 3000)
TimerHelper.cancel(id)
}
......
......@@ -13,6 +13,8 @@ import android.view.animation.RotateAnimation
import com.bgycc.smartcanteen.App
import com.bgycc.smartcanteen.AppConfig
import com.bgycc.smartcanteen.R
import com.bgycc.smartcanteen.action.ActionEnum
import com.bgycc.smartcanteen.action.UpdateAction
import com.bgycc.smartcanteen.event.*
import com.bgycc.smartcanteen.helper.EthernetHelper
import com.bgycc.smartcanteen.helper.TimerHelper
......@@ -31,6 +33,7 @@ import org.greenrobot.eventbus.ThreadMode
import java.text.SimpleDateFormat
import java.util.*
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
class MainActivity : BaseActivity() {
......@@ -202,7 +205,7 @@ class MainActivity : BaseActivity() {
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: WifiStateEvent) {
fun onMessageEvent(event: SettingStateEvent) {
if (event.progress < 0) {
_setting.animate().setDuration(300).alpha(0f)
} else {
......
package com.bgycc.smartcanteen.event;
public class WifiStateEvent {
public class SettingStateEvent {
public int progress;
public String message;
public WifiStateEvent(int progress) {
public SettingStateEvent(int progress) {
this.progress = progress;
this.message = "";
}
public WifiStateEvent(int progress, String message) {
public SettingStateEvent(int progress, String message) {
this.progress = progress;
this.message = message;
}
......
package com.bgycc.smartcanteen.manager
import java.io.File
object CacheManager {
private var mCount = 0
get() { return field++ }
fun getDir(dirName: String = ""): String {
var path = String.format("%s/CacheManager/", System.getProperty("java.io.tmpdir")!!)
if (dirName.isNotEmpty()) {
path += File.separator + dirName.trimStart('/', '\\')
}
val file = File(path)
file.mkdirs()
return file.absolutePath
}
fun buildNewFilePath(extension: String): String {
var path = getDir()
path += String.format("/%d-%d", System.currentTimeMillis(), mCount)
if (extension.isNotEmpty()) {
if (!extension.startsWith(".")) {
path += "."
}
path += extension
}
return path
}
}
\ No newline at end of file
package com.bgycc.smartcanteen.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.Uri
import com.blankj.utilcode.util.AppUtils
import java.util.*
class UpdateReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
"android.intent.action.PACKAGE_ADDED", "android.intent.action.PACKAGE_REPLACED" -> {
val packageName = Objects.requireNonNull<Uri>(intent.data).encodedSchemeSpecificPart
if (packageName == AppUtils.getAppPackageName()) {
val intentToStart = context.packageManager.getLaunchIntentForPackage(packageName)
context.startActivity(intentToStart)
}
}
}
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ import com.bgycc.smartcanteen.App;
import com.bgycc.smartcanteen.AppConfig;
import com.bgycc.smartcanteen.action.ActionEnum;
import com.bgycc.smartcanteen.action.PayOfflineAction;
import com.bgycc.smartcanteen.action.UpdateAction;
import com.bgycc.smartcanteen.helper.TimerHelper;
import com.bgycc.smartcanteen.server.websocket.event.ConnectStateEvent;
import com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent;
......@@ -191,6 +192,12 @@ public class MainWebSocket extends WebSocketClient {
super.send(j.toString());
return;
}
// 设备更新
else if (ActionEnum.CONFIG_UPDATE.name().equals(action)) {
JSONObject data = json.optJSONObject(FieldEnum.data.name());
UpdateAction.Companion.getDefault().exec(data);
return;
}
ArrayList<Response> list = mSubscribeList.get(action);
if (list != null && list.size() > 0) {
......
......@@ -190,6 +190,8 @@ public class QRCodeTask {
if (action.startsWith(ActionEnum.CONFIG_.name())) {
if (action.equals(ActionEnum.CONFIG_WIFI.name())) {
WifiAction.Companion.getDefault().exec(data);
} else if (action.equals(ActionEnum.CONFIG_UPDATE.name())) {
UpdateAction.Companion.getDefault().exec(data);
} else if (action.equals(ActionEnum.CONFIG_LOG.name())) {
EventBus.getDefault().post(new LogEvent());
}
......
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