Skip to content

Commit c6a066e

Browse files
test(fixtures): add Java and Kotlin fixtures for Android app entrypoints
Co-Authored-By: Bill Spooner <b@twodoors.dev>
1 parent 4eb10b7 commit c6a066e

File tree

5 files changed

+222
-1
lines changed

5 files changed

+222
-1
lines changed

fixtures/MainActivity.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.reactnativeglobalkeyevent;
2+
3+
import com.facebook.react.ReactActivity;
4+
5+
public class MainActivity extends ReactActivity {
6+
7+
/**
8+
* Returns the name of the main component registered from JavaScript. This is used to schedule
9+
* rendering of the component.
10+
*/
11+
@Override
12+
protected String getMainComponentName() {
13+
return "ReactNativeGlobalKeyEvent";
14+
}
15+
}

fixtures/MainActivity.kt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.reactnativeglobalkeyevent
2+
3+
import android.os.Build
4+
import android.os.Bundle
5+
6+
import com.facebook.react.ReactActivity
7+
import com.facebook.react.ReactActivityDelegate
8+
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
9+
import com.facebook.react.defaults.DefaultReactActivityDelegate
10+
11+
import expo.modules.ReactActivityDelegateWrapper
12+
13+
class MainActivity : ReactActivity() {
14+
override fun onCreate(savedInstanceState: Bundle?) {
15+
// Set the theme to AppTheme BEFORE onCreate to support
16+
// coloring the background, status bar, and navigation bar.
17+
// This is required for expo-splash-screen.
18+
setTheme(R.style.AppTheme);
19+
super.onCreate(null)
20+
}
21+
22+
/**
23+
* Returns the name of the main component registered from JavaScript. This is used to schedule
24+
* rendering of the component.
25+
*/
26+
override fun getMainComponentName(): String = "main"
27+
28+
/**
29+
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
30+
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
31+
*/
32+
override fun createReactActivityDelegate(): ReactActivityDelegate {
33+
return ReactActivityDelegateWrapper(
34+
this,
35+
BuildConfig.IS_NEW_ARCHITECTURE_ENABLED,
36+
object : DefaultReactActivityDelegate(
37+
this,
38+
mainComponentName,
39+
fabricEnabled
40+
){})
41+
}
42+
43+
/**
44+
* Align the back button behavior with Android S
45+
* where moving root activities to background instead of finishing activities.
46+
* @see <a href="https://developer.android.com/reference/android/app/Activity#onBackPressed()">onBackPressed</a>
47+
*/
48+
override fun invokeDefaultOnBackPressed() {
49+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
50+
if (!moveTaskToBack(false)) {
51+
// For non-root activities, use the default implementation to finish them.
52+
super.invokeDefaultOnBackPressed()
53+
}
54+
return
55+
}
56+
57+
// Use the default back button implementation on Android S
58+
// because it's doing more than [Activity.moveTaskToBack] in fact.
59+
super.invokeDefaultOnBackPressed()
60+
}
61+
}

fixtures/MainApplication.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.reactnativeglobalkeyevent;
2+
3+
import android.app.Application;
4+
import android.content.Context;
5+
import com.facebook.react.PackageList;
6+
import com.facebook.react.ReactApplication;
7+
import com.facebook.react.ReactInstanceManager;
8+
import com.facebook.react.ReactNativeHost;
9+
import com.facebook.react.ReactPackage;
10+
import com.facebook.soloader.SoLoader;
11+
import java.lang.reflect.InvocationTargetException;
12+
import java.util.List;
13+
14+
public class MainApplication extends Application implements ReactApplication {
15+
16+
private final ReactNativeHost mReactNativeHost =
17+
new ReactNativeHost(this) {
18+
@Override
19+
public boolean getUseDeveloperSupport() {
20+
return BuildConfig.DEBUG;
21+
}
22+
23+
@Override
24+
protected List<ReactPackage> getPackages() {
25+
@SuppressWarnings("UnnecessaryLocalVariable")
26+
List<ReactPackage> packages = new PackageList(this).getPackages();
27+
// Packages that cannot be autolinked yet can be added manually here, for example:
28+
// packages.add(new MyReactNativePackage());
29+
return packages;
30+
}
31+
32+
@Override
33+
protected String getJSMainModuleName() {
34+
return "index";
35+
}
36+
};
37+
38+
@Override
39+
public ReactNativeHost getReactNativeHost() {
40+
return mReactNativeHost;
41+
}
42+
43+
@Override
44+
public void onCreate() {
45+
super.onCreate();
46+
SoLoader.init(this, /* native exopackage */ false);
47+
initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
48+
}
49+
50+
/**
51+
* Loads Flipper in React Native templates. Call this in the onCreate method with something like
52+
* initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
53+
*
54+
* @param context
55+
* @param reactInstanceManager
56+
*/
57+
private static void initializeFlipper(
58+
Context context, ReactInstanceManager reactInstanceManager) {
59+
if (BuildConfig.DEBUG) {
60+
try {
61+
/*
62+
We use reflection here to pick up the class that initializes Flipper,
63+
since Flipper library is not available in release mode
64+
*/
65+
Class<?> aClass = Class.forName("com.reactnativeglobalkeyevent.ReactNativeFlipper");
66+
aClass
67+
.getMethod("initializeFlipper", Context.class, ReactInstanceManager.class)
68+
.invoke(null, context, reactInstanceManager);
69+
} catch (ClassNotFoundException e) {
70+
e.printStackTrace();
71+
} catch (NoSuchMethodException e) {
72+
e.printStackTrace();
73+
} catch (IllegalAccessException e) {
74+
e.printStackTrace();
75+
} catch (InvocationTargetException e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
}
80+
}

fixtures/MainApplication.kt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.reactnativeglobalkeyevent
2+
3+
import android.app.Application
4+
import android.content.res.Configuration
5+
6+
import com.facebook.react.PackageList
7+
import com.facebook.react.ReactApplication
8+
import com.facebook.react.ReactNativeHost
9+
import com.facebook.react.ReactPackage
10+
import com.facebook.react.ReactHost
11+
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
12+
import com.facebook.react.defaults.DefaultReactNativeHost
13+
import com.facebook.soloader.SoLoader
14+
15+
import expo.modules.ApplicationLifecycleDispatcher
16+
import expo.modules.ReactNativeHostWrapper
17+
18+
class MainApplication : Application(), ReactApplication {
19+
20+
override val reactNativeHost: ReactNativeHost = ReactNativeHostWrapper(
21+
this,
22+
object : DefaultReactNativeHost(this) {
23+
override fun getPackages(): List<ReactPackage> {
24+
// Packages that cannot be autolinked yet can be added manually here, for example:
25+
// packages.add(new MyReactNativePackage());
26+
return PackageList(this).packages
27+
}
28+
29+
override fun getJSMainModuleName(): String = ".expo/.virtual-metro-entry"
30+
31+
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
32+
33+
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
34+
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
35+
}
36+
)
37+
38+
override val reactHost: ReactHost
39+
get() = ReactNativeHostWrapper.createReactHost(applicationContext, reactNativeHost)
40+
41+
override fun onCreate() {
42+
super.onCreate()
43+
SoLoader.init(this, false)
44+
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
45+
// If you opted-in for the New Architecture, we load the native entry point for this app.
46+
load()
47+
}
48+
ApplicationLifecycleDispatcher.onApplicationCreate(this)
49+
}
50+
51+
override fun onConfigurationChanged(newConfig: Configuration) {
52+
super.onConfigurationChanged(newConfig)
53+
ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig)
54+
}
55+
}

fixtures/getFixtures.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import path from "path";
22
import fs from "fs";
33

4+
type FixtureFileName =
5+
| "AppDelegate.mm"
6+
| "app-build.gradle"
7+
| "build.gradle"
8+
| "Podfile"
9+
| "MainActivity.java"
10+
| "MainApplication.java"
11+
| "MainActivity.kt"
12+
| "MainApplication.kt";
13+
414
export function getFixture(
5-
name: "AppDelegate.mm" | "app-build.gradle" | "build.gradle" | "Podfile"
15+
name: FixtureFileName,
616
): string {
717
const filepath = path.join(__dirname, name);
818
return fs.readFileSync(filepath, "utf8");

0 commit comments

Comments
 (0)