Skip to content

Commit 71496c3

Browse files
committed
native code refactoring
1 parent 8e734f4 commit 71496c3

5 files changed

Lines changed: 168 additions & 108 deletions

File tree

android/src/main/java/com/bebnev/RNUserAgentModule.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,73 +15,87 @@
1515
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1616
import com.facebook.react.bridge.ReactMethod;
1717
import com.facebook.react.bridge.WritableArray;
18+
import com.facebook.react.module.annotations.ReactModule;
19+
import com.facebook.react.bridge.UiThreadUtil;
1820

1921
import java.util.ArrayList;
2022
import java.util.HashMap;
2123
import java.util.Map;
24+
import java.lang.Runtime;
2225

26+
@ReactModule(name = RNUserAgentModule.NAME)
2327
public class RNUserAgentModule extends ReactContextBaseJavaModule {
24-
25-
private final ReactApplicationContext reactContext;
28+
public static final String NAME = "RNUserAgent";
2629

2730
public RNUserAgentModule(ReactApplicationContext reactContext) {
2831
super(reactContext);
29-
this.reactContext = reactContext;
3032
}
3133

3234
@Override
3335
public String getName() {
34-
return "RNUserAgent";
36+
return NAME;
3537
}
3638

3739
protected String getUserAgent() {
38-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
40+
try {
41+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
42+
return WebSettings.getDefaultUserAgent(getReactApplicationContext());
43+
} else {
44+
return System.getProperty("http.agent");
45+
}
46+
} catch (RuntimeException e) {
3947
return System.getProperty("http.agent");
4048
}
41-
42-
return "";
4349
}
4450

45-
protected String getWebViewUserAgent() {
46-
if (Build.VERSION.SDK_INT >= 17) {
47-
return WebSettings.getDefaultUserAgent(this.reactContext);
51+
@ReactMethod
52+
protected void getWebViewUserAgent(final Promise p) {
53+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
54+
p.resolve(WebSettings.getDefaultUserAgent(getReactApplicationContext()));
4855
}
4956

50-
return new WebView(this.reactContext).getSettings().getUserAgentString();
57+
UiThreadUtil.runOnUiThread(
58+
new Runnable() {
59+
@Override
60+
public void run() {
61+
p.resolve(new WebView(getReactApplicationContext()).getSettings().getUserAgentString());
62+
}
63+
}
64+
);
65+
}
66+
67+
private PackageInfo getPackageInfo() throws Exception {
68+
return getReactApplicationContext().getPackageManager().getPackageInfo(getReactApplicationContext().getPackageName(), 0);
5169
}
5270

5371
@Override
5472
public Map<String, Object> getConstants() {
55-
HashMap<String, Object> constants = new HashMap<String, Object>();
56-
57-
PackageManager packageManager = this.reactContext.getPackageManager();
58-
String packageName = this.reactContext.getPackageName();
73+
String packageName = getReactApplicationContext().getPackageName();
5974
String shortPackageName = packageName.substring(packageName.lastIndexOf(".") + 1);
60-
String applicationName = "";
6175
String applicationVersion = "";
62-
Integer buildNumber = 0;
63-
String userAgent = this.getUserAgent();
76+
String applicationName = "";
77+
String buildNumber = "";
78+
String userAgent = "";
6479

6580
try {
66-
PackageInfo info = packageManager.getPackageInfo(packageName, 0);
67-
applicationName = this.reactContext.getApplicationInfo().loadLabel(this.reactContext.getPackageManager()).toString();
68-
applicationVersion = info.versionName;
69-
buildNumber = info.versionCode;
70-
userAgent = shortPackageName + '/' + applicationVersion + '.' + buildNumber.toString() + ' ' + userAgent;
71-
72-
} catch(PackageManager.NameNotFoundException e) {
81+
applicationName = getReactApplicationContext().getApplicationInfo().loadLabel(getReactApplicationContext().getPackageManager()).toString();
82+
applicationVersion = getPackageInfo().versionName;
83+
buildNumber = Integer.toString(getPackageInfo().versionCode);
84+
userAgent = shortPackageName + '/' + applicationVersion + '.' + buildNumber.toString() + ' ' + this.getUserAgent();
85+
} catch(Exception e) {
7386
e.printStackTrace();
7487
}
7588

89+
HashMap<String, Object> constants = new HashMap<String, Object>();
90+
7691
constants.put("systemName", "Android");
7792
constants.put("systemVersion", Build.VERSION.RELEASE);
7893
constants.put("packageName", packageName);
7994
constants.put("shortPackageName", shortPackageName);
8095
constants.put("applicationName", applicationName);
8196
constants.put("applicationVersion", applicationVersion);
82-
constants.put("applicationBuildNumber", buildNumber);
97+
constants.put("buildNumber", buildNumber);
8398
constants.put("userAgent", userAgent);
84-
constants.put("webViewUserAgent", this.getWebViewUserAgent());
8599

86100
return constants;
87101
}

index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ const { RNUserAgent } = NativeModules;
55

66
module.exports = {
77
...RNUserAgent,
8-
getUserAgent() {
8+
getUserAgent: () => {
99
return RNUserAgent.userAgent;
10-
},
11-
getWebViewUserAgent() {
12-
return RNUserAgent.webViewUserAgent;
13-
},
10+
}
1411
};
1512

ios/RNUserAgent.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
#import <sys/utsname.h>
3-
#import <UIKit/UIKit.h>
43

54
#if __has_include("RCTBridgeModule.h")
65
#import "RCTBridgeModule.h"
@@ -9,5 +8,4 @@
98
#endif
109

1110
@interface RNUserAgent : NSObject <RCTBridgeModule>
12-
@property (nonatomic) bool isEmulator;
1311
@end

0 commit comments

Comments
 (0)