@capawesome/capacitor-app-icon 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapawesomeCapacitorAppIcon.podspec +17 -0
- package/LICENSE +21 -0
- package/Package.swift +28 -0
- package/README.md +280 -0
- package/android/build.gradle +58 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/appicon/AppIcon.java +118 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/appicon/AppIconPlugin.java +94 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/appicon/classes/CustomException.java +20 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/appicon/classes/CustomExceptions.java +11 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/appicon/classes/options/SetIconOptions.java +29 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/appicon/classes/results/GetCurrentIconResult.java +25 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/appicon/classes/results/IsAvailableResult.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/appicon/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +174 -0
- package/dist/esm/definitions.d.ts +100 -0
- package/dist/esm/definitions.js +19 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +8 -0
- package/dist/esm/web.js +16 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +49 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +52 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/AppIcon.swift +40 -0
- package/ios/Plugin/AppIconPlugin.swift +77 -0
- package/ios/Plugin/Classes/Options/SetIconOptions.swift +17 -0
- package/ios/Plugin/Classes/Results/GetCurrentIconResult.swift +16 -0
- package/ios/Plugin/Classes/Results/IsAvailableResult.swift +16 -0
- package/ios/Plugin/Enums/CustomError.swift +31 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/Protocols/Result.swift +5 -0
- package/package.json +91 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'CapawesomeCapacitorAppIcon'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.license = package['license']
|
|
10
|
+
s.homepage = package['repository']['url']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
|
+
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
+
s.ios.deployment_target = '15.0'
|
|
15
|
+
s.dependency 'Capacitor'
|
|
16
|
+
s.swift_version = '5.1'
|
|
17
|
+
end
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robin Genz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Package.swift
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// swift-tools-version: 5.9
|
|
2
|
+
import PackageDescription
|
|
3
|
+
|
|
4
|
+
let package = Package(
|
|
5
|
+
name: "CapawesomeCapacitorAppIcon",
|
|
6
|
+
platforms: [.iOS(.v15)],
|
|
7
|
+
products: [
|
|
8
|
+
.library(
|
|
9
|
+
name: "CapawesomeCapacitorAppIcon",
|
|
10
|
+
targets: ["AppIconPlugin"])
|
|
11
|
+
],
|
|
12
|
+
dependencies: [
|
|
13
|
+
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "8.0.0")
|
|
14
|
+
],
|
|
15
|
+
targets: [
|
|
16
|
+
.target(
|
|
17
|
+
name: "AppIconPlugin",
|
|
18
|
+
dependencies: [
|
|
19
|
+
.product(name: "Capacitor", package: "capacitor-swift-pm"),
|
|
20
|
+
.product(name: "Cordova", package: "capacitor-swift-pm")
|
|
21
|
+
],
|
|
22
|
+
path: "ios/Plugin"),
|
|
23
|
+
.testTarget(
|
|
24
|
+
name: "AppIconPluginTests",
|
|
25
|
+
dependencies: ["AppIconPlugin"],
|
|
26
|
+
path: "ios/PluginTests")
|
|
27
|
+
]
|
|
28
|
+
)
|
package/README.md
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# @capawesome/capacitor-app-icon
|
|
2
|
+
|
|
3
|
+
Capacitor plugin to change the app icon at runtime.
|
|
4
|
+
|
|
5
|
+
<div class="capawesome-z29o10a">
|
|
6
|
+
<a href="https://cloud.capawesome.io/" target="_blank">
|
|
7
|
+
<img alt="Deliver Live Updates to your Capacitor app with Capawesome Cloud" src="https://cloud.capawesome.io/assets/banners/cloud-build-and-deploy-capacitor-apps.png?t=1" />
|
|
8
|
+
</a>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 🎨 **Alternate icons**: Switch the home-screen icon between the icons your app declares.
|
|
14
|
+
- 🔍 **Current icon**: Read the name of the icon that is currently in use.
|
|
15
|
+
- 🔄 **Reset**: Restore the default icon at any time.
|
|
16
|
+
- 📦 **CocoaPods & SPM**: Supports CocoaPods and Swift Package Manager for iOS.
|
|
17
|
+
- 🔁 **Up-to-date**: Always supports the latest Capacitor version.
|
|
18
|
+
|
|
19
|
+
Missing a feature? Just [open an issue](https://github.com/capawesome-team/capacitor-plugins/issues) and we'll take a look!
|
|
20
|
+
|
|
21
|
+
## Newsletter
|
|
22
|
+
|
|
23
|
+
Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our [Capawesome Newsletter](https://cloud.capawesome.io/newsletter/).
|
|
24
|
+
|
|
25
|
+
## Compatibility
|
|
26
|
+
|
|
27
|
+
| Plugin Version | Capacitor Version | Status |
|
|
28
|
+
| -------------- | ----------------- | -------------- |
|
|
29
|
+
| 0.x.x | >=8.x.x | Active support |
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
You can use our **AI-Assisted Setup** to install the plugin.
|
|
34
|
+
Add the [Capawesome Skills](https://github.com/capawesome-team/skills) to your AI tool using the following command:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npx skills add capawesome-team/skills --skill capacitor-plugins
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Then use the following prompt:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capawesome/capacitor-app-icon` plugin in my project.
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
If you prefer **Manual Setup**, install the plugin by running the following commands and follow the platform-specific instructions below:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm install @capawesome/capacitor-app-icon
|
|
50
|
+
npx cap sync
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
This plugin cannot add icons dynamically. Every icon you want to switch to must be **declared by the app beforehand**. The following sections describe how to declare alternate icons on each platform.
|
|
54
|
+
|
|
55
|
+
### Android
|
|
56
|
+
|
|
57
|
+
On Android, each alternate icon is declared as an [`<activity-alias>`](https://developer.android.com/guide/topics/manifest/activity-alias-element) that points at the launcher activity. The plugin enables the requested alias and disables the others.
|
|
58
|
+
|
|
59
|
+
Open your `AndroidManifest.xml` and remove the launcher `<intent-filter>` from your `.MainActivity`. Then add one `<activity-alias>` for the default icon and one for each alternate icon. Exactly one alias must be `android:enabled="true"` (the default icon); all others must be `android:enabled="false"`.
|
|
60
|
+
|
|
61
|
+
```xml
|
|
62
|
+
<!-- Remove the <intent-filter> from the MainActivity. -->
|
|
63
|
+
<activity
|
|
64
|
+
android:name=".MainActivity"
|
|
65
|
+
android:exported="true"
|
|
66
|
+
... />
|
|
67
|
+
|
|
68
|
+
<!-- The default icon (enabled). -->
|
|
69
|
+
<activity-alias
|
|
70
|
+
android:name=".AppIconDefault"
|
|
71
|
+
android:enabled="true"
|
|
72
|
+
android:exported="true"
|
|
73
|
+
android:icon="@mipmap/ic_launcher"
|
|
74
|
+
android:roundIcon="@mipmap/ic_launcher_round"
|
|
75
|
+
android:targetActivity=".MainActivity">
|
|
76
|
+
<intent-filter>
|
|
77
|
+
<action android:name="android.intent.action.MAIN" />
|
|
78
|
+
<category android:name="android.intent.category.LAUNCHER" />
|
|
79
|
+
</intent-filter>
|
|
80
|
+
</activity-alias>
|
|
81
|
+
|
|
82
|
+
<!-- An alternate icon (disabled). -->
|
|
83
|
+
<activity-alias
|
|
84
|
+
android:name=".AppIconChristmas"
|
|
85
|
+
android:enabled="false"
|
|
86
|
+
android:exported="true"
|
|
87
|
+
android:icon="@mipmap/ic_launcher_christmas"
|
|
88
|
+
android:roundIcon="@mipmap/ic_launcher_christmas_round"
|
|
89
|
+
android:targetActivity=".MainActivity">
|
|
90
|
+
<intent-filter>
|
|
91
|
+
<action android:name="android.intent.action.MAIN" />
|
|
92
|
+
<category android:name="android.intent.category.LAUNCHER" />
|
|
93
|
+
</intent-filter>
|
|
94
|
+
</activity-alias>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The icon name passed to `setIcon(...)` is the alias name **without the leading dot** (e.g. `AppIconChristmas`). Add the referenced icon resources (e.g. `@mipmap/ic_launcher_christmas`) to your `res/mipmap-*` folders.
|
|
98
|
+
|
|
99
|
+
> [!NOTE]
|
|
100
|
+
> The behavior after a change depends on the launcher. Some launchers apply the new icon only after the app's task is closed, and a few kill the app despite the plugin requesting otherwise. Shortcuts that were pinned to a now-disabled alias may stop working.
|
|
101
|
+
|
|
102
|
+
### iOS
|
|
103
|
+
|
|
104
|
+
On iOS, alternate icons are declared under the `CFBundleIcons` key in your app's `Info.plist`. The icon image files must be bundled with the app (e.g. `AppIconChristmas@2x.png` and `AppIconChristmas@3x.png`).
|
|
105
|
+
|
|
106
|
+
```xml
|
|
107
|
+
<key>CFBundleIcons</key>
|
|
108
|
+
<dict>
|
|
109
|
+
<key>CFBundleAlternateIcons</key>
|
|
110
|
+
<dict>
|
|
111
|
+
<key>AppIconChristmas</key>
|
|
112
|
+
<dict>
|
|
113
|
+
<key>CFBundleIconFiles</key>
|
|
114
|
+
<array>
|
|
115
|
+
<string>AppIconChristmas</string>
|
|
116
|
+
</array>
|
|
117
|
+
<key>UIPrerenderedIcon</key>
|
|
118
|
+
<false/>
|
|
119
|
+
</dict>
|
|
120
|
+
</dict>
|
|
121
|
+
</dict>
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
The icon name passed to `setIcon(...)` is the key inside `CFBundleAlternateIcons` (e.g. `AppIconChristmas`). The default icon (declared via `CFBundlePrimaryIcon` or the asset catalog) is restored with `resetIcon(...)`.
|
|
125
|
+
|
|
126
|
+
> [!NOTE]
|
|
127
|
+
> The system shows a user-visible alert every time the icon changes, and the icon cannot be changed while the app is in the background.
|
|
128
|
+
|
|
129
|
+
## Configuration
|
|
130
|
+
|
|
131
|
+
No configuration required for this plugin.
|
|
132
|
+
|
|
133
|
+
## Usage
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
import { AppIcon } from '@capawesome/capacitor-app-icon';
|
|
137
|
+
|
|
138
|
+
const isAvailable = async () => {
|
|
139
|
+
const { available } = await AppIcon.isAvailable();
|
|
140
|
+
return available;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const getCurrentIcon = async () => {
|
|
144
|
+
const { icon } = await AppIcon.getCurrentIcon();
|
|
145
|
+
return icon;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const setIcon = async () => {
|
|
149
|
+
await AppIcon.setIcon({ icon: 'AppIconChristmas' });
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const resetIcon = async () => {
|
|
153
|
+
await AppIcon.resetIcon();
|
|
154
|
+
};
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## API
|
|
158
|
+
|
|
159
|
+
<docgen-index>
|
|
160
|
+
|
|
161
|
+
* [`getCurrentIcon()`](#getcurrenticon)
|
|
162
|
+
* [`isAvailable()`](#isavailable)
|
|
163
|
+
* [`resetIcon()`](#reseticon)
|
|
164
|
+
* [`setIcon(...)`](#seticon)
|
|
165
|
+
* [Interfaces](#interfaces)
|
|
166
|
+
|
|
167
|
+
</docgen-index>
|
|
168
|
+
|
|
169
|
+
<docgen-api>
|
|
170
|
+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
171
|
+
|
|
172
|
+
### getCurrentIcon()
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
getCurrentIcon() => Promise<GetCurrentIconResult>
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Get the name of the icon that is currently in use.
|
|
179
|
+
|
|
180
|
+
Returns `null` if the default icon is in use.
|
|
181
|
+
|
|
182
|
+
Only available on Android and iOS.
|
|
183
|
+
|
|
184
|
+
**Returns:** <code>Promise<<a href="#getcurrenticonresult">GetCurrentIconResult</a>></code>
|
|
185
|
+
|
|
186
|
+
**Since:** 0.1.0
|
|
187
|
+
|
|
188
|
+
--------------------
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
### isAvailable()
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
isAvailable() => Promise<IsAvailableResult>
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Check if changing the app icon is supported on the current device.
|
|
198
|
+
|
|
199
|
+
On Android, this always resolves to `true`.
|
|
200
|
+
On iOS, this resolves to the value of `supportsAlternateIcons`.
|
|
201
|
+
|
|
202
|
+
Only available on Android and iOS.
|
|
203
|
+
|
|
204
|
+
**Returns:** <code>Promise<<a href="#isavailableresult">IsAvailableResult</a>></code>
|
|
205
|
+
|
|
206
|
+
**Since:** 0.1.0
|
|
207
|
+
|
|
208
|
+
--------------------
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
### resetIcon()
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
resetIcon() => Promise<void>
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
Restore the default app icon.
|
|
218
|
+
|
|
219
|
+
Only available on Android and iOS.
|
|
220
|
+
|
|
221
|
+
**Since:** 0.1.0
|
|
222
|
+
|
|
223
|
+
--------------------
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
### setIcon(...)
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
setIcon(options: SetIconOptions) => Promise<void>
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Change the app icon to the alternate icon with the given name.
|
|
233
|
+
|
|
234
|
+
The icon must be declared by the app beforehand. See the setup instructions
|
|
235
|
+
for [Android](https://capawesome.io/docs/plugins/app-icon/#android) and
|
|
236
|
+
[iOS](https://capawesome.io/docs/plugins/app-icon/#ios) for more information.
|
|
237
|
+
|
|
238
|
+
Only available on Android and iOS.
|
|
239
|
+
|
|
240
|
+
| Param | Type |
|
|
241
|
+
| ------------- | --------------------------------------------------------- |
|
|
242
|
+
| **`options`** | <code><a href="#seticonoptions">SetIconOptions</a></code> |
|
|
243
|
+
|
|
244
|
+
**Since:** 0.1.0
|
|
245
|
+
|
|
246
|
+
--------------------
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
### Interfaces
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
#### GetCurrentIconResult
|
|
253
|
+
|
|
254
|
+
| Prop | Type | Description | Since |
|
|
255
|
+
| ---------- | --------------------------- | -------------------------------------------------------------------------------------------- | ----- |
|
|
256
|
+
| **`icon`** | <code>string \| null</code> | The name of the icon that is currently in use. Returns `null` if the default icon is in use. | 0.1.0 |
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
#### IsAvailableResult
|
|
260
|
+
|
|
261
|
+
| Prop | Type | Description | Since |
|
|
262
|
+
| --------------- | -------------------- | ------------------------------------------------------------------------ | ----- |
|
|
263
|
+
| **`available`** | <code>boolean</code> | Whether or not changing the app icon is supported on the current device. | 0.1.0 |
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
#### SetIconOptions
|
|
267
|
+
|
|
268
|
+
| Prop | Type | Description | Since |
|
|
269
|
+
| ---------- | ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
|
|
270
|
+
| **`icon`** | <code>string</code> | The name of the alternate icon to use. On Android, this is the name of the `<activity-alias>` (without the leading dot). On iOS, this is the key of the icon inside `CFBundleAlternateIcons`. | 0.1.0 |
|
|
271
|
+
|
|
272
|
+
</docgen-api>
|
|
273
|
+
|
|
274
|
+
## Changelog
|
|
275
|
+
|
|
276
|
+
See [CHANGELOG.md](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/app-icon/CHANGELOG.md).
|
|
277
|
+
|
|
278
|
+
## License
|
|
279
|
+
|
|
280
|
+
See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/app-icon/LICENSE).
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
|
|
4
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
buildscript {
|
|
9
|
+
repositories {
|
|
10
|
+
google()
|
|
11
|
+
mavenCentral()
|
|
12
|
+
}
|
|
13
|
+
dependencies {
|
|
14
|
+
classpath 'com.android.tools.build:gradle:8.13.0'
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
apply plugin: 'com.android.library'
|
|
19
|
+
|
|
20
|
+
android {
|
|
21
|
+
namespace = "io.capawesome.capacitorjs.plugins.appicon"
|
|
22
|
+
compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
|
|
23
|
+
defaultConfig {
|
|
24
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
|
|
25
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
|
|
26
|
+
versionCode 1
|
|
27
|
+
versionName "1.0"
|
|
28
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
29
|
+
}
|
|
30
|
+
buildTypes {
|
|
31
|
+
release {
|
|
32
|
+
minifyEnabled false
|
|
33
|
+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
lintOptions {
|
|
37
|
+
abortOnError = false
|
|
38
|
+
}
|
|
39
|
+
compileOptions {
|
|
40
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
41
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
repositories {
|
|
46
|
+
google()
|
|
47
|
+
mavenCentral()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
dependencies {
|
|
52
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
53
|
+
implementation project(':capacitor-android')
|
|
54
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
55
|
+
testImplementation "junit:junit:$junitVersion"
|
|
56
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
57
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
58
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.appicon;
|
|
2
|
+
|
|
3
|
+
import android.content.ComponentName;
|
|
4
|
+
import android.content.Context;
|
|
5
|
+
import android.content.Intent;
|
|
6
|
+
import android.content.pm.PackageManager;
|
|
7
|
+
import android.content.pm.ResolveInfo;
|
|
8
|
+
import androidx.annotation.NonNull;
|
|
9
|
+
import androidx.annotation.Nullable;
|
|
10
|
+
import io.capawesome.capacitorjs.plugins.appicon.classes.CustomExceptions;
|
|
11
|
+
import io.capawesome.capacitorjs.plugins.appicon.classes.options.SetIconOptions;
|
|
12
|
+
import io.capawesome.capacitorjs.plugins.appicon.classes.results.GetCurrentIconResult;
|
|
13
|
+
import io.capawesome.capacitorjs.plugins.appicon.classes.results.IsAvailableResult;
|
|
14
|
+
import java.util.List;
|
|
15
|
+
|
|
16
|
+
public class AppIcon {
|
|
17
|
+
|
|
18
|
+
@NonNull
|
|
19
|
+
private final AppIconPlugin plugin;
|
|
20
|
+
|
|
21
|
+
public AppIcon(@NonNull AppIconPlugin plugin) {
|
|
22
|
+
this.plugin = plugin;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@NonNull
|
|
26
|
+
public GetCurrentIconResult getCurrentIcon() {
|
|
27
|
+
List<ResolveInfo> launcherComponents = getLauncherComponents();
|
|
28
|
+
String defaultIconName = getDefaultIconName(launcherComponents);
|
|
29
|
+
String activeIconName = getActiveIconName(launcherComponents);
|
|
30
|
+
boolean isDefault = activeIconName == null || activeIconName.equals(defaultIconName);
|
|
31
|
+
return new GetCurrentIconResult(isDefault ? null : activeIconName);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@NonNull
|
|
35
|
+
public IsAvailableResult isAvailable() {
|
|
36
|
+
return new IsAvailableResult(true);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public void resetIcon() throws Exception {
|
|
40
|
+
List<ResolveInfo> launcherComponents = getLauncherComponents();
|
|
41
|
+
String defaultIconName = getDefaultIconName(launcherComponents);
|
|
42
|
+
if (defaultIconName == null) {
|
|
43
|
+
throw CustomExceptions.CHANGE_FAILED;
|
|
44
|
+
}
|
|
45
|
+
setActiveIcon(launcherComponents, defaultIconName);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public void setIcon(@NonNull SetIconOptions options) throws Exception {
|
|
49
|
+
String icon = options.getIcon();
|
|
50
|
+
List<ResolveInfo> launcherComponents = getLauncherComponents();
|
|
51
|
+
if (!hasIcon(launcherComponents, icon)) {
|
|
52
|
+
throw CustomExceptions.ICON_NOT_FOUND;
|
|
53
|
+
}
|
|
54
|
+
setActiveIcon(launcherComponents, icon);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@Nullable
|
|
58
|
+
private String getActiveIconName(@NonNull List<ResolveInfo> launcherComponents) {
|
|
59
|
+
PackageManager packageManager = plugin.getContext().getPackageManager();
|
|
60
|
+
for (ResolveInfo info : launcherComponents) {
|
|
61
|
+
ComponentName componentName = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
|
|
62
|
+
int state = packageManager.getComponentEnabledSetting(componentName);
|
|
63
|
+
boolean isEnabled =
|
|
64
|
+
state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED ||
|
|
65
|
+
(state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT && info.activityInfo.enabled);
|
|
66
|
+
if (isEnabled) {
|
|
67
|
+
return getIconName(info);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@Nullable
|
|
74
|
+
private String getDefaultIconName(@NonNull List<ResolveInfo> launcherComponents) {
|
|
75
|
+
for (ResolveInfo info : launcherComponents) {
|
|
76
|
+
if (info.activityInfo.enabled) {
|
|
77
|
+
return getIconName(info);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
@NonNull
|
|
84
|
+
private String getIconName(@NonNull ResolveInfo info) {
|
|
85
|
+
String name = info.activityInfo.name;
|
|
86
|
+
int lastDotIndex = name.lastIndexOf('.');
|
|
87
|
+
return lastDotIndex == -1 ? name : name.substring(lastDotIndex + 1);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@NonNull
|
|
91
|
+
private List<ResolveInfo> getLauncherComponents() {
|
|
92
|
+
Context context = plugin.getContext();
|
|
93
|
+
Intent intent = new Intent(Intent.ACTION_MAIN);
|
|
94
|
+
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
|
95
|
+
intent.setPackage(context.getPackageName());
|
|
96
|
+
return context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DISABLED_COMPONENTS);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private boolean hasIcon(@NonNull List<ResolveInfo> launcherComponents, @NonNull String icon) {
|
|
100
|
+
for (ResolveInfo info : launcherComponents) {
|
|
101
|
+
if (getIconName(info).equals(icon)) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private void setActiveIcon(@NonNull List<ResolveInfo> launcherComponents, @NonNull String icon) {
|
|
109
|
+
PackageManager packageManager = plugin.getContext().getPackageManager();
|
|
110
|
+
for (ResolveInfo info : launcherComponents) {
|
|
111
|
+
ComponentName componentName = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
|
|
112
|
+
int newState = getIconName(info).equals(icon)
|
|
113
|
+
? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
|
114
|
+
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
|
|
115
|
+
packageManager.setComponentEnabledSetting(componentName, newState, PackageManager.DONT_KILL_APP);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.appicon;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
import com.getcapacitor.Logger;
|
|
6
|
+
import com.getcapacitor.Plugin;
|
|
7
|
+
import com.getcapacitor.PluginCall;
|
|
8
|
+
import com.getcapacitor.PluginMethod;
|
|
9
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
10
|
+
import io.capawesome.capacitorjs.plugins.appicon.classes.CustomException;
|
|
11
|
+
import io.capawesome.capacitorjs.plugins.appicon.classes.options.SetIconOptions;
|
|
12
|
+
import io.capawesome.capacitorjs.plugins.appicon.classes.results.GetCurrentIconResult;
|
|
13
|
+
import io.capawesome.capacitorjs.plugins.appicon.classes.results.IsAvailableResult;
|
|
14
|
+
import io.capawesome.capacitorjs.plugins.appicon.interfaces.Result;
|
|
15
|
+
|
|
16
|
+
@CapacitorPlugin(name = "AppIcon")
|
|
17
|
+
public class AppIconPlugin extends Plugin {
|
|
18
|
+
|
|
19
|
+
public static final String ERROR_UNKNOWN_ERROR = "An unknown error occurred.";
|
|
20
|
+
public static final String TAG = "AppIcon";
|
|
21
|
+
|
|
22
|
+
private AppIcon implementation;
|
|
23
|
+
|
|
24
|
+
@Override
|
|
25
|
+
public void load() {
|
|
26
|
+
implementation = new AppIcon(this);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@PluginMethod
|
|
30
|
+
public void getCurrentIcon(PluginCall call) {
|
|
31
|
+
try {
|
|
32
|
+
GetCurrentIconResult result = implementation.getCurrentIcon();
|
|
33
|
+
resolveCall(call, result);
|
|
34
|
+
} catch (Exception exception) {
|
|
35
|
+
rejectCall(call, exception);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@PluginMethod
|
|
40
|
+
public void isAvailable(PluginCall call) {
|
|
41
|
+
try {
|
|
42
|
+
IsAvailableResult result = implementation.isAvailable();
|
|
43
|
+
resolveCall(call, result);
|
|
44
|
+
} catch (Exception exception) {
|
|
45
|
+
rejectCall(call, exception);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
@PluginMethod
|
|
50
|
+
public void resetIcon(PluginCall call) {
|
|
51
|
+
try {
|
|
52
|
+
implementation.resetIcon();
|
|
53
|
+
resolveCall(call);
|
|
54
|
+
} catch (Exception exception) {
|
|
55
|
+
rejectCall(call, exception);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@PluginMethod
|
|
60
|
+
public void setIcon(PluginCall call) {
|
|
61
|
+
try {
|
|
62
|
+
SetIconOptions options = new SetIconOptions(call);
|
|
63
|
+
implementation.setIcon(options);
|
|
64
|
+
resolveCall(call);
|
|
65
|
+
} catch (Exception exception) {
|
|
66
|
+
rejectCall(call, exception);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private void rejectCall(@NonNull PluginCall call, @NonNull Exception exception) {
|
|
71
|
+
String message = exception.getMessage();
|
|
72
|
+
if (message == null) {
|
|
73
|
+
message = ERROR_UNKNOWN_ERROR;
|
|
74
|
+
}
|
|
75
|
+
String code = null;
|
|
76
|
+
if (exception instanceof CustomException) {
|
|
77
|
+
code = ((CustomException) exception).getCode();
|
|
78
|
+
}
|
|
79
|
+
Logger.error(TAG, message, exception);
|
|
80
|
+
call.reject(message, code);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private void resolveCall(@NonNull PluginCall call) {
|
|
84
|
+
call.resolve();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private void resolveCall(@NonNull PluginCall call, @Nullable Result result) {
|
|
88
|
+
if (result == null) {
|
|
89
|
+
call.resolve();
|
|
90
|
+
} else {
|
|
91
|
+
call.resolve(result.toJSObject());
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
package/android/src/main/java/io/capawesome/capacitorjs/plugins/appicon/classes/CustomException.java
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.appicon.classes;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
|
|
6
|
+
public class CustomException extends Exception {
|
|
7
|
+
|
|
8
|
+
@Nullable
|
|
9
|
+
private final String code;
|
|
10
|
+
|
|
11
|
+
public CustomException(@Nullable String code, @NonNull String message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Nullable
|
|
17
|
+
public String getCode() {
|
|
18
|
+
return code;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.appicon.classes;
|
|
2
|
+
|
|
3
|
+
public class CustomExceptions {
|
|
4
|
+
|
|
5
|
+
public static final CustomException CHANGE_FAILED = new CustomException("CHANGE_FAILED", "The app icon could not be changed.");
|
|
6
|
+
public static final CustomException ICON_MISSING = new CustomException(null, "icon must be provided.");
|
|
7
|
+
public static final CustomException ICON_NOT_FOUND = new CustomException(
|
|
8
|
+
"ICON_NOT_FOUND",
|
|
9
|
+
"The alternate icon with the given name could not be found."
|
|
10
|
+
);
|
|
11
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.appicon.classes.options;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.PluginCall;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.appicon.classes.CustomExceptions;
|
|
6
|
+
|
|
7
|
+
public class SetIconOptions {
|
|
8
|
+
|
|
9
|
+
@NonNull
|
|
10
|
+
private final String icon;
|
|
11
|
+
|
|
12
|
+
public SetIconOptions(@NonNull PluginCall call) throws Exception {
|
|
13
|
+
this.icon = getIconFromCall(call);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@NonNull
|
|
17
|
+
public String getIcon() {
|
|
18
|
+
return icon;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@NonNull
|
|
22
|
+
private static String getIconFromCall(@NonNull PluginCall call) throws Exception {
|
|
23
|
+
String icon = call.getString("icon");
|
|
24
|
+
if (icon == null) {
|
|
25
|
+
throw CustomExceptions.ICON_MISSING;
|
|
26
|
+
}
|
|
27
|
+
return icon;
|
|
28
|
+
}
|
|
29
|
+
}
|