Преглед на файлове

[new]调整oaid与aaid的获取

zk преди 11 месеца
родител
ревизия
45476b1219

+ 9 - 1
android/src/main/java/com/atmob/flutter_device/FlutterDevicePlugin.java

@@ -26,6 +26,8 @@ public class FlutterDevicePlugin implements FlutterPlugin, MethodCallHandler {
 
     private Context applicationContext;
     private final String METHOD_GET_DEVICE_INFO = "getDeviceInfo";
+    private final String METHOD_GET_OAID = "getOaid";
+    private final String METHOD_GET_AAID = "getAaid";
 
     @Override
     public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
@@ -39,7 +41,13 @@ public class FlutterDevicePlugin implements FlutterPlugin, MethodCallHandler {
     public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
         switch (call.method) {
             case METHOD_GET_DEVICE_INFO:
-                FlutterDeviceUtil.getDeviceInfo(applicationContext,call, result);
+                FlutterDeviceUtil.getDeviceInfo(applicationContext, call, result);
+                break;
+            case METHOD_GET_OAID:
+                FlutterDeviceUtil.getOaid(applicationContext, call, result);
+                break;
+            case METHOD_GET_AAID:
+                FlutterDeviceUtil.getAaid(applicationContext, call, result);
                 break;
             default:
                 result.notImplemented();

+ 41 - 39
android/src/main/java/com/atmob/flutter_device/FlutterDeviceUtil.java

@@ -1,15 +1,16 @@
 package com.atmob.flutter_device;
 
 import android.content.Context;
+import android.util.Log;
 
 import com.atmob.device.AtmobDevices;
 import com.atmob.device.DevicesResultCallback;
 
 import java.util.HashMap;
 import java.util.Map;
-import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import io.flutter.plugin.common.MethodCall;
 import io.flutter.plugin.common.MethodChannel;
@@ -19,7 +20,7 @@ public class FlutterDeviceUtil {
 
     public static void getDeviceInfo(Context applicationContext, MethodCall call, MethodChannel.Result result) {
         AtmobDevices atmobDevices = AtmobDevices.get(applicationContext);
-        Map<String,Object> deviceInfo = new HashMap<>();
+        Map<String, Object> deviceInfo = new HashMap<>();
         /**
          *   String? packageName;
          *
@@ -34,9 +35,6 @@ public class FlutterDeviceUtil {
          *   String? osVersion;
          *
          *   //Android特有
-         *   String? oaid;
-         *
-         *   String? aaid;
          *
          *   String? androidId;
          *
@@ -52,56 +50,60 @@ public class FlutterDeviceUtil {
          *
          *   String? userAgent;
          */
-        deviceInfo.put("packageName",atmobDevices.getPackageName());
-        deviceInfo.put("appVersionName",atmobDevices.getVersionName());
-        deviceInfo.put("appVersionCode",atmobDevices.getVersionCode());
-        deviceInfo.put("brand",atmobDevices.getBrand());
-        deviceInfo.put("model",atmobDevices.getModel());
-        deviceInfo.put("osVersion",String.valueOf(atmobDevices.getOSVersion()));
+        deviceInfo.put("packageName", atmobDevices.getPackageName());
+        deviceInfo.put("appVersionName", atmobDevices.getVersionName());
+        deviceInfo.put("appVersionCode", atmobDevices.getVersionCode());
+        deviceInfo.put("brand", atmobDevices.getBrand());
+        deviceInfo.put("model", atmobDevices.getModel());
+        deviceInfo.put("osVersion", String.valueOf(atmobDevices.getOSVersion()));
+
+        deviceInfo.put("androidId", atmobDevices.getAndroidId());
+        deviceInfo.put("imei", atmobDevices.getIMEI());
+        deviceInfo.put("simImei0", atmobDevices.getSimIMEI0());
+        deviceInfo.put("simImei1", atmobDevices.getSimIMEI1());
+        deviceInfo.put("mac", atmobDevices.getMac());
+        deviceInfo.put("wifiName", atmobDevices.getSSID());
+        deviceInfo.put("userAgent", atmobDevices.getUA());
 
-        ExecutorService executorService = Executors.newCachedThreadPool();
-        CountDownLatch latch = new CountDownLatch(2);
+        result.success(deviceInfo);
+    }
 
-        atmobDevices.getMdidOAID(executorService, new DevicesResultCallback<String>() {
+    public static void getOaid(Context applicationContext, MethodCall call, MethodChannel.Result result) {
+        AtmobDevices atmobDevices = AtmobDevices.get(applicationContext);
+        AtomicBoolean isCall = new AtomicBoolean(false);
+        atmobDevices.getMdidOAID(Executors.newCachedThreadPool(), new DevicesResultCallback<String>() {
             @Override
             public void onResult(String oaid) {
-                deviceInfo.put("oaid", oaid);
-                latch.countDown();
+                if (isCall.compareAndSet(false, true)) {
+                    result.success(oaid);
+                }
             }
 
             @Override
             public void onError(Exception e) {
-                latch.countDown();
+                if (isCall.compareAndSet(false, true)) {
+                    result.error("-1", e.getMessage(), null);
+                }
             }
         });
-        atmobDevices.getMdidAAID(executorService, new DevicesResultCallback<String>() {
+    }
+
+    public static void getAaid(Context applicationContext, MethodCall call, MethodChannel.Result result) {
+        AtmobDevices atmobDevices = AtmobDevices.get(applicationContext);
+        AtomicBoolean isCall = new AtomicBoolean(false);
+        atmobDevices.getGoogleAAID(Executors.newCachedThreadPool(), new DevicesResultCallback<String>() {
             @Override
             public void onResult(String aaid) {
-                deviceInfo.put("aaid", aaid);
-                latch.countDown();
+                if (isCall.compareAndSet(false, true)) {
+                    result.success(aaid);
+                }
             }
 
             @Override
             public void onError(Exception e) {
-                latch.countDown();
-            }
-        });
-
-        deviceInfo.put("androidId",atmobDevices.getAndroidId());
-        deviceInfo.put("imei",atmobDevices.getIMEI());
-        deviceInfo.put("simImei0",atmobDevices.getSimIMEI0());
-        deviceInfo.put("simImei1",atmobDevices.getSimIMEI1());
-        deviceInfo.put("mac",atmobDevices.getMac());
-        deviceInfo.put("wifiName",atmobDevices.getSSID());
-        deviceInfo.put("userAgent",atmobDevices.getUA());
-
-        executorService.submit(() -> {
-            try {
-                latch.await();
-                result.success(deviceInfo);
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-                result.error("error","get device info error",null);
+                if (isCall.compareAndSet(false, true)) {
+                    result.error("-1", e.getMessage(), null);
+                }
             }
         });
     }

+ 8 - 0
lib/src/flutter_device.dart

@@ -7,4 +7,12 @@ class FlutterDevice {
   static Future<DevicePlatformInfo> getDeviceInfo() async {
     return FlutterDevicePlatform.instance.getDeviceInfo();
   }
+
+  static Future<String?> getOaid() async {
+    return FlutterDevicePlatform.instance.getOaid();
+  }
+
+  static Future<String?> getAaid() async {
+    return FlutterDevicePlatform.instance.getAaid();
+  }
 }

+ 20 - 2
lib/src/flutter_device_method_channel.dart

@@ -1,3 +1,5 @@
+import 'dart:io';
+
 import 'package:flutter/foundation.dart';
 import 'package:flutter/services.dart';
 
@@ -13,8 +15,24 @@ class MethodChannelFlutterDevice extends FlutterDevicePlatform {
 
   @override
   Future<DevicePlatformInfo> getDeviceInfo() async {
-    final Map<String, dynamic> deviceInfo =
-        await methodChannel.invokeMethod('getDeviceInfo');
+    final Map<String, dynamic>? deviceInfo =
+        await methodChannel.invokeMapMethod<String, dynamic>('getDeviceInfo');
     return DevicePlatformUtil.fromMap(deviceInfo);
   }
+
+  @override
+  Future<String?> getOaid() async {
+    if (Platform.isAndroid) {
+      return await methodChannel.invokeMethod<String?>('getOaid');
+    }
+    return null;
+  }
+
+  @override
+  Future<String?> getAaid() async {
+    if (Platform.isAndroid) {
+      return await methodChannel.invokeMethod<String?>('getAaid');
+    }
+    return null;
+  }
 }

+ 8 - 0
lib/src/flutter_device_platform_interface.dart

@@ -27,4 +27,12 @@ abstract class FlutterDevicePlatform extends PlatformInterface {
   Future<DevicePlatformInfo> getDeviceInfo() {
     throw UnimplementedError('getDeviceInfo() has not been implemented.');
   }
+
+  Future<String?> getOaid() {
+    throw UnimplementedError('getOaid() has not been implemented.');
+  }
+
+  Future<String?> getAaid() {
+    throw UnimplementedError('getAaid() has not been implemented.');
+  }
 }

+ 0 - 9
lib/src/model/device_platform_info.dart

@@ -16,9 +16,6 @@ class DevicePlatformInfo {
   String? _wifiName;
 
   //Android特有
-  String? _oaid;
-
-  String? _aaid;
 
   String? _androidId;
 
@@ -62,8 +59,6 @@ class DevicePlatformInfo {
     _brand = brand;
     _model = model;
     _osVersion = osVersion;
-    _oaid = oaid;
-    _aaid = aaid;
     _androidId = androidId;
     _imei = imei;
     _simImei0 = simImei0;
@@ -93,10 +88,6 @@ class DevicePlatformInfo {
 
   String? get androidId => _androidId;
 
-  String? get aaid => _aaid;
-
-  String? get oaid => _oaid;
-
   String? get osVersion => _osVersion;
 
   String? get model => _model;

+ 18 - 18
lib/src/utils/device_platform_util.dart

@@ -1,25 +1,25 @@
 import '../../device.dart';
 
 class DevicePlatformUtil {
-  static DevicePlatformInfo fromMap(Map<String, dynamic> deviceInfo) {
+  static DevicePlatformInfo fromMap(Map<String, dynamic>? deviceInfo) {
     return DevicePlatformInfo(
-      packageName: deviceInfo['packageName'] as String?,
-      appVersionName: deviceInfo['appVersionName'] as String?,
-      appVersionCode: deviceInfo['appVersionCode'] as int?,
-      brand: deviceInfo['brand'] as String?,
-      model: deviceInfo['model'] as String?,
-      osVersion: deviceInfo['osVersion'] as String?,
-      oaid: deviceInfo['oaid'] as String?,
-      aaid: deviceInfo['aaid'] as String?,
-      androidId: deviceInfo['androidId'] as String?,
-      imei: deviceInfo['imei'] as String?,
-      simImei0: deviceInfo['simImei0'] as String?,
-      simImei1: deviceInfo['simImei1'] as String?,
-      mac: deviceInfo['mac'] as String?,
-      wifiName: deviceInfo['wifiName'] as String?,
-      userAgent: deviceInfo['userAgent'] as String?,
-      idfa: deviceInfo['idfa'] as String?,
-      idfv: deviceInfo['idfv'] as String?,
+      packageName: deviceInfo?['packageName'] as String?,
+      appVersionName: deviceInfo?['appVersionName'] as String?,
+      appVersionCode: deviceInfo?['appVersionCode'] as int?,
+      brand: deviceInfo?['brand'] as String?,
+      model: deviceInfo?['model'] as String?,
+      osVersion: deviceInfo?['osVersion'] as String?,
+      oaid: deviceInfo?['oaid'] as String?,
+      aaid: deviceInfo?['aaid'] as String?,
+      androidId: deviceInfo?['androidId'] as String?,
+      imei: deviceInfo?['imei'] as String?,
+      simImei0: deviceInfo?['simImei0'] as String?,
+      simImei1: deviceInfo?['simImei1'] as String?,
+      mac: deviceInfo?['mac'] as String?,
+      wifiName: deviceInfo?['wifiName'] as String?,
+      userAgent: deviceInfo?['userAgent'] as String?,
+      idfa: deviceInfo?['idfa'] as String?,
+      idfv: deviceInfo?['idfv'] as String?,
     );
   }
 }