Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/empty-spiders-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capawesome/capacitor-app-shortcuts': minor
---

feat(android): support base64 string for `androidIcon`
16 changes: 8 additions & 8 deletions packages/app-shortcuts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,14 @@ Remove all listeners for this plugin.

#### Shortcut

| Prop | Type | Description | Since |
| ----------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
| **`description`** | <code>string</code> | The description. On **Android**, the launcher shows this instead of the short title when it has enough space. **Attention**: On **iOS**, the icon and the description must be used together. | 6.0.0 |
| **`id`** | <code>string</code> | The unique identifier. | 6.0.0 |
| **`title`** | <code>string</code> | The display name. | 6.0.0 |
| **`icon`** | <code>string \| number</code> | The icon to display. On **Android**, the icon is the constant integer value of the [R.drawable](https://developer.android.com/reference/android/R.drawable) enum (e.g. `17301547`). On **iOS**, the icon can be one of the following: - The constant integer value of the [UIApplicationShortcutIcon.IconType](https://developer.apple.com/documentation/uikit/uiapplicationshortcuticon/icontype) enum (e.g. `6`). - A system symbol name (e.g. `star.fill`). - Name of the image asset from the asset catalogue. | 6.1.0 |
| **`androidIcon`** | <code>string \| number</code> | The icon to display on Android. The icon is the constant name or the integer value of the [R.drawable](https://developer.android.com/reference/android/R.drawable) enum (e.g. `17301547`, `"alert_dark_frame"`). | 7.2.0 |
| **`iosIcon`** | <code>string \| number</code> | The icon to display on iOS. The icon can be one of the following: - The constant integer value of the [UIApplicationShortcutIcon.IconType](https://developer.apple.com/documentation/uikit/uiapplicationshortcuticon/icontype) enum (e.g. `6`). - A system symbol name (e.g. `star.fill`). - Name of the image asset from the asset catalogue. | 7.2.0 |
| Prop | Type | Description | Since |
| ----------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
| **`description`** | <code>string</code> | The description. On **Android**, the launcher shows this instead of the short title when it has enough space. **Attention**: On **iOS**, the icon and the description must be used together. | 6.0.0 |
| **`id`** | <code>string</code> | The unique identifier. | 6.0.0 |
| **`title`** | <code>string</code> | The display name. | 6.0.0 |
| **`icon`** | <code>string \| number</code> | The icon to display. On **Android**, the icon can be one of the following: - An integer value of the [R.drawable](https://developer.android.com/reference/android/R.drawable) enum (e.g. `17301547`). - A string that represents the name of the drawable resource (e.g. `"alert_dark_frame"`). - A base64 encoded image string (e.g. `"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QC..."`). On **iOS**, the icon can be one of the following: - The constant integer value of the [UIApplicationShortcutIcon.IconType](https://developer.apple.com/documentation/uikit/uiapplicationshortcuticon/icontype) enum (e.g. `6`). - A system symbol name (e.g. `star.fill`). - Name of the image asset from the asset catalogue. | 6.1.0 |
| **`androidIcon`** | <code>string \| number</code> | The icon to display on Android. The icon can be one of the following: - An integer value of the [R.drawable](https://developer.android.com/reference/android/R.drawable) enum (e.g. `17301547`). - A string that represents the name of the drawable resource (e.g. `"alert_dark_frame"`). - A base64 encoded image string (e.g. `"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QC..."`). | 7.2.0 |
| **`iosIcon`** | <code>string \| number</code> | The icon to display on iOS. The icon can be one of the following: - The constant integer value of the [UIApplicationShortcutIcon.IconType](https://developer.apple.com/documentation/uikit/uiapplicationshortcuticon/icontype) enum (e.g. `6`). - A system symbol name (e.g. `star.fill`). - Name of the image asset from the asset catalogue. | 7.2.0 |


#### SetOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.graphics.drawable.IconCompat;
import com.getcapacitor.Bridge;
Expand Down Expand Up @@ -66,8 +70,14 @@ public static List<ShortcutInfoCompat> createShortcutInfoCompatList(JSArray shor
// If not found in app resources, try system resources
iconResId = context.getResources().getIdentifier((String) androidIcon, "drawable", "android");
}

shortcutInfoCompat.setIcon(IconCompat.createWithResource(context, iconResId));
if (iconResId != 0) {
shortcutInfoCompat.setIcon(IconCompat.createWithResource(context, iconResId));
} else {
Bitmap bitmap = AppShortcutsHelper.decodeBase64((String) androidIcon);
if (bitmap != null) {
shortcutInfoCompat.setIcon(IconCompat.createWithBitmap(bitmap));
}
}
} catch (Exception exception) {
shortcutInfoCompat.setIcon(IconCompat.createWithResource(context, (int) androidIcon));
}
Expand All @@ -79,4 +89,11 @@ public static List<ShortcutInfoCompat> createShortcutInfoCompatList(JSArray shor
}
return shortcutInfoCompatList;
}

@Nullable
private static Bitmap decodeBase64(@NonNull String base64) {
base64 = base64.replaceFirst("data:[a-zA-Z+/]+;base64,", "");
byte[] bytes = Base64.decode(base64, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
}
11 changes: 9 additions & 2 deletions packages/app-shortcuts/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ export interface Shortcut {
/**
* The icon to display.
*
* On **Android**, the icon is the constant integer value of the [R.drawable](https://developer.android.com/reference/android/R.drawable) enum (e.g. `17301547`).
* On **Android**, the icon can be one of the following:
* - An integer value of the [R.drawable](https://developer.android.com/reference/android/R.drawable) enum (e.g. `17301547`).
* - A string that represents the name of the drawable resource (e.g. `"alert_dark_frame"`).
* - A base64 encoded image string (e.g. `"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QC..."`).
*
* On **iOS**, the icon can be one of the following:
* - The constant integer value of the [UIApplicationShortcutIcon.IconType](https://developer.apple.com/documentation/uikit/uiapplicationshortcuticon/icontype) enum (e.g. `6`).
Expand All @@ -132,11 +135,15 @@ export interface Shortcut {
/**
* The icon to display on Android.
*
* The icon is the constant name or the integer value of the [R.drawable](https://developer.android.com/reference/android/R.drawable) enum (e.g. `17301547`, `"alert_dark_frame"`).
* The icon can be one of the following:
* - An integer value of the [R.drawable](https://developer.android.com/reference/android/R.drawable) enum (e.g. `17301547`).
* - A string that represents the name of the drawable resource (e.g. `"alert_dark_frame"`).
* - A base64 encoded image string (e.g. `"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QC..."`).
*
* @since 7.2.0
* @example 17301547
* @example "alert_dark_frame"
* @example "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QC..."
*/
androidIcon?: number | string;
/**
Expand Down