From f10668c79e974157e4978fb49351bcdfba01df41 Mon Sep 17 00:00:00 2001 From: abbasfatullaev Date: Wed, 22 Mar 2023 02:43:07 +0600 Subject: [PATCH 1/4] getName() and reset() added. --- .../plugins/example/AppIconBase.java | 61 +++++++++++++++---- .../plugins/example/AppIconPlugin.java | 23 ++++++- package.json | 2 +- src/definitions.ts | 8 ++- 4 files changed, 77 insertions(+), 17 deletions(-) diff --git a/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java b/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java index 6ae6e41..508f81c 100644 --- a/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java +++ b/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java @@ -1,10 +1,12 @@ package com.mycompany.plugins.example; import com.getcapacitor.JSArray; + import org.json.JSONException; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import android.app.Activity; import android.content.Context; @@ -27,30 +29,63 @@ public AppIconBase(Activity activity, Context context) { pm = context.getApplicationContext().getPackageManager(); activeIconName = ""; } - - public void changeIcon(String enableName, JSArray disableNames) { - - int action; - try{ + + public String getName() { + ComponentName componentName = new ComponentName(this.activity, this.activity.getClass()); + int status = pm.getComponentEnabledSetting(componentName); + if (status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) { + // The component is currently enabled + String name = componentName.getShortClassName(); + if (Objects.equals(name, ".MainActivity")) { + return null; + } + return componentName.getShortClassName(); + } else { + // The component is currently disabled + return null; + } + } + + public void change(String enableName, JSArray disableNames) { + try { List newList = disableNames.toList(); pm.setComponentEnabledSetting( - new ComponentName(this.packageName, this.packageName + "." + enableName), - PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP + new ComponentName(this.packageName, this.packageName + "." + enableName), + PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP ); - - for(String value : newList) { + + for (String value : newList) { Log.i("AppIconBase", this.packageName + "." + value); pm.setComponentEnabledSetting( - new ComponentName(this.packageName, this.packageName + "." + value), - PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP + new ComponentName(this.packageName, this.packageName + "." + value), + PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP ); } - } - catch(JSONException ignore){ + } catch (JSONException ignore) { // do nothing } } + public void reset(JSArray disableNames) { + try { + List newList = disableNames.toList(); + // Reset the icon to the default icon + pm.setComponentEnabledSetting( + new ComponentName(packageName, packageName + ".MainActivity"), + PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP + ); + for (String value : newList) { + Log.i("AppIconBaseReset", this.packageName + "." + value); + pm.setComponentEnabledSetting( + new ComponentName(this.packageName, this.packageName + "." + value), + PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP + ); + } + } catch (JSONException ignore) { + // do nothing + } + } + } diff --git a/android/src/main/java/com/mycompany/plugins/example/AppIconPlugin.java b/android/src/main/java/com/mycompany/plugins/example/AppIconPlugin.java index 0670e95..3c5adb2 100644 --- a/android/src/main/java/com/mycompany/plugins/example/AppIconPlugin.java +++ b/android/src/main/java/com/mycompany/plugins/example/AppIconPlugin.java @@ -16,7 +16,14 @@ public void load() { implementation = new AppIconBase(this.getActivity(), this.getContext()); } - @PluginMethod(returnType = PluginMethod.RETURN_NONE) + @PluginMethod() + public void getName(PluginCall call) { + JSObject r = new JSObject(); + r.put("value", implementation.getName()); + call.resolve(r); + } + + @PluginMethod() public void change(PluginCall call) { if (!call.getData().has("name")) { call.reject("Must provide an icon name"); @@ -27,7 +34,19 @@ public void change(PluginCall call) { return; } - implementation.changeIcon(call.getString("name", null), call.getArray("disable", null)); + implementation.change(call.getString("name", null), call.getArray("disable", null)); call.resolve(); } + + @PluginMethod() + public void reset(PluginCall call) { + if (!call.getData().has("disable")) { + call.reject("Must provide an array of icon names to disable"); + return; + } + + implementation.reset(call.getArray("disable", null)); + call.resolve(); + } + } diff --git a/package.json b/package.json index 74d5a14..2520c74 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@capacitor-community/app-icon", - "version": "3.1.0-beta.0", + "version": "3.1.0-beta.1", "description": "Capacitor community plugin for changing an iOS app icon.", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", diff --git a/src/definitions.ts b/src/definitions.ts index 8bf1292..49f7742 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -18,7 +18,13 @@ export interface ResetOptions { /** * Flag controlling the in app notification which shows after icon is changed (iOS). */ - suppressNotification: boolean + suppressNotification: boolean; + + /** + * Name of icons to disable. This is not used for iOS, but required for Android. + * @since 3.1.1 + */ + disable?: string[]; } export interface AppIconPlugin { From d2b84148f07b19c26df4ebdc67878d9262d5aa37 Mon Sep 17 00:00:00 2001 From: abbasfatullaev Date: Wed, 22 Mar 2023 02:53:27 +0600 Subject: [PATCH 2/4] return name fixed --- .../main/java/com/mycompany/plugins/example/AppIconBase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java b/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java index 508f81c..dfa49be 100644 --- a/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java +++ b/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java @@ -39,7 +39,7 @@ public String getName() { if (Objects.equals(name, ".MainActivity")) { return null; } - return componentName.getShortClassName(); + return name.substring(1); } else { // The component is currently disabled return null; From b782b08455058e0a28cc277675a85009e9ebb1a0 Mon Sep 17 00:00:00 2001 From: abbasfatullaev Date: Thu, 30 Mar 2023 14:39:41 +0600 Subject: [PATCH 3/4] android fix on change icon; --- .../java/com/mycompany/plugins/example/AppIconBase.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java b/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java index dfa49be..902aa8f 100644 --- a/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java +++ b/android/src/main/java/com/mycompany/plugins/example/AppIconBase.java @@ -62,6 +62,12 @@ public void change(String enableName, JSArray disableNames) { PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP ); } + + // Always disable main app icon + pm.setComponentEnabledSetting( + new ComponentName(this.packageName, this.packageName + ".MainActivity"), + PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP + ); } catch (JSONException ignore) { // do nothing } From c1014ca2424bb76780a44c5602f331440eedddb9 Mon Sep 17 00:00:00 2001 From: abbasfatullaev Date: Mon, 3 Apr 2023 20:51:15 +0600 Subject: [PATCH 4/4] added docgen autogeneration api --- README.md | 76 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index c627c28..0c0d2a0 100644 --- a/README.md +++ b/README.md @@ -150,73 +150,103 @@ const changeIcon = async (iconName) => { ## API + + +* [`isSupported()`](#issupported) +* [`getName()`](#getname) +* [`change(...)`](#change) +* [`reset(...)`](#reset) +* [Interfaces](#interfaces) + + + + + + ### isSupported() ```typescript -isSupported() => Promise<{value: boolean}> +isSupported() => Promise<{ value: boolean; }> ``` -Checks to see if using alternate icons is supported on your device. +Checks if changing the app icon is supported. (iOS only) + +**Returns:** Promise<{ value: boolean; }> + +**Since:** 1.0.0 + +-------------------- ---- ### getName() ```typescript -getName(): Promise<{value: string | null}>; +getName() => Promise<{ value: string | null; }> ``` Gets the name of currently set alternate icon. If original icon is set, returns null. ---- +**Returns:** Promise<{ value: string | null; }> + +**Since:** 1.0.0 + +-------------------- + ### change(...) ```typescript -change(options: IconOptions): Promise; +change(options: IconOptions) => Promise ``` Changes app icon to specified alternate. | Param | Type | | ------------- | --------------------------------------------------- | -| **`options`** | IconOptions | +| **`options`** | IconOptions | + +**Since:** 1.0.0 + +-------------------- ---- ### reset(...) ```typescript -reset(options: ResetOptions): Promise; +reset(options: ResetOptions) => Promise ``` -Changes app icon to specified alternate. +Reverts app icon to original. -| Param | Type | -| ------------- | --------------------------------------------------- | -| **`options`** | ResetOptions | +| Param | Type | +| ------------- | ----------------------------------------------------- | +| **`options`** | ResetOptions | + +**Since:** 1.0.0 + +-------------------- ---- ### Interfaces #### IconOptions -Represents the options passed to `change`. +| Prop | Type | Description | Since | +| -------------------------- | --------------------- | --------------------------------------------------------------------------------- | ----- | +| **`name`** | string | Name of alternate icon to set | | +| **`disable`** | string[] | Name of icons to disable. This is not used for iOS, but required for Android. | 3.1.0 | +| **`suppressNotification`** | boolean | Flag controlling the in app notification which shows after icon is changed. (iOS) | | -| Prop | Type | Description | Since | -| ----------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ----- | -| **`name`** | string | Name of alternate icon to set. | 1.0.0 | -| **`suppressNotification`** | boolean | Flag controlling the in app notification which shows after icon is changed. | 1.0.0 | #### ResetOptions -Represents the options passed to `reset`. +| Prop | Type | Description | Since | +| -------------------------- | --------------------- | --------------------------------------------------------------------------------- | ----- | +| **`suppressNotification`** | boolean | Flag controlling the in app notification which shows after icon is changed (iOS). | | +| **`disable`** | string[] | Name of icons to disable. This is not used for iOS, but required for Android. | 3.1.1 | -| Prop | Type | Description | Since | -| ----------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ----- | -| **`suppressNotification`** | boolean | Flag controlling the in app notification which shows after icon is changed. | 1.0.0 | + ## Contributors ✨