Skip to content
This repository has been archived by the owner on May 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #39 from amplitude/chores/add-missing-device-data-wip
Browse files Browse the repository at this point in the history
Chores/add missing device data
  • Loading branch information
haoliu-amp authored Nov 26, 2019
2 parents 207dd8f + 4f06fdf commit 0b830ac
Show file tree
Hide file tree
Showing 22 changed files with 269 additions and 45 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.1.0
* add `device_manufacturer` info
* add ability to retrieve carrier info
* add config option to turn on and off retrieving carrier info

## 1.0.1

* Wait for device info to be available before sending events
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@ Find the [API key](https://amplitude.zendesk.com/hc/en-us/articles/235649848-Set

Import `package:amplitude_flutter/amplitude_flutter.dart`, and instantiate `AmplitudeFlutter` with your API key.

In addition, a [`Config`](https://github.com/amplitude/Amplitude-Flutter/blob/master/lib/src/config.dart) object can be passed as a constructor argument for additional options.
In the example below - replace the string `API_KEY` with your API Key.

In addition, a [`Config`](https://github.com/amplitude/Amplitude-Flutter/blob/master/lib/src/config.dart) object can be passed as a constructor argument for additional options.
NOTE: This plugin's methods should only be called from the main isolate.

### Adding Carrier Information
You can set an option in the config object titled `getCarrierInfo` to retreive carrier name for a device. This is the ( [`Config`](https://github.com/amplitude/Amplitude-Flutter/blob/master/lib/src/config.dart)). This object can be passed as a constructor argument for additional options.

If you set `getCarrierInfo` to `true` - **recipients on Android devices will see a dialog box asking them for perimssion** . This dialog will say
`allow app to make and manage phone calls`. This is a message sent from the android operating system for the `READ_PHONE_STATE` permission and carrier info is grouped into this.
If the user denies permission - carrier information will not be retrieved. The new android operating systems require asking a user for permission before retrieving this information.

By default the Config will set `getCarrierInfo` will default to false.

And example can be found here [`Example`](https://github.com/amplitude/Amplitude-Flutter/blob/chores/add-missing-device-data-wip/example/lib/my_app.dart#L30).

### Example

```dart
import 'package:amplitude_flutter/amplitude_flutter.dart';
// replace 'API_KEY' with your project's API_KEY
Future<void> example() async {
final AmplitudeFlutter analytics = AmplitudeFlutter('API KEY');
Expand Down
7 changes: 6 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.android.tools.build:gradle:3.3.0'
}
}

Expand All @@ -32,3 +32,8 @@ android {
disable 'InvalidPackage'
}
}

dependencies {
implementation 'android.arch.lifecycle:common-java8:1.1.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
}
1 change: 1 addition & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amplitude.amplitude_flutter">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,25 +1,112 @@
package com.amplitude.amplitude_flutter;

import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.app.ActivityCompat;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import android.telephony.TelephonyManager;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

/** AmplitudeFlutterPlugin */
public class AmplitudeFlutterPlugin implements MethodCallHandler {
private final Activity mActivity;
String carrierName;
private final TelephonyManager mTelephonyManager;
private Result mResult;
boolean permissionGranted;
private static int READ_PHONE_STATE = 123;

private AmplitudeFlutterPlugin(Activity activity) {
this.mActivity = activity;
mTelephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
}

/** Plugin registration. */
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "amplitude_flutter");
channel.setMethodCallHandler(new AmplitudeFlutterPlugin());
final AmplitudeFlutterPlugin amplitudeFlutterPlugin = new AmplitudeFlutterPlugin(registrar.activity());
channel.setMethodCallHandler(amplitudeFlutterPlugin);

/** Adds a callback allowing the plugin to take part in handling incoming calls
* to Activity#onRequestPermissionsResult(int, String[], int[])
**/
registrar.addRequestPermissionsResultListener(new PluginRegistry.RequestPermissionsResultListener() {
@Override
public boolean onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
boolean permissionGranted;
if ((requestCode == READ_PHONE_STATE) && (grantResults.length > 0)
&& (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
permissionGranted = true;
} else {
permissionGranted = false;

}
amplitudeFlutterPlugin.processCarrierResult(permissionGranted);
return permissionGranted;
}
});
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
mResult = result;
if (call.method.equals("carrierName")) {
if (Build.VERSION.SDK_INT >= 23) {
getCarrierInfoWithPermissions(mResult);
} else {
processCarrierResult(true);
}
} else {
result.notImplemented();
}
}

private void getCarrierInfoWithPermissions(Result result) {
boolean permissionGranted;
permissionGranted = checkAlreadyExistingPermission();
if (permissionGranted) {
carrierName = getCarrierName();
result.success(carrierName);
} else {
ActivityCompat.requestPermissions(this.mActivity, new String[] { Manifest.permission.READ_PHONE_STATE },
READ_PHONE_STATE);
}
}

private boolean checkAlreadyExistingPermission() {
int result = ContextCompat.checkSelfPermission(this.mActivity, Manifest.permission.READ_PHONE_STATE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}

private void processCarrierResult(boolean permission) {
if (permission) {
String name = getCarrierName();
mResult.success(name);
} else {
mResult.error("PERMISSION_DENIED", "PERMISSION_DENIED", null);
}
mResult = null;
}

private String getCarrierName() {
String networkOperatorName = mTelephonyManager.getNetworkOperatorName();
if (networkOperatorName == null) {
networkOperatorName = "null";
}
return networkOperatorName;
}
}
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.android.tools.build:gradle:3.3.0'
}
}

Expand Down
4 changes: 3 additions & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
platform :ios, '9.0'
use_frameworks!

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down Expand Up @@ -64,6 +65,7 @@ post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
config.build_settings['SWIFT_VERSION'] = '4.0'
end
end
end
6 changes: 4 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ PODS:
DEPENDENCIES:
- amplitude_flutter (from `.symlinks/plugins/amplitude_flutter/ios`)
- device_info (from `.symlinks/plugins/device_info/ios`)
- flt_telephony_info (from `.symlinks/plugins/flt_telephony_info/ios`)
- Flutter (from `.symlinks/flutter/ios`)
- package_info (from `.symlinks/plugins/package_info/ios`)
- sqflite (from `.symlinks/plugins/sqflite/ios`)
Expand All @@ -39,11 +40,12 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
amplitude_flutter: 751cb63e6431fd950b5aa91a0ad084f1d13bc744
device_info: 3ebad48f726348f69abd802f3334a8d1ed795fbd
Flutter: 58dd7d1b27887414a370fcccb9e645c08ffd7a6a
flt_telephony_info: effecf5e0b0dc252e6022077cca1d473fce831f6
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
package_info: 78cabb3c322943c55d39676f4a5bfc748c01d055
sqflite: ff1d9da63c06588cc8d1faf7256d741f16989d5a

PODFILE CHECKSUM: aff02bfeed411c636180d6812254b2daeea14d09

COCOAPODS: 1.6.1
COCOAPODS: 1.8.4
3 changes: 1 addition & 2 deletions example/lib/my_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
String _message = '';
AmplitudeFlutter analytics;

@override
void initState() {
super.initState();
analytics = AmplitudeFlutter(widget.apiKey, Config(bufferSize: 8));
analytics = AmplitudeFlutter(widget.apiKey, Config(bufferSize: 8, getCarrierInfo: false));
analytics.logEvent(name: 'MyApp startup');
}

Expand Down
22 changes: 11 additions & 11 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
amplitude_flutter:
dependency: "direct dev"
description:
path: ".."
relative: true
source: path
version: "1.0.1"
version: "1.1.0"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.3.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "1.0.5"
charcode:
dependency: transitive
description:
Expand Down Expand Up @@ -101,7 +101,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
version: "1.1.7"
package_info:
dependency: transitive
description:
Expand All @@ -115,21 +115,21 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.2"
version: "1.6.4"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
version: "1.8.0+1"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "2.0.5"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -169,7 +169,7 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "1.0.5"
synchronized:
dependency: transitive
description:
Expand All @@ -190,7 +190,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.4"
version: "0.2.5"
typed_data:
dependency: transitive
description:
Expand All @@ -213,5 +213,5 @@ packages:
source: hosted
version: "2.0.8"
sdks:
dart: ">=2.2.0 <3.0.0"
dart: ">=2.2.2 <3.0.0"
flutter: ">=1.2.1 <2.0.0"
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: amplitude_flutter_example
description: Demonstrates how to use the amplitude_flutter plugin.
publish_to: 'none'
version: 0.12.3
version: 0.12.4

environment:
sdk: ">=2.1.0 <3.0.0"
Expand All @@ -20,4 +20,4 @@ dev_dependencies:
path: ../

flutter:
uses-material-design: true
uses-material-design: true
29 changes: 27 additions & 2 deletions ios/Classes/AmplitudeFlutterPlugin.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#import "AmplitudeFlutterPlugin.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
#import <sys/utsname.h>

@implementation AmplitudeFlutterPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
Expand All @@ -10,11 +13,33 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"getPlatformVersion" isEqualToString:call.method]) {
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
if ([@"carrierName" isEqualToString:call.method]) {
NSString *strNative = [self carrierName];
result(strNative);
} else if ([@"deviceModel" isEqualToString:call.method]) {
NSString *strNative = [self deviceName];
result(strNative);
} else {
result(FlutterMethodNotImplemented);
}
}

- (NSString*)carrierName {
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netinfo subscriberCellularProvider];
NSString* name = [carrier carrierName];
if (name != nil) {
return name;
} else {
return @"SIM State not available";
}
}

- (NSString*)deviceName{
struct utsname systemInfo;
uname(&systemInfo);

return [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
}
@end
Loading

0 comments on commit 0b830ac

Please sign in to comment.