diff --git a/shell/platform/android/AndroidManifest.xml b/shell/platform/android/AndroidManifest.xml index 2a5d3b89be28b..82f4b0a7c5da9 100644 --- a/shell/platform/android/AndroidManifest.xml +++ b/shell/platform/android/AndroidManifest.xml @@ -5,7 +5,7 @@ --> - + diff --git a/shell/platform/android/io/flutter/embedding/android/AndroidTouchProcessor.java b/shell/platform/android/io/flutter/embedding/android/AndroidTouchProcessor.java index 1dcace3cfeed1..f3a75a991b309 100644 --- a/shell/platform/android/io/flutter/embedding/android/AndroidTouchProcessor.java +++ b/shell/platform/android/io/flutter/embedding/android/AndroidTouchProcessor.java @@ -198,7 +198,9 @@ public boolean onTouchEvent(@NonNull MotionEvent event, @NonNull Matrix transfor public boolean onGenericMotionEvent(@NonNull MotionEvent event, @NonNull Context context) { // Method isFromSource is only available in API 18+ (Jelly Bean MR2) // Mouse hover support is not implemented for API < 18. - boolean isPointerEvent = event.isFromSource(InputDevice.SOURCE_CLASS_POINTER); + boolean isPointerEvent = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 + && event.isFromSource(InputDevice.SOURCE_CLASS_POINTER); boolean isMovementEvent = (event.getActionMasked() == MotionEvent.ACTION_HOVER_MOVE || event.getActionMasked() == MotionEvent.ACTION_SCROLL); diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterFragment.java b/shell/platform/android/io/flutter/embedding/android/FlutterFragment.java index a83f9d80c1041..586d60c9c6980 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterFragment.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterFragment.java @@ -8,6 +8,7 @@ import android.content.ComponentCallbacks2; import android.content.Context; import android.content.Intent; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -171,14 +172,16 @@ public class FlutterFragment extends Fragment @RequiresApi(18) private final OnWindowFocusChangeListener onWindowFocusChangeListener = - new OnWindowFocusChangeListener() { - @Override - public void onWindowFocusChanged(boolean hasFocus) { - if (stillAttachedForEvent("onWindowFocusChanged")) { - delegate.onWindowFocusChanged(hasFocus); + Build.VERSION.SDK_INT >= 18 + ? new OnWindowFocusChangeListener() { + @Override + public void onWindowFocusChanged(boolean hasFocus) { + if (stillAttachedForEvent("onWindowFocusChanged")) { + delegate.onWindowFocusChanged(hasFocus); + } + } } - } - }; + : null; /** * Creates a {@code FlutterFragment} with a default configuration. @@ -1125,16 +1128,20 @@ public void onStop() { @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - view.getViewTreeObserver().addOnWindowFocusChangeListener(onWindowFocusChangeListener); + if (Build.VERSION.SDK_INT >= 18) { + view.getViewTreeObserver().addOnWindowFocusChangeListener(onWindowFocusChangeListener); + } } @Override public void onDestroyView() { super.onDestroyView(); - // onWindowFocusChangeListener is API 18+ only. - requireView() - .getViewTreeObserver() - .removeOnWindowFocusChangeListener(onWindowFocusChangeListener); + if (Build.VERSION.SDK_INT >= 18) { + // onWindowFocusChangeListener is API 18+ only. + requireView() + .getViewTreeObserver() + .removeOnWindowFocusChangeListener(onWindowFocusChangeListener); + } if (stillAttachedForEvent("onDestroyView")) { delegate.onDestroyView(); } diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java b/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java index 702a994cef311..65bf6936cc88b 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java @@ -39,6 +39,7 @@ * an {@link android.media.Image} and renders it to the {@link android.graphics.Canvas} in {@code * onDraw}. */ +@TargetApi(19) public class FlutterImageView extends View implements RenderSurface { private static final String TAG = "FlutterImageView"; @@ -98,6 +99,7 @@ private static void logW(String format, Object... args) { Log.w(TAG, String.format(Locale.US, format, args)); } + @TargetApi(19) @SuppressLint("WrongConstant") // RGBA_8888 is a valid constant. @NonNull private static ImageReader createImageReader(int width, int height) { @@ -192,6 +194,7 @@ public void resume() { * Acquires the next image to be drawn to the {@link android.graphics.Canvas}. Returns true if * there's an image available in the queue. */ + @TargetApi(19) public boolean acquireLatestImage() { if (!isAttachedToFlutterRenderer) { return false; diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterView.java b/shell/platform/android/io/flutter/embedding/android/FlutterView.java index 9623d48872574..4cfa3c8839ddc 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterView.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterView.java @@ -290,6 +290,7 @@ public FlutterView(@NonNull Context context, @NonNull FlutterTextureView flutter *

{@code FlutterView} requires an {@code Activity} instead of a generic {@code Context} to be * compatible with {@link PlatformViewsController}. */ + @TargetApi(19) public FlutterView(@NonNull Context context, @NonNull FlutterImageView flutterImageView) { this(context, null, flutterImageView); } @@ -356,6 +357,7 @@ private FlutterView( init(); } + @TargetApi(19) private FlutterView( @NonNull Context context, @Nullable AttributeSet attrs, diff --git a/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java b/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java index 4a4abb5bada54..1bbeefe9c0ae9 100644 --- a/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java +++ b/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java @@ -16,6 +16,7 @@ import android.os.Looper; import android.os.SystemClock; import android.util.DisplayMetrics; +import android.view.WindowManager; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import io.flutter.BuildConfig; @@ -169,9 +170,17 @@ public void startInitialization(@NonNull Context applicationContext, @NonNull Se flutterApplicationInfo = ApplicationInfoLoader.load(appContext); VsyncWaiter waiter; - final DisplayManager dm = - (DisplayManager) appContext.getSystemService(Context.DISPLAY_SERVICE); - waiter = VsyncWaiter.getInstance(dm, flutterJNI); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 /* 17 */) { + final DisplayManager dm = + (DisplayManager) appContext.getSystemService(Context.DISPLAY_SERVICE); + waiter = VsyncWaiter.getInstance(dm, flutterJNI); + } else { + float fps = + ((WindowManager) appContext.getSystemService(Context.WINDOW_SERVICE)) + .getDefaultDisplay() + .getRefreshRate(); + waiter = VsyncWaiter.getInstance(fps, flutterJNI); + } waiter.init(); // Use a background thread for initialization tasks that require disk access. diff --git a/shell/platform/android/io/flutter/embedding/engine/mutatorsstack/FlutterMutatorView.java b/shell/platform/android/io/flutter/embedding/engine/mutatorsstack/FlutterMutatorView.java index b6eaa3d434725..c195b28728e9b 100644 --- a/shell/platform/android/io/flutter/embedding/engine/mutatorsstack/FlutterMutatorView.java +++ b/shell/platform/android/io/flutter/embedding/engine/mutatorsstack/FlutterMutatorView.java @@ -3,6 +3,7 @@ import static android.view.View.OnFocusChangeListener; import android.annotation.SuppressLint; +import android.annotation.TargetApi; import android.content.Context; import android.graphics.Canvas; import android.graphics.Matrix; @@ -22,6 +23,7 @@ * A view that applies the {@link io.flutter.embedding.engine.mutatorsstack.FlutterMutatorsStack} to * its children. */ +@TargetApi(19) public class FlutterMutatorView extends FrameLayout { private FlutterMutatorsStack mutatorsStack; private float screenDensity; diff --git a/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java b/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java index ff71c3d28df9b..81eea4ea2ac3c 100644 --- a/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java +++ b/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java @@ -355,6 +355,7 @@ public long id() { } @Override + @TargetApi(19) public void release() { if (released) { return; @@ -368,6 +369,7 @@ public void release() { } @Override + @TargetApi(19) public void pushImage(Image image) { if (released) { return; @@ -431,6 +433,7 @@ public Image acquireLatestImage() { } @Override + @TargetApi(19) protected void finalize() throws Throwable { try { if (released) { diff --git a/shell/platform/android/io/flutter/plugin/localization/LocalizationPlugin.java b/shell/platform/android/io/flutter/plugin/localization/LocalizationPlugin.java index 4ce63975928be..acb1e904ca629 100644 --- a/shell/platform/android/io/flutter/plugin/localization/LocalizationPlugin.java +++ b/shell/platform/android/io/flutter/plugin/localization/LocalizationPlugin.java @@ -7,6 +7,7 @@ import android.annotation.SuppressLint; import android.content.Context; import android.content.res.Configuration; +import android.content.res.Resources; import android.os.Build; import android.os.LocaleList; import androidx.annotation.NonNull; @@ -39,9 +40,18 @@ public String getStringResource(@NonNull String key, @Nullable String localeStri Locale locale = localeFromString(localeString); // setLocale and createConfigurationContext is only available on API >= 17 - Configuration config = new Configuration(context.getResources().getConfiguration()); - config.setLocale(locale); - localContext = context.createConfigurationContext(config); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + Configuration config = new Configuration(context.getResources().getConfiguration()); + config.setLocale(locale); + localContext = context.createConfigurationContext(config); + } else { + // In API < 17, we have to update the locale in Configuration. + Resources resources = context.getResources(); + Configuration config = resources.getConfiguration(); + savedLocale = config.locale; + config.locale = locale; + resources.updateConfiguration(config, null); + } } String packageName = context.getPackageName(); @@ -51,6 +61,14 @@ public String getStringResource(@NonNull String key, @Nullable String localeStri stringToReturn = localContext.getResources().getString(resId); } + // In API < 17, we had to restore the original locale after using. + if (localeString != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { + Resources resources = context.getResources(); + Configuration config = resources.getConfiguration(); + config.locale = savedLocale; + resources.updateConfiguration(config, null); + } + return stringToReturn; } }; diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java b/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java index aff13bd3261a5..41ffa23853c7c 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java @@ -282,7 +282,8 @@ private void setSystemChromeEnabledSystemUIMode(PlatformChannel.SystemUiMode sys | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN; - } else if (systemUiMode == PlatformChannel.SystemUiMode.IMMERSIVE) { + } else if (systemUiMode == PlatformChannel.SystemUiMode.IMMERSIVE + && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // IMMERSIVE // Available starting at 19 // Should not show overlays, swipe from edges to reveal overlays, needs onChange callback @@ -297,7 +298,8 @@ private void setSystemChromeEnabledSystemUIMode(PlatformChannel.SystemUiMode sys | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN; - } else if (systemUiMode == PlatformChannel.SystemUiMode.IMMERSIVE_STICKY) { + } else if (systemUiMode == PlatformChannel.SystemUiMode.IMMERSIVE_STICKY + && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // STICKY IMMERSIVE // Available starting at 19 // Should not show overlays, swipe from edges to reveal overlays. The app will also receive @@ -344,7 +346,7 @@ private void setSystemChromeEnabledSystemUIOverlays( // The SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag was introduced in API 19, so we // apply it // if desired, and if the current Android version is 19 or greater. - if (overlaysToShow.size() == 0) { + if (overlaysToShow.size() == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { enabledOverlays |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; } diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java index f3edd6492b6b6..6db1564f58469 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -152,6 +152,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega private final PlatformViewsChannel.PlatformViewsHandler channelHandler = new PlatformViewsChannel.PlatformViewsHandler() { + @TargetApi(19) @Override // TODO(egarciad): Remove the need for this. // https://github.com/flutter/flutter/issues/96679 @@ -414,6 +415,7 @@ public void onTouch(@NonNull PlatformViewsChannel.PlatformViewTouch touch) { view.dispatchTouchEvent(event); } + @TargetApi(17) @Override public void setDirection(int viewId, int direction) { if (!validateDirection(direction)) { @@ -498,6 +500,7 @@ private void ensureValidRequest( // Creates a platform view based on `request`, performs configuration that's common to // all display modes, and adds it to `platformViews`. + @TargetApi(19) @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) public PlatformView createPlatformView( @NonNull PlatformViewsChannel.PlatformViewCreationRequest request, boolean wrapContext) { @@ -1081,6 +1084,7 @@ private void initializeRootImageViewIfNeeded() { * testing. */ @VisibleForTesting + @TargetApi(Build.VERSION_CODES.KITKAT) void initializePlatformViewIfNeeded(int viewId) { final PlatformView platformView = platformViews.get(viewId); if (platformView == null) { @@ -1292,6 +1296,7 @@ private void finishFrame(boolean isFrameRenderedUsingImageReaders) { * for public use, and is only visible for testing. */ @VisibleForTesting + @TargetApi(19) @NonNull public FlutterOverlaySurface createOverlaySurface(@NonNull PlatformOverlayView imageView) { final int id = nextOverlayLayerId++; @@ -1306,6 +1311,7 @@ public FlutterOverlaySurface createOverlaySurface(@NonNull PlatformOverlayView i * *

This member is not intended for public use, and is only visible for testing. */ + @TargetApi(19) @NonNull public FlutterOverlaySurface createOverlaySurface() { // Overlay surfaces have the same size as the background surface. diff --git a/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java b/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java index c272f859286ee..b7ca559ac7c64 100644 --- a/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java +++ b/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java @@ -7,6 +7,7 @@ import static android.content.Context.WINDOW_SERVICE; import static android.view.View.OnFocusChangeListener; +import android.annotation.TargetApi; import android.app.AlertDialog; import android.app.Presentation; import android.content.Context; @@ -14,6 +15,7 @@ import android.content.MutableContextWrapper; import android.graphics.Rect; import android.graphics.drawable.ColorDrawable; +import android.os.Build; import android.os.Bundle; import android.view.Display; import android.view.Gravity; @@ -49,6 +51,7 @@ * EmbeddedView */ @Keep +@TargetApi(Build.VERSION_CODES.KITKAT) class SingleViewPresentation extends Presentation { private static final String TAG = "PlatformViewsController"; @@ -120,7 +123,9 @@ public SingleViewPresentation( .setFlags( WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); - getWindow().setType(WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + getWindow().setType(WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION); + } } /** diff --git a/shell/platform/android/io/flutter/util/ViewUtils.java b/shell/platform/android/io/flutter/util/ViewUtils.java index abcd807714dcd..7915f28ff427d 100644 --- a/shell/platform/android/io/flutter/util/ViewUtils.java +++ b/shell/platform/android/io/flutter/util/ViewUtils.java @@ -7,6 +7,7 @@ import android.app.Activity; import android.content.Context; import android.content.ContextWrapper; +import android.os.Build; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; @@ -67,7 +68,10 @@ public static Activity getActivity(@Nullable Context context) { * @return the view id. */ public static int generateViewId(int fallbackId) { - return View.generateViewId(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + return View.generateViewId(); + } + return fallbackId; } /** diff --git a/shell/platform/android/io/flutter/view/AccessibilityBridge.java b/shell/platform/android/io/flutter/view/AccessibilityBridge.java index 543254d64bef9..c7c249713fff8 100644 --- a/shell/platform/android/io/flutter/view/AccessibilityBridge.java +++ b/shell/platform/android/io/flutter/view/AccessibilityBridge.java @@ -379,6 +379,7 @@ public void onAccessibilityStateChanged(boolean accessibilityEnabled) { // Listener that is notified when accessibility touch exploration is turned on/off. // This is guarded at instantiation time. + @TargetApi(19) @RequiresApi(19) private final AccessibilityManager.TouchExplorationStateChangeListener touchExplorationStateChangeListener; @@ -399,8 +400,10 @@ public void onChange(boolean selfChange, Uri uri) { } // Retrieve the current value of TRANSITION_ANIMATION_SCALE from the OS. String value = - Settings.Global.getString( - contentResolver, Settings.Global.TRANSITION_ANIMATION_SCALE); + Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1 + ? null + : Settings.Global.getString( + contentResolver, Settings.Global.TRANSITION_ANIMATION_SCALE); boolean shouldAnimationsBeDisabled = value != null && value.equals("0"); if (shouldAnimationsBeDisabled) { @@ -448,34 +451,40 @@ public AccessibilityBridge( // Tell Flutter whether touch exploration is initially active or not. Then register a listener // to be notified of changes in the future. - touchExplorationStateChangeListener = - new AccessibilityManager.TouchExplorationStateChangeListener() { - @Override - public void onTouchExplorationStateChanged(boolean isTouchExplorationEnabled) { - if (isReleased) { - return; - } - if (!isTouchExplorationEnabled) { - setAccessibleNavigation(false); - onTouchExplorationExit(); - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + touchExplorationStateChangeListener = + new AccessibilityManager.TouchExplorationStateChangeListener() { + @Override + public void onTouchExplorationStateChanged(boolean isTouchExplorationEnabled) { + if (isReleased) { + return; + } + if (!isTouchExplorationEnabled) { + setAccessibleNavigation(false); + onTouchExplorationExit(); + } - if (onAccessibilityChangeListener != null) { - onAccessibilityChangeListener.onAccessibilityChanged( - accessibilityManager.isEnabled(), isTouchExplorationEnabled); + if (onAccessibilityChangeListener != null) { + onAccessibilityChangeListener.onAccessibilityChanged( + accessibilityManager.isEnabled(), isTouchExplorationEnabled); + } } - } - }; - touchExplorationStateChangeListener.onTouchExplorationStateChanged( - accessibilityManager.isTouchExplorationEnabled()); - this.accessibilityManager.addTouchExplorationStateChangeListener( - touchExplorationStateChangeListener); + }; + touchExplorationStateChangeListener.onTouchExplorationStateChanged( + accessibilityManager.isTouchExplorationEnabled()); + this.accessibilityManager.addTouchExplorationStateChangeListener( + touchExplorationStateChangeListener); + } else { + touchExplorationStateChangeListener = null; + } // Tell Flutter whether animations should initially be enabled or disabled. Then register a // listener to be notified of changes in the future. - animationScaleObserver.onChange(false); - Uri transitionUri = Settings.Global.getUriFor(Settings.Global.TRANSITION_ANIMATION_SCALE); - this.contentResolver.registerContentObserver(transitionUri, false, animationScaleObserver); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + animationScaleObserver.onChange(false); + Uri transitionUri = Settings.Global.getUriFor(Settings.Global.TRANSITION_ANIMATION_SCALE); + this.contentResolver.registerContentObserver(transitionUri, false, animationScaleObserver); + } // Tells Flutter whether the text should be bolded or not. If the user changes bold text // setting, the configuration will change and trigger a re-build of the accesibiltyBridge. @@ -498,8 +507,10 @@ public void release() { platformViewsAccessibilityDelegate.detachAccessibilityBridge(); setOnAccessibilityChangeListener(null); accessibilityManager.removeAccessibilityStateChangeListener(accessibilityStateChangeListener); - accessibilityManager.removeTouchExplorationStateChangeListener( - touchExplorationStateChangeListener); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + accessibilityManager.removeTouchExplorationStateChangeListener( + touchExplorationStateChangeListener); + } contentResolver.unregisterContentObserver(animationScaleObserver); accessibilityChannel.setAccessibilityMessageHandler(null); } @@ -658,7 +669,9 @@ public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) { } // Work around for https://github.com/flutter/flutter/issues/2101 - result.setViewIdResourceName(""); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + result.setViewIdResourceName(""); + } result.setPackageName(rootAccessibilityView.getContext().getPackageName()); result.setClassName("android.view.View"); result.setSource(rootAccessibilityView, virtualViewId); @@ -676,16 +689,20 @@ public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) { if (!semanticsNode.hasFlag(Flag.IS_READ_ONLY)) { result.setClassName("android.widget.EditText"); } - result.setEditable(!semanticsNode.hasFlag(Flag.IS_READ_ONLY)); - if (semanticsNode.textSelectionBase != -1 && semanticsNode.textSelectionExtent != -1) { - result.setTextSelection(semanticsNode.textSelectionBase, semanticsNode.textSelectionExtent); - } - // Text fields will always be created as a live region when they have input focus, - // so that updates to the label trigger polite announcements. This makes it easy to - // follow a11y guidelines for text fields on Android. - if (accessibilityFocusedSemanticsNode != null - && accessibilityFocusedSemanticsNode.id == virtualViewId) { - result.setLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + result.setEditable(!semanticsNode.hasFlag(Flag.IS_READ_ONLY)); + if (semanticsNode.textSelectionBase != -1 && semanticsNode.textSelectionExtent != -1) { + result.setTextSelection( + semanticsNode.textSelectionBase, semanticsNode.textSelectionExtent); + } + // Text fields will always be created as a live region when they have input focus, + // so that updates to the label trigger polite announcements. This makes it easy to + // follow a11y guidelines for text fields on Android. + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2 + && accessibilityFocusedSemanticsNode != null + && accessibilityFocusedSemanticsNode.id == virtualViewId) { + result.setLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE); + } } // Cursor movements @@ -720,17 +737,19 @@ public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) { // These are non-ops on older devices. Attempting to interact with the text will cause Talkback // to read the contents of the text box instead. - if (semanticsNode.hasAction(Action.SET_SELECTION)) { - result.addAction(AccessibilityNodeInfo.ACTION_SET_SELECTION); - } - if (semanticsNode.hasAction(Action.COPY)) { - result.addAction(AccessibilityNodeInfo.ACTION_COPY); - } - if (semanticsNode.hasAction(Action.CUT)) { - result.addAction(AccessibilityNodeInfo.ACTION_CUT); - } - if (semanticsNode.hasAction(Action.PASTE)) { - result.addAction(AccessibilityNodeInfo.ACTION_PASTE); + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) { + if (semanticsNode.hasAction(Action.SET_SELECTION)) { + result.addAction(AccessibilityNodeInfo.ACTION_SET_SELECTION); + } + if (semanticsNode.hasAction(Action.COPY)) { + result.addAction(AccessibilityNodeInfo.ACTION_COPY); + } + if (semanticsNode.hasAction(Action.CUT)) { + result.addAction(AccessibilityNodeInfo.ACTION_CUT); + } + if (semanticsNode.hasAction(Action.PASTE)) { + result.addAction(AccessibilityNodeInfo.ACTION_PASTE); + } } // Set text API isn't available until API 21. @@ -748,7 +767,8 @@ public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) { // TODO(jonahwilliams): Figure out a way conform to the expected id from TalkBack's // CustomLabelManager. talkback/src/main/java/labeling/CustomLabelManager.java#L525 } - if (semanticsNode.hasAction(Action.DISMISS)) { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2 + && semanticsNode.hasAction(Action.DISMISS)) { result.setDismissable(true); result.addAction(AccessibilityNodeInfo.ACTION_DISMISS); } @@ -842,7 +862,8 @@ && shouldSetCollectionInfo(semanticsNode)) { result.setClassName("android.widget.HorizontalScrollView"); } } else { - if (shouldSetCollectionInfo(semanticsNode)) { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2 + && shouldSetCollectionInfo(semanticsNode)) { result.setCollectionInfo( AccessibilityNodeInfo.CollectionInfo.obtain( semanticsNode.scrollChildren, // rows @@ -877,7 +898,8 @@ && shouldSetCollectionInfo(semanticsNode)) { result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); } } - if (semanticsNode.hasFlag(Flag.IS_LIVE_REGION)) { + if (semanticsNode.hasFlag(Flag.IS_LIVE_REGION) + && Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) { result.setLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE); } @@ -1089,10 +1111,26 @@ public boolean performAction( } case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: { + // Text selection APIs aren't available until API 18. We can't handle the case here so + // return false + // instead. It's extremely unlikely that this case would ever be triggered in the first + // place in API < + // 18. + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { + return false; + } return performCursorMoveAction(semanticsNode, virtualViewId, arguments, false); } case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: { + // Text selection APIs aren't available until API 18. We can't handle the case here so + // return false + // instead. It's extremely unlikely that this case would ever be triggered in the first + // place in API < + // 18. + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { + return false; + } return performCursorMoveAction(semanticsNode, virtualViewId, arguments, true); } case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: @@ -1145,6 +1183,14 @@ public boolean performAction( } case AccessibilityNodeInfo.ACTION_SET_SELECTION: { + // Text selection APIs aren't available until API 18. We can't handle the case here so + // return false + // instead. It's extremely unlikely that this case would ever be triggered in the first + // place in API < + // 18. + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { + return false; + } final Map selection = new HashMap<>(); final boolean hasSelection = arguments != null @@ -1220,6 +1266,7 @@ public boolean performAction( * Handles the responsibilities of {@link #performAction(int, int, Bundle)} for the specific * scenario of cursor movement. */ + @TargetApi(18) @RequiresApi(18) private boolean performCursorMoveAction( @NonNull SemanticsNode semanticsNode, @@ -1949,7 +1996,9 @@ private void setAccessibilityPaneTitle(String title) { private void sendWindowContentChangeEvent(int virtualViewId) { AccessibilityEvent event = obtainAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); - event.setContentChangeTypes(AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + event.setContentChangeTypes(AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE); + } sendAccessibilityEvent(event); } @@ -2000,6 +2049,7 @@ private boolean doesLayoutInDisplayCutoutModeRequireLeftInset() { * Hook called just before a {@link SemanticsNode} is removed from the Android cache of Flutter's * semantics tree. */ + @TargetApi(19) @RequiresApi(19) private void willRemoveSemanticsNode(SemanticsNode semanticsNodeToBeRemoved) { if (BuildConfig.DEBUG) { diff --git a/shell/platform/android/io/flutter/view/AccessibilityViewEmbedder.java b/shell/platform/android/io/flutter/view/AccessibilityViewEmbedder.java index eb4981d0533d1..368e14ce5fc29 100644 --- a/shell/platform/android/io/flutter/view/AccessibilityViewEmbedder.java +++ b/shell/platform/android/io/flutter/view/AccessibilityViewEmbedder.java @@ -230,16 +230,20 @@ private void copyAccessibilityFields( output.setText(input.getText()); output.setVisibleToUser(input.isVisibleToUser()); - output.setEditable(input.isEditable()); - output.setCanOpenPopup(input.canOpenPopup()); - output.setCollectionInfo(input.getCollectionInfo()); - output.setCollectionItemInfo(input.getCollectionItemInfo()); - output.setContentInvalid(input.isContentInvalid()); - output.setDismissable(input.isDismissable()); - output.setInputType(input.getInputType()); - output.setLiveRegion(input.getLiveRegion()); - output.setMultiLine(input.isMultiLine()); - output.setRangeInfo(input.getRangeInfo()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + output.setEditable(input.isEditable()); + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + output.setCanOpenPopup(input.canOpenPopup()); + output.setCollectionInfo(input.getCollectionInfo()); + output.setCollectionItemInfo(input.getCollectionItemInfo()); + output.setContentInvalid(input.isContentInvalid()); + output.setDismissable(input.isDismissable()); + output.setInputType(input.getInputType()); + output.setLiveRegion(input.getLiveRegion()); + output.setMultiLine(input.isMultiLine()); + output.setRangeInfo(input.getRangeInfo()); + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { output.setError(input.getError()); output.setMaxTextLength(input.getMaxTextLength()); diff --git a/shell/platform/android/io/flutter/view/VsyncWaiter.java b/shell/platform/android/io/flutter/view/VsyncWaiter.java index b99b089a5c55e..80f5658dea569 100644 --- a/shell/platform/android/io/flutter/view/VsyncWaiter.java +++ b/shell/platform/android/io/flutter/view/VsyncWaiter.java @@ -4,6 +4,7 @@ package io.flutter.view; +import android.annotation.TargetApi; import android.hardware.display.DisplayManager; import android.view.Choreographer; import android.view.Display; @@ -13,6 +14,7 @@ // TODO(mattcarroll): add javadoc. public class VsyncWaiter { + @TargetApi(17) class DisplayListener implements DisplayManager.DisplayListener { DisplayListener(DisplayManager displayManager) { this.displayManager = displayManager; @@ -57,6 +59,7 @@ public static VsyncWaiter getInstance(float fps, @NonNull FlutterJNI flutterJNI) return instance; } + @TargetApi(17) @NonNull public static VsyncWaiter getInstance( @NonNull DisplayManager displayManager, @NonNull FlutterJNI flutterJNI) { diff --git a/shell/platform/android/test/io/flutter/view/AccessibilityBridgeTest.java b/shell/platform/android/test/io/flutter/view/AccessibilityBridgeTest.java index ddcbe6842666f..28077a2c6877e 100644 --- a/shell/platform/android/test/io/flutter/view/AccessibilityBridgeTest.java +++ b/shell/platform/android/test/io/flutter/view/AccessibilityBridgeTest.java @@ -64,6 +64,7 @@ @Config(manifest = Config.NONE) @RunWith(AndroidJUnit4.class) +@TargetApi(19) public class AccessibilityBridgeTest { @Test diff --git a/shell/platform/android/test/io/flutter/view/VsyncWaiterTest.java b/shell/platform/android/test/io/flutter/view/VsyncWaiterTest.java index ef56b9936b87b..9afeb9c5deebb 100644 --- a/shell/platform/android/test/io/flutter/view/VsyncWaiterTest.java +++ b/shell/platform/android/test/io/flutter/view/VsyncWaiterTest.java @@ -14,6 +14,7 @@ import static org.mockito.Mockito.when; import static org.robolectric.Shadows.shadowOf; +import android.annotation.TargetApi; import android.hardware.display.DisplayManager; import android.os.Looper; import android.view.Display; @@ -49,6 +50,7 @@ public void itSetsFpsBelowApi17() { verify(mockFlutterJNI, times(1)).onVsync(anyLong(), eq(1000000000l / 10l), eq(1l)); } + @TargetApi(17) @Test public void itSetsFpsWhenDisplayManagerUpdates() { FlutterJNI mockFlutterJNI = mock(FlutterJNI.class); @@ -84,6 +86,7 @@ public void itSetsFpsWhenDisplayManagerUpdates() { verify(mockFlutterJNI, times(1)).onVsync(anyLong(), eq(1000000000l / 60l), eq(1l)); } + @TargetApi(17) @Test public void itSetsFpsWhenDisplayManagerDoesNotUpdate() { FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);