Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(mod) prepare UVCCamera for Android Oreo #443

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions libuvccamera/src/main/java/com/serenegiant/usb/USBMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public final class USBMonitor {
private final WeakReference<Context> mWeakContext;
private final UsbManager mUsbManager;
private final OnDeviceConnectListener mOnDeviceConnectListener;
private UsbDevice connectionInProgressDevice = null;
private PendingIntent mPermissionIntent = null;
private List<DeviceFilter> mDeviceFilters = new ArrayList<DeviceFilter>();

Expand Down Expand Up @@ -169,7 +170,7 @@ public synchronized void register() throws IllegalStateException {
if (context != null) {
mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
final IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
// ACTION_USB_DEVICE_ATTACHED never comes on some devices so it should not be added here
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
context.registerReceiver(mUsbReceiver, filter);
}
Expand Down Expand Up @@ -488,6 +489,16 @@ public void onReceive(final Context context, final Intent intent) {
}
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
final UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if(
connectionInProgressDevice != null &&
connectionInProgressDevice.getVendorId() == device.getVendorId() &&
connectionInProgressDevice.getProductId() == device.getProductId() &&
connectionInProgressDevice.getDeviceClass() == device.getDeviceClass() &&
connectionInProgressDevice.getDeviceSubclass() == device.getDeviceSubclass()
){
// probably our last connection was interrupted by kernel detaching a v4l mount, try immediate reconnect
requestPermission(device);
}
updatePermission(device, hasPermission(device));
processAttach(device);
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
Expand Down Expand Up @@ -551,6 +562,7 @@ public void run() {
*/
private final void processConnect(final UsbDevice device) {
if (destroyed) return;
connectionInProgressDevice = device;
updatePermission(device, true);
mAsyncHandler.post(new Runnable() {
@Override
Expand All @@ -566,7 +578,8 @@ public void run() {
} else {
createNew = false;
}
if (mOnDeviceConnectListener != null) {
if (mOnDeviceConnectListener != null && ctrlBlock.getConnection() != null) {
connectionInProgressDevice = null;
mOnDeviceConnectListener.onConnect(device, ctrlBlock, createNew);
}
}
Expand Down Expand Up @@ -882,6 +895,7 @@ public static UsbDeviceInfo getDeviceInfo(final Context context, final UsbDevice
* @param _info
* @return
*/
@SuppressLint("NewApi")
public static UsbDeviceInfo updateDeviceInfo(final UsbManager manager, final UsbDevice device, final UsbDeviceInfo _info) {
final UsbDeviceInfo info = _info != null ? _info : new UsbDeviceInfo();
info.clear();
Expand All @@ -897,6 +911,7 @@ public static UsbDeviceInfo updateDeviceInfo(final UsbManager manager, final Usb
}
if ((manager != null) && manager.hasPermission(device)) {
final UsbDeviceConnection connection = manager.openDevice(device);
if(connection == null) return info; // return what we have without a connection
final byte[] desc = connection.getRawDescriptors();

if (TextUtils.isEmpty(info.usb_version)) {
Expand Down Expand Up @@ -1228,6 +1243,7 @@ public synchronized UsbInterface getInterface(final int interface_id) throws Ill
* @return
* @throws IllegalStateException
*/
@SuppressLint("NewApi")
public synchronized UsbInterface getInterface(final int interface_id, final int altsetting) throws IllegalStateException {
checkConnection();
SparseArray<UsbInterface> intfs = mInterfaces.get(interface_id);
Expand Down
13 changes: 12 additions & 1 deletion usbCameraTest/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/Theme.AppCompat" >
<activity
android:name="com.serenegiant.usbcameratest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--uncomment to get permanent USB Permission
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
-->
</activity>
</application>

Expand Down
3 changes: 1 addition & 2 deletions usbCameraTest/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
android:id="@+id/UVCCameraTextureView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#ff000000" />
android:layout_gravity="center" />

<ImageButton
android:id="@+id/camera_button"
Expand Down
2 changes: 1 addition & 1 deletion usbCameraTest0/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/Theme.AppCompat" >
<activity
android:name="com.serenegiant.usbcameratest0.MainActivity"
android:label="@string/app_name" >
Expand Down
2 changes: 1 addition & 1 deletion usbCameraTest2/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/Theme.AppCompat" >
<activity
android:name="com.serenegiant.usbcameratest2.MainActivity"
android:label="@string/app_name" >
Expand Down
2 changes: 1 addition & 1 deletion usbCameraTest3/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/Theme.AppCompat" >
<activity
android:name="com.serenegiant.usbcameratest3.MainActivity"
android:screenOrientation="sensorLandscape"
Expand Down
5 changes: 3 additions & 2 deletions usbCameraTest4/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

Expand All @@ -35,7 +36,7 @@
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/Theme.AppCompat" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
Expand Down Expand Up @@ -65,4 +66,4 @@
</service>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

package com.serenegiant.service;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.support.v4.app.NotificationCompat;
import android.app.NotificationChannel;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
Expand All @@ -40,6 +44,7 @@
import com.serenegiant.usb.USBMonitor.UsbControlBlock;
import com.serenegiant.usbcameratest4.MainActivity;
import com.serenegiant.usbcameratest4.R;
import com.serenegiant.utils.BuildCheck;

public class UVCService extends BaseService {
private static final boolean DEBUG = true;
Expand Down Expand Up @@ -111,6 +116,17 @@ public boolean onUnbind(final Intent intent) {
return true;
}

@TargetApi(26)
private synchronized String createChannel() {
String channelId = "usbCameraTest4Service";
NotificationChannel mChannel = new NotificationChannel(channelId, "UsbCameraTest4 Service", NotificationManager.IMPORTANCE_LOW);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(mChannel);
} else {
stopSelf();
}
return channelId;
}
//********************************************************************************
/**
* helper method to show/change message on notification area
Expand All @@ -119,8 +135,14 @@ public boolean onUnbind(final Intent intent) {
*/
private void showNotification(final CharSequence text) {
if (DEBUG) Log.v(TAG, "showNotification:" + text);
// Set the info for the views that show in the notification panel.
final Notification notification = new Notification.Builder(this)
String channelId = "";
// If earlier version channel ID is not used
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
if (BuildCheck.isAndroid8()) {
channelId = createChannel();
}
// Set the info for the views that show in the notification panel.
final Notification notification = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher) // the status icon
.setTicker(text) // the status text
.setWhen(System.currentTimeMillis()) // the time stamp
Expand All @@ -132,7 +154,7 @@ private void showNotification(final CharSequence text) {
startForeground(NOTIFICATION, notification);
// Send the notification.
mNotificationManager.notify(NOTIFICATION, notification);
}
}

private final OnDeviceConnectListener mOnDeviceConnectListener = new OnDeviceConnectListener() {
@Override
Expand Down
2 changes: 1 addition & 1 deletion usbCameraTest5/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/Theme.AppCompat" >
<activity
android:name="com.serenegiant.usbcameratest5.MainActivity"
android:label="@string/app_name" >
Expand Down
2 changes: 1 addition & 1 deletion usbCameraTest6/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/Theme.AppCompat" >
<activity
android:name="com.serenegiant.usbcameratest6.MainActivity"
android:label="@string/app_name"
Expand Down
2 changes: 1 addition & 1 deletion usbCameraTest7/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/Theme.AppCompat" >
<activity
android:name="com.serenegiant.usbcameratest7.MainActivity"
android:label="@string/app_name"
Expand Down
4 changes: 2 additions & 2 deletions usbCameraTest8/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/Theme.AppCompat">
<activity
android:name=".MainActivity"
android:screenOrientation="sensorLandscape"
Expand All @@ -47,4 +47,4 @@
</activity>
</application>

</manifest>
</manifest>