Commit 222765ea by patpat

ButtonTask替换为kotlin

parent 29fa5e2b
package com.bgycc.smartcanteen.task;
import android.content.Context;
import android.media.AudioManager;
import android.util.Log;
import com.bgycc.smartcanteen.App;
import com.szxb.jni.libszxb;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ButtonTask {
private static final String TAG = ButtonTask.class.getSimpleName();
private static ButtonTask sInstance;
public static synchronized ButtonTask getInstance() {
if (sInstance == null) {
sInstance = new ButtonTask();
}
return sInstance;
}
private ScheduledExecutorService mScanExec;
private AudioManager mAudioManager;
private ButtonTask() {}
public void start() {
if (mScanExec == null) {
mAudioManager = (AudioManager) App.Companion.getDefault().getSystemService(Context.AUDIO_SERVICE);
mScanExec = Executors.newSingleThreadScheduledExecutor();
mScanExec.scheduleAtFixedRate(new LoopTimerTask(), 0, 200, TimeUnit.MILLISECONDS);
}
}
public void stop() {
mAudioManager = null;
if (mScanExec != null) {
mScanExec.shutdown();
mScanExec = null;
}
}
private class LoopTimerTask extends TimerTask {
@Override
public void run() {
try {
byte[] buf = new byte[5];
libszxb.devicekey(buf);
// Log.i(TAG, String.format("%02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3]));
if (buf[1] == 1) {
mAudioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
}
if (buf[2] == 1) {
mAudioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.bgycc.smartcanteen.task
import android.content.Context
import android.media.AudioManager
import android.util.Log
import com.bgycc.smartcanteen.App
import com.bgycc.smartcanteen.helper.TimerHelper
import com.szxb.jni.libszxb
object ButtonTask {
private val TAG = ButtonTask::class.java.simpleName
private var mTimerId: Long = -1
private var mAudioManager: AudioManager? = null
fun start() {
if (mTimerId < 0) {
mAudioManager = App.getDefault().getSystemService(Context.AUDIO_SERVICE) as AudioManager
mTimerId = TimerHelper.loop({
val buf = ByteArray(5)
libszxb.devicekey(buf)
Log.i(TAG, String.format("%02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3]));
if (buf[1].toInt() == 1) {
mAudioManager!!.adjustStreamVolume(
AudioManager.STREAM_SYSTEM,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_SHOW_UI
)
}
if (buf[2].toInt() == 1) {
mAudioManager!!.adjustStreamVolume(
AudioManager.STREAM_SYSTEM,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI
)
}
true
}, 200)
}
}
fun stop() {
mAudioManager = null
TimerHelper.cancel(mTimerId)
mTimerId = -1
}
}
\ 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