Skip to content

Commit 157f042

Browse files
authored
Merge pull request #28 from qliqdev/get_name_n_reset
getName() and reset() added.
2 parents 4317675 + c1014ca commit 157f042

File tree

5 files changed

+136
-40
lines changed

5 files changed

+136
-40
lines changed

README.md

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,73 +150,103 @@ const changeIcon = async (iconName) => {
150150

151151
## API
152152

153+
<docgen-index>
154+
155+
* [`isSupported()`](#issupported)
156+
* [`getName()`](#getname)
157+
* [`change(...)`](#change)
158+
* [`reset(...)`](#reset)
159+
* [Interfaces](#interfaces)
160+
161+
</docgen-index>
162+
163+
<docgen-api>
164+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
165+
153166
### isSupported()
154167

155168
```typescript
156-
isSupported() => Promise<{value: boolean}>
169+
isSupported() => Promise<{ value: boolean; }>
157170
```
158171

159-
Checks to see if using alternate icons is supported on your device.
172+
Checks if changing the app icon is supported. (iOS only)
173+
174+
**Returns:** <code>Promise&lt;{ value: boolean; }&gt;</code>
175+
176+
**Since:** 1.0.0
177+
178+
--------------------
160179

161-
---
162180

163181
### getName()
164182

165183
```typescript
166-
getName(): Promise<{value: string | null}>;
184+
getName() => Promise<{ value: string | null; }>
167185
```
168186

169187
Gets the name of currently set alternate icon. If original icon is set, returns null.
170188

171-
---
189+
**Returns:** <code>Promise&lt;{ value: string | null; }&gt;</code>
190+
191+
**Since:** 1.0.0
192+
193+
--------------------
194+
172195

173196
### change(...)
174197

175198
```typescript
176-
change(options: IconOptions): Promise<void>;
199+
change(options: IconOptions) => Promise<void>
177200
```
178201

179202
Changes app icon to specified alternate.
180203

181204
| Param | Type |
182205
| ------------- | --------------------------------------------------- |
183-
| **`options`** | <code><a href="#IconOptions">IconOptions</a></code> |
206+
| **`options`** | <code><a href="#iconoptions">IconOptions</a></code> |
207+
208+
**Since:** 1.0.0
209+
210+
--------------------
184211

185-
---
186212

187213
### reset(...)
188214

189215
```typescript
190-
reset(options: ResetOptions): Promise<void>;
216+
reset(options: ResetOptions) => Promise<void>
191217
```
192218

193-
Changes app icon to specified alternate.
219+
Reverts app icon to original.
194220

195-
| Param | Type |
196-
| ------------- | --------------------------------------------------- |
197-
| **`options`** | <code><a href="#IconOptions">ResetOptions</a></code> |
221+
| Param | Type |
222+
| ------------- | ----------------------------------------------------- |
223+
| **`options`** | <code><a href="#resetoptions">ResetOptions</a></code> |
224+
225+
**Since:** 1.0.0
226+
227+
--------------------
198228

199-
---
200229

201230
### Interfaces
202231

203232

204233
#### IconOptions
205234

206-
Represents the options passed to `change`.
235+
| Prop | Type | Description | Since |
236+
| -------------------------- | --------------------- | --------------------------------------------------------------------------------- | ----- |
237+
| **`name`** | <code>string</code> | Name of alternate icon to set | |
238+
| **`disable`** | <code>string[]</code> | Name of icons to disable. This is not used for iOS, but required for Android. | 3.1.0 |
239+
| **`suppressNotification`** | <code>boolean</code> | Flag controlling the in app notification which shows after icon is changed. (iOS) | |
207240

208-
| Prop | Type | Description | Since |
209-
| ----------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
210-
| **`name`** | <code>string</code> | Name of alternate icon to set. | 1.0.0 |
211-
| **`suppressNotification`** | <code>boolean</code> | Flag controlling the in app notification which shows after icon is changed. | 1.0.0 |
212241

213242
#### ResetOptions
214243

215-
Represents the options passed to `reset`.
244+
| Prop | Type | Description | Since |
245+
| -------------------------- | --------------------- | --------------------------------------------------------------------------------- | ----- |
246+
| **`suppressNotification`** | <code>boolean</code> | Flag controlling the in app notification which shows after icon is changed (iOS). | |
247+
| **`disable`** | <code>string[]</code> | Name of icons to disable. This is not used for iOS, but required for Android. | 3.1.1 |
216248

217-
| Prop | Type | Description | Since |
218-
| ----------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
219-
| **`suppressNotification`** | <code>boolean</code> | Flag controlling the in app notification which shows after icon is changed. | 1.0.0 |
249+
</docgen-api>
220250

221251
## Contributors ✨
222252

android/src/main/java/com/mycompany/plugins/example/AppIconBase.java

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.mycompany.plugins.example;
22

33
import com.getcapacitor.JSArray;
4+
45
import org.json.JSONException;
56

67
import java.util.ArrayList;
78
import java.util.List;
9+
import java.util.Objects;
810

911
import android.app.Activity;
1012
import android.content.Context;
@@ -27,30 +29,69 @@ public AppIconBase(Activity activity, Context context) {
2729
pm = context.getApplicationContext().getPackageManager();
2830
activeIconName = "";
2931
}
30-
31-
public void changeIcon(String enableName, JSArray disableNames) {
32-
33-
int action;
34-
try{
32+
33+
public String getName() {
34+
ComponentName componentName = new ComponentName(this.activity, this.activity.getClass());
35+
int status = pm.getComponentEnabledSetting(componentName);
36+
if (status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
37+
// The component is currently enabled
38+
String name = componentName.getShortClassName();
39+
if (Objects.equals(name, ".MainActivity")) {
40+
return null;
41+
}
42+
return name.substring(1);
43+
} else {
44+
// The component is currently disabled
45+
return null;
46+
}
47+
}
48+
49+
public void change(String enableName, JSArray disableNames) {
50+
try {
3551
List<String> newList = disableNames.toList();
3652

3753
pm.setComponentEnabledSetting(
38-
new ComponentName(this.packageName, this.packageName + "." + enableName),
39-
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
54+
new ComponentName(this.packageName, this.packageName + "." + enableName),
55+
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
4056
);
41-
42-
for(String value : newList) {
57+
58+
for (String value : newList) {
4359
Log.i("AppIconBase", this.packageName + "." + value);
4460
pm.setComponentEnabledSetting(
45-
new ComponentName(this.packageName, this.packageName + "." + value),
46-
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
61+
new ComponentName(this.packageName, this.packageName + "." + value),
62+
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
4763
);
4864
}
49-
}
50-
catch(JSONException ignore){
65+
66+
// Always disable main app icon
67+
pm.setComponentEnabledSetting(
68+
new ComponentName(this.packageName, this.packageName + ".MainActivity"),
69+
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
70+
);
71+
} catch (JSONException ignore) {
5172
// do nothing
5273
}
5374

5475
}
5576

77+
public void reset(JSArray disableNames) {
78+
try {
79+
List<String> newList = disableNames.toList();
80+
// Reset the icon to the default icon
81+
pm.setComponentEnabledSetting(
82+
new ComponentName(packageName, packageName + ".MainActivity"),
83+
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
84+
);
85+
for (String value : newList) {
86+
Log.i("AppIconBaseReset", this.packageName + "." + value);
87+
pm.setComponentEnabledSetting(
88+
new ComponentName(this.packageName, this.packageName + "." + value),
89+
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
90+
);
91+
}
92+
} catch (JSONException ignore) {
93+
// do nothing
94+
}
95+
}
96+
5697
}

android/src/main/java/com/mycompany/plugins/example/AppIconPlugin.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ public void load() {
1616
implementation = new AppIconBase(this.getActivity(), this.getContext());
1717
}
1818

19-
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
19+
@PluginMethod()
20+
public void getName(PluginCall call) {
21+
JSObject r = new JSObject();
22+
r.put("value", implementation.getName());
23+
call.resolve(r);
24+
}
25+
26+
@PluginMethod()
2027
public void change(PluginCall call) {
2128
if (!call.getData().has("name")) {
2229
call.reject("Must provide an icon name");
@@ -27,7 +34,19 @@ public void change(PluginCall call) {
2734
return;
2835
}
2936

30-
implementation.changeIcon(call.getString("name", null), call.getArray("disable", null));
37+
implementation.change(call.getString("name", null), call.getArray("disable", null));
3138
call.resolve();
3239
}
40+
41+
@PluginMethod()
42+
public void reset(PluginCall call) {
43+
if (!call.getData().has("disable")) {
44+
call.reject("Must provide an array of icon names to disable");
45+
return;
46+
}
47+
48+
implementation.reset(call.getArray("disable", null));
49+
call.resolve();
50+
}
51+
3352
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@capacitor-community/app-icon",
3-
"version": "3.1.0-beta.0",
3+
"version": "3.1.0-beta.1",
44
"description": "Capacitor community plugin for changing an iOS app icon.",
55
"main": "dist/plugin.cjs.js",
66
"module": "dist/esm/index.js",

src/definitions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ export interface ResetOptions {
1818
/**
1919
* Flag controlling the in app notification which shows after icon is changed (iOS).
2020
*/
21-
suppressNotification: boolean
21+
suppressNotification: boolean;
22+
23+
/**
24+
* Name of icons to disable. This is not used for iOS, but required for Android.
25+
* @since 3.1.1
26+
*/
27+
disable?: string[];
2228
}
2329

2430
export interface AppIconPlugin {

0 commit comments

Comments
 (0)