현재 맡은 프로젝트가 테이블에 들어가는 앱인데,
해당 앱은 제조사가 모두 본인들의 splash screen 을 사용하고 싶어했다.
여러 라이브러리를 찾아보던중, 그냥 기존에 쓰던 splash screen 을 patch-package 해서 사용하기로했다.
SplashScreen.java
package org.devio.rn.splashscreen;
import android.app.Activity;
import android.app.Dialog;
import android.os.Build;
import android.view.WindowManager;
import android.util.Log;
import android.content.Context;
import android.content.SharedPreferences;
import com.facebook.react.bridge.ReactApplicationContext;
import java.lang.ref.WeakReference;
/**
* SplashScreen
* 启动屏
* Author:CrazyCodeBoy
* Email:crazycodeboy@gmail.com
*/
public class SplashScreen {
private static Dialog mSplashDialog;
private static WeakReference<Activity> mActivity;
/**
* 打开启动屏
*/
public static void show(final Activity activity, final int themeResId, final boolean fullScreen, final String code ) {
if (activity == null) return;
mActivity = new WeakReference<Activity>(activity);
Context context = activity.getApplicationContext();
SharedPreferences sharedPref = context.getSharedPreferences(
"splash", Context.MODE_PRIVATE);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (!activity.isFinishing()) {
mSplashDialog = new Dialog(activity, themeResId);
mSplashDialog.setContentView(R.layout.launch_screen);
if(String.valueOf(code).equals("L")){ //LG
mSplashDialog.setContentView(R.layout.launch_screen_lg);
}
if(String.valueOf(code).equals("S")){ //SK
mSplashDialog.setContentView(R.layout.launch_screen_sk);
}
if(String.valueOf(code).equals("D")){ //D
mSplashDialog.setContentView(R.layout.launch_screen_d);
}
if(String.valueOf(code).equals("B")){ //Beaver
mSplashDialog.setContentView(R.layout.launch_screen);
}
mSplashDialog.setCancelable(false);
if (fullScreen) {
setActivityAndroidP(mSplashDialog);
}
if (!mSplashDialog.isShowing()) {
mSplashDialog.show();
}
}
}
});
}
/**
* 打开启动屏
*/
public static void show(final Activity activity, final boolean fullScreen,final String code) {
int resourceId = fullScreen ? R.style.SplashScreen_Fullscreen : R.style.SplashScreen_SplashTheme;
show(activity, resourceId, fullScreen, code);
}
/**
* 打开启动屏
*/
public static void show(final Activity activity,final String code) {
show(activity, false,code);
}
/**
* 关闭启动屏
*/
public static void hide(Activity activity) {
if (activity == null) {
if (mActivity == null) {
return;
}
activity = mActivity.get();
}
if (activity == null) return;
final Activity _activity = activity;
_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mSplashDialog != null && mSplashDialog.isShowing()) {
boolean isDestroyed = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
isDestroyed = _activity.isDestroyed();
}
if (!_activity.isFinishing() && !isDestroyed) {
mSplashDialog.dismiss();
}
mSplashDialog = null;
}
}
});
}
private static void setActivityAndroidP(Dialog dialog) {
//设置全屏展示
if (Build.VERSION.SDK_INT >= 28) {
if (dialog != null && dialog.getWindow() != null) {
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);//全屏显示
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
dialog.getWindow().setAttributes(lp);
}
}
}
}
해당 java 중 show function 을 수정했다.
기본 앱이 react native 이기때문에, 안드로이드와 가장 가볍게 쓸수있는 데이터 저장소를 SharedPreference 로 정하고,
api 로 받아온 스플래시 스크린의 구분자를 SharedPreference 로 저장해주고,
해당 값에 따라
SharedPreferences sharedPreferences = getSharedPreferences("splash", 0);
String result = sharedPreferences.getString("splash", "");
SplashScreen.show(this,result);
show 해주는 식으로 작성했다.
서버에서 데이터 값을 받아와서 브릿지를 통해 안드로이드의 SharedPreference에 저장하는 방법은
const updateSplashCode = React.useCallback(() => {
RNSharedPreferences.getString('splash', 'splash', (result) => {
if (result !== SPLASH_CODE) {
RNSharedPreferences.putString(
'splash',
'splash',
SPLASH_CODE,
// eslint-disable-next-line @typescript-eslint/no-empty-function
(_result) => {},
);
}
});
}, [SPLASH_CODE]);
해당 방법으로 해결했으며
android 의 sharedPreference 에 접근하는 방법은
RNHaredPreferencesPagkage.java
package com.bw_frontend_table_order_app.sharedPreference;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;
public class RNSharedPreferencesPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new RNSharedPreferencesModule(reactContext));
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
RNSharedPreferencesModul.java
package com.bw_frontend_table_order_app.sharedPreference;
import android.content.SharedPreferences;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
public class RNSharedPreferencesModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
public RNSharedPreferencesModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "RNSharedPreferences";
}
@ReactMethod
public void getString(String prefName, String keyName, Callback resultCallback) {
SharedPreferences sharedPreferences = this.reactContext.getSharedPreferences(prefName, 0);
String result = sharedPreferences.getString(keyName, "");
resultCallback.invoke(result);
}
@ReactMethod
public void putString(String prefName, String keyName, String value, Callback resultCallback) {
SharedPreferences sharedPreferences = this.reactContext.getSharedPreferences(prefName, 0);
boolean commitResult = sharedPreferences.edit().putString(keyName, value).commit();
resultCallback.invoke(commitResult);
}
}
로 작성했고
const { RNSharedPreferences } = NativeModules;
로 해결했다.