Skip to content

Commit

Permalink
Merge pull request #28 from qliqdev/get_name_n_reset
Browse files Browse the repository at this point in the history
getName() and reset() added.
  • Loading branch information
johnborges authored Apr 3, 2023
2 parents 4317675 + c1014ca commit 157f042
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 40 deletions.
76 changes: 53 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,73 +150,103 @@ const changeIcon = async (iconName) => {

## API

<docgen-index>

* [`isSupported()`](#issupported)
* [`getName()`](#getname)
* [`change(...)`](#change)
* [`reset(...)`](#reset)
* [Interfaces](#interfaces)

</docgen-index>

<docgen-api>
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->

### 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:** <code>Promise&lt;{ value: boolean; }&gt;</code>

**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:** <code>Promise&lt;{ value: string | null; }&gt;</code>

**Since:** 1.0.0

--------------------


### change(...)

```typescript
change(options: IconOptions): Promise<void>;
change(options: IconOptions) => Promise<void>
```

Changes app icon to specified alternate.

| Param | Type |
| ------------- | --------------------------------------------------- |
| **`options`** | <code><a href="#IconOptions">IconOptions</a></code> |
| **`options`** | <code><a href="#iconoptions">IconOptions</a></code> |

**Since:** 1.0.0

--------------------

---

### reset(...)

```typescript
reset(options: ResetOptions): Promise<void>;
reset(options: ResetOptions) => Promise<void>
```

Changes app icon to specified alternate.
Reverts app icon to original.

| Param | Type |
| ------------- | --------------------------------------------------- |
| **`options`** | <code><a href="#IconOptions">ResetOptions</a></code> |
| Param | Type |
| ------------- | ----------------------------------------------------- |
| **`options`** | <code><a href="#resetoptions">ResetOptions</a></code> |

**Since:** 1.0.0

--------------------

---

### Interfaces


#### IconOptions

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

| Prop | Type | Description | Since |
| ----------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
| **`name`** | <code>string</code> | Name of alternate icon to set. | 1.0.0 |
| **`suppressNotification`** | <code>boolean</code> | 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`** | <code>boolean</code> | Flag controlling the in app notification which shows after icon is changed (iOS). | |
| **`disable`** | <code>string[]</code> | Name of icons to disable. This is not used for iOS, but required for Android. | 3.1.1 |

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

## Contributors ✨

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,30 +29,69 @@ 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 name.substring(1);
} else {
// The component is currently disabled
return null;
}
}

public void change(String enableName, JSArray disableNames) {
try {
List<String> 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){

// 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
}

}

public void reset(JSArray disableNames) {
try {
List<String> 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
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
}

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 7 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 157f042

Please sign in to comment.