Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create icon from memory #20

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.4.0

* **New Feature** Generate icon from JPEG images in memory (Uint8List) `ShortcutIconAsset.memoryAsset`

## v1.3.0

* **Breaking Change** renamed `FlutterShortcutsItem` to `ShortcutItem`
Expand Down
70 changes: 44 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

Flutter Shortcuts Plugin is known for :

| Flutter Shortcuts |
| :--------------------------------- |
| Flutter Shortcuts |
| :---------------------------- |
| Fast, performant & compatible |
| Free & Open-source |
| Production ready |
| Make App Reactive |
| Free & Open-source |
| Production ready |
| Make App Reactive |

## Features

Expand All @@ -34,8 +34,8 @@ All the features listed below can be performed at the runtime.

## Demo

|<img height=500 src="https://i.imgur.com/UPcyPEl.gif"/>|
|---|
| <img height=500 src="https://i.imgur.com/UPcyPEl.gif"/> |
| ------------------------------------------------------- |

## Quick Start

Expand Down Expand Up @@ -88,31 +88,49 @@ Flutter Shortcuts allows you to create shortcut icon from both android `drawable

* If you want to use icon from Android resources, `drawable` or `mipmap`.

use: `ShortcutIconAsset.androidAsset`
use: `ShortcutIconAsset.androidAsset`

```dart
ShortcutItem(
id: "2",
action: 'Bookmark page action',
shortLabel: 'Bookmark Page',
icon: "ic_launcher",
shortcutIconAsset: ShortcutIconAsset.androidAsset,
),
```dart
ShortcutItem(
id: "2",
action: 'Bookmark page action',
shortLabel: 'Bookmark Page',
icon: "ic_launcher",
shortcutIconAsset: ShortcutIconAsset.androidAsset,
),
```

* If you want to create shortcut icon from flutter asset. (DEFAULT)

use: `ShortcutIconAsset.flutterAsset`
use: `ShortcutIconAsset.flutterAsset`
sadespresso marked this conversation as resolved.
Show resolved Hide resolved

```dart
ShortcutItem(
id: "2",
action: 'Bookmark page action',
shortLabel: 'Bookmark Page',
icon: 'assets/icons/bookmark.png',
shortcutIconAsset: ShortcutIconAsset.flutterAsset,
),
```
```dart
ShortcutItem(
id: "2",
action: 'Bookmark page action',
shortLabel: 'Bookmark Page',
icon: 'assets/icons/bookmark.png',
shortcutIconAsset: ShortcutIconAsset.flutterAsset,
),
```

* If you want to create shortcut icon from memory (`Uint8List`).

**This only works with JPEG images**!

use: `ShortcutIconAsset.memoryAsset`

```dart
Unit8List jpegData = ...; // JPEG image in memory

ShortcutItem(
id: "2",
action: 'Bookmark page action',
shortLabel: 'Bookmark Page',
icon: ShortcutMemoryIcon(jpegData).toList(),
shortcutIconAsset: ShortcutIconAsset.flutterAsset,
),
```

### Set shortcut items

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Base64;

import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.loader.FlutterLoader;
Expand Down Expand Up @@ -394,6 +395,9 @@ private void setIconCompat(int iconType,String icon,ShortcutInfoCompat.Builder s
case 1:
setIconFromFlutterCompat(shortcutBuilderCompat, icon);
break;
case 2:
setIconFromBase64StringCompat(shortcutBuilderCompat, icon);
break;
default:
break;
}
Expand All @@ -414,17 +418,27 @@ private void setIconFromFlutterCompat(ShortcutInfoCompat.Builder shortcutBuilder
}
}

private void setIconFromBase64StringCompat(ShortcutInfoCompat.Builder shortcutBuilder, String icon) {
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
shortcutBuilder.setIcon(IconCompat.createFromIcon(context, getIconFromMemoryAsset(context, icon)));
}
}

/* *********************** Person ******************* */
private void setIconCompat(int iconType,String icon,Person.Builder personBuilderCompat) {
// 0 - ShortcutIconType.androidAsset
// 1 - ShortcutIconType.flutterAsset
// 2 - ShortcutIconType.memoryAsset
switch (iconType) {
case 0:
setIconFromNativeCompat(personBuilderCompat, icon);
break;
case 1:
setIconFromFlutterCompat(personBuilderCompat, icon);
break;
case 2:
setIconFromBase64StringCompat(personBuilderCompat, icon);
break;
default:
break;
}
Expand All @@ -445,6 +459,12 @@ private void setIconFromFlutterCompat(Person.Builder personBuilder, String icon)
}
}

private void setIconFromBase64StringCompat(Person.Builder personBuilder, String icon) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
personBuilder.setIcon(IconCompat.createFromIcon(context,getIconFromMemoryAsset(context,icon)));
}
}

@RequiresApi(api = Build.VERSION_CODES.O)
private Icon getIconFromFlutterAsset(Context context, String path) {
AssetManager assetManager = context.getAssets();
Expand All @@ -466,6 +486,19 @@ private Icon getIconFromFlutterAsset(Context context, String path) {
return Icon.createWithAdaptiveBitmap(image);
}

@RequiresApi(api = Build.VERSION_CODES.O)
private Icon getIconFromMemoryAsset(Context context, String base64Jpeg) {
try {
byte[] bytes = Base64.getDecoder().decode(base64Jpeg);
Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

return Icon.createWithAdaptiveBitmap(image);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

private int loadResourceId(Context context, String icon) {
if (icon == null) {
return 0;
Expand Down
Loading