Skip to content

Commit

Permalink
add user agent, server url
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelborzikov committed Feb 12, 2019
1 parent b9f55dc commit ae910f9
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ dependencies {
implementation project(':react-native-recyclerview-list')
implementation project(':react-native-webview')
implementation project(':react-native-gesture-handler')
implementation project(':react-native-user-agent')

// For animated GIF support
implementation 'com.facebook.fresco:fresco:1.10.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.wix.reactnativenotifications.core.AppLaunchHelper;
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
import com.wix.reactnativenotifications.core.JsIOHelper;
import com.mattermost.rnbeta.UserAgentPackage;

import android.util.Log;

Expand Down Expand Up @@ -86,7 +87,8 @@ public List<ReactPackage> createAdditionalReactPackages() {
new InitializationPackage(this),
new RNRecyclerviewListPackage(),
new RNCWebViewPackage(),
new RNGestureHandlerPackage()
new RNGestureHandlerPackage(),
new UserAgentPackage()
);
}

Expand Down
101 changes: 101 additions & 0 deletions android/app/src/main/java/com/mattermost/rnbeta/UserAgentModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.mattermost.rnbeta;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;
import android.util.Log;
import android.os.Build;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class UserAgentModule extends ReactContextBaseJavaModule {

private final ReactApplicationContext reactContext;

public UserAgentModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}

@Override
public String getName() {
return "RNUserAgent";
}

protected String getUserAgent() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return System.getProperty("http.agent");
}

return "";
}

protected String getWebViewUserAgent() {
if (Build.VERSION.SDK_INT >= 17) {
return WebSettings.getDefaultUserAgent(this.reactContext);
}

// try {
// Constructor<WebSettings> constructor = WebSettings.class.getDeclaredConstructor(Context.class, WebView.class);
// constructor.setAccessible(true);
// try {
// WebSettings settings = constructor.newInstance(this.reactContext, null);
// return settings.getUserAgentString();
// } finally {
// constructor.setAccessible(false);
// }
// } catch (Exception e) {
// return new WebView(this.reactContext).getSettings().getUserAgentString();
// }

return new WebView(this.reactContext).getSettings().getUserAgentString();
}

@Override
public Map<String, Object> getConstants() {
HashMap<String, Object> constants = new HashMap<String, Object>();

PackageManager packageManager = this.reactContext.getPackageManager();
String packageName = this.reactContext.getPackageName();
String shortPackageName = packageName.substring(packageName.lastIndexOf(".") + 1);
String applicationName = "";
String applicationVersion = "";
Integer buildNumber = 0;
String userAgent = this.getUserAgent();

try {
PackageInfo info = packageManager.getPackageInfo(packageName, 0);
applicationName = this.reactContext.getApplicationInfo().loadLabel(this.reactContext.getPackageManager()).toString();
applicationVersion = info.versionName;
buildNumber = info.versionCode;
userAgent = shortPackageName + '/' + applicationVersion + '.' + buildNumber.toString() + ' ' + userAgent;

} catch(PackageManager.NameNotFoundException e) {
e.printStackTrace();
}

constants.put("systemName", "Android");
constants.put("systemVersion", Build.VERSION.RELEASE);
constants.put("packageName", packageName);
constants.put("shortPackageName", shortPackageName);
constants.put("applicationName", applicationName);
constants.put("applicationVersion", applicationVersion);
constants.put("applicationBuildNumber", buildNumber);
constants.put("userAgent", userAgent);
constants.put("webViewUserAgent", this.getWebViewUserAgent());

return constants;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mattermost.rnbeta;

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 UserAgentPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new UserAgentModule(reactContext));
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
2 changes: 2 additions & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ include ':react-native-recyclerview-list'
project(':react-native-recyclerview-list').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-recyclerview-list/android')
include ':react-native-webview'
project(':react-native-webview').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-webview/android')
include ':react-native-user-agent'
project(':react-native-user-agent').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-user-agent/android')
2 changes: 1 addition & 1 deletion app/screens/select_server/select_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class SelectServer extends PureComponent {
connected: false,
connecting: false,
error: null,
url: props.serverUrl,
url: 'https://chat.fasten.com',
};

this.cancelPing = null;
Expand Down
2 changes: 2 additions & 0 deletions app/screens/sso/sso.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Loading from 'app/components/loading';
import StatusBar from 'app/components/status_bar';
import {changeOpacity, makeStyleSheetFromTheme} from 'app/utils/theme';
import tracker from 'app/utils/time_tracker';
import UserAgent from 'react-native-user-agent';

const HEADERS = {
'X-Mobile-App': 'mattermost',
Expand Down Expand Up @@ -222,6 +223,7 @@ class SSO extends PureComponent {
} else {
content = (
<WebView
userAgent={UserAgent.getUserAgent()}
ref={this.webViewRef}
source={{uri: this.loginUrl, headers: HEADERS}}
javaScriptEnabledAndroid={true}
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"react-native-slider": "0.11.0",
"react-native-status-bar-size": "0.3.3",
"react-native-svg": "8.0.8",
"react-native-user-agent": "1.0.1",
"react-native-tableview": "2.4.1",
"react-native-vector-icons": "6.1.0",
"react-native-video": "3.2.1",
Expand Down

0 comments on commit ae910f9

Please sign in to comment.