Commit 8cd63a9a authored by Nicolas's avatar Nicolas Committed by Mehdi Achour

feat: add getInstallReferrer() for Android (#344)

parent d59cbe7e
......@@ -209,6 +209,7 @@ var DeviceInfo = require('react-native-device-info');
| [getFontScale()](#getfontscale) | `number` | ✅ | ✅ | ❌ | 0.15.0 |
| [getFreeDiskStorage()](#getfreediskstorage) | `number` | ✅ | ✅ | ❌ | 0.15.0 |
| [getIPAddress()](#getipaddress) | `Promise<string>` | ❌ | ✅ | ❌ | 0.12.0 |
| [getInstallReferrer()](#getinstallreferrer) | `string` | ❌ | ✅ | ❌ | 0.19.0 |
| [getInstanceID()](#getinstanceid) | `string` | ❌ | ✅ | ❌ | ? |
| [getLastUpdateTime()](#getlastupdatetime) | `number` | ❌ | ✅ | ❌ | 0.12.0 |
| [getMACAddress()](#getmacaddress) | `Promise<string>` | ❌ | ✅ | ❌ | 0.12.0 |
......@@ -480,6 +481,21 @@ DeviceInfo.getIPAddress().then(ip => {
---
### getInstallReferrer
Gets the referrer string upon application installation.
**Examples**
```js
const referrer = DeviceInfo.getInstallReferrer();
// If the app was installed from https://play.google.com/store/apps/details?id=com.myapp&referrer=my_install_referrer
// the result will be "my_install_referrer"
```
---
### getInstanceID()
Gets the application instance ID.
......
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnium.RNDeviceInfo">
<application>
<receiver
android:name="com.learnium.RNDeviceInfo.RNDeviceReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
</application>
</manifest>
......@@ -4,6 +4,7 @@ import android.Manifest;
import android.app.KeyguardManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
......@@ -207,6 +208,11 @@ public class RNDeviceModule extends ReactContextBaseJavaModule {
p.resolve(batteryLevel);
}
public String getInstallReferrer() {
SharedPreferences sharedPref = getReactApplicationContext().getSharedPreferences("react-native-device-info", Context.MODE_PRIVATE);
return sharedPref.getString("installReferrer", null);
}
@Override
public @Nullable
Map<String, Object> getConstants() {
......@@ -291,6 +297,7 @@ public class RNDeviceModule extends ReactContextBaseJavaModule {
constants.put("carrier", this.getCarrier());
constants.put("totalDiskCapacity", this.getTotalDiskCapacity());
constants.put("freeDiskStorage", this.getFreeDiskStorage());
constants.put("installReferrer", this.getInstallReferrer());
Runtime rt = Runtime.getRuntime();
constants.put("maxMemory", rt.maxMemory());
......
package com.learnium.RNDeviceInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class RNDeviceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.android.vending.INSTALL_REFERRER")) {
SharedPreferences sharedPref = context.getSharedPreferences("react-native-device-info", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("installReferrer", intent.getStringExtra("referrer"));
editor.commit();
}
}
}
......@@ -94,6 +94,9 @@ module.exports = {
getFirstInstallTime: function() {
return RNDeviceInfo.firstInstallTime;
},
getInstallReferrer: function() {
return RNDeviceInfo.installReferrer;
},
getLastUpdateTime: function() {
return RNDeviceInfo.lastUpdateTime;
},
......
......@@ -30,6 +30,7 @@ module.exports = {
is24Hour: false,
isPinOrFingerprintSet: callback => callback && callback(false),
firstInstallTime: 0,
installReferrer: '',
lastUpdateTime: 0,
phoneNumber: '',
carrier: '',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment