1.添加文件证书
通过数据线将手机连接到电脑,在手机存储根目录下新建文件夹并命名为koal_cert,将证书命名为filecert.pfx,然后把证书拷贝至koal_cert文件夹。
2.按照图片两步,添加aidl文件和认证客户端。
11.png
3.添加登录YpLoginAction
package com.epoint.yangpu.action;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;
import com.epoint.frame.action.FrmLockPatternAction;
import com.epoint.frame.action.FrmTaskDealFlow;
import com.epoint.frame.core.app.BaseActivity;
import com.epoint.frame.core.controls.EpointToast;
import com.epoint.frame.core.task.BaseRequestor;
import com.epoint.frame.core.utils.PhoneUtil;
import com.epoint.frame.core.utils.ToastUtil;
import com.epoint.mobileoa.action.MOABaseAction;
import com.epoint.mobileoa.action.MOALoginAction;
import com.epoint.mobileoa.actys.MOAMainActivity;
import com.epoint.yangpu.utils.Config;
import com.epoint.yangpu.task.YP_CheckLoginTask;
import com.epoint.yangpu.utils.SSLUtils;
import com.google.gson.JsonObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import koal.ssl.IAutoService;
/**
* 登录的方法集合
* Created by a1278 on 17/1/19.
*/
public class YpLoginAction extends MOABaseAction implements Config {
public static IAutoService autoService = null;
private ServiceConnection serviceConnection = null;
private ServiceMon srvMonitor = null;
private String idCard;
// 判断服务是否启动成功标志
private int isKoalStart;
public YpLoginAction(BaseActivity _activity) {
super(_activity);
//检查安全认证客户端是否安装
if (!SSLUtils.checkApkExist(_activity, sslName)) {
SSLUtils.installSSLAPK(_activity);
} else {
bindKoalService();
}
}
public void bindKoalService() {
// 广播接收器,用来监听SSL服务发出的广播
srvMonitor = new ServiceMon();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_INTENT_STARTSERVER_INPROC);
filter.addAction(ACTION_INTENT_STARTSERVER_SUCCESS);
filter.addAction(ACTION_INTENT_STARTSERVER_FAILURE);
filter.addAction(ACTION_INTENT_DOWNLOADCFG_SUCCESS);
filter.addAction(ACTION_INTENT_STOPSERVER_SUCCESS);
filter.addAction(ACTION_INTENT_UPGRADE);
filter.addAction(ACTION_INTENT_NETWORK_CONNECTED);
filter.addAction(ACTION_INTENT_NETWORK_DISCONNECTED);
filter.addAction(ACTION_INTENT_CHECKAPPS_SUCCESS);
filter.addAction(ACTION_INTENT_CHECKAPPS_FAILURE);
activity.registerReceiver(srvMonitor, filter);
serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
autoService = IAutoService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
autoService = null;
}
};
// 绑定SSL服务
Intent mIntent = new Intent();
mIntent.setAction(KOAL_SERVICE);
Intent eintent = new Intent(SSLUtils.getExplicitIntent(activity,mIntent));
activity.startService(eintent);
activity.bindService(eintent, serviceConnection, activity.BIND_AUTO_CREATE);
}
/*
* SSL广播接收器 为防止接收器的阻塞,最好将耗时的操作放入handle中完成
*/
private class ServiceMon extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String data = intent.getStringExtra(ACTION_INTENT_DATA);
if (intent.getAction().equals(ACTION_INTENT_STARTSERVER_INPROC)) {
handleMessage(handler, MSG_SHOWLOG, data);
} else if (intent.getAction().equals(
ACTION_INTENT_STARTSERVER_SUCCESS)) {
handleMessage(handler, MSG_SHOWLOG, "启动服务成功!");
isKoalStart = 1;
getCerInfo();
} else if (intent.getAction().equals(
ACTION_INTENT_STARTSERVER_FAILURE)) {
handleMessage(handler, MSG_SHOWLOG, "启动服务失败!");
handlerErrToast("无法登录,请检查证书密码后重试!");
} else if (intent.getAction().equals(
ACTION_INTENT_DOWNLOADCFG_SUCCESS)) {
handleMessage(handler, MSG_SHOWLOG, "下载策略成功!");
} else if (intent.getAction().equals(
ACTION_INTENT_STOPSERVER_SUCCESS)) {
handleMessage(handler, MSG_SHOWLOG, "停止服务成功!");
} else if (intent.getAction().equals(ACTION_INTENT_UPGRADE)) {
handleMessage(handler, MSG_UPGRADE, data);
} else if (intent.getAction().equals(
ACTION_INTENT_NETWORK_CONNECTED)) {
handleMessage(handler, MSG_SHOWLOG, "网络已链接");
} else if (intent.getAction().equals(
ACTION_INTENT_NETWORK_DISCONNECTED)) {
handleMessage(handler, MSG_SHOWLOG, "网络已断开");
} else if (intent.getAction().equals(
ACTION_INTENT_CHECKAPPS_SUCCESS)) {
handleMessage(handler, MSG_SHOWLOG, "后台应用服务检测成功:" + data);
} else if (intent.getAction().equals(
ACTION_INTENT_CHECKAPPS_FAILURE)) {
handleMessage(handler, MSG_SHOWLOG, "后台应用服务检测失败:" + data);
}
}
}
private void handleMessage(Handler h, int msgID, String data) {
Message msg = new Message();
msg.what = msgID;
Bundle bundle = new Bundle();
bundle.putString(MSG_KEY, data);
msg.setData(bundle);
handler.sendMessage(msg);
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
String data = msg.getData().getString(MSG_KEY);
switch (msg.what) {
case MSG_SHOWLOG: // 向Log控件输出数据
Log.i("KOAL", data);
break;
case MSG_UPGRADE: // 弹出对话框,询问是否升级
new AlertDialog.Builder(activity)
.setTitle("自动升级")
.setMessage("最新版本:" + data + ",是否立即升级?")
.setCancelable(true)
.setPositiveButton("立即升级",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
try {
autoService.upgrade();
} catch (RemoteException e) {
}
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
}
}).show();
break;
case LOGINTASK:
YP_CheckLoginTask task = new YP_CheckLoginTask();
task.identityId = idCard;
task.identifyFlag = "1";
task.refreshHandler = new LoginRefresh();
task.start();
break;
case 1003:
try {
Thread.sleep(2000);
if (autoService == null) {
throw new Exception();
}
String idblock = autoService.getCertInfo(3);
String[] info2part = idblock.split(",");
String sfzblock = info2part[4];
String identUlt = sfzblock.split("=")[1];
if (identUlt.length() == 0) {
throw new Exception();
} else {
final String id = identUlt.toLowerCase()
.replace(" ", "").replace("sf", "");
getIdCard(id);
System.out.println(id);
Message msg1=new Message();
msg1.what = LOGINTASK;
sendMessage(msg1);
}
} catch (Exception e) {
e.printStackTrace();
handlerErrToast("证书信息读取失败!");
System.out.println("异常是:" + e);
}
break;
default:
break;
}
};
};
public void getCerInfo() {
Message msg = new Message();
msg.what = 1003;
handler.sendMessage(msg);
}
class LoginRefresh implements BaseRequestor.RefreshHandler {
@Override
public void refresh(final Object obj) {
activity.hideLoading();
new FrmTaskDealFlow(activity, (JsonObject) obj, new FrmTaskDealFlow.BusinessOperation() {
@Override
public void deal() {
MOALoginAction.setUserInfo(obj, "","");
Intent intent = new Intent(activity, MOAMainActivity.class);
activity.startActivity(intent);
activity.finish();
//清除手势密码
FrmLockPatternAction.cleanLockPattern();
// 去掉通知图标
PhoneUtil.cancelAllNotify(activity);
}
}, null, new FrmTaskDealFlow.NetErrorOperation() {
@Override
public void deal() {
EpointToast.showShort(activity, "网络请求超时");
}
}, new FrmTaskDealFlow.ServerErrorOperation() {
@Override
public void deal() {
EpointToast.showShort(activity, "服务器请求失败");
}
}).dealFlow();
}
}
public void handlerErrToast(final String msg) {
handler.post(new Runnable() {
@Override
public void run() {
ToastUtil.toastShort(activity,msg);
}
});
}
public String getIdCard(String id) {
idCard = id;
return idCard;
}
/**
* 取消ssl服务
* 页面destory的时候取消
*/
public void unregisterReceiver(){
// 取消绑定SSL服务
activity.unregisterReceiver(srvMonitor);
activity.unbindService(serviceConnection);
}
/**
* 登录
*/
public void loginZH(final String cerPin) {
new Thread(new Runnable() {
@Override
public void run() {
// // TODO Auto-generated method stub
try {
if (autoService == null) {
Thread.sleep(2000);
loginZH(cerPin);
}
if (!autoService.isStarted()) {
autoService.setServerAddr(serverUri, serverPort);
autoService.setPin(cerPin, false);
autoService.start();
} else {
getCerInfo();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
4.增加YP_CheckLoginTask文件
package com.epoint.yangpu.task;
import com.epoint.frame.core.String.StringHelp;
import com.epoint.frame.core.app.AppUtil;
import com.epoint.frame.core.db.service.FrmDBService;
import com.epoint.frame.core.net.WebServiceUtilDAL;
import com.epoint.frame.core.security.MDUtil;
import com.epoint.frame.core.task.BaseRequestor;
import com.epoint.frame.core.utils.PhoneUtil;
import com.epoint.mobileoa.action.MOACommonAction;
import com.epoint.mobileoa.action.MOAConfigKeys;
import com.epoint.mobileoa.model.MOAPlatformLoginModel;
import com.epoint.mobileoa.utils.MOABaseInfo;
import com.google.gson.JsonObject;
/**
* Created by liyachun on 15/6/18.
*/
public class YP_CheckLoginTask extends BaseRequestor {
public String loginId;
public String psw;
public String identityId;
public String identifyFlag;
@Override
public Object execute() {
if (MOABaseInfo.needPlatform()) {
return checkPlatform();
} else {
return checkLogin();
}
}
private Object checkPlatform() {
String deviceId = MOABaseInfo.getAppDeviceId();
String userType = MOABaseInfo.getUserType();
String interfaceFlag = MOABaseInfo.getInterfaceFlag();
String appguid = MOABaseInfo.getAppGuid();
String method = "checkLoginInfo";
String url = MOABaseInfo.getPlatformWebservice();
String namespace = "http://server.service.axis2";
WebServiceUtilDAL ws = new WebServiceUtilDAL(url, method, namespace);
ws.addProperty("IMEI", deviceId);
ws.addProperty("LoginId", "admin");
ws.addProperty("MobileVersion", MOABaseInfo.getMobileVersion());
ws.addProperty("InterfaceFlag", interfaceFlag);
ws.addProperty("appguid", appguid);
ws.addProperty("userType", userType);
ws.addProperty("VersionName", MOABaseInfo.getAppVersionInfo());
String bs = ws.start();
boolean isRequestOK = !StringHelp.getXMLAtt(bs, "EpointDataBody").equals("");
if (isRequestOK) {
MOAPlatformLoginModel model = new MOAPlatformLoginModel();
String ReturnInfo = StringHelp.getAttOut(bs, "ReturnInfo");
model.Status = StringHelp.getXMLAtt(ReturnInfo, "Status");
model.Description = StringHelp.getXMLAtt(ReturnInfo, "Description");
if (model.Status.toLowerCase().equals("true")) {
String UserArea = StringHelp.getXMLAtt(bs, "UserArea");
model.BusinessWebServiceUrl = StringHelp.getXMLAtt(UserArea, "BusinessWebServiceUrl");
model.needHandwrite = StringHelp.getXMLAtt(UserArea, "needHandwrite");
model.mqttServer = StringHelp.getXMLAtt(UserArea, "mqttServer");
//保存信息
String handWriteValue = model.needHandwrite.toLowerCase().equals("true") ? "1" : "0";
FrmDBService.setConfigValue(MOAConfigKeys.WriteSign, handWriteValue);
FrmDBService.setConfigValue(MOAConfigKeys.MQTT_URI, model.mqttServer);
FrmDBService.setConfigValue(MOAConfigKeys.Interface_Address, model.BusinessWebServiceUrl);
Object bsJson = checkLogin();
if (bsJson == null) {
return null;
} else {
return bsJson;
}
} else {
return createPlatformMessageJson(model.Description);
}
}
return null;
}
private JsonObject createPlatformMessageJson(String message) {
JsonObject jsonObject = new JsonObject();
JsonObject ReturnInfoJson = new JsonObject();
ReturnInfoJson.addProperty("Code", "1");
ReturnInfoJson.addProperty("Description", "");
jsonObject.add("ReturnInfo", ReturnInfoJson);
JsonObject BusinessInfoJson = new JsonObject();
BusinessInfoJson.addProperty("Code", "0");
BusinessInfoJson.addProperty("Description", message);
jsonObject.add("BusinessInfo", BusinessInfoJson);
return jsonObject;
}
/**
* "LoginID": "登录名",
"Password": "登录密码",
"loginType": "登录类型(1用户名密码认证,2身份证+标识认证)",
"identityId": "身份证",
"identifyFlag": "标识",
"OSVersion": "",
"SoftwareVersion": "",
"manufacturer": ""
* @return
*/
private Object checkLogin() {
JsonObject paras = new JsonObject();
paras.addProperty("LoginID", "");
paras.addProperty("Password","");
paras.addProperty("loginType", "2");
paras.addProperty("identityId",identityId);
paras.addProperty("identifyFlag", identifyFlag);
paras.addProperty("OSVersion", "");
paras.addProperty("SoftwareVersion", "");
paras.addProperty("manufacturer", "");
JsonObject bs = MOACommonAction.request(paras, "LoginV2_V6",MOABaseInfo.GETGXHURL());
return bs;
}
}
5.登录页面添加部分
A.在登录页面中引入YpLoginAction
action = new YpLoginAction(this);
B.在登录按钮的点击事件中实现登录接口
showLoading();
action.loginZH(etPassword.getText().toString());
C.在页面销毁的时候注销广播
@Override
protected void onDestroy() {
super.onDestroy();
action.unregisterReceiver();
}