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 #45 from amplitude/use-detached
Browse files Browse the repository at this point in the history
Migrate from AppLifecycleState.suspending to AppLifecycleState.detached & fix the crash triggered by permission from other libraries
  • Loading branch information
haoliu-amp authored Dec 12, 2019
2 parents 59fd3f7 + a5e820c commit 871faa1
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 30 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.1
* Migrated from AppLifecycleState.suspending to AppLifecycleState.detached. Cause suspending is
deprecated. Thanks @otopba for fixing it.
* Fixed the possible crash while other permission is requested by other libraries.

## 1.1.0
* add `device_manufacturer` info
* add ability to retrieve carrier info
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
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 android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.TelephonyManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import io.flutter.plugin.common.MethodCall;
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 Activity mActivity;
private final TelephonyManager mTelephonyManager;
private Result mResult;
boolean permissionGranted;
private static int READ_PHONE_STATE = 123;

private AmplitudeFlutterPlugin(Activity activity) {
Expand All @@ -43,16 +43,14 @@ public static void registerWith(Registrar registrar) {
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;
if (requestCode == READ_PHONE_STATE) {
boolean permissionGranted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
amplitudeFlutterPlugin.processCarrierResult(permissionGranted);
return true;
} else {
permissionGranted = false;

// We don't care other permissions other than READ_PHONE_STATE
return false;
}
amplitudeFlutterPlugin.processCarrierResult(permissionGranted);
return permissionGranted;
}
});
}
Expand All @@ -69,6 +67,7 @@ public void onMethodCall(MethodCall call, Result result) {
} else {
result.notImplemented();
}
mResult = null;
}

private void getCarrierInfoWithPermissions(Result result) {
Expand All @@ -93,6 +92,12 @@ private boolean checkAlreadyExistingPermission() {
}

private void processCarrierResult(boolean permission) {
// We only proceed only when permission is triggered when `device` method is called.
// That means mResult is not null.
if (mResult == null) {
return;
}

if (permission) {
String name = getCarrierName();
mResult.success(name);
Expand Down
3 changes: 3 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

# Flutter related
.flutter-plugins-dependencies
3 changes: 3 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
1 change: 1 addition & 0 deletions example/lib/app_state.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:amplitude_flutter/amplitude_flutter.dart';

Expand Down
49 changes: 42 additions & 7 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@ packages:
path: ".."
relative: true
source: path
version: "1.1.0"
version: "1.1.1"
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
version: "2.4.0"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -49,7 +63,7 @@ packages:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
version: "2.1.3"
cupertino_icons:
dependency: "direct main"
description:
Expand Down Expand Up @@ -88,20 +102,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.5"
version: "0.12.6"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.7"
version: "1.1.8"
package_info:
dependency: transitive
description:
Expand All @@ -123,6 +144,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0+1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
quiver:
dependency: transitive
description:
Expand Down Expand Up @@ -190,7 +218,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.5"
version: "0.2.11"
typed_data:
dependency: transitive
description:
Expand All @@ -212,6 +240,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.0"
sdks:
dart: ">=2.2.2 <3.0.0"
dart: ">=2.4.0 <3.0.0"
flutter: ">=1.2.1 <2.0.0"
2 changes: 1 addition & 1 deletion lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Session with WidgetsBindingObserver {
switch (state) {
case AppLifecycleState.inactive:
case AppLifecycleState.paused:
case AppLifecycleState.suspending:
case AppLifecycleState.detached:
enterBackground();
break;
case AppLifecycleState.resumed:
Expand Down
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: amplitude_flutter
description: Official Flutter plugin for tracking events and revenue to Amplitude.
version: 1.1.0
author: Amplitude <platform@amplitude.com>
version: 1.1.1
homepage: https://github.com/amplitude/Amplitude-Flutter

environment:
Expand Down
2 changes: 2 additions & 0 deletions test/sim_info_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;

final MethodChannel channel = MethodChannel('amplitude_flutter');

// Register the mock handler.
Expand Down

0 comments on commit 871faa1

Please sign in to comment.