|
101 | 101 | import android.graphics.Canvas; |
102 | 102 | import android.util.TypedValue; |
103 | 103 |
|
| 104 | +import android.provider.MediaStore; |
| 105 | +import android.graphics.Bitmap; |
| 106 | +import android.os.Build; |
| 107 | +import android.graphics.ImageDecoder; |
| 108 | + |
104 | 109 |
|
105 | 110 |
|
106 | 111 | public class System extends CordovaPlugin { |
@@ -1095,31 +1100,55 @@ private void pinFileShortcut(JSONObject shortcutJson, CallbackContext callback) |
1095 | 1100 | } |
1096 | 1101 |
|
1097 | 1102 | if (!ShortcutManagerCompat.isRequestPinShortcutSupported(context)) { |
1098 | | - callback.error("Not supported"); |
| 1103 | + callback.error("Pin shortcut not supported on this launcher"); |
1099 | 1104 | return; |
1100 | 1105 | } |
1101 | 1106 |
|
1102 | 1107 | try { |
1103 | 1108 | Uri dataUri = Uri.parse(uriString); |
1104 | | - |
1105 | 1109 | String packageName = context.getPackageName(); |
1106 | | - Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName); |
| 1110 | + PackageManager pm = context.getPackageManager(); |
| 1111 | + |
| 1112 | + Intent launchIntent = pm.getLaunchIntentForPackage(packageName); |
| 1113 | + if (launchIntent == null) { |
| 1114 | + callback.error("Launch intent not found for package: " + packageName); |
| 1115 | + return; |
| 1116 | + } |
| 1117 | + |
1107 | 1118 | ComponentName componentName = launchIntent.getComponent(); |
| 1119 | + if (componentName == null) { |
| 1120 | + callback.error("ComponentName is null"); |
| 1121 | + return; |
| 1122 | + } |
1108 | 1123 |
|
1109 | 1124 | Intent intent = new Intent(Intent.ACTION_VIEW); |
1110 | 1125 | intent.setComponent(componentName); |
1111 | | - |
1112 | 1126 | intent.setData(dataUri); |
1113 | 1127 | intent.putExtra("acodeFileUri", uriString); |
1114 | 1128 |
|
1115 | 1129 | IconCompat icon; |
| 1130 | + |
1116 | 1131 | if (iconSrc != null && !iconSrc.isEmpty()) { |
1117 | | - ImageDecoder.Source imgSrc = ImageDecoder.createSource( |
1118 | | - context.getContentResolver(), |
1119 | | - Uri.parse(iconSrc) |
1120 | | - ); |
1121 | | - Bitmap bitmap = ImageDecoder.decodeBitmap(imgSrc); |
1122 | | - icon = IconCompat.createWithBitmap(bitmap); |
| 1132 | + try { |
| 1133 | + Uri iconUri = Uri.parse(iconSrc); |
| 1134 | + Bitmap bitmap; |
| 1135 | + |
| 1136 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { |
| 1137 | + ImageDecoder.Source imgSrc = |
| 1138 | + ImageDecoder.createSource(context.getContentResolver(), iconUri); |
| 1139 | + bitmap = ImageDecoder.decodeBitmap(imgSrc); |
| 1140 | + } else { |
| 1141 | + bitmap = MediaStore.Images.Media.getBitmap( |
| 1142 | + context.getContentResolver(), |
| 1143 | + iconUri |
| 1144 | + ); |
| 1145 | + } |
| 1146 | + |
| 1147 | + icon = IconCompat.createWithBitmap(bitmap); |
| 1148 | + |
| 1149 | + } catch (Exception e) { |
| 1150 | + icon = getFileShortcutIcon(label); |
| 1151 | + } |
1123 | 1152 | } else { |
1124 | 1153 | icon = getFileShortcutIcon(label); |
1125 | 1154 | } |
|
0 commit comments