Explorar el Código

[new]增加友盟初始化插件接口

zk hace 1 año
padre
commit
d1cb752a1c

+ 8 - 0
android/app/build.gradle

@@ -110,6 +110,14 @@ dependencies {
 
     //oaid
     implementation 'extension.oaid:oaid:2.3.0'
+
+    //友盟统计
+    implementation 'com.umeng.umsdk:common:9.6.7'
+    implementation 'com.umeng.umsdk:asms:1.8.0'
+    implementation 'com.umeng.umsdk:apm:1.9.4'
+
+    //渠道包信息读取
+    implementation "extra.pack:channel:1.1.0-SNAPSHOT"
 }
 
 flutter {

+ 2 - 0
android/app/src/main/java/com/atmob/elec_asst/MyApplication.java

@@ -2,6 +2,7 @@ package com.atmob.elec_asst;
 
 import android.app.Application;
 
+import com.atmob.elec_asst.sdk.umeng.UmengHelper;
 import com.atmob.elec_asst.utils.SharedPreferencesUtil;
 
 public class MyApplication extends Application {
@@ -10,5 +11,6 @@ public class MyApplication extends Application {
     public void onCreate() {
         super.onCreate();
         SharedPreferencesUtil.init(this);
+        UmengHelper.preInit(this);
     }
 }

+ 9 - 0
android/app/src/main/java/com/atmob/elec_asst/activity/MainActivity.java

@@ -3,8 +3,12 @@ package com.atmob.elec_asst.activity;
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
 import android.view.KeyEvent;
 
+import androidx.annotation.Nullable;
+
 import io.flutter.embedding.android.FlutterActivity;
 
 public class MainActivity extends FlutterActivity {
@@ -18,6 +22,11 @@ public class MainActivity extends FlutterActivity {
         context.startActivity(intent);
     }
 
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        Log.d("MainActivity", "onCreate");
+    }
 
     public void exit() {
         try {

+ 42 - 0
android/app/src/main/java/com/atmob/elec_asst/sdk/umeng/UmengHelper.java

@@ -0,0 +1,42 @@
+package com.atmob.elec_asst.sdk.umeng;
+
+import android.app.Application;
+import android.content.Context;
+
+import androidx.annotation.NonNull;
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleEventObserver;
+import androidx.lifecycle.ProcessLifecycleOwner;
+
+import com.atmob.elec_asst.BuildConfig;
+
+public class UmengHelper {
+
+    private static final String UMENG_APP_KEY =  UmengId.UMENG_APP_KEY;
+
+    private UmengHelper() {
+
+    }
+
+    public static void preInit(@NonNull Application application){
+        if (BuildConfig.DEBUG) {
+            return;
+        }
+        UMConfigure.preInit(application,UMENG_APP_KEY, ChannelReader.getChannel(application));
+        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
+
+        ProcessLifecycleOwner.get().getLifecycle().addObserver((LifecycleEventObserver) (source, event) -> {
+            if (event == Lifecycle.Event.ON_DESTROY) {
+                saveData(application);
+            }
+        });
+    }
+
+    /**
+     * 当程序退出时,用于保存统计数据
+     */
+    public static void saveData(Context context) {
+        MobclickAgent.onKillProcess(context.getApplicationContext());
+    }
+
+}

+ 6 - 0
android/app/src/main/java/com/atmob/elec_asst/sdk/umeng/UmengId.java

@@ -0,0 +1,6 @@
+package com.atmob.elec_asst.sdk.umeng;
+
+public class UmengId {
+
+    public static final String UMENG_APP_KEY = "66b07216cac2a664de82d222";
+}

+ 9 - 0
android/build.gradle

@@ -12,6 +12,15 @@ allprojects {
     repositories {
         google()
         mavenCentral()
+
+        maven {
+            credentials {
+                username "$atmob_maven_username"
+                password "$atmob_maven_password"
+            }
+            allowInsecureProtocol = true
+            url "$atmob_maven_url/repository/android-group/"
+        }
     }
 }
 

+ 18 - 0
lib/data/consts/build_config.dart

@@ -9,6 +9,24 @@ class BuildConfig {
 }
 
 
+final class UmengConfig {
+  UmengConfig._();
+
+  //友盟统计配置
+  static const _umengAndroidAppKey = "66b07216cac2a664de82d222";
+  static const _umengIosAppKey = "66b07337cac2a664de82d457";
+
+  static String get umengAppKey {
+    if (Platform.isAndroid) {
+      return _umengAndroidAppKey;
+    } else if (Platform.isIOS) {
+      return _umengIosAppKey;
+    } else {
+      return '';
+    }
+  }
+}
+
 final class GravityConfig {
   GravityConfig._();
   //引力引擎配置

+ 36 - 2
plugin/flutter_umeng/android/build.gradle

@@ -16,9 +16,29 @@ rootProject.allprojects {
     repositories {
         google()
         mavenCentral()
+        maven {
+            credentials {
+                username "$atmob_maven_username"
+                password "$atmob_maven_password"
+            }
+            allowInsecureProtocol = true
+            url "$atmob_maven_url/repository/android-group/"
+        }
+    }
+}
+
+// 加载 local.properties 文件
+def localProperties = new Properties()
+def localPropertiesFile = rootProject.file('local.properties')
+if (localPropertiesFile.exists()) {
+    localPropertiesFile.withInputStream { stream ->
+        localProperties.load(stream)
     }
 }
 
+// 读取变量
+def flutterSdk = localProperties.getProperty('flutter.sdk')
+
 apply plugin: "com.android.library"
 
 android {
@@ -38,8 +58,22 @@ android {
     }
 
     dependencies {
-        testImplementation("junit:junit:4.13.2")
-        testImplementation("org.mockito:mockito-core:5.0.0")
+        //flutter
+        compileOnly files("$flutterSdk/bin/cache/artifacts/engine/android-arm/flutter.jar")
+
+        //AndroidX
+        compileOnly "androidx.annotation:annotation:1.1.0"
+
+        //AndroidX
+        compileOnly "androidx.core:core:1.13.1"
+
+        //友盟统计
+        compileOnly 'com.umeng.umsdk:common:9.6.7'
+        compileOnly 'com.umeng.umsdk:asms:1.8.0'
+        compileOnly 'com.umeng.umsdk:apm:1.9.4'
+
+        //渠道包信息读取
+        implementation "extra.pack:channel:1.1.0-SNAPSHOT"
     }
 
     testOptions {

+ 23 - 0
plugin/flutter_umeng/android/gradle.properties

@@ -0,0 +1,23 @@
+# Project-wide Gradle settings.
+# IDE (e.g. Android Studio) users:
+# Gradle settings configured through the IDE *will override*
+# any settings specified in this file.
+# For more details on how to configure your build environment visit
+# http://www.gradle.org/docs/current/userguide/build_environment.html
+# Specifies the JVM arguments used for the daemon process.
+# The setting is particularly useful for tweaking memory settings.
+org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
+# When configured, Gradle will run in incubating parallel mode.
+# This option should only be used with decoupled projects. More details, visit
+# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
+# org.gradle.parallel=true
+# AndroidX package structure to make it clearer which packages are bundled with the
+# Android operating system, and which are packaged with your app's APK
+# https://developer.android.com/topic/libraries/support-library/androidx-rn
+android.useAndroidX=true
+# Enables namespacing of each library's R class so that its R class includes only the
+# resources declared in the library itself and none from the library's dependencies,
+# thereby reducing the size of the R class for that library
+android.nonTransitiveRClass=true
+android.enableJetifier=true
+android.injected.testOnly=false

+ 2 - 0
plugin/flutter_umeng/android/src/main/AndroidManifest.xml

@@ -1,3 +1,5 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.atmob.flutter_umeng">
+
+
 </manifest>

+ 64 - 24
plugin/flutter_umeng/android/src/main/java/com/atmob/flutter_umeng/FlutterUmengPlugin.java

@@ -1,38 +1,78 @@
 package com.atmob.flutter_umeng;
 
+import android.content.Context;
+
 import androidx.annotation.NonNull;
 
+import io.flutter.Log;
 import io.flutter.embedding.engine.plugins.FlutterPlugin;
 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;
 
-/** FlutterUmengPlugin */
+/**
+ * FlutterUmengPlugin
+ */
 public class FlutterUmengPlugin implements FlutterPlugin, MethodCallHandler {
-  /// The MethodChannel that will the communication between Flutter and native Android
-  ///
-  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
-  /// when the Flutter Engine is detached from the Activity
-  private MethodChannel channel;
-
-  @Override
-  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
-    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_umeng");
-    channel.setMethodCallHandler(this);
-  }
-
-  @Override
-  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
-    if (call.method.equals("getPlatformVersion")) {
-      result.success("Android " + android.os.Build.VERSION.RELEASE);
-    } else {
-      result.notImplemented();
+    /// The MethodChannel that will the communication between Flutter and native Android
+    ///
+    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
+    /// when the Flutter Engine is detached from the Activity
+    private MethodChannel channel;
+
+
+    private static final String METHOD_POLICY_GRANT = "setPolicyGrantResult";
+    private static final String METHOD_INIT_COMMON = "method_init_common";
+
+    private Context applicationContext;
+
+
+    @Override
+    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
+        Log.d("FlutterUmengPlugin", "onAttachedToEngine");
+        applicationContext = flutterPluginBinding.getApplicationContext();
+        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_umeng");
+        channel.setMethodCallHandler(this);
+
     }
-  }
 
-  @Override
-  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
-    channel.setMethodCallHandler(null);
-  }
+    @Override
+    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
+        switch (call.method) {
+            case METHOD_POLICY_GRANT:
+                setPolicyGrantResult(call, result);
+                break;
+            case METHOD_INIT_COMMON:
+                initCommon(call, result);
+                break;
+            default:
+                result.notImplemented();
+        }
+    }
+
+    private void initCommon(MethodCall call, Result result) {
+        if (applicationContext == null) {
+            result.error("-1", "applicationContext is null", null);
+            return;
+        }
+        String appKey = call.argument("appKey");
+        UmengHelper.initCommon(applicationContext, appKey);
+        result.success(null);
+    }
+
+    private void setPolicyGrantResult(MethodCall call, Result result) {
+        if (applicationContext == null) {
+            result.error("-1", "applicationContext is null", null);
+            return;
+        }
+        boolean granted = Boolean.TRUE.equals(call.argument("granted"));
+        UmengHelper.submitPolicyGrantResult(applicationContext, granted);
+        result.success(null);
+    }
+
+    @Override
+    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
+        channel.setMethodCallHandler(null);
+    }
 }

+ 62 - 0
plugin/flutter_umeng/android/src/main/java/com/atmob/flutter_umeng/UmengHelper.java

@@ -0,0 +1,62 @@
+package com.atmob.flutter_umeng;
+
+import android.app.Application;
+import android.content.Context;
+import androidx.annotation.NonNull;
+import com.atmob.channelreader.ChannelReader;
+import com.umeng.analytics.MobclickAgent;
+import com.umeng.commonsdk.UMConfigure;
+import java.util.Map;
+
+public class UmengHelper {
+
+
+    private UmengHelper() {
+
+    }
+
+    public static void initCommon(@NonNull Context context,String appKey) {
+        if (BuildConfig.DEBUG) {
+            return;
+        }
+        if (UMConfigure.needSendZcfgEnv(context)) {
+            initAfterGrantedAgreement(context,appKey);
+        }
+    }
+
+    public static void initAfterGrantedAgreement(@NonNull Context context, String appKey) {
+        if (BuildConfig.DEBUG) {
+            return;
+        }
+        UMConfigure.init(context, appKey, ChannelReader.getChannel(context), UMConfigure.DEVICE_TYPE_PHONE, null);//last param is PushSecretKey
+    }
+
+
+    /**
+     * 上报用户是否同意隐私政策
+     *
+     * @param granted 是否同意
+     */
+    public static void submitPolicyGrantResult(Context context, boolean granted) {
+        UMConfigure.submitPolicyGrantResult(context.getApplicationContext(), granted);
+    }
+
+    /**
+     * 事件上报
+     *
+     * @param eventId 事件id
+     */
+    public static void reportEvent(Context context, String eventId) {
+        MobclickAgent.onEvent(context, eventId);
+    }
+
+    /**
+     * 携带信息的事件上报
+     *
+     * @param eventId 事件id
+     * @param map     额外信息, object类型由第一次上报时的为准, 后续不能更改
+     */
+    public static void reportEvent(Context context, String eventId, Map<String, Object> map) {
+        MobclickAgent.onEventObject(context, eventId, map);
+    }
+}

+ 5 - 1
plugin/flutter_umeng/lib/flutter_umeng.dart

@@ -1,7 +1,11 @@
 import 'flutter_umeng_platform_interface.dart';
 
 class FlutterUmeng {
-  static Future<bool> setPolicyGrantResult(bool granted) {
+  static Future<void> setPolicyGrantResult(bool granted) {
     return FlutterUmengPlatform.instance.setPolicyGrantResult(granted);
   }
+
+  static Future<void> initCommon(String appKey) {
+    return FlutterUmengPlatform.instance.initCommon(appKey);
+  }
 }

+ 8 - 1
plugin/flutter_umeng/lib/flutter_umeng_method_channel.dart

@@ -10,9 +10,16 @@ class MethodChannelFlutterUmeng extends FlutterUmengPlatform {
   final methodChannel = const MethodChannel('flutter_umeng');
 
   @override
-  Future<bool> setPolicyGrantResult(bool granted) async {
+  Future<void> setPolicyGrantResult(bool granted) async {
     return await methodChannel.invokeMethod('setPolicyGrantResult', {
       "granted": granted,
     });
   }
+
+  @override
+  Future<void> initCommon(String appKey) async {
+    return await methodChannel.invokeMethod('initCommon', {
+      "appKey": appKey,
+    });
+  }
 }

+ 5 - 1
plugin/flutter_umeng/lib/flutter_umeng_platform_interface.dart

@@ -23,8 +23,12 @@ abstract class FlutterUmengPlatform extends PlatformInterface {
     _instance = instance;
   }
 
-  Future<bool> setPolicyGrantResult(bool granted) {
+  Future<void> setPolicyGrantResult(bool granted) {
     throw UnimplementedError(
         'setPolicyGrantResult() has not been implemented.');
   }
+
+  Future<void> initCommon(String appKey) {
+    throw UnimplementedError('initCommon() has not been implemented.');
+  }
 }