|
| 1 | +package com.sensorsdata.analytics.sensorsanalyticsflutterplugin; |
| 2 | + |
| 3 | +import android.content.BroadcastReceiver; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.IntentFilter; |
| 7 | + |
| 8 | +import com.sensorsdata.analytics.android.sdk.SALog; |
| 9 | + |
| 10 | +import io.flutter.plugin.common.MethodChannel; |
| 11 | + |
| 12 | +public class FlutterVisual { |
| 13 | + private static final String TAG = "SA.FlutterVisual"; |
| 14 | + private final DynamicReceiver mDynamicReceiver; |
| 15 | + private MethodChannel mMethodChannel; |
| 16 | + private static final String FLUTTER_ACTION = "android.intent.action.FLUTTER_VISUALIZED"; |
| 17 | + private static final String FLUTTER_EXTRA = "visualizedChanged"; |
| 18 | + private volatile boolean isRegister = false; |
| 19 | + private static volatile FlutterVisual mFlutterVisual; |
| 20 | + |
| 21 | + private FlutterVisual() { |
| 22 | + mDynamicReceiver = new DynamicReceiver(); |
| 23 | + } |
| 24 | + |
| 25 | + public static FlutterVisual getInstance() { |
| 26 | + if (mFlutterVisual == null) { |
| 27 | + synchronized (FlutterVisual.class) { |
| 28 | + if (mFlutterVisual == null) { |
| 29 | + mFlutterVisual = new FlutterVisual(); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + return mFlutterVisual; |
| 34 | + } |
| 35 | + |
| 36 | + public void setMethodChannel(MethodChannel methodChannel) { |
| 37 | + this.mMethodChannel = methodChannel; |
| 38 | + } |
| 39 | + |
| 40 | + class DynamicReceiver extends BroadcastReceiver { |
| 41 | + @Override |
| 42 | + public void onReceive(Context context, Intent intent) { |
| 43 | + if (mMethodChannel != null && intent != null && intent.getStringExtra(FLUTTER_EXTRA) != null) { |
| 44 | + if (intent.getStringExtra(FLUTTER_EXTRA).equals("visualizedConnectionStatusChanged")) { |
| 45 | + SALog.i(TAG, "visualizedConnectionStatusChanged"); |
| 46 | + mMethodChannel.invokeMethod("visualizedConnectionStatusChanged", null); |
| 47 | + } else if (intent.getStringExtra(FLUTTER_EXTRA).equals("visualizedPropertiesConfigChanged")) { |
| 48 | + SALog.i(TAG, "visualizedPropertiesConfigChanged"); |
| 49 | + mMethodChannel.invokeMethod("visualizedPropertiesConfigChanged", null); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public synchronized void registerBroadcast(Context context) { |
| 56 | + SALog.i(TAG, "registerBroadcast:" + isRegister); |
| 57 | + if (!isRegister) { |
| 58 | + SALog.i(TAG, "registerBroadcast"); |
| 59 | + IntentFilter filter = new IntentFilter(); |
| 60 | + filter.addAction(FLUTTER_ACTION); |
| 61 | + context.registerReceiver(mDynamicReceiver, filter); |
| 62 | + isRegister = true; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public synchronized void unRegisterBroadcast(Context context) { |
| 67 | + SALog.i(TAG, "unRegisterBroadcast"); |
| 68 | + context.unregisterReceiver(mDynamicReceiver); |
| 69 | + isRegister = false; |
| 70 | + } |
| 71 | +} |
0 commit comments