From 7fcd70a96166b94502b308d917c948b1b319b5c1 Mon Sep 17 00:00:00 2001
From: "shaode.lsd" <shaode.lsd@alibaba-inc.com>
Date: Tue, 11 Aug 2020 14:30:30 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=20=20=E4=BF=AE=E5=A4=8DXPlatformPlugin?=
 =?UTF-8?q?=E5=A4=8D=E7=94=A8=E5=90=8E=E5=AF=BC=E8=87=B4=E7=9A=84activity?=
 =?UTF-8?q?=E5=8F=AF=E8=83=BD=E4=B8=BA=E7=A9=BA=E7=9A=84=E9=94=99=E8=AF=AF?=
 =?UTF-8?q?=20=20=20=E4=BF=9D=E8=AF=81=E6=AF=8F=E6=AC=A1=E9=A1=B5=E9=9D=A2?=
 =?UTF-8?q?=E8=BF=9B=E5=85=A5=E6=97=B6=E9=83=BD=E8=BF=9B=E8=A1=8CTheme?=
 =?UTF-8?q?=E7=9A=84=E5=88=B7=E6=96=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../flutterboost/XPlatformPlugin.java         | 67 +++++++++++++++----
 lib/container/boost_container.dart            |  3 +
 2 files changed, 57 insertions(+), 13 deletions(-)

diff --git a/android/src/main/java/com/idlefish/flutterboost/XPlatformPlugin.java b/android/src/main/java/com/idlefish/flutterboost/XPlatformPlugin.java
index 0fb2a40..7813b8b 100644
--- a/android/src/main/java/com/idlefish/flutterboost/XPlatformPlugin.java
+++ b/android/src/main/java/com/idlefish/flutterboost/XPlatformPlugin.java
@@ -118,14 +118,20 @@ public class XPlatformPlugin {
     }
 
     private void playSystemSound(PlatformChannel.SoundType soundType) {
+        if (getActivity() == null) {
+            return;
+        }
         if (soundType == PlatformChannel.SoundType.CLICK) {
-            View view = activity.getWindow().getDecorView();
+            View view = getActivity().getWindow().getDecorView();
             view.playSoundEffect(SoundEffectConstants.CLICK);
         }
     }
 
     private void vibrateHapticFeedback(PlatformChannel.HapticFeedbackType feedbackType) {
-        View view = activity.getWindow().getDecorView();
+        if (getActivity() == null) {
+            return;
+        }
+        View view = getActivity().getWindow().getDecorView();
         switch (feedbackType) {
             case STANDARD:
                 view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
@@ -147,11 +153,17 @@ public class XPlatformPlugin {
     }
 
     private void setSystemChromePreferredOrientations(int androidOrientation) {
-        activity.setRequestedOrientation(androidOrientation);
+        if (getActivity() == null) {
+            return;
+        }
+        getActivity().setRequestedOrientation(androidOrientation);
     }
 
     @SuppressWarnings("deprecation")
     private void setSystemChromeApplicationSwitcherDescription(PlatformChannel.AppSwitcherDescription description) {
+        if (getActivity() == null) {
+            return;
+        }
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
             return;
         }
@@ -159,11 +171,12 @@ public class XPlatformPlugin {
         // Linter refuses to believe we're only executing this code in API 28 unless we use distinct if blocks and
         // hardcode the API 28 constant.
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P && Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
-            activity.setTaskDescription(new ActivityManager.TaskDescription(description.label, /*icon=*/ null, description.color));
+            getActivity().setTaskDescription(new ActivityManager.TaskDescription(description.label, /*icon=*/ null, description.color));
+
         }
         if (Build.VERSION.SDK_INT >= 28) {
             ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(description.label, 0, description.color);
-            activity.setTaskDescription(taskDescription);
+            getActivity().setTaskDescription(taskDescription);
         }
     }
 
@@ -205,7 +218,10 @@ public class XPlatformPlugin {
      * {@code PlatformPlugin}.
      */
     public void updateSystemUiOverlays(){
-        activity.getWindow().getDecorView().setSystemUiVisibility(mEnabledOverlays);
+        if (getActivity() == null) {
+            return;
+        }
+        getActivity().getWindow().getDecorView().setSystemUiVisibility(mEnabledOverlays);
         if (currentTheme != null) {
             setSystemChromeSystemUIOverlayStyle(currentTheme);
         }
@@ -216,7 +232,10 @@ public class XPlatformPlugin {
     }
 
     private void setSystemChromeSystemUIOverlayStyle(PlatformChannel.SystemChromeStyle systemChromeStyle) {
-        Window window = activity.getWindow();
+        if (getActivity() == null) {
+            return;
+        }
+        Window window = getActivity().getWindow();
         View view = window.getDecorView();
         int flags = view.getSystemUiVisibility();
         // You can change the navigation bar color (including translucent colors)
@@ -265,32 +284,44 @@ public class XPlatformPlugin {
     }
 
     private void popSystemNavigator() {
-        activity.finish();
+        if (getActivity() == null) {
+            return;
+        }
+        getActivity().finish();
     }
 
     private CharSequence getClipboardData(PlatformChannel.ClipboardContentFormat format) {
-        ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
+        if (getActivity() == null) {
+            return null;
+        }
+        ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
         ClipData clip = clipboard.getPrimaryClip();
         if (clip == null)
             return null;
 
         if (format == null || format == PlatformChannel.ClipboardContentFormat.PLAIN_TEXT) {
-            return clip.getItemAt(0).coerceToText(activity);
+            return clip.getItemAt(0).coerceToText(getActivity());
         }
 
         return null;
     }
 
     private void setClipboardData(String text) {
-        ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
+        if (getActivity() == null) {
+            return;
+        }
+        ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
         ClipData clip = ClipData.newPlainText("text label?", text);
         clipboard.setPrimaryClip(clip);
     }
 
 
     private List<Rect> getSystemGestureExclusionRects() {
+        if (getActivity() == null) {
+            return null;
+        }
         if (Build.VERSION.SDK_INT >= 29) {
-            Window window = activity.getWindow();
+            Window window = getActivity().getWindow();
             View view = window.getDecorView();
             return view.getSystemGestureExclusionRects();
         }
@@ -299,13 +330,23 @@ public class XPlatformPlugin {
     }
 
     private void setSystemGestureExclusionRects(ArrayList<Rect> rects) {
+        if (getActivity() == null) {
+            return;
+        }
         if (Build.VERSION.SDK_INT < 29) {
             return;
         }
 
-        Window window = activity.getWindow();
+        Window window = getActivity().getWindow();
         View view = window.getDecorView();
         view.setSystemGestureExclusionRects(rects);
     }
 
+    @Nullable
+    private Activity getActivity() {
+        if (activity != null) {
+            return activity;
+        }
+        return FlutterBoost.instance().currentActivity();
+    }
 }
diff --git a/lib/container/boost_container.dart b/lib/container/boost_container.dart
index 6e20f9a..d47fb05 100755
--- a/lib/container/boost_container.dart
+++ b/lib/container/boost_container.dart
@@ -22,6 +22,7 @@
  * THE SOFTWARE.
  */
 import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
 import 'container_manager.dart';
 import '../flutter_boost.dart';
 import 'boost_page_route.dart';
@@ -222,6 +223,8 @@ class BoostContainerState extends NavigatorState {
 
     routerHistory.add(route);
 
+    // 复用XPlatformPlugin后,每次进入页面时都需要在这里反复通知Native更新Theme
+    SystemChrome.restoreSystemUIOverlays();
     if (FlutterBoost.containerManager.postPushRoute != null) {
       FlutterBoost.containerManager
           .postPushRoute(name, uniqueId, params, newRoute ?? route, future);
-- 
2.26.2