forked from trent_larson/crowd-funder-for-time-pwa
fix: update Android share intent handling for API 33+ compatibility
Fix deprecation warnings and ensure future compatibility by updating
getParcelableExtra() calls to use the new API introduced in Android 13
(API 33), while maintaining backward compatibility with older versions.
Changes:
- Update getParcelableExtra() to use typed version (Uri.class) on API 33+
- Update getParcelableArrayListExtra() to use typed version on API 33+
- Add version checks with Build.VERSION.SDK_INT >= TIRAMISU
- Maintain backward compatibility for API 22-32 using deprecated API
with @SuppressWarnings("deprecation")
- No functional changes - share feature works identically across versions
The new API (getParcelableExtra(key, Class)) was introduced in API 33
to provide type safety. The old API is deprecated but still functional
on older versions, so we use runtime checks to support both.
This ensures the app builds cleanly with targetSdkVersion 36 and remains
compatible with minSdkVersion 22.
This commit is contained in:
@@ -90,7 +90,16 @@ public class MainActivity extends BridgeActivity {
|
|||||||
|
|
||||||
// Handle single image share
|
// Handle single image share
|
||||||
if (Intent.ACTION_SEND.equals(action) && type != null && type.startsWith("image/")) {
|
if (Intent.ACTION_SEND.equals(action) && type != null && type.startsWith("image/")) {
|
||||||
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
Uri imageUri;
|
||||||
|
// Use new API for API 33+ (Android 13+), fall back to deprecated API for older versions
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri.class);
|
||||||
|
} else {
|
||||||
|
// Deprecated but still works on older versions
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
||||||
|
imageUri = uri;
|
||||||
|
}
|
||||||
if (imageUri != null) {
|
if (imageUri != null) {
|
||||||
String fileName = intent.getStringExtra(Intent.EXTRA_TEXT);
|
String fileName = intent.getStringExtra(Intent.EXTRA_TEXT);
|
||||||
processSharedImage(imageUri, fileName);
|
processSharedImage(imageUri, fileName);
|
||||||
@@ -98,7 +107,16 @@ public class MainActivity extends BridgeActivity {
|
|||||||
}
|
}
|
||||||
// Handle multiple images share (we'll just process the first one)
|
// Handle multiple images share (we'll just process the first one)
|
||||||
else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null && type.startsWith("image/")) {
|
else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null && type.startsWith("image/")) {
|
||||||
java.util.ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
|
java.util.ArrayList<Uri> imageUris;
|
||||||
|
// Use new API for API 33+ (Android 13+), fall back to deprecated API for older versions
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM, Uri.class);
|
||||||
|
} else {
|
||||||
|
// Deprecated but still works on older versions
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
java.util.ArrayList<Uri> uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
|
||||||
|
imageUris = uris;
|
||||||
|
}
|
||||||
if (imageUris != null && !imageUris.isEmpty()) {
|
if (imageUris != null && !imageUris.isEmpty()) {
|
||||||
processSharedImage(imageUris.get(0), null);
|
processSharedImage(imageUris.get(0), null);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user