fctom1215
2020-03-10 e5e41dec2d41651c7160662ecf43325d49b07b66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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<IListenerInterface> 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, int id) {
        if (speaker != null && ttsInitSucc) {
            speaker.speak(text, Integer.toString(id));
        } else {
            TextSpeakEnd(id);
        }
    }
 
    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(String s) {
            Log.d(TAG, "TTS引擎播放开始");
        }
 
        @Override
        public void PlayDone(String s) {
            Log.d(TAG, "TTS引擎播放结束");
            TextSpeakEnd(Integer.valueOf(s));
        }
 
        @Override
        public void PlayError(String s) {
            Log.d(TAG, "TTS引擎播放出错");
            TextSpeakEnd(Integer.valueOf(s));
        }
    }
 
    // 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);
    public native void TextSpeakEnd(int id);
}