From e38af08efdec84d8c4d9fad626cbaff6d633d0d3 Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Sat, 23 Sep 2017 22:37:31 +0300 Subject: [PATCH] Add support for Android --- android/build.gradle | 22 +++++ android/src/main/AndroidManifest.xml | 3 + .../AppMetrica/AppMetricaModule.java | 92 +++++++++++++++++++ .../AppMetrica/AppMetricaPackage.java | 25 +++++ 4 files changed, 142 insertions(+) create mode 100644 android/build.gradle create mode 100644 android/src/main/AndroidManifest.xml create mode 100644 android/src/main/java/com/aandrosov/AppMetrica/AppMetricaModule.java create mode 100644 android/src/main/java/com/aandrosov/AppMetrica/AppMetricaPackage.java diff --git a/android/build.gradle b/android/build.gradle new file mode 100644 index 0000000..1c5f5fa --- /dev/null +++ b/android/build.gradle @@ -0,0 +1,22 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.1" + + defaultConfig { + minSdkVersion 16 + targetSdkVersion 22 + versionCode 1 + versionName "1.0" + } + + lintOptions { + warning 'InvalidPackage' + } +} + +dependencies { + compile 'com.facebook.react:react-native:+' + compile 'com.yandex.android:mobmetricalib:2.76' +} \ No newline at end of file diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..d8b1766 --- /dev/null +++ b/android/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/android/src/main/java/com/aandrosov/AppMetrica/AppMetricaModule.java b/android/src/main/java/com/aandrosov/AppMetrica/AppMetricaModule.java new file mode 100644 index 0000000..c96ccb8 --- /dev/null +++ b/android/src/main/java/com/aandrosov/AppMetrica/AppMetricaModule.java @@ -0,0 +1,92 @@ +package com.aandrosov.AppMetrica; + +import android.app.Activity; +import android.app.Application; +import android.support.annotation.Nullable; +import android.util.Log; + +import com.facebook.react.ReactApplication; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.ReadableMapKeySetIterator; +import com.facebook.react.modules.core.DeviceEventManagerModule; + +import java.util.HashMap; +import java.util.Map; +import java.lang.Exception; + +import org.json.JSONObject; + +import com.yandex.metrica.YandexMetrica; + +public class AppMetricaModule extends ReactContextBaseJavaModule { + final static String ModuleName = "AppMetrica"; + + public AppMetricaModule(ReactApplicationContext reactContext) { + super(reactContext); + } + + @Override + public String getName() { + return ModuleName; + } + + + @ReactMethod + public void activateWithApiKey(String key) { + YandexMetrica.activate(getReactApplicationContext().getApplicationContext(), key); + + Activity activity = getCurrentActivity(); + Application application = activity.getApplication(); + YandexMetrica.enableActivityAutoTracking(application); + } + + @ReactMethod + public void reportEvent(String message, @Nullable ReadableMap params) { + if (params != null) { + YandexMetrica.reportEvent(message, convertReadableMapToJson(params)); + } else { + YandexMetrica.reportEvent(message); + } + } + + private String convertReadableMapToJson(final ReadableMap readableMap) { + ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); + JSONObject json = new JSONObject(); + + try { + while (iterator.hasNextKey()) { + String key = iterator.nextKey(); + + switch (readableMap.getType(key)) { + case Null: + json.put(key, null); + break; + case Boolean: + json.put(key, readableMap.getBoolean(key)); + break; + case Number: + json.put(key, readableMap.getDouble(key)); + break; + case String: + json.put(key, readableMap.getString(key)); + break; + case Array: + json.put(key, readableMap.getArray(key)); + break; + case Map: + json.put(key, convertReadableMapToJson(readableMap.getMap(key))); + break; + default: + break; + } + } + } catch (Exception ex) { + Log.d(ModuleName, "convertReadableMapToJson fail: " + ex); + } + + return json.toString(); + } +} diff --git a/android/src/main/java/com/aandrosov/AppMetrica/AppMetricaPackage.java b/android/src/main/java/com/aandrosov/AppMetrica/AppMetricaPackage.java new file mode 100644 index 0000000..a4fd7ca --- /dev/null +++ b/android/src/main/java/com/aandrosov/AppMetrica/AppMetricaPackage.java @@ -0,0 +1,25 @@ +package com.aandrosov.AppMetrica; + +import com.facebook.react.ReactPackage; +import com.facebook.react.bridge.JavaScriptModule; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.uimanager.ViewManager; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class AppMetricaPackage implements ReactPackage { + @Override + public List createNativeModules(ReactApplicationContext reactContext) { + return Arrays.asList( + new AppMetricaModule(reactContext) + ); + } + + @Override + public List createViewManagers(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } +}