package com.anyun.exam.lib; import android.app.Service; import android.content.Intent; import android.content.SharedPreferences; import android.os.IBinder; import android.os.Process; import android.os.RemoteCallbackList; import android.os.RemoteException; import android.util.Base64; import android.util.Log; import com.anyun.exam.lib.util.DESUtil; import com.anyun.exam.lib.util.NetUtils; import com.anyun.exam.lib.util.Speaker; import com.anyun.exam.lib.util.SpeakerCallback; import androidx.annotation.Nullable; import java.io.UnsupportedEncodingException; import java.util.concurrent.atomic.AtomicBoolean; /** * MyApplication2 * Created by lzw on 2019/6/5. 13:22:46 * 邮箱:632393724@qq.com * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class RemoteService extends Service { private static final String TAG = "RemoteService"; /**服务是否销毁标志*/ private AtomicBoolean mIsServiceDestroyed = new AtomicBoolean(false); private RemoteCallbackList mListenerList = new RemoteCallbackList(); private IListenerInterface mListener; private int msgId = 0; private Speaker speaker = null; private boolean ttsInitSucc = false; private IRemoteInterface.Stub iRemoteInterface = new IRemoteInterface.Stub(){ @Override public void registListener(IListenerInterface i) throws RemoteException { //注册回调方法 mListenerList.register(i); } @Override public void unRegistListener(IListenerInterface i) throws RemoteException { mListenerList.unregister(i); } @Override public void add() throws RemoteException { Log.i(TAG,"客户端调用服务端方法"); } @Override public void SendCmd(int cmd, String value) throws RemoteException { if (cmd == 0x8100) { try { byte [] file = value.getBytes("ISO-8859-1"); Log.d(TAG, byte2hex(file)); MainProcBinMsgEntry(cmd, file, file.length); } catch (UnsupportedEncodingException e) { Log.d(TAG, "UnsupportedEncodingException"); } } else { Log.d(TAG, "Process " + Process.myPid() + " Thread " + Thread.currentThread().getId() + " RecvMsgFromMainProc cmd = " + String.format(" %04X ", cmd) + " length " + value.length() + " value " + value.trim()); MainProcMsgEntry(cmd, value.trim()); } } }; @Nullable @Override public IBinder onBind(Intent intent) { return iRemoteInterface; } @Override public void onCreate() { super.onCreate(); Log.i(TAG,"onCreate()"); speaker = new Speaker(/*getApplicationContext()*/this, new TTSCallback()); new Thread(new StartNative()).start(); new Thread(new TestThread()).start(); } class TestThread implements Runnable { @Override public void run() { try { Thread.sleep(500); } catch (InterruptedException e) { } } } class StartNative implements Runnable { @Override public void run() { startNative(); } } @Override public void onDestroy() { super.onDestroy(); Log.i(TAG,"onDestroy"); mIsServiceDestroyed.set(true); } private void onMessageArrived(int cmd, String json) { int N = mListenerList.getRegisteredCallbackCount(); synchronized(this) { mListenerList.beginBroadcast(); for (int i = 0; i < N; i++) { mListener = mListenerList.getBroadcastItem(i); if (mListener != null) { try { mListener.onMessageArrived(cmd, json); } catch (RemoteException e) { e.printStackTrace(); } } } mListenerList.finishBroadcast(); } } public void SendMsgToMainProc(int cmd, String value) { if (!mIsServiceDestroyed.get()){ onMessageArrived(cmd, value); } Log.d(TAG, "SendMsgToMainProc cmd = " + String.format("%04X", cmd) + " json = " + value); } public String javaDESEncrypt(String plaintext, String key) { try { byte []des = DESUtil.encrypt(plaintext.getBytes("utf-8"), key); return Base64.encodeToString(des, Base64.DEFAULT); } catch (Exception e) { } return null; } public byte[] javaDESEncrypt(byte []plaintext, byte []key) { try { return DESUtil.encrypt(plaintext, key); } catch (Exception e) { } return null; } public void TextSpeak(String text) { if (speaker != null && ttsInitSucc) { speaker.speak(text); } } public void TextSpeakInitCallback(boolean ret) { ttsInitSucc = ret; } private String byte2hex(byte [] buffer){ StringBuilder h = new StringBuilder(); Log.d(TAG, "byte buffer length = " + buffer.length); for(int i = 0; i < buffer.length; i++){ String temp = Integer.toHexString(buffer[i] & 0xFF); if(temp.length() == 1){ temp = "0" + temp; } h.append(temp); h.append(" "); } return h.toString(); } class TTSCallback implements SpeakerCallback { @Override public void PlayInit(boolean ret) { Log.d(TAG, "TTS引擎初始化成功"); ttsInitSucc = ret; } @Override public void PlayStart() { Log.d(TAG, "TTS引擎播放开始"); } @Override public void PlayDone() { Log.d(TAG, "TTS引擎播放结束"); } @Override public void PlayError() { Log.d(TAG, "TTS引擎播放出错"); } } // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("native-lib"); } public native void startNative(); public native void MainProcMsgEntry(int cmd, String value); public native void MainProcBinMsgEntry(int cmd, byte []data, int length); }