Commit 9b942d2b by patpat

TimerHelper增加Runnable传入类型

parent 222765ea
package com.bgycc.smartcanteen.action;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.util.Log;
import com.bgycc.smartcanteen.event.WifiStateEvent;
import com.bgycc.smartcanteen.helper.WifiHelpler;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
public class WifiAction extends Action {
private static final String TAG = WifiAction.class.getSimpleName();
private static WifiAction sInstance;
public static WifiAction getDefault() {
if (sInstance == null) {
sInstance = new WifiAction();
}
return sInstance;
}
protected WifiAction() {
super(ActionEnum.CONFIG_WIFI.name());
}
protected void fail() {
fail("");
timeout(new Runnable() {
@Override
public void run() {
setState(State.INITED);
EventBus.getDefault().post(new WifiStateEvent(-1));
}
}, 3000);
}
public void exec(JSONObject data) {
if (getState() != State.INITED) return;
setState(State.STARTED);
try {
String ssid = data.getString("ssid");
String pwd = data.getString("pwd");
String type = data.optString("type", "wpa");
String identity = data.optString("identity");
WifiHelpler.connect(ssid, identity, pwd, type);
EventBus.getDefault().post(new WifiStateEvent(1, "正在启动Wifi"));
if (!WifiHelpler.setEnable(true)) {
EventBus.getDefault().post(new WifiStateEvent(1, "无法启动Wifi"));
timeout(new Runnable() {
@Override
public void run() {
setState(State.INITED);
EventBus.getDefault().post(new WifiStateEvent(-1));
}
}, 3000);
return;
}
EventBus.getDefault().post(new WifiStateEvent(10, "正在配置Wifi"));
WifiConfiguration config = WifiHelpler.createWifiConfiguration(ssid, identity, pwd, type);
if (config == null) {
EventBus.getDefault().post(new WifiStateEvent(10, "Wifi参数有误"));
timeout(new Runnable() {
@Override
public void run() {
setState(State.INITED);
EventBus.getDefault().post(new WifiStateEvent(-1));
}
}, 3000);
return;
}
try {
if (!WifiHelpler.removeWifiConfiguration(ssid)) throw new Exception("无法修改Wifi配置");
int netId = WifiHelpler.addNetwork(config);
if (!WifiHelpler.enableNetwork(netId)) throw new Exception("无法应用Wifi配置");
} catch (Exception e) {
EventBus.getDefault().post(new WifiStateEvent(20, e.getMessage()));
timeout(new Runnable() {
@Override
public void run() {
setState(State.INITED);
EventBus.getDefault().post(new WifiStateEvent(-1));
}
}, 3000);
return;
}
EventBus.getDefault().post(new WifiStateEvent(30, "正在连接Wifi"));
loop(new Runnable() {
boolean finish = false;
@Override
public void run() {
if (finish) return;
WifiInfo info = WifiHelpler.getWifiInfo();
if (info != null) {
Log.i(TAG, "ip: " + info.getIpAddress());
if (info.getIpAddress() != 0) {
finish = true;
setState(State.SUCCESS);
EventBus.getDefault().post(new WifiStateEvent(30, "已连接Wifi"));
timeout(new Runnable() {
@Override
public void run() {
setState(State.INITED);
EventBus.getDefault().post(new WifiStateEvent(-1));
}
}, 3000);
}
}
}
}, new Runnable() {
@Override
public void run() {
if (getState() == State.STARTED) {
setState(State.FAIL);
EventBus.getDefault().post(new WifiStateEvent(30, "无法连接Wifi"));
timeout(new Runnable() {
@Override
public void run() {
setState(State.INITED);
EventBus.getDefault().post(new WifiStateEvent(-1));
}
}, 3000);
}
}
}, 10, 1000);
} catch (Exception e) {
}
}
}
......@@ -30,16 +30,31 @@ object TimerHelper {
} catch (e: Exception) {}
}
fun timeout(runnable: Runnable, time: Long): Long {
return timeout({runnable.run()}, time)
}
fun timeout(runnable: () -> Unit, time: Long): Long {
val id = mId
mFutureList.put(id, mScheduledExecutorService.schedule({
mFutureList.remove(id)
runnable()
try {
runnable()
} catch (e: Exception) {
Log.w(TAG, "timeout id: $id times error: ${e.message}")
}
}, time, TimeUnit.MILLISECONDS))
return id
}
fun loop(runnable: (id: Long) -> Boolean, period: Long, times: Int = -1): Long {
fun loop(runnable: Runnable, period: Long, times: Int = -1): Long {
return loop({ _: Long, _: Boolean ->
runnable.run()
true
}, period, times)
}
fun loop(runnable: (id: Long, isLastTime: Boolean) -> Boolean, period: Long, times: Int = -1): Long {
val id = mId
mFutureList.put(id, mScheduledExecutorService.scheduleWithFixedDelay(object: Runnable {
var vTimes = 0
......@@ -51,24 +66,15 @@ object TimerHelper {
return
}
}
if (!runnable(id)) {
cancel(id)
try {
if (!runnable(id, vTimes == times)) {
cancel(id)
}
} catch (e: Exception) {
Log.w(TAG, "loop id: $id times error: ${e.message}")
}
}
}, 0, period, TimeUnit.MILLISECONDS))
return id
}
fun test() {
Log.i(TAG, "start")
timeout({
Log.i(TAG, "timeout")
}, 1000)
loop({
Log.i(TAG, "loop")
return@loop true
}, 1000, 4)
}
}
\ No newline at end of file
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