mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-10-31 02:17:23 +00:00 
			
		
		
		
	target sdk 34
Pending intent and broadcast receiver changed according to sdk 34 release notes. Permisssion dialog now shown while fragment is paused.
This commit is contained in:
		
							parent
							
								
									83646d6955
								
							
						
					
					
						commit
						26a2f9363e
					
				| @ -3,7 +3,7 @@ plugins { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| android { | android { | ||||||
|     compileSdkVersion 33 |     compileSdkVersion 34 | ||||||
| 
 | 
 | ||||||
|     compileOptions { |     compileOptions { | ||||||
|         sourceCompatibility JavaVersion.VERSION_1_8 |         sourceCompatibility JavaVersion.VERSION_1_8 | ||||||
| @ -12,7 +12,7 @@ android { | |||||||
| 
 | 
 | ||||||
|     defaultConfig { |     defaultConfig { | ||||||
|         minSdkVersion 17 |         minSdkVersion 17 | ||||||
|         targetSdkVersion 33 |         targetSdkVersion 34 | ||||||
|         vectorDrawables.useSupportLibrary = true |         vectorDrawables.useSupportLibrary = true | ||||||
| 
 | 
 | ||||||
|         missingDimensionStrategy 'device', 'anyDevice' |         missingDimensionStrategy 'device', 'anyDevice' | ||||||
|  | |||||||
| @ -28,6 +28,7 @@ import android.widget.ToggleButton; | |||||||
| 
 | 
 | ||||||
| import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||||
| import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||||
|  | import androidx.core.content.ContextCompat; | ||||||
| import androidx.fragment.app.Fragment; | import androidx.fragment.app.Fragment; | ||||||
| 
 | 
 | ||||||
| import com.hoho.android.usbserial.driver.UsbSerialDriver; | import com.hoho.android.usbserial.driver.UsbSerialDriver; | ||||||
| @ -89,12 +90,22 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag | |||||||
|         withIoManager = getArguments().getBoolean("withIoManager"); |         withIoManager = getArguments().getBoolean("withIoManager"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @Override | ||||||
|  |     public void onStart() { | ||||||
|  |         super.onStart(); | ||||||
|  |         ContextCompat.registerReceiver(getActivity(), broadcastReceiver, new IntentFilter(INTENT_ACTION_GRANT_USB), ContextCompat.RECEIVER_NOT_EXPORTED); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void onStop() { | ||||||
|  |         getActivity().unregisterReceiver(broadcastReceiver); | ||||||
|  |         super.onStop(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void onResume() { |     public void onResume() { | ||||||
|         super.onResume(); |         super.onResume(); | ||||||
|         getActivity().registerReceiver(broadcastReceiver, new IntentFilter(INTENT_ACTION_GRANT_USB)); |         if(!connected && (usbPermission == UsbPermission.Unknown || usbPermission == UsbPermission.Granted)) | ||||||
| 
 |  | ||||||
|         if(usbPermission == UsbPermission.Unknown || usbPermission == UsbPermission.Granted) |  | ||||||
|             mainLooper.post(this::connect); |             mainLooper.post(this::connect); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -104,7 +115,6 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag | |||||||
|             status("disconnected"); |             status("disconnected"); | ||||||
|             disconnect(); |             disconnect(); | ||||||
|         } |         } | ||||||
|         getActivity().unregisterReceiver(broadcastReceiver); |  | ||||||
|         super.onPause(); |         super.onPause(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -213,7 +223,9 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag | |||||||
|         if(usbConnection == null && usbPermission == UsbPermission.Unknown && !usbManager.hasPermission(driver.getDevice())) { |         if(usbConnection == null && usbPermission == UsbPermission.Unknown && !usbManager.hasPermission(driver.getDevice())) { | ||||||
|             usbPermission = UsbPermission.Requested; |             usbPermission = UsbPermission.Requested; | ||||||
|             int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_MUTABLE : 0; |             int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_MUTABLE : 0; | ||||||
|             PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent(INTENT_ACTION_GRANT_USB), flags); |             Intent intent = new Intent(INTENT_ACTION_GRANT_USB); | ||||||
|  |             intent.setPackage(getActivity().getPackageName()); | ||||||
|  |             PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, flags); | ||||||
|             usbManager.requestPermission(driver.getDevice(), usbPermissionIntent); |             usbManager.requestPermission(driver.getDevice(), usbPermissionIntent); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -4,11 +4,11 @@ plugins { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| android { | android { | ||||||
|     compileSdkVersion 33 |     compileSdkVersion 34 | ||||||
| 
 | 
 | ||||||
|     defaultConfig { |     defaultConfig { | ||||||
|         minSdkVersion 17 |         minSdkVersion 17 | ||||||
|         targetSdkVersion 33 |         targetSdkVersion 34 | ||||||
|         consumerProguardFiles 'proguard-rules.pro' |         consumerProguardFiles 'proguard-rules.pro' | ||||||
|          |          | ||||||
|         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" |         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||||||
|  | |||||||
| @ -86,9 +86,11 @@ public class UsbWrapper implements SerialInputOutputManager.Listener { | |||||||
|                 } |                 } | ||||||
|             }; |             }; | ||||||
|             int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_MUTABLE : 0; |             int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_MUTABLE : 0; | ||||||
|             PendingIntent permissionIntent = PendingIntent.getBroadcast(context, 0, new Intent("com.android.example.USB_PERMISSION"), flags); |             Intent intent = new Intent("com.android.example.USB_PERMISSION"); | ||||||
|  |             intent.setPackage(context.getPackageName()); | ||||||
|  |             PendingIntent permissionIntent = PendingIntent.getBroadcast(context, 0, intent, flags); | ||||||
|             IntentFilter filter = new IntentFilter("com.android.example.USB_PERMISSION"); |             IntentFilter filter = new IntentFilter("com.android.example.USB_PERMISSION"); | ||||||
|             context.registerReceiver(usbReceiver, filter); |             context.registerReceiver(usbReceiver, filter, Context.RECEIVER_NOT_EXPORTED); | ||||||
|             usbManager.requestPermission(serialDriver.getDevice(), permissionIntent); |             usbManager.requestPermission(serialDriver.getDevice(), permissionIntent); | ||||||
|             for(int i=0; i<5000; i++) { |             for(int i=0; i<5000; i++) { | ||||||
|                 if(granted[0] != null) break; |                 if(granted[0] != null) break; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user