├── .gitignore ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── tcl │ │ └── navigator │ │ └── accessorychart │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── tcl │ │ │ └── navigator │ │ │ └── accessorychart │ │ │ ├── activity │ │ │ └── MainActivity.java │ │ │ ├── base │ │ │ └── MyApplication.java │ │ │ ├── receiver │ │ │ ├── OpenAccessoryReceiver.java │ │ │ └── UsbDetachedReceiver.java │ │ │ └── utils │ │ │ └── CrashHandler.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ ├── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ │ └── xml │ │ └── accessory_filter.xml │ └── test │ └── java │ └── com │ └── tcl │ └── navigator │ └── accessorychart │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.tcl.navigator.accessorychart" 8 | minSdkVersion 15 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:24.1.1' 28 | compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4' 29 | testCompile 'junit:junit:4.12' 30 | } 31 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\Administrator.WIN-20160308XVA\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/tcl/navigator/accessorychart/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.tcl.navigator.accessorychart; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.tcl.navigator.accessorychart", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/tcl/navigator/accessorychart/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.tcl.navigator.accessorychart.activity; 2 | 3 | import android.app.PendingIntent; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.content.IntentFilter; 7 | import android.hardware.usb.UsbAccessory; 8 | import android.hardware.usb.UsbManager; 9 | import android.os.Bundle; 10 | import android.os.Handler; 11 | import android.os.Message; 12 | import android.os.ParcelFileDescriptor; 13 | import android.support.v7.app.AppCompatActivity; 14 | import android.text.TextUtils; 15 | import android.view.View; 16 | import android.widget.Button; 17 | import android.widget.EditText; 18 | import android.widget.TextView; 19 | 20 | import com.tcl.navigator.accessorychart.R; 21 | import com.tcl.navigator.accessorychart.receiver.OpenAccessoryReceiver; 22 | import com.tcl.navigator.accessorychart.receiver.UsbDetachedReceiver; 23 | 24 | import java.io.FileDescriptor; 25 | import java.io.FileInputStream; 26 | import java.io.FileOutputStream; 27 | import java.io.IOException; 28 | import java.util.concurrent.ExecutorService; 29 | import java.util.concurrent.Executors; 30 | 31 | public class MainActivity extends AppCompatActivity implements OpenAccessoryReceiver.OpenAccessoryListener, View.OnClickListener, UsbDetachedReceiver.UsbDetachedListener { 32 | 33 | private static final int SEND_MESSAGE_SUCCESS = 0; 34 | private static final int RECEIVER_MESSAGE_SUCCESS = 1; 35 | private static final String USB_ACTION = "com.tcl.navigator.accessorychart"; 36 | private TextView mLog; 37 | private EditText mMessage; 38 | private Button mSend; 39 | private UsbManager mUsbManager; 40 | private OpenAccessoryReceiver mOpenAccessoryReceiver; 41 | private ParcelFileDescriptor mParcelFileDescriptor; 42 | private FileInputStream mFileInputStream; 43 | private FileOutputStream mFileOutputStream; 44 | private ExecutorService mThreadPool; 45 | private byte[] mBytes = new byte[1024]; 46 | private StringBuffer mStringBuffer = new StringBuffer(); 47 | private UsbDetachedReceiver mUsbDetachedReceiver; 48 | 49 | private final Handler mHandler = new Handler() { 50 | @Override 51 | public void handleMessage(Message msg) { 52 | switch (msg.what) { 53 | case SEND_MESSAGE_SUCCESS: 54 | mMessage.setText(""); 55 | mMessage.clearComposingText(); 56 | break; 57 | 58 | case RECEIVER_MESSAGE_SUCCESS: 59 | mLog.setText(mStringBuffer.toString()); 60 | break; 61 | } 62 | } 63 | }; 64 | 65 | @Override 66 | protected void onCreate(Bundle savedInstanceState) { 67 | super.onCreate(savedInstanceState); 68 | setContentView(R.layout.activity_main); 69 | 70 | init(); 71 | } 72 | 73 | private void init() { 74 | initView(); 75 | initListener(); 76 | initData(); 77 | } 78 | 79 | private void initView() { 80 | mLog = (TextView) findViewById(R.id.log); 81 | mMessage = (EditText) findViewById(R.id.message); 82 | mSend = (Button) findViewById(R.id.send); 83 | } 84 | 85 | private void initListener() { 86 | mSend.setOnClickListener(this); 87 | } 88 | 89 | private void initData() { 90 | mSend.setEnabled(false); 91 | mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 92 | mThreadPool = Executors.newFixedThreadPool(3); 93 | 94 | mUsbDetachedReceiver = new UsbDetachedReceiver(this); 95 | IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_ACCESSORY_DETACHED); 96 | registerReceiver(mUsbDetachedReceiver, filter); 97 | 98 | mOpenAccessoryReceiver = new OpenAccessoryReceiver(this); 99 | PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(USB_ACTION), 0); 100 | IntentFilter intentFilter = new IntentFilter(USB_ACTION); 101 | registerReceiver(mOpenAccessoryReceiver, intentFilter); 102 | 103 | UsbAccessory[] accessories = mUsbManager.getAccessoryList(); 104 | UsbAccessory usbAccessory = (accessories == null ? null : accessories[0]); 105 | if (usbAccessory != null) { 106 | if (mUsbManager.hasPermission(usbAccessory)) { 107 | openAccessory(usbAccessory); 108 | } else { 109 | mUsbManager.requestPermission(usbAccessory, pendingIntent); 110 | } 111 | } 112 | } 113 | 114 | /** 115 | * 打开Accessory模式 116 | * 117 | * @param usbAccessory 118 | */ 119 | private void openAccessory(UsbAccessory usbAccessory) { 120 | mParcelFileDescriptor = mUsbManager.openAccessory(usbAccessory); 121 | if (mParcelFileDescriptor != null) { 122 | FileDescriptor fileDescriptor = mParcelFileDescriptor.getFileDescriptor(); 123 | mFileInputStream = new FileInputStream(fileDescriptor); 124 | mFileOutputStream = new FileOutputStream(fileDescriptor); 125 | mSend.setEnabled(true); 126 | 127 | mThreadPool.execute(new Runnable() { 128 | @Override 129 | public void run() { 130 | int i = 0; 131 | while (i >= 0) { 132 | try { 133 | i = mFileInputStream.read(mBytes); 134 | } catch (IOException e) { 135 | e.printStackTrace(); 136 | break; 137 | } 138 | if (i > 0) { 139 | mStringBuffer.append(new String(mBytes, 0, i) + "\n"); 140 | mHandler.sendEmptyMessage(RECEIVER_MESSAGE_SUCCESS); 141 | } 142 | } 143 | } 144 | }); 145 | } 146 | } 147 | 148 | @Override 149 | public void openAccessoryModel(UsbAccessory usbAccessory) { 150 | openAccessory(usbAccessory); 151 | } 152 | 153 | @Override 154 | public void openAccessoryError() { 155 | 156 | } 157 | 158 | @Override 159 | public void onClick(View v) { 160 | final String mMessageContent = mMessage.getText().toString(); 161 | if (!TextUtils.isEmpty(mMessageContent)) { 162 | mThreadPool.execute(new Runnable() { 163 | @Override 164 | public void run() { 165 | try { 166 | mFileOutputStream.write(mMessageContent.getBytes()); 167 | mHandler.sendEmptyMessage(SEND_MESSAGE_SUCCESS); 168 | } catch (IOException e) { 169 | e.printStackTrace(); 170 | } 171 | } 172 | }); 173 | } 174 | } 175 | 176 | @Override 177 | public void usbDetached() { 178 | finish(); 179 | } 180 | 181 | @Override 182 | protected void onDestroy() { 183 | mHandler.removeCallbacksAndMessages(null); 184 | super.onDestroy(); 185 | 186 | unregisterReceiver(mOpenAccessoryReceiver); 187 | unregisterReceiver(mUsbDetachedReceiver); 188 | if (mParcelFileDescriptor != null) { 189 | try { 190 | mParcelFileDescriptor.close(); 191 | } catch (IOException e) { 192 | e.printStackTrace(); 193 | } 194 | } 195 | if (mFileInputStream != null) { 196 | try { 197 | mFileInputStream.close(); 198 | } catch (IOException e) { 199 | e.printStackTrace(); 200 | } 201 | } 202 | if (mFileOutputStream != null) { 203 | try { 204 | mFileOutputStream.close(); 205 | } catch (IOException e) { 206 | e.printStackTrace(); 207 | } 208 | } 209 | } 210 | } 211 | -------------------------------------------------------------------------------- /app/src/main/java/com/tcl/navigator/accessorychart/base/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.tcl.navigator.accessorychart.base; 2 | 3 | import android.app.Application; 4 | 5 | import com.tcl.navigator.accessorychart.utils.CrashHandler; 6 | 7 | /** 8 | * Created by yaohui on 2017/4/19. 9 | */ 10 | 11 | public class MyApplication extends Application { 12 | 13 | @Override 14 | public void onCreate() { 15 | super.onCreate(); 16 | 17 | CrashHandler.getInstance().init(this); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/tcl/navigator/accessorychart/receiver/OpenAccessoryReceiver.java: -------------------------------------------------------------------------------- 1 | package com.tcl.navigator.accessorychart.receiver; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.hardware.usb.UsbAccessory; 7 | import android.hardware.usb.UsbManager; 8 | 9 | public class OpenAccessoryReceiver extends BroadcastReceiver { 10 | 11 | private OpenAccessoryListener mOpenAccessoryListener; 12 | 13 | public OpenAccessoryReceiver(OpenAccessoryListener openAccessoryListener) { 14 | mOpenAccessoryListener = openAccessoryListener; 15 | } 16 | 17 | @Override 18 | public void onReceive(Context context, Intent intent) { 19 | UsbAccessory usbAccessory = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); 20 | if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { 21 | if (usbAccessory != null) { 22 | mOpenAccessoryListener.openAccessoryModel(usbAccessory); 23 | } else { 24 | mOpenAccessoryListener.openAccessoryError(); 25 | } 26 | } else { 27 | mOpenAccessoryListener.openAccessoryError(); 28 | } 29 | } 30 | 31 | public interface OpenAccessoryListener { 32 | /** 33 | * 打开Accessory模式 34 | * 35 | * @param usbAccessory 36 | */ 37 | void openAccessoryModel(UsbAccessory usbAccessory); 38 | 39 | /** 40 | * 打开设备(手机)失败 41 | */ 42 | void openAccessoryError(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/tcl/navigator/accessorychart/receiver/UsbDetachedReceiver.java: -------------------------------------------------------------------------------- 1 | package com.tcl.navigator.accessorychart.receiver; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | 7 | public class UsbDetachedReceiver extends BroadcastReceiver { 8 | 9 | private UsbDetachedListener mUsbDetachedListener; 10 | 11 | public UsbDetachedReceiver(UsbDetachedListener usbDetachedListener) { 12 | mUsbDetachedListener = usbDetachedListener; 13 | } 14 | 15 | @Override 16 | public void onReceive(Context context, Intent intent) { 17 | mUsbDetachedListener.usbDetached(); 18 | } 19 | 20 | public interface UsbDetachedListener { 21 | /** 22 | * usb断开连接 23 | */ 24 | void usbDetached(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/tcl/navigator/accessorychart/utils/CrashHandler.java: -------------------------------------------------------------------------------- 1 | package com.tcl.navigator.accessorychart.utils; 2 | 3 | import android.content.Context; 4 | import android.content.pm.PackageInfo; 5 | import android.content.pm.PackageManager; 6 | import android.os.Build; 7 | import android.os.Environment; 8 | import android.os.Process; 9 | 10 | import java.io.BufferedWriter; 11 | import java.io.File; 12 | import java.io.FileWriter; 13 | import java.io.IOException; 14 | import java.io.PrintWriter; 15 | import java.text.SimpleDateFormat; 16 | 17 | /** 18 | * Created by yaohui on 2017/4/13. 19 | */ 20 | 21 | public class CrashHandler implements Thread.UncaughtExceptionHandler { 22 | 23 | private static final String FILE_NAME = "crash"; 24 | private static final String FILE_NAME_SUFFIX = ".trace"; 25 | private Thread.UncaughtExceptionHandler mDefaulCrashHandler; 26 | private Context mContext; 27 | private SimpleDateFormat mSdf; 28 | 29 | private CrashHandler() { 30 | } 31 | 32 | public static CrashHandler getInstance() { 33 | return CrashHandlerHolder.CRASH_HANDLER; 34 | } 35 | 36 | private static class CrashHandlerHolder { 37 | private static final CrashHandler CRASH_HANDLER = new CrashHandler(); 38 | } 39 | 40 | public void init(Context context) { 41 | // mDefaulCrashHandler = Thread.getDefaultUncaughtExceptionHandler(); 42 | Thread.setDefaultUncaughtExceptionHandler(this); 43 | mContext = context.getApplicationContext(); 44 | mSdf = (SimpleDateFormat) SimpleDateFormat.getInstance(); 45 | mSdf.applyPattern("yyyy-MM-dd HH:mm:ss"); 46 | } 47 | 48 | @Override 49 | public void uncaughtException(Thread t, Throwable e) { 50 | try { 51 | dumpException(e); 52 | uploadExceptionToServer(); 53 | } catch (IOException e1) { 54 | e1.printStackTrace(); 55 | } 56 | 57 | e.printStackTrace(); 58 | if (mDefaulCrashHandler != null) 59 | mDefaulCrashHandler.uncaughtException(t, e); 60 | else 61 | Process.killProcess(Process.myPid()); 62 | } 63 | 64 | private void dumpException(Throwable ex) throws IOException { 65 | String path = null; 66 | if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) 67 | path = mContext.getExternalFilesDir(null).getAbsolutePath() + "/crash/log"; 68 | else 69 | path = mContext.getFilesDir().getAbsolutePath() + "/crash/log"; 70 | File dir = new File(path); 71 | if (!dir.exists()) 72 | dir.mkdirs(); 73 | long current = System.currentTimeMillis(); 74 | String time = mSdf.format(current); 75 | File file = new File(path + File.separator + FILE_NAME + time + FILE_NAME_SUFFIX); 76 | try { 77 | PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file))); 78 | pw.println(time); 79 | dumpPhoneInfo(pw); 80 | pw.println(); 81 | ex.printStackTrace(pw); 82 | pw.close(); 83 | } catch (PackageManager.NameNotFoundException e) { 84 | e.printStackTrace(); 85 | } 86 | } 87 | 88 | private void dumpPhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException { 89 | PackageManager pm = mContext.getPackageManager(); 90 | PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES); 91 | pw.print("App Version: "); 92 | pw.print(pi.versionName); 93 | pw.print('_'); 94 | pw.println(pi.versionCode); 95 | 96 | //Android版本号 97 | pw.print("OS Version: "); 98 | pw.print(Build.VERSION.RELEASE); 99 | pw.print('_'); 100 | pw.println(Build.VERSION.SDK_INT); 101 | 102 | //手机制造商 103 | pw.print("Vendor: "); 104 | pw.println(Build.MANUFACTURER); 105 | 106 | //手机型号 107 | pw.print("Model: "); 108 | pw.println(Build.MODEL); 109 | 110 | //CPU架构 111 | pw.print("CPU ABI: "); 112 | pw.println(Build.CPU_ABI); 113 | } 114 | 115 | private void uploadExceptionToServer() { 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 12 | 13 | 18 | 19 | 20 | 25 | 26 | 30 | 31 |