This repository has been archived by the owner on May 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from amplitude/chores/add-missing-device-data-wip
Chores/add missing device data
- Loading branch information
Showing
22 changed files
with
269 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
93 changes: 90 additions & 3 deletions
93
android/src/main/java/com/amplitude/amplitude_flutter/AmplitudeFlutterPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.