105 lines
3.8 KiB
Java
105 lines
3.8 KiB
Java
package com.fuying.sn.service;
|
|
|
|
import android.app.Service;
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.hardware.usb.UsbManager;
|
|
import android.hardware.usb.UsbPortStatus;
|
|
import android.os.Bundle;
|
|
import android.os.IBinder;
|
|
import android.util.Log;
|
|
|
|
public class ControlPanelService extends Service {
|
|
private static final String TAG = ControlPanelService.class.getSimpleName();
|
|
|
|
public ControlPanelService() {
|
|
}
|
|
|
|
@Override
|
|
public IBinder onBind(Intent intent) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
registerUsbConnectionReceiver();
|
|
}
|
|
|
|
@Override
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
return super.onStartCommand(intent, flags, startId);
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
private void registerUsbConnectionReceiver() {
|
|
if (mUsbConnectionReceiver == null) {
|
|
mUsbConnectionReceiver = new UsbConnectionReceiver();
|
|
}
|
|
IntentFilter filter = new IntentFilter();
|
|
filter.addAction(UsbManager.ACTION_USB_STATE);
|
|
filter.addAction(UsbManager.ACTION_USB_PORT_CHANGED);
|
|
registerReceiver(mUsbConnectionReceiver, filter);
|
|
}
|
|
|
|
UsbConnectionReceiver mUsbConnectionReceiver;
|
|
|
|
private boolean mConnected;
|
|
private long mFunctions;
|
|
// private int mDataRole;
|
|
// private int mPowerRole;
|
|
|
|
public class UsbConnectionReceiver extends BroadcastReceiver {
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
Log.e(TAG, "onReceive: " + intent.getAction());
|
|
Bundle bundle = intent.getExtras();
|
|
if (UsbManager.ACTION_USB_STATE.equals(intent.getAction())) {
|
|
mConnected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED)
|
|
|| intent.getExtras().getBoolean(UsbManager.USB_HOST_CONNECTED);
|
|
long functions = UsbManager.FUNCTION_NONE;
|
|
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_MTP)
|
|
&& intent.getExtras().getBoolean(UsbManager.USB_DATA_UNLOCKED, false)) {
|
|
functions |= UsbManager.FUNCTION_MTP;
|
|
}
|
|
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_PTP)
|
|
&& intent.getExtras().getBoolean(UsbManager.USB_DATA_UNLOCKED, false)) {
|
|
functions |= UsbManager.FUNCTION_PTP;
|
|
}
|
|
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_MIDI)) {
|
|
functions |= UsbManager.FUNCTION_MIDI;
|
|
}
|
|
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_RNDIS)) {
|
|
functions |= UsbManager.FUNCTION_RNDIS;
|
|
}
|
|
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_ACCESSORY)) {
|
|
functions |= UsbManager.FUNCTION_ACCESSORY;
|
|
}
|
|
mFunctions = functions;
|
|
|
|
// mDataRole = mUsbBackend.getDataRole();
|
|
// mPowerRole = mUsbBackend.getPowerRole();
|
|
} else if (UsbManager.ACTION_USB_PORT_CHANGED.equals(intent.getAction())) {
|
|
UsbPortStatus portStatus = intent.getExtras()
|
|
.getParcelable(UsbManager.EXTRA_PORT_STATUS);
|
|
if (portStatus != null) {
|
|
// mDataRole = portStatus.getCurrentDataRole();
|
|
// mPowerRole = portStatus.getCurrentPowerRole();
|
|
}
|
|
}
|
|
// if (mUsbConnectionListener != null) {
|
|
// mUsbConnectionListener.onUsbConnectionChanged(mConnected, mFunctions, mPowerRole,
|
|
// mDataRole);
|
|
// }
|
|
}
|
|
}
|
|
}
|