Skip to content

Commit f9e98e8

Browse files
committed
support new arch + android changes
1 parent 55e6d86 commit f9e98e8

File tree

8 files changed

+60
-26
lines changed

8 files changed

+60
-26
lines changed

NativeChangeIcon.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { TurboModule } from 'react-native';
2+
import { TurboModuleRegistry } from 'react-native';
3+
4+
export interface Spec extends TurboModule {
5+
readonly getConstants: () => {};
6+
changeIcon: (iconName?: string) => Promise<string>;
7+
resetIcon: () => Promise<string>;
8+
getIcon: () => Promise<string>;
9+
}
10+
11+
export default TurboModuleRegistry.get<Spec>('ChangeIcon');

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@
125125
**Android 🤖**
126126
1. Add an alias for each of your new icons within the `AndroidManifest.xml` (within `<application>`).
127127
- Make sure these have the properties as shown below.
128-
- For the name prefix it `MainActivity...` followed by the name you will use to reference your icon. e.g. for our light icon we will use `com.myapp.MainActivityLight` (replace `com.myapp` with your package name)
128+
- Create an alias for `.MainActivityDefault` as well but for this, set `android:enabled="true"`.
129+
- For the name prefix it `.MainActivity...` followed by the name you will use to reference your icon. e.g. for our light icon we will use `.MainActivityLight`
130+
2. You'll have to remove the `LAUNCHER` intent filter from the main `<activity>` as we have added the launcher in `.MainActivityDefault`.
129131

130132

131133
<div align="center">
@@ -134,7 +136,7 @@
134136

135137
```xml
136138
<activity-alias
137-
android:name="com.myapp.MainActivityLight"
139+
android:name=".MainActivityLight"
138140
android:enabled="false"
139141
android:exported="true"
140142
android:icon="@mipmap/ic_launcher_light"
@@ -151,8 +153,12 @@
151153
2. Within this dictionary add another key for `CFBundleAlternateIcons`
152154
3. Finally then within this dictionary you can add in the keys for you new icons
153155
- The `key` is the name you will reference from within code.
154-
- Set the first array item to the name of the `.appiconset` we created earlier.
156+
- Set the first array item to the name of the `.appiconset` we created earlier.
157+
4. In XCode, in your app's `General` settings, under `App Icons and Launch Screen`, set "App Icon" to `Default` and check the "Include all app icon assets" checkbox below.
155158

159+
<div align="center">
160+
<img src="docs/ios-example-app-icon.png" alt="Example App Icons and Launch Screen">
161+
</div>
156162
<div align="center">
157163
<a href="./docs/examples/Step-4/Info.plist">Example Info.plist</a>
158164
</div>

android/src/main/java/com/reactnativechangeicon/ChangeIconModule.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
import com.facebook.react.module.annotations.ReactModule;
1616

1717
import java.util.ArrayList;
18-
import java.util.Collections;
1918
import java.util.List;
2019

2120
@ReactModule(name = "ChangeIcon")
2221
public class ChangeIconModule extends ReactContextBaseJavaModule implements Application.ActivityLifecycleCallbacks {
2322
public static final String NAME = "ChangeIcon";
2423
private final String packageName;
25-
private List<String> classesToKill = new ArrayList<>();
24+
private final List<String> classesToKill = new ArrayList<>();
2625
private Boolean iconChanged = false;
2726
private String componentClass = "";
2827

@@ -69,10 +68,10 @@ public void changeIcon(String iconName, Promise promise) {
6968
return;
7069
}
7170
if (this.componentClass.isEmpty()) {
72-
this.componentClass = activity.getComponentName().getClassName();
71+
this.componentClass = activityName.endsWith("MainActivity") ? activityName + "Default" : activityName;
7372
}
7473

75-
final String newIconName = (iconName == null || iconName.isEmpty() || iconName.equals("Default")) ? "" : iconName;
74+
final String newIconName = (iconName == null || iconName.isEmpty()) ? "Default" : iconName;
7675
final String activeClass = this.packageName + ".MainActivity" + newIconName;
7776
if (this.componentClass.equals(activeClass)) {
7877
promise.reject("ANDROID:ICON_ALREADY_USED:" + this.componentClass);
@@ -83,7 +82,7 @@ public void changeIcon(String iconName, Promise promise) {
8382
new ComponentName(this.packageName, activeClass),
8483
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
8584
PackageManager.DONT_KILL_APP);
86-
promise.resolve(newIconName.isEmpty() ? "Default" : newIconName);
85+
promise.resolve(newIconName);
8786
} catch (Exception e) {
8887
promise.reject("ANDROID:ICON_INVALID");
8988
return;

android/src/main/java/com/reactnativechangeicon/ChangeIconPackage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.List;
1313

1414
public class ChangeIconPackage implements ReactPackage {
15-
private String packageName;
15+
private final String packageName;
1616

1717
public ChangeIconPackage(String packageName) {
1818
this.packageName = packageName;

docs/examples/Step-4/AndroidManifest.xml

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
3+
<uses-permission android:name="android.permission.INTERNET" />
24

35
<application
46
android:name=".MainApplication"
5-
android:label="@string/app_name"
6-
android:icon="@mipmap/ic_launcher"
77
android:allowBackup="false"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
810
android:theme="@style/AppTheme">
9-
1011
<activity
1112
android:name=".MainActivity"
12-
android:label="@string/app_name"
13-
android:screenOrientation="portrait"
1413
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
14+
android:exported="true"
1515
android:launchMode="singleTask"
16-
android:windowSoftInputMode="adjustResize"
17-
android:exported="true">
16+
android:windowSoftInputMode="adjustResize">
1817
<intent-filter>
1918
<action android:name="android.intent.action.MAIN" />
20-
<category android:name="android.intent.category.LAUNCHER" />
2119
</intent-filter>
2220
</activity>
2321

24-
<activity-alias
25-
android:name="com.myCompany.myApp.MainActivityLight"
22+
<activity-alias
23+
android:name=".MainActivityDefault"
24+
android:enabled="true"
25+
android:exported="true"
26+
android:icon="@mipmap/ic_launcher"
27+
android:targetActivity=".MainActivity">
28+
<intent-filter>
29+
<action android:name="android.intent.action.MAIN" />
30+
<category android:name="android.intent.category.LAUNCHER" />
31+
</intent-filter>
32+
</activity-alias>
33+
<activity-alias
34+
android:name=".MainActivityLight"
2635
android:enabled="false"
2736
android:exported="true"
2837
android:icon="@mipmap/ic_launcher_light"
@@ -32,9 +41,8 @@
3241
<category android:name="android.intent.category.LAUNCHER" />
3342
</intent-filter>
3443
</activity-alias>
35-
36-
<activity-alias
37-
android:name="com.myCompany.myApp.MainActivityDark"
44+
<activity-alias
45+
android:name=".MainActivityDark"
3846
android:enabled="false"
3947
android:exported="true"
4048
android:icon="@mipmap/ic_launcher_dark"

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"android",
1212
"ios",
1313
"dist/**/*",
14+
"NativeChangeIcon.ts",
1415
"react-native-change-icon.podspec",
1516
"react-native.config.js",
1617
"README.md"
@@ -41,5 +42,13 @@
4142
"peerDependencies": {
4243
"react-native": "*"
4344
},
44-
"packageManager": "^yarn@1.22.19"
45+
"packageManager": "^yarn@1.22.19",
46+
"codegenConfig": {
47+
"name": "RNChangeIconSpec",
48+
"type": "all",
49+
"jsSrcsDir": ".",
50+
"android": {
51+
"javaPackageName": "com.facebook.fbreact.specs"
52+
}
53+
}
4554
}

react-native-change-icon.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Pod::Spec.new do |s|
1111
s.license = package["license"]
1212
s.authors = package["author"]
1313

14-
s.platforms = { :ios => "11.0" }
14+
s.platforms = { :ios => "12.0" }
1515
s.source = { :git => "https://github.com/skb1129/react-native-change-icon.git", :tag => "#{s.version}" }
1616

1717
s.source_files = "ios/**/*.{h,m,mm}"

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
"strict": true,
2727
"target": "esnext",
2828
"verbatimModuleSyntax": true
29-
}
29+
},
30+
"files": ["./src/index.ts"]
3031
}

0 commit comments

Comments
 (0)