Commit c46db9416a5637f603b6272645bc5e83723dbcc9

Authored by 孙向锦
1 parent 0b1cc51c

添加新文件

Showing 41 changed files with 1776 additions and 0 deletions
C5/app/src/main/aidl/com/sunvote/basebridge/IPadSystem.aidl 0 → 100644
  1 +// IPadSystem.aidl
  2 +package com.sunvote.basebridge;
  3 +
  4 +// Declare any non-default types here with import statements
  5 +
  6 +interface IPadSystem {
  7 + /**
  8 + * 控制系统关机
  9 + */
  10 + boolean powerOffXPad();
  11 +
  12 + /**
  13 + * 控制系统重启
  14 + * @return true 成功
  15 + * false 失败
  16 + */
  17 + boolean rebootXPad();
  18 + /**
  19 + * 控制系统禁/启用通知栏
  20 + * @return true 成功
  21 + * false 失败
  22 + */
  23 + boolean setNavgation(boolean visible);
  24 +
  25 + /**
  26 + * 是否激活
  27 + * @return true 成功
  28 + * false 失败
  29 + */
  30 + boolean isActiveMe();
  31 +
  32 +
  33 + boolean setStatusBarExpandPanelDisabled(boolean disable);
  34 +
  35 + boolean setNavigationBarDisabled(boolean disable);
  36 + /**
  37 + * 控制系统禁/启用Home键
  38 + * @return true 成功
  39 + * false 失败
  40 + */
  41 + boolean setHomeButtonDisabled(boolean disable);
  42 + /**
  43 + * 控制系统禁/启用BACK键
  44 + * @return true 成功
  45 + * false 失败
  46 + */
  47 + boolean setBackButtonDisabled(boolean disable);
  48 + /**
  49 + * 控制系统禁/启用power off键
  50 + * @return true 成功
  51 + * false 失败
  52 + */
  53 + boolean setPowerDisabled(boolean disable);
  54 +
  55 + /**
  56 + * 控制系统禁/启用最近使用栏
  57 + * @return true 成功
  58 + * false 失败
  59 + */
  60 + boolean setTaskButtonDisabled(boolean disable);
  61 +
  62 + boolean installPackage(String packagePath);
  63 + boolean uninstallPackage(String packageName, boolean keepData);
  64 + boolean clearPackageData(String packageName);
  65 + /**
  66 + * 注册开机启动程序
  67 + * packageName 启动名称
  68 + * activi 是否激活
  69 + * @return 0 表示应用成功
  70 + * 1 下次生效
  71 + * 2 表示已成功
  72 + * -1 失败
  73 + */
  74 + int registerBootStarted(String packageName,String activity ,boolean activi);
  75 +
  76 + /**
  77 + * 移除其他所有开机启动程序
  78 + * @return 0 成功
  79 + * <0 失败
  80 + * >=0 成功
  81 + */
  82 + int removeAllBootStarted();
  83 +
  84 +}
... ...
C5/app/src/main/java/com/sunvote/sdk/HuaWeiSDK.java 0 → 100644
  1 +package com.sunvote.sdk;
  2 +
  3 +import android.app.Activity;
  4 +import android.content.ComponentName;
  5 +import android.content.Context;
  6 +import android.content.Intent;
  7 +import android.content.ServiceConnection;
  8 +import android.content.pm.PackageManager;
  9 +import android.content.pm.ResolveInfo;
  10 +import android.os.IBinder;
  11 +import android.os.RemoteException;
  12 +
  13 +import com.sunvote.basebridge.IPadSystem;
  14 +
  15 +import java.util.List;
  16 +
  17 +public class HuaWeiSDK {
  18 +
  19 + private Activity activity;
  20 + private IPadSystem iPadSystem;
  21 +
  22 + private ServiceConnection serviceConnection = new ServiceConnection() {
  23 + @Override
  24 + public void onServiceConnected(ComponentName name, IBinder service) {
  25 + iPadSystem = IPadSystem.Stub.asInterface(service);
  26 + }
  27 +
  28 + @Override
  29 + public void onServiceDisconnected(ComponentName name) {
  30 + iPadSystem = null;
  31 + }
  32 + };
  33 +
  34 + private HuaWeiSDK(Activity activity) {
  35 + this.activity = activity;
  36 + }
  37 +
  38 + private static HuaWeiSDK instance = null ;
  39 +
  40 + public static HuaWeiSDK getInstance(Activity activity){
  41 + if(instance == null){
  42 + synchronized(HuaWeiSDK.class){
  43 + if(instance == null){
  44 + instance = new HuaWeiSDK(activity);
  45 + }
  46 + }
  47 + }
  48 + instance.activity = activity;
  49 + return instance;
  50 + }
  51 +
  52 + /**
  53 + * 必须在同一个activity里面开启和关闭,如果在一个里面没有关闭,而在另外一个开启,会报错。
  54 + * 建议:在activity onCreate 里面调用open,在ondestroy 里面调用close
  55 + * 调用功能前 必须先open,不调用了 需要close
  56 + */
  57 + public void open() {
  58 + bindService();
  59 + }
  60 +
  61 + /**
  62 + * 必须在同一个activity里面开启和关闭,如果在一个里面没有关闭,而在另外一个开启,会报错。
  63 + * 建议:在activity onCreate 里面调用open,在ondestroy 里面调用close
  64 + * 调用功能前 必须先open,不调用了 需要close
  65 + */
  66 + public void close(){
  67 + activity.unbindService(serviceConnection);
  68 + }
  69 +
  70 + private void bindService() {
  71 + Intent intent = new Intent();
  72 + intent.setPackage("com.sunvote.basebridge");
  73 + intent.setAction("com.sunvote.basebridge.service.PadSystemService");
  74 + Intent tintent = getExplicitIntent(activity, intent);
  75 + if(tintent != null){
  76 + activity.bindService(getExplicitIntent(activity, intent), serviceConnection, Context.BIND_AUTO_CREATE);
  77 + }
  78 + }
  79 +
  80 + public Intent getExplicitIntent(Context context, Intent implicitIntent) {
  81 + PackageManager pm = context.getPackageManager();
  82 + List<ResolveInfo> resolveInfos = pm.queryIntentServices(implicitIntent, 0);
  83 + if (resolveInfos == null || resolveInfos.size() != 1) {
  84 + return null;
  85 + }
  86 + Intent explicitIntent = null;
  87 + ResolveInfo info = resolveInfos.get(0);
  88 + String packageName = info.serviceInfo.packageName;
  89 + String className = info.serviceInfo.name;
  90 + ComponentName component = new ComponentName(packageName, className);
  91 + explicitIntent = new Intent(implicitIntent);
  92 + explicitIntent.setComponent(component);
  93 + return explicitIntent;
  94 + }
  95 +
  96 + /**
  97 + * 控制系统关机
  98 + */
  99 + public boolean powerOffXPad() {
  100 + if (iPadSystem != null) {
  101 + try {
  102 + return iPadSystem.powerOffXPad();
  103 + } catch (RemoteException e) {
  104 + return false;
  105 + }
  106 + }
  107 + return false;
  108 + }
  109 +
  110 + /**
  111 + * 控制系统重启
  112 + *
  113 + * @return true 成功
  114 + * false 失败
  115 + */
  116 + public boolean rebootXPad() {
  117 + if (iPadSystem != null) {
  118 + try {
  119 + return iPadSystem.rebootXPad();
  120 + } catch (RemoteException e) {
  121 + return false;
  122 + }
  123 + }
  124 + return false;
  125 + }
  126 +
  127 + /**
  128 + * 控制系统禁/启用通知栏
  129 + *
  130 + * @return true 成功
  131 + * false 失败
  132 + */
  133 + public boolean setNavgation(boolean visible) {
  134 + if (iPadSystem != null) {
  135 + try {
  136 + return iPadSystem.setNavgation(visible);
  137 + } catch (RemoteException e) {
  138 + return false;
  139 + }
  140 + }
  141 + return false;
  142 + }
  143 +
  144 + /**
  145 + * 是否激活
  146 + *
  147 + * @return true 成功
  148 + * false 失败
  149 + */
  150 + public boolean isActiveMe() {
  151 + if (iPadSystem != null) {
  152 + try {
  153 + return iPadSystem.isActiveMe();
  154 + } catch (RemoteException e) {
  155 + return false;
  156 + }
  157 + }
  158 + return false;
  159 + }
  160 +
  161 +
  162 + public boolean setStatusBarExpandPanelDisabled(boolean disable) {
  163 + if (iPadSystem != null) {
  164 + try {
  165 + return iPadSystem.setStatusBarExpandPanelDisabled(disable);
  166 + } catch (RemoteException e) {
  167 + return false;
  168 + }
  169 + }
  170 + return false;
  171 + }
  172 +
  173 + public boolean setNavigationBarDisabled(boolean disable) {
  174 + if (iPadSystem != null) {
  175 + try {
  176 + return iPadSystem.setNavigationBarDisabled(disable);
  177 + } catch (RemoteException e) {
  178 + return false;
  179 + }
  180 + }
  181 + return false;
  182 + }
  183 +
  184 + /**
  185 + * 控制系统禁/启用Home键
  186 + *
  187 + * @return true 成功
  188 + * false 失败
  189 + */
  190 + public boolean setHomeButtonDisabled(boolean disable) {
  191 + if (iPadSystem != null) {
  192 + try {
  193 + return iPadSystem.setHomeButtonDisabled(disable);
  194 + } catch (RemoteException e) {
  195 + return false;
  196 + }
  197 + }
  198 + return false;
  199 + }
  200 +
  201 + /**
  202 + * 控制系统禁/启用BACK键
  203 + *
  204 + * @return true 成功
  205 + * false 失败
  206 + */
  207 + public boolean setBackButtonDisabled(boolean disable) {
  208 + if (iPadSystem != null) {
  209 + try {
  210 + return iPadSystem.setBackButtonDisabled(disable);
  211 + } catch (RemoteException e) {
  212 + return false;
  213 + }
  214 + }
  215 + return false;
  216 + }
  217 +
  218 + /**
  219 + * 控制系统禁/启用power off键
  220 + *
  221 + * @return true 成功
  222 + * false 失败
  223 + */
  224 + public boolean setPowerDisabled(boolean disable) {
  225 + if (iPadSystem != null) {
  226 + try {
  227 + return iPadSystem.setPowerDisabled(disable);
  228 + } catch (RemoteException e) {
  229 + return false;
  230 + }
  231 + }
  232 + return false;
  233 + }
  234 +
  235 + /**
  236 + * 控制系统禁/启用最近使用栏
  237 + *
  238 + * @return true 成功
  239 + * false 失败
  240 + */
  241 + public boolean setTaskButtonDisabled(boolean disable) {
  242 + if (iPadSystem != null) {
  243 + try {
  244 + return iPadSystem.setTaskButtonDisabled(disable);
  245 + } catch (RemoteException e) {
  246 + return false;
  247 + }
  248 + }
  249 + return false;
  250 + }
  251 +
  252 + public boolean installPackage(String packagePath) {
  253 + if (iPadSystem != null) {
  254 + try {
  255 + return iPadSystem.installPackage(packagePath);
  256 + } catch (RemoteException e) {
  257 + return false;
  258 + }
  259 + }
  260 + return false;
  261 + }
  262 +
  263 + public boolean uninstallPackage(String packageName, boolean keepData) {
  264 + if (iPadSystem != null) {
  265 + try {
  266 + return iPadSystem.uninstallPackage(packageName, keepData);
  267 + } catch (RemoteException e) {
  268 + return false;
  269 + }
  270 + }
  271 + return false;
  272 + }
  273 +
  274 + public boolean clearPackageData(String packageName) {
  275 + if (iPadSystem != null) {
  276 + try {
  277 + return iPadSystem.clearPackageData(packageName);
  278 + } catch (RemoteException e) {
  279 + return false;
  280 + }
  281 + }
  282 + return false;
  283 + }
  284 +
  285 + /**
  286 + * 注册开机启动程序
  287 + * packageName 启动名称
  288 + * activi 是否激活
  289 + *
  290 + * @return 0 表示应用成功
  291 + * 1 下次生效
  292 + * 2 表示已成功
  293 + * -1 失败
  294 + */
  295 + public int registerBootStarted(String packageName, String activity, boolean activi) {
  296 + if (iPadSystem != null) {
  297 + try {
  298 + return iPadSystem.registerBootStarted(packageName, activity, activi);
  299 + } catch (RemoteException e) {
  300 + return -1;
  301 + }
  302 + }
  303 + return -1;
  304 + }
  305 +
  306 + /**
  307 + * 移除其他所有开机启动程序
  308 + *
  309 + * @return 0 成功
  310 + * <0 失败
  311 + * >=0 成功
  312 + */
  313 + public int removeAllBootStarted() {
  314 + if (iPadSystem != null) {
  315 + try {
  316 + return iPadSystem.removeAllBootStarted();
  317 + } catch (RemoteException e) {
  318 + return -1;
  319 + }
  320 + }
  321 + return -1;
  322 + }
  323 +}
... ...
C5/app/src/main/java/com/sunvote/xpadcomm/AbsDownloadProcess.java 0 → 100644
  1 +package com.sunvote.xpadcomm;
  2 +
  3 +import java.io.File;
  4 +import java.io.IOException;
  5 +
  6 +/**
  7 + * Created by Elvis on 2018/3/20 11:18
  8 + * Email:Eluis@psunsky.com
  9 + * 版权所有:长沙中天电子设计开发有限公司
  10 + * Description: 人大通用版XPadAppRendaMac
  11 + */
  12 +public abstract class AbsDownloadProcess {
  13 +
  14 + private static String foldPath = "/sdcard/Sunvote/download/";
  15 +
  16 + protected static String fileName = "default.obj";
  17 +
  18 + private long length ;
  19 + private long curComplete = 0 ;
  20 +
  21 + private static File file ;
  22 +
  23 + public File getFile() {
  24 + if(file == null){
  25 + curComplete = 0 ;
  26 + File folder = new File(foldPath);
  27 + if(!folder.exists()){
  28 + folder.mkdirs();
  29 + }
  30 + if(fileName == null || "".equals(fileName.trim())){
  31 + fileName = "default.obj" ;
  32 + }
  33 + file = new File(foldPath , fileName);
  34 + file.deleteOnExit();
  35 + if(!file.exists()){
  36 + try {
  37 + file.createNewFile();
  38 + } catch (IOException e) {
  39 + onDownloadError(e);
  40 + }
  41 + }
  42 + }
  43 + return file;
  44 + }
  45 +
  46 + protected long getLength() {
  47 + return length;
  48 + }
  49 +
  50 + public String getFoldPath() {
  51 + return foldPath;
  52 + }
  53 +
  54 + protected void setLength(long length) {
  55 + this.length = length;
  56 + curComplete = 0 ;
  57 + }
  58 +
  59 + public void setFileName(String fileName) {
  60 + if(!(fileName == null || "".equals(fileName.trim()))) {
  61 + this.fileName = fileName;
  62 + file = null;
  63 + }
  64 + }
  65 +
  66 + protected String getFileName() {
  67 + return fileName;
  68 + }
  69 +
  70 + public void setFoldPath(String foldPath) {
  71 + this.foldPath = foldPath;
  72 + }
  73 +
  74 + protected void onStartDownload(){
  75 + onStartDownload(getFile(),getLength());
  76 + }
  77 +
  78 + public abstract void onStartDownload(File file,long length);
  79 +
  80 + protected void onDownloadAddProcess(int curPack) {
  81 + curComplete += curPack;
  82 + double result = 0;
  83 + if(curComplete <= length){
  84 + result = (double)curComplete / length ;
  85 + }else{
  86 + result = 1;
  87 + }
  88 +
  89 + try{
  90 + onDownloadProcess(result);
  91 + }catch (Exception ex){
  92 + ex.printStackTrace();
  93 + }
  94 + try{
  95 + int ri = (int)(result * 100);
  96 + onDownloadProcess(ri + "%");
  97 + }catch (Exception ex){
  98 + ex.printStackTrace();
  99 + }
  100 + }
  101 +
  102 + public abstract void onDownloadProcess(double process);
  103 +
  104 + public abstract void onDownloadProcess(String process);
  105 +
  106 + protected void onStopDownload(){
  107 + onStopDownload(getFile());
  108 + }
  109 +
  110 + public abstract void onStopDownload(File file);
  111 +
  112 + public abstract void onDownloadError(Exception e);
  113 +
  114 +}
... ...
C5/app/src/main/java/com/sunvote/xpadcomm/IUploadListener.java 0 → 100644
  1 +package com.sunvote.xpadcomm;
  2 +
  3 +/**
  4 + * Created by Elvis on 2018/3/22 13:43
  5 + * Email:Eluis@psunsky.com
  6 + * 版权所有:长沙中天电子设计开发有限公司
  7 + * Description: 人大通用版XPadAppRendaMac
  8 + */
  9 +public interface IUploadListener {
  10 +
  11 + public void onUploadStart();
  12 +
  13 + public void onUploadProcess(double process);
  14 +
  15 + public void onUploadStop();
  16 +
  17 + public void onFail();
  18 +}
... ...
C5/app/src/main/java/com/sunvote/xpadcomm/RFFileDownloadModule.java 0 → 100644
  1 +package com.sunvote.xpadcomm;
  2 +
  3 +import android.os.Handler;
  4 +import android.os.Looper;
  5 +
  6 +import com.sunvote.util.LogUtil;
  7 +
  8 +import java.io.File;
  9 +
  10 +/**
  11 + * Created by Elvis on 2018/3/20 11:16
  12 + * Email:Eluis@psunsky.com
  13 + * 版权所有:长沙中天电子设计开发有限公司
  14 + * Description: 人大通用版XPadAppRendaMac
  15 + */
  16 +public class RFFileDownloadModule {
  17 +
  18 + private RFFileDownloadModule(){
  19 + handler = new Handler(Looper.getMainLooper());
  20 + }
  21 + private Handler handler ;
  22 + private long overtime = 15 * 1000 ;
  23 + private static RFFileDownloadModule instance = new RFFileDownloadModule();
  24 +
  25 + public static RFFileDownloadModule getInstance() {
  26 + return instance;
  27 + }
  28 +
  29 + private AbsDownloadProcess downloadProcess;
  30 + private AbsDownloadProcess tdownloadProcess = new AbsDownloadProcess() {
  31 +
  32 + private boolean start = false;
  33 + private double process = 0 ;
  34 + private Runnable runnable = new Runnable() {
  35 + @Override
  36 + public void run() {
  37 + onDownloadError(new Exception("overtime"));
  38 + }
  39 + };
  40 +
  41 + @Override
  42 + public void onStartDownload(File file, long length) {
  43 + start = true;
  44 + process = 0;
  45 + LogUtil.i("onStartDownload", file.getName() + "," + length);
  46 + if(downloadProcess != null){
  47 + downloadProcess.onStartDownload(file,length);
  48 + }
  49 + handler.removeCallbacks(runnable);
  50 + handler.postDelayed(runnable,overtime);
  51 + }
  52 +
  53 + @Override
  54 + public void onDownloadProcess(double process) {
  55 + this.process = process;
  56 + LogUtil.i("onDownloadProcess", "process," + process);
  57 + if(downloadProcess != null && start){
  58 + downloadProcess.onDownloadProcess(process);
  59 + }
  60 + handler.removeCallbacks(runnable);
  61 + handler.postDelayed(runnable,overtime);
  62 + }
  63 +
  64 + @Override
  65 + public void onDownloadProcess(String process) {
  66 + LogUtil.i("onDownloadProcess", "process," + process);
  67 + if(downloadProcess != null && start){
  68 + downloadProcess.onDownloadProcess(process);
  69 + }
  70 + handler.removeCallbacks(runnable);
  71 + handler.postDelayed(runnable,overtime);
  72 + }
  73 +
  74 + @Override
  75 + public void onStopDownload(File file) {
  76 + LogUtil.i("onStopDownload", "process," + file.getName());
  77 + if(downloadProcess != null && start){
  78 + if(process >= 1.0){
  79 + downloadProcess.onStopDownload(file);
  80 + }else{
  81 + downloadProcess.onDownloadError(new Exception("file download error"));
  82 + }
  83 + }
  84 + start = false;
  85 + handler.removeCallbacks(runnable);
  86 + file.deleteOnExit();
  87 + }
  88 +
  89 + @Override
  90 + public void onDownloadError(Exception e) {
  91 + LogUtil.i("onStopDownload", "error", e);
  92 + if(downloadProcess != null && start){
  93 + downloadProcess.onDownloadError(e);
  94 + }
  95 + start = false;
  96 + handler.removeCallbacks(runnable);
  97 + }
  98 + };
  99 +
  100 + public void setOvertime(long overtime) {
  101 + if(overtime > 0){
  102 + this.overtime = overtime;
  103 + }
  104 + }
  105 +
  106 + public synchronized void registerDownloadProcess(AbsDownloadProcess downloadProcess){
  107 + this.downloadProcess = downloadProcess ;
  108 + }
  109 +
  110 + public synchronized void unRegisterDownloadProcess(AbsDownloadProcess downloadProcess){
  111 + this.downloadProcess = null;
  112 + }
  113 +
  114 + protected synchronized AbsDownloadProcess getDownloadProcess() {
  115 +
  116 + return tdownloadProcess;
  117 + }
  118 +
  119 +
  120 +}
... ...
C5/app/src/main/java/com/sunvote/xpadcomm/RFFileUploadModule.java 0 → 100644
  1 +package com.sunvote.xpadcomm;
  2 +
  3 +import android.os.Handler;
  4 +import android.os.HandlerThread;
  5 +
  6 +import com.sunvote.util.LogUtil;
  7 +
  8 +import java.io.File;
  9 +import java.io.FileInputStream;
  10 +import java.io.IOException;
  11 +import java.io.UnsupportedEncodingException;
  12 +
  13 +/**
  14 + * Created by Elvis on 2018/3/21 16:13
  15 + * Email:Eluis@psunsky.com
  16 + * 版权所有:长沙中天电子设计开发有限公司
  17 + * Description: 文件上传模块
  18 + */
  19 +public class RFFileUploadModule {
  20 +
  21 + public static final byte PACKTYPE = 5 ;
  22 + public static final int SLEEP_TIME = 25 ;
  23 + public static byte index = 0 ;
  24 + private RFFileUploadModule(){
  25 + handlerThread = new HandlerThread(RFFileUploadModule.class.getSimpleName());
  26 + handlerThread.start();
  27 + handler = new Handler(handlerThread.getLooper());
  28 + }
  29 +
  30 + private static RFFileUploadModule instance ;
  31 +
  32 + public static RFFileUploadModule getInstance() {
  33 + if(instance == null){
  34 + synchronized (RFFileUploadModule.class){
  35 + if(instance == null){
  36 + instance = new RFFileUploadModule();
  37 + }
  38 + }
  39 + }
  40 + return instance;
  41 + }
  42 +
  43 + private IUploadListener uploadListener = new IUploadListener() {
  44 + @Override
  45 + public void onUploadStart() {
  46 + System.out.println("onUploadStart");
  47 + }
  48 +
  49 + @Override
  50 + public void onUploadProcess(double process) {
  51 + System.out.println("onUploadProcess:" + process);
  52 + }
  53 +
  54 + @Override
  55 + public void onUploadStop() {
  56 + System.out.println("onUploadStop");
  57 + }
  58 +
  59 + @Override
  60 + public void onFail() {
  61 + System.out.println("onFail");
  62 + }
  63 + };
  64 +
  65 +
  66 + private String filename;
  67 + private byte[] datas = new byte[1024];
  68 + private int length = 1024;
  69 + private HandlerThread handlerThread ;
  70 + private Handler handler ;
  71 + private byte[] keyid = new byte[2];
  72 + private Runnable task;
  73 + private int packTotal ;
  74 + private boolean isStop = false ;
  75 +
  76 + /**
  77 + * 文件名不能超过16个字符(中文不能超过8个),超过16个字符将会出现截断。
  78 + * 文件长度不能超过65535个字节
  79 + * @param file 文件
  80 + * @return 是否成功发起上传
  81 + */
  82 + public boolean uploadFile(File file){
  83 + LogUtil.i("RFFileUploadModule","uploadFile" + file);
  84 + if(file != null){
  85 + length = (int)file.length();
  86 + filename = file.getName();
  87 + datas = new byte[length+4];
  88 + FileInputStream fis = null ;
  89 + try {
  90 + fis = new FileInputStream(file);
  91 + int start = 0 ;
  92 + // 把文件数据读入字节数组。
  93 + while(start < length){
  94 + int ret = fis.read(datas,start,length-start);
  95 + if(ret < 0){
  96 + break;
  97 + }
  98 + start += ret ;
  99 + }
  100 + datas[length] = 0x13 ;
  101 + datas[length + 1] = 0x0 ;
  102 + datas[length + 2] = 0x10 ;
  103 + datas[length + 3] = 0x0 ;
  104 +
  105 + if(start >= length){
  106 + task = new A0(filename,length);
  107 + handler.removeCallbacks(task);
  108 + handler.post(task);
  109 +// packetConfirmation((byte)0);
  110 + return true;
  111 + }
  112 +
  113 + } catch (Exception e) {
  114 + e.printStackTrace();
  115 + }finally {
  116 + if(fis != null){
  117 + try {
  118 + fis.close();
  119 + } catch (IOException e) {
  120 + e.printStackTrace();
  121 + }
  122 + fis = null;
  123 + }
  124 + }
  125 + }
  126 + return false;
  127 + }
  128 +
  129 + public void setKeyid(byte[] keyid) {
  130 + setKeyid(keyid,0);
  131 + }
  132 + public void setKeyid(byte[] keyid,int start) {
  133 + this.keyid[0] = keyid[start];
  134 + this.keyid[1] = keyid[start+1];
  135 + }
  136 +
  137 + public void onUploadFile(byte data, byte[] keyid, byte packid, byte packH, byte[] packLs) {
  138 + if (data == 1) {
  139 + setKeyid(keyid);
  140 + packetConfirmation(packid);
  141 + }
  142 + if (data == 2) {
  143 + uploadData(packid, packH, packLs);
  144 + }
  145 + if (data == 3) {
  146 + packetReceptionConfirmed(packid, packH);
  147 + }
  148 + }
  149 +
  150 + public void uploadData(byte packid, byte packH, byte[] packLs){
  151 + System.out.println("uploadData");
  152 + handler.removeCallbacks(task);
  153 + if(task instanceof A2){
  154 + ((A2)task).setStop(true);
  155 + }
  156 + task = new A2(keyid,packid,packH,datas,packLs);
  157 + handler.post(task);
  158 + if(uploadListener != null){
  159 + double process = 0 ;
  160 + if(packTotal > 0){
  161 + int count = 0 ;
  162 + int sen = (packLs[1] & 0xFF) * 256 + (packLs[0] & 0XFF);
  163 + for(int i = 0 ; i < 16 ; i++){
  164 + if((sen & (1 << i)) == 0){
  165 + count ++ ;
  166 + }
  167 + }
  168 + int been = packH * 16 + count ;
  169 + if(been > packTotal){
  170 + been = packTotal ;
  171 + }
  172 + process = (double)(been) / packTotal ;
  173 + }
  174 + uploadListener.onUploadProcess(process);
  175 + }
  176 + }
  177 +
  178 + public void packetConfirmation(byte packid){
  179 + System.out.println("packetConfirmation");
  180 + handler.removeCallbacks(task);
  181 + byte[] names = null;
  182 + if(filename != null){
  183 + try {
  184 + names = filename.getBytes("GB2312");
  185 + } catch (UnsupportedEncodingException e) {
  186 + e.printStackTrace();
  187 + }
  188 + }
  189 + task = new A1(keyid,(byte)((length + 4 ) / 65535), (byte)(((length + 4) % 65535) / 256),(byte)((length + 4) % 256),names);
  190 + handler.post(task);
  191 + }
  192 +
  193 + public void packetReceptionConfirmed(byte packid,byte packH){
  194 + System.out.println("packetReceptionConfirmed");
  195 + handler.removeCallbacks(task);
  196 + task = new A3(keyid,packid,packH);
  197 + handler.post(task);
  198 + }
  199 +
  200 + public boolean uploadFile(byte[] datas){
  201 + System.out.println("uploadFile" + datas);
  202 + if(datas != null){
  203 + length = datas.length ;
  204 + filename = null ;
  205 + this.datas = new byte[length + 4];
  206 + System.arraycopy(datas,0,this.datas,0,length);
  207 + datas[length] = 0x13 ;
  208 + datas[length + 1] = 0x0 ;
  209 + datas[length + 2] = 0x10 ;
  210 + datas[length + 3] = 0x0 ;
  211 + task = new A0(filename,length);
  212 + handler.removeCallbacks(task);
  213 + handler.post(task);
  214 +// packetConfirmation((byte)0);
  215 + return true;
  216 + }
  217 + return false;
  218 + }
  219 +
  220 +
  221 + public void setUploadListener(IUploadListener uploadListener) {
  222 + this.uploadListener = uploadListener;
  223 + }
  224 +
  225 + class A0 implements Runnable{
  226 + private int length ;
  227 + private String filename;
  228 + private int times ;
  229 + public A0(String filename,int length){
  230 + this.length = length ;
  231 + this.filename = filename;
  232 + packTotal = length / 16 + 1;
  233 + times = 0 ;
  234 + index++;
  235 + }
  236 +
  237 + @Override
  238 + public void run() {
  239 + LogUtil.i("RFFileUploadModule","A0 run");
  240 + XPadApi.getInstance().applyFileUpload(this.length,this.filename,PACKTYPE,index);
  241 + times ++ ;
  242 + if(task != null && times < 5){
  243 + handler.removeCallbacks(task);
  244 + handler.postDelayed(task,5000);
  245 + }
  246 + }
  247 + }
  248 +
  249 + class A1 implements Runnable{
  250 +
  251 + private byte[] keyid;
  252 + private byte packid ;
  253 + private byte packH ;
  254 + private byte packL ;
  255 + private byte[] names ;
  256 + private int times ;
  257 +
  258 + public A1(byte[] keyid, byte packid,byte packH,byte packL,byte[] names) {
  259 + LogUtil.i("RFFileUploadModule","A1");
  260 + this.keyid = keyid;
  261 + this.packid = packid;
  262 + this.packH = packH ;
  263 + this.packL = packL ;
  264 + this.names = names ;
  265 + times = 0 ;
  266 + }
  267 +
  268 + @Override
  269 + public void run() {
  270 + LogUtil.i("RFFileUploadModule","A1 run");
  271 + XPadApi.getInstance().packetConfirmation(this.keyid,this.packid,this.packH,this.packL,this.names,PACKTYPE);
  272 + times ++ ;
  273 + if(task != null && times < 5){
  274 + handler.removeCallbacks(task);
  275 + handler.postDelayed(task,5000);
  276 + }
  277 + if(uploadListener != null){
  278 + uploadListener.onUploadStart();
  279 + isStop = false;
  280 + }
  281 + }
  282 + }
  283 +
  284 + class A2 implements Runnable{
  285 + private byte[] keyid;
  286 + private byte packid;
  287 + private byte packH;
  288 + private byte[] datas ;
  289 + private byte[] packLs;
  290 + private int times ;
  291 + private boolean stop = false;
  292 +
  293 + public void setStop(boolean stop) {
  294 + this.stop = stop;
  295 + }
  296 +
  297 + public A2(byte[] keyid, byte packid, byte packH, byte[] datas, byte[] packLs) {
  298 + LogUtil.i("RFFileUploadModule","A2");
  299 + this.keyid = keyid;
  300 + this.packid = packid;
  301 + this.packH = packH;
  302 + this.datas = datas;
  303 + this.packLs = packLs;
  304 + times = 0 ;
  305 + }
  306 +
  307 + @Override
  308 + public void run() {
  309 + LogUtil.i("RFFileUploadModule","A2 run");
  310 + int packLsi = (packLs[1] & 0xFF) * 256 + (packLs[0] & 0xFF);
  311 + int offset = 0 ;
  312 + int length = 16;
  313 + System.out.println("packLsi:" + packLsi);
  314 + for(byte packL = 0 ;packL < 16 && !stop ;packL ++) {
  315 + if((packLsi & (1 << packL)) != 0){
  316 + offset = ((packid * 65535) + (packH) * 256 + 16 * (packL)) & 0xFFFF;
  317 + XPadApi.getInstance().uploadFileData(this.keyid, this.packid, this.packH, (byte)(packL ), this.datas, offset, length,PACKTYPE);
  318 + // need sleep ????
  319 + try{
  320 + Thread.sleep(SLEEP_TIME);
  321 + } catch (Exception e) {
  322 + e.printStackTrace();
  323 + }
  324 + }
  325 + }
  326 +
  327 +// datas = "123".getBytes();
  328 +// XPadApi.getInstance().uploadFileData(this.keyid, this.packid, this.packH, (byte)0, this.datas, offset, length);
  329 +
  330 + times ++ ;
  331 + if(task != null && times < 5){
  332 + handler.removeCallbacks(task);
  333 + handler.postDelayed(task,5000);
  334 + }
  335 + }
  336 + }
  337 +
  338 + class A3 implements Runnable{
  339 + private byte[] keyid;
  340 + private byte packid ;
  341 + private byte packH;
  342 + private int times ;
  343 +
  344 + public A3(byte[] keyid, byte packid, byte packH) {
  345 + LogUtil.i("RFFileUploadModule","A3");
  346 + this.keyid = keyid;
  347 + this.packid = packid;
  348 + this.packH = packH;
  349 + times = 0 ;
  350 + }
  351 +
  352 + @Override
  353 + public void run() {
  354 + LogUtil.i("RFFileUploadModule","A3 run");
  355 + XPadApi.getInstance().packetReceptionConfirmed(this.keyid,this.packid,this.packH,PACKTYPE);
  356 + if(uploadListener != null && !isStop){
  357 + isStop = true;
  358 + uploadListener.onUploadStop();
  359 + }
  360 + }
  361 + }
  362 +}
... ...
C5/app/src/main/java/com/sunvote/xpadcomm/RFMessageUploadModule.java 0 → 100644
  1 +package com.sunvote.xpadcomm;
  2 +
  3 +import android.os.Handler;
  4 +import android.os.HandlerThread;
  5 +
  6 +import com.sunvote.util.LogUtil;
  7 +
  8 +import java.io.UnsupportedEncodingException;
  9 +
  10 +/**
  11 + * Created by Elvis on 2018/3/27 9:48
  12 + * Email:Eluis@psunsky.com
  13 + * 版权所有:长沙中天电子设计开发有限公司
  14 + * Description: 人大通用版
  15 + * 短消息上传模块
  16 + */
  17 +public class RFMessageUploadModule {
  18 +
  19 + public static final byte PACKTYPE = 3 ;
  20 + private static RFMessageUploadModule instance ;
  21 +
  22 + private IUploadListener uploadListener ;
  23 +
  24 + private String filename;
  25 + private byte[] datas = "test message".getBytes();
  26 + private int length = 1024;
  27 + private HandlerThread handlerThread ;
  28 + private Handler handler ;
  29 + private byte[] keyid;
  30 + private Runnable task;
  31 + private int packTotal ;
  32 + private boolean stop = false;
  33 +
  34 + private RFMessageUploadModule(){
  35 + handlerThread = new HandlerThread(RFMessageUploadModule.class.getSimpleName());
  36 + handlerThread.start();
  37 + handler = new Handler(handlerThread.getLooper());
  38 + }
  39 +
  40 + public static RFMessageUploadModule getInstance() {
  41 + if(instance == null){
  42 + instance = new RFMessageUploadModule();
  43 + }
  44 + return instance;
  45 + }
  46 +
  47 + public void setMessageContent(byte[] messageContent) {
  48 + this.datas = messageContent;
  49 + uploadMsgData(datas);
  50 + removeTask();
  51 + }
  52 +
  53 + public void setMessageContent(String message) {
  54 + try {
  55 + this.datas = message.getBytes("GB2312");
  56 + uploadMsgData(datas);
  57 + removeTask();
  58 + handler.removeCallbacks(failCall);
  59 + handler.postDelayed(failCall,10000);
  60 + } catch (Exception e) {
  61 + e.printStackTrace();
  62 + }
  63 + }
  64 +
  65 + private boolean uploadMsgData(byte[] ds){
  66 + System.out.println("uploadMsgData" + ds);
  67 + if(ds != null){
  68 + length = ds.length ;
  69 + filename = null ;
  70 + this.datas = new byte[length ];
  71 + System.arraycopy(ds,0,this.datas,0,length);
  72 +// this.datas[length] = 0x13 ;
  73 +// this.datas[length + 1] = 0x0 ;
  74 +// this.datas[length + 2] = 0x10 ;
  75 +// this.datas[length + 3] = 0x0 ;
  76 + task = new A0(filename,length);
  77 + handler.removeCallbacks(task);
  78 + handler.post(task);
  79 + return true;
  80 + }
  81 + return false;
  82 + }
  83 +
  84 +
  85 + public void setKeyid(byte[] keyid) {
  86 + this.keyid = keyid;
  87 + }
  88 +
  89 + public void uploadData(byte packid, byte packH, byte[] packLs){
  90 + System.out.println("uploadData");
  91 + handler.removeCallbacks(task);
  92 + task = new A2(keyid,packid,packH,datas,packLs);
  93 + handler.post(task);
  94 + if(uploadListener != null){
  95 + double process = 0 ;
  96 + if(packTotal > 0){
  97 + int count = 0 ;
  98 + int sen = (packLs[1] & 0xFF) * 256 + (packLs[0] & 0XFF);
  99 + for(int i = 0 ; i < 16 ; i++){
  100 + if((sen & (1 << i)) != 0){
  101 + count ++ ;
  102 + }
  103 + }
  104 + process = (double)(packH * 16 + count) / packTotal ;
  105 + }
  106 + uploadListener.onUploadProcess(process);
  107 + }
  108 + }
  109 +
  110 + public void packetConfirmation(byte packid){
  111 + System.out.println("packetConfirmation");
  112 + handler.removeCallbacks(task);
  113 + byte[] names = null;
  114 + if(filename != null){
  115 + try {
  116 + names = filename.getBytes("GB2312");
  117 + } catch (UnsupportedEncodingException e) {
  118 + e.printStackTrace();
  119 + }
  120 + }
  121 + task = new A1(keyid,(byte)((length ) / 65535), (byte)(((length ) % 65535) / 256),(byte)(((length ) % 256 / 16 + (length % 16 == 0 ? 0 : 1))),names);
  122 + handler.post(task);
  123 + }
  124 +
  125 + public void packetReceptionConfirmed(byte packid,byte packH){
  126 + System.out.println("packetReceptionConfirmed");
  127 + handler.removeCallbacks(task);
  128 + task = new A3(keyid,packid,packH);
  129 + handler.post(task);
  130 + }
  131 +
  132 + public boolean uploadMessage(byte[] datas){
  133 + System.out.println("uploadMsgData" + datas);
  134 + if(datas != null){
  135 + length = datas.length ;
  136 + filename = null ;
  137 + this.datas = new byte[length ];
  138 + System.arraycopy(datas,0,this.datas,0,length);
  139 +// datas[length] = 0x13 ;
  140 +// datas[length + 1] = 0x0 ;
  141 +// datas[length + 2] = 0x10 ;
  142 +// datas[length + 3] = 0x0 ;
  143 + task = new A0(filename,length);
  144 + handler.removeCallbacks(task);
  145 + handler.post(task);
  146 + return true;
  147 + }
  148 + return false;
  149 + }
  150 +
  151 +
  152 + public void setUploadListener(IUploadListener uploadListener) {
  153 + this.uploadListener = uploadListener;
  154 + }
  155 +
  156 + class A0 implements Runnable{
  157 + private int length ;
  158 + private String filename;
  159 + private int times ;
  160 + public A0(String filename,int length){
  161 + this.length = length ;
  162 + this.filename = filename;
  163 + packTotal = length / 16 + 1;
  164 + times = 0 ;
  165 + }
  166 +
  167 + @Override
  168 + public void run() {
  169 + XPadApi.getInstance().applyFileUpload(this.length,this.filename,PACKTYPE,RFFileUploadModule.index);
  170 + }
  171 + }
  172 +
  173 + class A1 implements Runnable{
  174 +
  175 + private byte[] keyid;
  176 + private byte packid ;
  177 + private byte packH ;
  178 + private byte packL ;
  179 + private byte[] names ;
  180 + private int times ;
  181 +
  182 + public A1(byte[] keyid, byte packid,byte packH,byte packL,byte[] names) {
  183 + this.keyid = keyid;
  184 + this.packid = packid;
  185 + this.packH = packH ;
  186 + this.packL = packL ;
  187 + this.names = names ;
  188 + times = 0 ;
  189 + }
  190 +
  191 + @Override
  192 + public void run() {
  193 + XPadApi.getInstance().packetConfirmation(this.keyid,this.packid,this.packH,this.packL,this.names,PACKTYPE);
  194 + times ++ ;
  195 + if(task != null && times < 5){
  196 + handler.postDelayed(task,1000);
  197 + }
  198 + if(uploadListener != null){
  199 + uploadListener.onUploadStart();
  200 + }
  201 +
  202 + handler.removeCallbacks(failCall);
  203 + handler.postDelayed(failCall,10000);
  204 + }
  205 + }
  206 +
  207 + class A2 implements Runnable{
  208 + private byte[] keyid;
  209 + private byte packid;
  210 + private byte packH;
  211 + private byte[] datas ;
  212 + private byte[] packLs;
  213 + private int times ;
  214 +
  215 + public A2(byte[] keyid, byte packid, byte packH, byte[] datas, byte[] packLs) {
  216 + this.keyid = keyid;
  217 + this.packid = packid;
  218 + this.packH = packH;
  219 + this.datas = datas;
  220 + this.packLs = packLs;
  221 + times = 0 ;
  222 + }
  223 +
  224 + @Override
  225 + public void run() {
  226 +
  227 + int packLsi = packLs[1] * 256 + packLs[0];
  228 + int offset = 0 ;
  229 + int length = 16;
  230 + System.out.println("packLsi:" + packLsi);
  231 + for(byte packL = 0 ;packL < 16 ;packL ++) {
  232 + if((packLsi & (1 << packL)) != 0){
  233 + offset = ((packid * 65535) + (packH) * 256 + 16 * (packL)) & 0xFFFF;
  234 + XPadApi.getInstance().uploadFileData(this.keyid, this.packid, this.packH, (byte)(packL), this.datas, offset, length,PACKTYPE);
  235 + try{
  236 + Thread.sleep(50);
  237 + } catch (Exception e) {
  238 + e.printStackTrace();
  239 + }
  240 + }
  241 + }
  242 + times ++ ;
  243 + if(task != null && times < 5){
  244 + handler.postDelayed(task,1000);
  245 + }
  246 + handler.removeCallbacks(failCall);
  247 + handler.postDelayed(failCall,10000);
  248 + }
  249 + }
  250 +
  251 + class A3 implements Runnable{
  252 + private byte[] keyid;
  253 + private byte packid ;
  254 + private byte packH;
  255 +
  256 + public A3(byte[] keyid, byte packid, byte packH) {
  257 + this.keyid = keyid;
  258 + this.packid = packid;
  259 + this.packH = packH;
  260 + }
  261 +
  262 + @Override
  263 + public void run() {
  264 + XPadApi.getInstance().packetReceptionConfirmed(this.keyid,this.packid,this.packH,PACKTYPE);
  265 + if(uploadListener != null){
  266 + uploadListener.onUploadStop();
  267 + }
  268 + handler.removeCallbacks(failCall);
  269 + }
  270 + }
  271 +
  272 + public void onUploadMessage(byte data, byte[] keyid, byte packid, byte packH, byte[] packLs) {
  273 + LogUtil.i("RFMessageUploadModule","onUploadMessage:" + data);
  274 + if (data == 1) {
  275 + setKeyid(keyid);
  276 + packetConfirmation(packid);
  277 + }
  278 + if (data == 2) {
  279 + uploadData(packid, packH, packLs);
  280 + }
  281 + if (data == 3) {
  282 + packetReceptionConfirmed(packid, packH);
  283 + }
  284 + }
  285 +
  286 + private void removeTask(){
  287 + handler.postDelayed(new Runnable() {
  288 + @Override
  289 + public void run() {
  290 + handler.removeCallbacks(task);
  291 + }
  292 + },5000);
  293 + }
  294 +
  295 + private Runnable failCall = new Runnable() {
  296 + @Override
  297 + public void run() {
  298 + if(uploadListener != null){
  299 + uploadListener.onFail();
  300 + }
  301 + }
  302 + };
  303 +}
... ...
C5/app/src/main/java/com/sunvote/xpadcomm/usb/UsbTransferManager.java 0 → 100644
  1 +package com.sunvote.xpadcomm.usb;
  2 +
  3 +import android.app.PendingIntent;
  4 +import android.content.BroadcastReceiver;
  5 +import android.content.Context;
  6 +import android.content.Intent;
  7 +import android.content.IntentFilter;
  8 +import android.hardware.usb.UsbDevice;
  9 +import android.hardware.usb.UsbDeviceConnection;
  10 +import android.hardware.usb.UsbEndpoint;
  11 +import android.hardware.usb.UsbInterface;
  12 +import android.hardware.usb.UsbManager;
  13 +import android.os.Handler;
  14 +import android.os.HandlerThread;
  15 +
  16 +import com.sunvote.util.LogUtil;
  17 +
  18 +import java.io.IOException;
  19 +import java.io.InputStream;
  20 +import java.io.OutputStream;
  21 +import java.util.HashMap;
  22 +import java.util.LinkedList;
  23 +import java.util.concurrent.locks.Condition;
  24 +import java.util.concurrent.locks.Lock;
  25 +import java.util.concurrent.locks.ReentrantLock;
  26 +
  27 +/***
  28 + * @Auther Elvis
  29 + */
  30 +public class UsbTransferManager {
  31 +
  32 + public interface OnUsbConnectListener{
  33 + boolean onConnect(boolean isConnected);
  34 + }
  35 +
  36 + public static final String TAG = UsbTransferManager.class.getSimpleName();
  37 + private static UsbTransferManager instance = null ;
  38 + private HandlerThread workThread = new HandlerThread("usb workThread");
  39 + private Handler workHandler = null;
  40 + private boolean stop = false;
  41 + private OnUsbConnectListener onUsbConnectListener;
  42 +
  43 + public void setOnUsbConnectListener(OnUsbConnectListener onUsbConnectListener) {
  44 + this.onUsbConnectListener = onUsbConnectListener;
  45 + }
  46 +
  47 + private UsbTransferManager(){
  48 + workThread.start();
  49 + workHandler = new Handler(workThread.getLooper());
  50 + }
  51 +
  52 + public static UsbTransferManager getInstance(){
  53 + if(instance == null){
  54 + synchronized(UsbTransferManager.class){
  55 + if(instance == null){
  56 + instance = new UsbTransferManager();
  57 + }
  58 + }
  59 + }
  60 + return instance;
  61 + }
  62 +
  63 + public void startWork(){
  64 + stop = false;
  65 + workHandler.post(usbConnectInit);
  66 + }
  67 +
  68 + public void stopWork(){
  69 + mUsbDevice = null;
  70 + mUsbConnection = null;
  71 + epIn = null;
  72 + epOut = null;
  73 + workHandler.removeCallbacks(usbConnectInit);
  74 + workHandler.removeCallbacks(usbConnect);
  75 + workHandler.removeCallbacks(usbConnectionReceiverTask);
  76 + stop = true;
  77 + }
  78 +
  79 + private UsbOutputStream outputStream = new UsbOutputStream();
  80 + private UsbInputStream inputStream = new UsbInputStream();
  81 +
  82 + public UsbInputStream getInputStream() {
  83 + return inputStream;
  84 + }
  85 +
  86 + public UsbOutputStream getOutputStream() {
  87 + return outputStream;
  88 + }
  89 +
  90 + private long lastOpenTime;
  91 + private class UsbPermissionReceiver extends BroadcastReceiver {
  92 + public void onReceive(Context context, Intent intent) {
  93 + String action = intent.getAction();
  94 + if (ACTION_USB_PERMISSION.equals(action)) {
  95 + synchronized (this) {
  96 + UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  97 + if (device.getDeviceName().equals(mUsbDevice.getDeviceName())) {
  98 + if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
  99 + // 授权成功,在这里进行打开设备操作
  100 + if (System.currentTimeMillis() - lastOpenTime > 1000) {
  101 + lastOpenTime = System.currentTimeMillis();
  102 + workHandler.removeCallbacks(usbConnectInit);
  103 + workHandler.removeCallbacks(usbConnect);
  104 + if(!stop){
  105 + workHandler.postDelayed(usbConnect,0);
  106 + }
  107 + }
  108 + }else{
  109 + if(!stop){
  110 + workHandler.postDelayed(usbConnectInit,20000);
  111 + }
  112 + }
  113 + }
  114 + }
  115 + }
  116 + }
  117 + }
  118 +
  119 + private Runnable usbConnectionReceiverTask = new Runnable() {
  120 + @Override
  121 + public void run() {
  122 + if (usbManager != null) {
  123 + HashMap<String, UsbDevice> map = usbManager.getDeviceList();
  124 + boolean find = false;
  125 + for (UsbDevice device : map.values()) {
  126 + LogUtil.d(TAG, "找到基站: Vid:" + device.getVendorId() + " Pid:" + device.getProductId());
  127 + if (device.getVendorId() == VendorID && device.getProductId() == ProductID
  128 + || device.getVendorId() == VendorID_2 && device.getProductId() == ProductID_2
  129 + || device.getVendorId() == VendorID_3 && device.getProductId() == ProductID_3) {
  130 + find = true;
  131 + workHandler.postDelayed(usbConnectionReceiverTask, 1000);
  132 + break;
  133 + }
  134 + }
  135 + if (!find) {
  136 + stopWork();
  137 + startWork();
  138 + }
  139 + }
  140 + }
  141 + };
  142 +
  143 + private Runnable usbConnectInit = new Runnable() {
  144 + @Override
  145 + public void run() {
  146 + LogUtil.i(TAG, "发起USB初始化!");
  147 + boolean find = false;
  148 + workHandler.removeCallbacks(usbConnectionReceiverTask);
  149 + if(usbManager != null) {
  150 + HashMap<String, UsbDevice> map = usbManager.getDeviceList();
  151 + for (UsbDevice device : map.values()) {
  152 + LogUtil.d(TAG, "找到基站: Vid:" + device.getVendorId() + " Pid:" + device.getProductId());
  153 + if (device.getVendorId() == VendorID && device.getProductId() == ProductID
  154 + || device.getVendorId() == VendorID_2 && device.getProductId() == ProductID_2
  155 + || device.getVendorId() == VendorID_3 && device.getProductId() == ProductID_3) {
  156 + mUsbDevice = device;
  157 + find = true;
  158 + if (!usbManager.hasPermission(device)) {
  159 + if (usbPermissionReceiver == null) {
  160 + usbPermissionReceiver = new UsbPermissionReceiver();
  161 + }
  162 + // 申请权限
  163 + Intent intent = new Intent(ACTION_USB_PERMISSION);
  164 + PendingIntent mPermissionIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
  165 + IntentFilter permissionFilter = new IntentFilter(ACTION_USB_PERMISSION);
  166 + mContext.registerReceiver(usbPermissionReceiver, permissionFilter);
  167 + usbManager.requestPermission(device, mPermissionIntent);
  168 + if(!stop){
  169 + workHandler.postDelayed(usbConnectInit,20 * 1000);
  170 + }
  171 + } else {
  172 + if(!stop){
  173 + workHandler.postDelayed(usbConnect,10);
  174 + }
  175 + }
  176 + }
  177 + }
  178 +
  179 + }
  180 + if(!find){
  181 + workHandler.removeCallbacks(usbConnectInit);
  182 + if(!stop){
  183 + workHandler.postDelayed(usbConnectInit,2000);
  184 + }
  185 + }
  186 + }
  187 + };
  188 +
  189 +
  190 + private Runnable usbConnect = new Runnable() {
  191 + @Override
  192 + public void run() {
  193 + LogUtil.i(TAG, "发起USB键盘连接!");
  194 + if(openUsbDevice()){
  195 + if(onUsbConnectListener != null){
  196 + onUsbConnectListener.onConnect(true);
  197 + }
  198 + workHandler.postDelayed(usbConnectionReceiverTask,2000);
  199 + };
  200 + }
  201 + };
  202 +
  203 + private Runnable readData = new Runnable() {
  204 + @Override
  205 + public void run() {
  206 + byte[] datas = receiveUsbRequestData();
  207 + LogUtil.i(TAG, "RECEIVEA DATA:", datas);
  208 + if(datas != null) {
  209 + int length = datas.length;
  210 + try {
  211 + try {
  212 + lock.lock();
  213 + if (linkedList.size() < MAX_CACHE_SIZE) {
  214 + for (int i = length - 1; i >= 0; i--) {
  215 + linkedList.push(datas[i]);
  216 + }
  217 + }
  218 + } finally {
  219 + empty.signal();
  220 + lock.unlock();
  221 + }
  222 + } catch (Exception e) {
  223 + LogUtil.e(TAG, "UDP receiver message", e);
  224 + close = true;
  225 + }
  226 + if(!stop){
  227 + workHandler.post(readData);
  228 + }
  229 + }else{
  230 + stopWork();
  231 + startWork();
  232 + }
  233 +
  234 + }
  235 + };
  236 +
  237 + /**
  238 + * 打开连接
  239 + *
  240 + * @paramdevice
  241 + */
  242 + private boolean openUsbDevice() {
  243 +
  244 + if (mUsbDevice == null)
  245 + return false;
  246 +
  247 + if(mUsbDevice.getInterfaceCount() == 0){
  248 + return false;
  249 + }
  250 + mUsbInterface = mUsbDevice.getInterface(0);
  251 + setEndpoint(mUsbInterface);
  252 + mUsbConnection = usbManager.openDevice(mUsbDevice);
  253 + LogUtil.i(TAG,"打开USB连接");
  254 + if (mUsbConnection != null) {
  255 + return mUsbConnection.claimInterface(mUsbInterface, true);
  256 + }
  257 + return false;
  258 + }
  259 +
  260 + private int sendDataBulkTransfer(byte[] buffer) {
  261 + final int length = buffer.length;
  262 + int ref = -100;
  263 + if (epOut != null && mUsbConnection != null) {
  264 + ref = mUsbConnection.bulkTransfer(epOut, buffer, length, 100);
  265 + mUsbConnection.claimInterface(mUsbInterface, true);
  266 + LogUtil.d(TAG, "发送数据成功有:" + ref);
  267 + if(ref <0 ){
  268 + if(onUsbConnectListener != null){
  269 + onUsbConnectListener.onConnect(false);
  270 + }
  271 + workHandler.removeCallbacks(usbConnectionReceiverTask);
  272 + workHandler.post(usbConnectionReceiverTask);
  273 + }
  274 + }else{
  275 + LogUtil.d(TAG, "未连接:" + ref);
  276 + }
  277 + return ref;
  278 + }
  279 +
  280 + public byte[] receiveUsbRequestData() {
  281 + if(epIn != null && mUsbConnection != null) {
  282 + byte[] bytes = new byte[epIn.getMaxPacketSize()];
  283 + int ret = mUsbConnection.bulkTransfer(epIn, bytes, bytes.length, 100);
  284 + if (ret > 0) {
  285 + return bytes;
  286 + }
  287 + }
  288 + return null;
  289 + }
  290 +
  291 + /**
  292 + * UsbInterface 进行端点设置和通讯
  293 + *
  294 + * @param intf
  295 + */
  296 + private void setEndpoint(UsbInterface intf) {
  297 + if (intf == null)
  298 + return;
  299 + // 设置接收数据的端点
  300 + if (intf.getEndpoint(0) != null) {
  301 + epIn = intf.getEndpoint(0);
  302 + }
  303 + // 当端点为2的时候
  304 + if (intf.getEndpointCount() == 2) {
  305 + // 设置发送数据的断点
  306 + if (intf.getEndpoint(1) != null)
  307 + epOut = intf.getEndpoint(1);
  308 + }
  309 + }
  310 +
  311 + public void setUsbManager(UsbManager usbManager) {
  312 + this.usbManager = usbManager;
  313 + }
  314 +
  315 + public void setContext(Context mContext) {
  316 + this.mContext = mContext;
  317 + }
  318 +
  319 + public class UsbOutputStream extends OutputStream{
  320 +
  321 + @Override
  322 + public void write(int b) throws IOException {
  323 + sendDataBulkTransfer(new byte[]{(byte)b});
  324 + }
  325 +
  326 + @Override
  327 + public void write(byte[] b) throws IOException {
  328 + sendDataBulkTransfer(b);
  329 + }
  330 +
  331 + @Override
  332 + public void write(byte[] b, int off, int len) throws IOException {
  333 + byte[] temp = new byte[len];
  334 + System.arraycopy(b, off, temp, 0, len);
  335 + sendDataBulkTransfer(temp);
  336 + }
  337 + }
  338 +
  339 + private LinkedList<Byte> linkedList = new LinkedList<Byte>();
  340 + public long MAX_CACHE_SIZE = 4 * 1024;
  341 + private Lock lock = new ReentrantLock();
  342 + private Condition empty = lock.newCondition();
  343 + private boolean close = false;
  344 + public class UsbInputStream extends InputStream{
  345 + @Override
  346 + public int read() throws IOException {
  347 + if (close) {
  348 + throw new IOException("the stream has closed");
  349 + }
  350 +
  351 + try {
  352 + lock.lock();
  353 + while (linkedList.isEmpty()) {
  354 + try {
  355 + empty.await();
  356 + } catch (InterruptedException e) {
  357 + LogUtil.e(TAG,e);
  358 + }
  359 + }
  360 + return linkedList.pop();
  361 + } finally {
  362 + lock.unlock();
  363 + }
  364 + }
  365 +
  366 +
  367 + public boolean isClose() {
  368 + return close;
  369 + }
  370 +
  371 + @Override
  372 + public void close() throws IOException {
  373 + close = true;
  374 + super.close();
  375 + }
  376 +
  377 + @Override
  378 + public int read(byte[] buffer) throws IOException {
  379 + try {
  380 + lock.lock();
  381 + if (buffer == null) {
  382 + throw new IOException("buffer is empty");
  383 + }
  384 + if (close) {
  385 + throw new IOException("the stream has closed");
  386 + }
  387 +
  388 + while (linkedList.isEmpty()) {
  389 + try {
  390 + empty.await();
  391 + } catch (InterruptedException e) {
  392 + e.printStackTrace();
  393 + }
  394 + }
  395 + int index = 0;
  396 + while (index < buffer.length && !linkedList.isEmpty()) {
  397 + buffer[index++] = linkedList.pop();
  398 + }
  399 + return index;
  400 + } finally {
  401 + lock.unlock();
  402 + }
  403 + }
  404 +
  405 +
  406 + @Override
  407 + public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
  408 + try {
  409 + lock.lock();
  410 + if (buffer == null) {
  411 + throw new IOException("buffer is empty");
  412 + }
  413 + if (close) {
  414 + throw new IOException("the stream has closed");
  415 + }
  416 +
  417 + while (linkedList.isEmpty()) {
  418 + try {
  419 + empty.await();
  420 + } catch (InterruptedException e) {
  421 + e.printStackTrace();
  422 + }
  423 + }
  424 + int index = 0;
  425 + while (index < byteCount && !linkedList.isEmpty()) {
  426 + index++;
  427 + buffer[byteOffset++] = linkedList.pop();
  428 + }
  429 + return index;
  430 + } finally {
  431 + lock.unlock();
  432 + }
  433 + }
  434 + }
  435 +
  436 + private UsbManager usbManager ;
  437 + private UsbDevice mUsbDevice;
  438 + private UsbDeviceConnection mUsbConnection;
  439 + private UsbInterface mUsbInterface;
  440 + private UsbEndpoint epOut, epIn;
  441 + private UsbPermissionReceiver usbPermissionReceiver;
  442 + private Context mContext ;
  443 +
  444 + private static final int VendorID = 0x03eb;
  445 + private static final int ProductID = 0x6201;
  446 + private static final int VendorID_2 = 0x0d8c;
  447 + private static final int ProductID_2 = 0xEA10;
  448 + private static final int VendorID_3 = 0x2F70;
  449 + private static final int ProductID_3 = 0xEA10;
  450 + private final String ACTION_USB_PERMISSION = "com.hhd.USB_PERMISSION";
  451 + private boolean isUsbReceiver = false;
  452 +}
... ...
C5/app/src/main/res/drawable-xhdpi/signal_1.png 0 → 100644

1.76 KB

C5/app/src/main/res/drawable-xhdpi/signal_2.png 0 → 100644

1.72 KB

C5/app/src/main/res/drawable-xhdpi/signal_3.png 0 → 100644

1.77 KB

C5/app/src/main/res/drawable-xhdpi/signal_4.png 0 → 100644

1.66 KB

C5/app/src/main/res/drawable-xhdpi/signal_5.png 0 → 100644

1.51 KB

C5/app/src/main/res/drawable-xxhdpi/signal_1.png 0 → 100644

1.64 KB

C5/app/src/main/res/drawable-xxhdpi/signal_2.png 0 → 100644

1.74 KB

C5/app/src/main/res/drawable-xxhdpi/signal_3.png 0 → 100644

1.74 KB

C5/app/src/main/res/drawable-xxhdpi/signal_4.png 0 → 100644

1.71 KB

C5/app/src/main/res/drawable-xxhdpi/signal_5.png 0 → 100644

1.45 KB

C5/app/src/main/res/drawable-xxxhdpi/signal_1.png 0 → 100644

2.82 KB

C5/app/src/main/res/drawable-xxxhdpi/signal_2.png 0 → 100644

2.63 KB

C5/app/src/main/res/drawable-xxxhdpi/signal_3.png 0 → 100644

2.68 KB

C5/app/src/main/res/drawable-xxxhdpi/signal_4.png 0 → 100644

2.44 KB

C5/app/src/main/res/drawable-xxxhdpi/signal_5.png 0 → 100644

2.13 KB

C5/app/src/main/res/mipmap-xhdpi/signal.png 0 → 100644

1.66 KB

C5/app/src/main/res/mipmap-xhdpi/signal_1.png 0 → 100644

1.76 KB

C5/app/src/main/res/mipmap-xhdpi/signal_2.png 0 → 100644

1.72 KB

C5/app/src/main/res/mipmap-xhdpi/signal_3.png 0 → 100644

1.77 KB

C5/app/src/main/res/mipmap-xhdpi/signal_4.png 0 → 100644

1.66 KB

C5/app/src/main/res/mipmap-xhdpi/signal_5.png 0 → 100644

1.51 KB

C5/app/src/main/res/mipmap-xxhdpi/signal.png 0 → 100644

1.54 KB

C5/app/src/main/res/mipmap-xxhdpi/signal_1.png 0 → 100644

1.64 KB

C5/app/src/main/res/mipmap-xxhdpi/signal_2.png 0 → 100644

1.74 KB

C5/app/src/main/res/mipmap-xxhdpi/signal_3.png 0 → 100644

1.74 KB

C5/app/src/main/res/mipmap-xxhdpi/signal_4.png 0 → 100644

1.71 KB

C5/app/src/main/res/mipmap-xxhdpi/signal_5.png 0 → 100644

1.45 KB

C5/app/src/main/res/mipmap-xxxhdpi/signal.png 0 → 100644

2.48 KB

C5/app/src/main/res/mipmap-xxxhdpi/signal_1.png 0 → 100644

2.82 KB

C5/app/src/main/res/mipmap-xxxhdpi/signal_2.png 0 → 100644

2.63 KB

C5/app/src/main/res/mipmap-xxxhdpi/signal_3.png 0 → 100644

2.68 KB

C5/app/src/main/res/mipmap-xxxhdpi/signal_4.png 0 → 100644

2.44 KB

C5/app/src/main/res/mipmap-xxxhdpi/signal_5.png 0 → 100644

2.13 KB