package com.anyun.exam.lib;
|
|
import android.content.ComponentName;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.ServiceConnection;
|
import android.os.IBinder;
|
import android.os.RemoteException;
|
import android.text.TextUtils;
|
|
|
import com.anyun.basecommonlib.MyLog;
|
|
import java.util.LinkedList;
|
|
/**
|
* MyApplication2
|
* Created by lzw on 2019/9/19. 18:19:48
|
* 邮箱:632393724@qq.com
|
* All Rights Saved! Chongqing AnYun Tech co. LTD
|
*/
|
class SvrManager {
|
private static final SvrManager ourInstance = new SvrManager();
|
private static final String TAG = "SvrManager";
|
private MyServiceConn conn;
|
private Intent intent;
|
private Context mContext;
|
|
|
private IAYExamListener mCallback;
|
//远程服务接口
|
private IRemoteInterface remoteInterface;
|
private int BIND_SUCC = 0;
|
private int BIND_binderDied = -1;
|
|
static SvrManager getInstance() {
|
return ourInstance;
|
}
|
|
private SvrManager() {
|
}
|
/**
|
* 检查远程服务是否可以使用
|
* @return 可以用false,不可以用true
|
*/
|
private boolean checkRemoteIsNull() {
|
return remoteInterface==null?true:false;
|
}
|
/**
|
* Binder断裂回调
|
* 如果服务端进程由于某种异常原因停止
|
*/
|
private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
|
@Override
|
public void binderDied() {
|
MyLog.e(TAG,"IBinder.DeathRecipient binderDied");
|
if (mCallback != null){
|
mCallback.bindStatus(BIND_binderDied);
|
}
|
if (remoteInterface == null){
|
MyLog.e(TAG,"IBinder.DeathRecipient binderDied remoteInterface == null ,return");
|
return;
|
}
|
remoteInterface.asBinder().unlinkToDeath(mDeathRecipient,0);
|
remoteInterface = null;
|
MyLog.i("检测到远程服务异常死亡,重新绑定");
|
bindRemoteService(mContext);
|
}
|
};
|
|
public void registerListener(IAYExamListener iayExamListener) {
|
this.mCallback = iayExamListener;
|
}
|
|
public void add() {
|
if (!checkRemoteIsNull()){
|
try {
|
remoteInterface.add();
|
} catch (RemoteException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
public void sendCmd(int cmd,String s) {
|
if (!checkRemoteIsNull()){
|
try {
|
remoteInterface.SendCmd(cmd,s);
|
if(cmd == 0x8100){
|
MyLog.i(TAG,"CMD="+ String.format("0x%x",cmd) +" contentLength:"+(!TextUtils.isEmpty(s)?s.length():0));
|
}else{
|
|
MyLog.i(TAG,"CMD="+ String.format("0x%x",cmd) +" content:"+(!TextUtils.isEmpty(s)?s:"null"));
|
}
|
} catch (RemoteException e) {
|
e.printStackTrace();
|
}
|
}else{
|
MyLog.i(TAG,"远程服务接口==null");
|
}
|
}
|
|
class MyServiceConn implements ServiceConnection {
|
@Override
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
remoteInterface = IRemoteInterface.Stub.asInterface(service);
|
try {
|
//设置死亡代理
|
service.linkToDeath(mDeathRecipient,0);
|
MyLog.i("绑定服务onServiceConnected");
|
if(mCallback!=null){
|
mCallback.bindStatus(BIND_SUCC);
|
}
|
remoteInterface.registListener(RemoteCallback);
|
} catch (RemoteException e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public void onServiceDisconnected(ComponentName name) {
|
MyLog.i("绑定服务失败onServiceDisconnected");
|
}
|
|
|
}
|
public void bindRemoteService(Context mContext){
|
this.mContext = mContext;
|
intent = new Intent(mContext,RemoteService.class);
|
conn = new MyServiceConn();
|
mContext.bindService(intent,conn, Context.BIND_AUTO_CREATE);
|
}
|
|
public void unBindRemoteService(Context mContext){
|
if (remoteInterface!=null && remoteInterface.asBinder().isBinderAlive()){
|
try {
|
MyLog.i("摧毁SDK,需要解绑远程回调");
|
remoteInterface.unRegistListener(RemoteCallback);
|
|
} catch (RemoteException e) {
|
e.printStackTrace();
|
}
|
}
|
MyLog.i("摧毁SDK,需要解绑远程服务");
|
mContext.unbindService(conn);
|
if (intent!=null){
|
MyLog.i("摧毁SDK,需要停止远程服务");
|
mContext.stopService(intent);
|
}
|
}
|
|
/**
|
* 远程服务回调通知客户端消息
|
*/
|
IListenerInterface RemoteCallback = new IListenerInterface.Stub(){
|
@Override
|
public void onMessageArrived(int cmd, String json) throws RemoteException {
|
|
if (mCallback != null){
|
mCallback.callBackMsg(cmd, json);
|
|
}
|
}
|
|
|
};
|
|
}
|