diff --git a/android/app/build.gradle b/android/app/build.gradle index ad02d127952..9566efd06a1 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -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' diff --git a/android/app/src/main/java/com/mattermost/rnbeta/MainApplication.java b/android/app/src/main/java/com/mattermost/rnbeta/MainApplication.java index 665001cccc4..4a0850ff7db 100644 --- a/android/app/src/main/java/com/mattermost/rnbeta/MainApplication.java +++ b/android/app/src/main/java/com/mattermost/rnbeta/MainApplication.java @@ -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; @@ -86,7 +87,8 @@ public List createAdditionalReactPackages() { new InitializationPackage(this), new RNRecyclerviewListPackage(), new RNCWebViewPackage(), - new RNGestureHandlerPackage() + new RNGestureHandlerPackage(), + new UserAgentPackage() ); } diff --git a/android/app/src/main/java/com/mattermost/rnbeta/UserAgentModule.java b/android/app/src/main/java/com/mattermost/rnbeta/UserAgentModule.java new file mode 100644 index 00000000000..47b7f7d0011 --- /dev/null +++ b/android/app/src/main/java/com/mattermost/rnbeta/UserAgentModule.java @@ -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 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 getConstants() { + HashMap constants = new HashMap(); + + 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; + } +} diff --git a/android/app/src/main/java/com/mattermost/rnbeta/UserAgentPackage.java b/android/app/src/main/java/com/mattermost/rnbeta/UserAgentPackage.java new file mode 100644 index 00000000000..ac4ee5727c7 --- /dev/null +++ b/android/app/src/main/java/com/mattermost/rnbeta/UserAgentPackage.java @@ -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 createNativeModules(ReactApplicationContext reactContext) { + return Arrays.asList(new UserAgentModule(reactContext)); + } + + @Override + public List createViewManagers(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } +} diff --git a/android/settings.gradle b/android/settings.gradle index b3afe985e21..10030650429 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -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') diff --git a/app/screens/select_server/select_server.js b/app/screens/select_server/select_server.js index 01579b99636..3f5ac802312 100644 --- a/app/screens/select_server/select_server.js +++ b/app/screens/select_server/select_server.js @@ -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; diff --git a/app/screens/sso/sso.js b/app/screens/sso/sso.js index eaa7a33a512..abfa84cdaad 100644 --- a/app/screens/sso/sso.js +++ b/app/screens/sso/sso.js @@ -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', @@ -222,6 +223,7 @@ class SSO extends PureComponent { } else { content = (