Commit 7fcd70a9 authored by shaode.lsd's avatar shaode.lsd

fix:

  修复XPlatformPlugin复用后导致的activity可能为空的错误
  保证每次页面进入时都进行Theme的刷新
parent 200c249d
...@@ -118,14 +118,20 @@ public class XPlatformPlugin { ...@@ -118,14 +118,20 @@ public class XPlatformPlugin {
} }
private void playSystemSound(PlatformChannel.SoundType soundType) { private void playSystemSound(PlatformChannel.SoundType soundType) {
if (getActivity() == null) {
return;
}
if (soundType == PlatformChannel.SoundType.CLICK) { if (soundType == PlatformChannel.SoundType.CLICK) {
View view = activity.getWindow().getDecorView(); View view = getActivity().getWindow().getDecorView();
view.playSoundEffect(SoundEffectConstants.CLICK); view.playSoundEffect(SoundEffectConstants.CLICK);
} }
} }
private void vibrateHapticFeedback(PlatformChannel.HapticFeedbackType feedbackType) { private void vibrateHapticFeedback(PlatformChannel.HapticFeedbackType feedbackType) {
View view = activity.getWindow().getDecorView(); if (getActivity() == null) {
return;
}
View view = getActivity().getWindow().getDecorView();
switch (feedbackType) { switch (feedbackType) {
case STANDARD: case STANDARD:
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
...@@ -147,11 +153,17 @@ public class XPlatformPlugin { ...@@ -147,11 +153,17 @@ public class XPlatformPlugin {
} }
private void setSystemChromePreferredOrientations(int androidOrientation) { private void setSystemChromePreferredOrientations(int androidOrientation) {
activity.setRequestedOrientation(androidOrientation); if (getActivity() == null) {
return;
}
getActivity().setRequestedOrientation(androidOrientation);
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private void setSystemChromeApplicationSwitcherDescription(PlatformChannel.AppSwitcherDescription description) { private void setSystemChromeApplicationSwitcherDescription(PlatformChannel.AppSwitcherDescription description) {
if (getActivity() == null) {
return;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return; return;
} }
...@@ -159,11 +171,12 @@ public class XPlatformPlugin { ...@@ -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 // 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. // hardcode the API 28 constant.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P && Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { 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) { if (Build.VERSION.SDK_INT >= 28) {
ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(description.label, 0, description.color); ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(description.label, 0, description.color);
activity.setTaskDescription(taskDescription); getActivity().setTaskDescription(taskDescription);
} }
} }
...@@ -205,7 +218,10 @@ public class XPlatformPlugin { ...@@ -205,7 +218,10 @@ public class XPlatformPlugin {
* {@code PlatformPlugin}. * {@code PlatformPlugin}.
*/ */
public void updateSystemUiOverlays(){ public void updateSystemUiOverlays(){
activity.getWindow().getDecorView().setSystemUiVisibility(mEnabledOverlays); if (getActivity() == null) {
return;
}
getActivity().getWindow().getDecorView().setSystemUiVisibility(mEnabledOverlays);
if (currentTheme != null) { if (currentTheme != null) {
setSystemChromeSystemUIOverlayStyle(currentTheme); setSystemChromeSystemUIOverlayStyle(currentTheme);
} }
...@@ -216,7 +232,10 @@ public class XPlatformPlugin { ...@@ -216,7 +232,10 @@ public class XPlatformPlugin {
} }
private void setSystemChromeSystemUIOverlayStyle(PlatformChannel.SystemChromeStyle systemChromeStyle) { private void setSystemChromeSystemUIOverlayStyle(PlatformChannel.SystemChromeStyle systemChromeStyle) {
Window window = activity.getWindow(); if (getActivity() == null) {
return;
}
Window window = getActivity().getWindow();
View view = window.getDecorView(); View view = window.getDecorView();
int flags = view.getSystemUiVisibility(); int flags = view.getSystemUiVisibility();
// You can change the navigation bar color (including translucent colors) // You can change the navigation bar color (including translucent colors)
...@@ -265,32 +284,44 @@ public class XPlatformPlugin { ...@@ -265,32 +284,44 @@ public class XPlatformPlugin {
} }
private void popSystemNavigator() { private void popSystemNavigator() {
activity.finish(); if (getActivity() == null) {
return;
}
getActivity().finish();
} }
private CharSequence getClipboardData(PlatformChannel.ClipboardContentFormat format) { 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(); ClipData clip = clipboard.getPrimaryClip();
if (clip == null) if (clip == null)
return null; return null;
if (format == null || format == PlatformChannel.ClipboardContentFormat.PLAIN_TEXT) { if (format == null || format == PlatformChannel.ClipboardContentFormat.PLAIN_TEXT) {
return clip.getItemAt(0).coerceToText(activity); return clip.getItemAt(0).coerceToText(getActivity());
} }
return null; return null;
} }
private void setClipboardData(String text) { 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); ClipData clip = ClipData.newPlainText("text label?", text);
clipboard.setPrimaryClip(clip); clipboard.setPrimaryClip(clip);
} }
private List<Rect> getSystemGestureExclusionRects() { private List<Rect> getSystemGestureExclusionRects() {
if (getActivity() == null) {
return null;
}
if (Build.VERSION.SDK_INT >= 29) { if (Build.VERSION.SDK_INT >= 29) {
Window window = activity.getWindow(); Window window = getActivity().getWindow();
View view = window.getDecorView(); View view = window.getDecorView();
return view.getSystemGestureExclusionRects(); return view.getSystemGestureExclusionRects();
} }
...@@ -299,13 +330,23 @@ public class XPlatformPlugin { ...@@ -299,13 +330,23 @@ public class XPlatformPlugin {
} }
private void setSystemGestureExclusionRects(ArrayList<Rect> rects) { private void setSystemGestureExclusionRects(ArrayList<Rect> rects) {
if (getActivity() == null) {
return;
}
if (Build.VERSION.SDK_INT < 29) { if (Build.VERSION.SDK_INT < 29) {
return; return;
} }
Window window = activity.getWindow(); Window window = getActivity().getWindow();
View view = window.getDecorView(); View view = window.getDecorView();
view.setSystemGestureExclusionRects(rects); view.setSystemGestureExclusionRects(rects);
} }
@Nullable
private Activity getActivity() {
if (activity != null) {
return activity;
}
return FlutterBoost.instance().currentActivity();
}
} }
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'container_manager.dart'; import 'container_manager.dart';
import '../flutter_boost.dart'; import '../flutter_boost.dart';
import 'boost_page_route.dart'; import 'boost_page_route.dart';
...@@ -222,6 +223,8 @@ class BoostContainerState extends NavigatorState { ...@@ -222,6 +223,8 @@ class BoostContainerState extends NavigatorState {
routerHistory.add(route); routerHistory.add(route);
// 复用XPlatformPlugin后,每次进入页面时都需要在这里反复通知Native更新Theme
SystemChrome.restoreSystemUIOverlays();
if (FlutterBoost.containerManager.postPushRoute != null) { if (FlutterBoost.containerManager.postPushRoute != null) {
FlutterBoost.containerManager FlutterBoost.containerManager
.postPushRoute(name, uniqueId, params, newRoute ?? route, future); .postPushRoute(name, uniqueId, params, newRoute ?? route, future);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment