Skip to content

Commit

Permalink
target sdk 34
Browse files Browse the repository at this point in the history
Pending intent and broadcast receiver changed according to sdk 34 release notes.
Permisssion dialog now shown while fragment is paused.
  • Loading branch information
kai-morich committed Feb 18, 2024
1 parent 83646d6 commit 26a2f93
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions usbSerialExamples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

android {
compileSdkVersion 33
compileSdkVersion 34

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -12,7 +12,7 @@ android {

defaultConfig {
minSdkVersion 17
targetSdkVersion 33
targetSdkVersion 34
vectorDrawables.useSupportLibrary = true

missingDimensionStrategy 'device', 'anyDevice'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
Expand Down Expand Up @@ -89,12 +90,22 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
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
public void onResume() {
super.onResume();
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(INTENT_ACTION_GRANT_USB));

if(usbPermission == UsbPermission.Unknown || usbPermission == UsbPermission.Granted)
if(!connected && (usbPermission == UsbPermission.Unknown || usbPermission == UsbPermission.Granted))
mainLooper.post(this::connect);
}

Expand All @@ -104,7 +115,6 @@ public void onPause() {
status("disconnected");
disconnect();
}
getActivity().unregisterReceiver(broadcastReceiver);
super.onPause();
}

Expand Down Expand Up @@ -213,7 +223,9 @@ private void connect() {
if(usbConnection == null && usbPermission == UsbPermission.Unknown && !usbManager.hasPermission(driver.getDevice())) {
usbPermission = UsbPermission.Requested;
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);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions usbSerialForAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ plugins {
}

android {
compileSdkVersion 33
compileSdkVersion 34

defaultConfig {
minSdkVersion 17
targetSdkVersion 33
targetSdkVersion 34
consumerProguardFiles 'proguard-rules.pro'

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ public void onReceive(Context context, Intent intent) {
}
};
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");
context.registerReceiver(usbReceiver, filter);
context.registerReceiver(usbReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
usbManager.requestPermission(serialDriver.getDevice(), permissionIntent);
for(int i=0; i<5000; i++) {
if(granted[0] != null) break;
Expand Down

0 comments on commit 26a2f93

Please sign in to comment.