Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Use io.flutter.Build.API_LEVELS rather than android.os.Build.VERSION_CODES #51171

Merged
merged 14 commits into from
Mar 8, 2024
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -39011,6 +39011,7 @@ ORIGIN: ../../../flutter/shell/platform/android/image_external_texture_gl.h + ..
ORIGIN: ../../../flutter/shell/platform/android/image_external_texture_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/image_lru.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/image_lru.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/Build.java + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/BuildConfig.java + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/FlutterInjector.java + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/Log.java + ../../../flutter/LICENSE
Expand Down Expand Up @@ -41869,6 +41870,7 @@ FILE: ../../../flutter/shell/platform/android/image_external_texture_vk.cc
FILE: ../../../flutter/shell/platform/android/image_external_texture_vk.h
FILE: ../../../flutter/shell/platform/android/image_lru.cc
FILE: ../../../flutter/shell/platform/android/image_lru.h
FILE: ../../../flutter/shell/platform/android/io/flutter/Build.java
FILE: ../../../flutter/shell/platform/android/io/flutter/BuildConfig.java
FILE: ../../../flutter/shell/platform/android/io/flutter/FlutterInjector.java
FILE: ../../../flutter/shell/platform/android/io/flutter/Log.java
Expand Down
1 change: 1 addition & 0 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ embedding_sources_jar_filename = "$embedding_artifact_id-sources.jar"
embedding_source_jar_path = "$root_out_dir/$embedding_sources_jar_filename"

android_java_sources = [
"io/flutter/Build.java",
"io/flutter/FlutterInjector.java",
"io/flutter/Log.java",
"io/flutter/app/FlutterActivity.java",
Expand Down
27 changes: 27 additions & 0 deletions shell/platform/android/io/flutter/Build.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter;

/** A replacement of utilities from android.os.Build. */
public class Build {
/** For use in place of the Android Build.VERSION_CODES class. */
public static class API_LEVELS {
public static final int API_21 = 21;
public static final int API_22 = 22;
public static final int API_23 = 23;
public static final int API_24 = 24;
public static final int API_25 = 25;
public static final int API_26 = 26;
public static final int API_27 = 27;
public static final int API_28 = 28;
public static final int API_29 = 29;
public static final int API_30 = 30;
public static final int API_31 = 31;
public static final int API_32 = 32;
public static final int API_33 = 33;
public static final int API_34 = 34;
public static final int API_35 = 35;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.flutter.embedding.android;

import static io.flutter.Build.API_LEVELS;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Matrix;
Expand Down Expand Up @@ -427,7 +429,7 @@ private void addPointerForIndex(
}

private float getHorizontalScrollFactor(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_26) {
return ViewConfiguration.get(context).getScaledHorizontalScrollFactor();
} else {
// Vertical scroll factor is not a typo. This is what View.java does in android.
Expand All @@ -436,14 +438,14 @@ private float getHorizontalScrollFactor(@NonNull Context context) {
}

private float getVerticalScrollFactor(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_26) {
return getVerticalScrollFactorAbove26(context);
} else {
return getVerticalScrollFactorPre26(context);
}
}

@TargetApi(26)
@TargetApi(API_LEVELS.API_26)
private float getVerticalScrollFactorAbove26(@NonNull Context context) {
return ViewConfiguration.get(context).getScaledVerticalScrollFactor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package io.flutter.embedding.android;

import static io.flutter.Build.API_LEVELS;
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.DART_ENTRYPOINT_META_DATA_KEY;
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.DART_ENTRYPOINT_URI_META_DATA_KEY;
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.DEFAULT_BACKGROUND_MODE;
Expand Down Expand Up @@ -654,7 +655,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
*/
@VisibleForTesting
public void registerOnBackInvokedCallback() {
if (Build.VERSION.SDK_INT >= 33) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_33) {
getOnBackInvokedDispatcher()
.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback);
Expand All @@ -670,14 +671,14 @@ public void registerOnBackInvokedCallback() {
*/
@VisibleForTesting
public void unregisterOnBackInvokedCallback() {
if (Build.VERSION.SDK_INT >= 33) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_33) {
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback);
hasRegisteredBackCallback = false;
}
}

private final OnBackInvokedCallback onBackInvokedCallback =
Build.VERSION.SDK_INT >= 33
Build.VERSION.SDK_INT >= API_LEVELS.API_33
? new OnBackInvokedCallback() {
// TODO(garyq): Remove SuppressWarnings annotation. This was added to workaround
// a google3 bug where the linter is not properly running against API 33, causing
Expand Down Expand Up @@ -1369,7 +1370,7 @@ public void onFlutterUiDisplayed() {
// * reportFullyDrawn behavior isn't tested on pre-Q versions.
// See https://github.com/flutter/flutter/issues/46172, and
// https://github.com/flutter/flutter/issues/88767.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_29) {
reportFullyDrawn();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package io.flutter.embedding.android;

import static io.flutter.Build.API_LEVELS;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
Expand Down Expand Up @@ -109,7 +111,7 @@ private static ImageReader createImageReader(int width, int height) {
logW("ImageReader height must be greater than 0, but given height=%d, set height=1", height);
height = 1;
}
if (android.os.Build.VERSION.SDK_INT >= 29) {
if (android.os.Build.VERSION.SDK_INT >= API_LEVELS.API_29) {
return ImageReader.newInstance(
width,
height,
Expand Down Expand Up @@ -251,9 +253,9 @@ private void closeCurrentImage() {
}
}

@TargetApi(29)
@TargetApi(API_LEVELS.API_29)
private void updateCurrentBitmap() {
if (android.os.Build.VERSION.SDK_INT >= 29) {
if (android.os.Build.VERSION.SDK_INT >= API_LEVELS.API_29) {
final HardwareBuffer buffer = currentImage.getHardwareBuffer();
currentBitmap = Bitmap.wrapHardwareBuffer(buffer, ColorSpace.get(ColorSpace.Named.SRGB));
buffer.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package io.flutter.embedding.android;

import static io.flutter.Build.API_LEVELS;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
Expand Down Expand Up @@ -385,7 +387,7 @@ private void init() {
// FlutterView needs to be focusable so that the InputMethodManager can interact with it.
setFocusable(true);
setFocusableInTouchMode(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_26) {
setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_YES);
}
}
Expand Down Expand Up @@ -532,7 +534,7 @@ protected void onDetachedFromWindow() {
* Refresh {@link androidx.window.layout.WindowInfoTracker} and {@link android.view.DisplayCutout}
* display features. Fold, hinge and cutout areas are populated here.
*/
@TargetApi(28)
@TargetApi(API_LEVELS.API_28)
protected void setWindowInfoListenerDisplayFeatures(WindowLayoutInfo layoutInfo) {
List<DisplayFeature> displayFeatures = layoutInfo.getDisplayFeatures();
List<FlutterRenderer.DisplayFeature> result = new ArrayList<>();
Expand Down Expand Up @@ -574,7 +576,7 @@ protected void setWindowInfoListenerDisplayFeatures(WindowLayoutInfo layoutInfo)

// Data from the DisplayCutout bounds. Cutouts for cameras and other sensors are
// populated here. DisplayCutout was introduced in API 28.
if (Build.VERSION.SDK_INT >= 28) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_28) {
WindowInsets insets = getRootWindowInsets();
if (insets != null) {
DisplayCutout cutout = insets.getDisplayCutout();
Expand Down Expand Up @@ -619,7 +621,7 @@ private ZeroSides calculateShouldZeroSides() {
return ZeroSides.RIGHT;
} else if (rotation == Surface.ROTATION_270) {
// In android API >= 23, the nav bar always appears on the "bottom" (USB) side.
return Build.VERSION.SDK_INT >= 23 ? ZeroSides.LEFT : ZeroSides.RIGHT;
return Build.VERSION.SDK_INT >= API_LEVELS.API_23 ? ZeroSides.LEFT : ZeroSides.RIGHT;
}
// Ambiguous orientation due to landscape left/right default. Zero both sides.
else if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
Expand Down Expand Up @@ -677,7 +679,7 @@ public final WindowInsets onApplyWindowInsets(@NonNull WindowInsets insets) {
WindowInsets newInsets = super.onApplyWindowInsets(insets);

// getSystemGestureInsets() was introduced in API 29 and immediately deprecated in 30.
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT == API_LEVELS.API_29) {
Insets systemGestureInsets = insets.getSystemGestureInsets();
viewportMetrics.systemGestureInsetTop = systemGestureInsets.top;
viewportMetrics.systemGestureInsetRight = systemGestureInsets.right;
Expand All @@ -689,7 +691,7 @@ public final WindowInsets onApplyWindowInsets(@NonNull WindowInsets insets) {
boolean navigationBarVisible =
(SYSTEM_UI_FLAG_HIDE_NAVIGATION & getWindowSystemUiVisibility()) == 0;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_30) {
int mask = 0;
if (navigationBarVisible) {
mask = mask | android.view.WindowInsets.Type.navigationBars();
Expand Down Expand Up @@ -956,7 +958,7 @@ public AccessibilityNodeProvider getAccessibilityNodeProvider() {
@SuppressLint("SoonBlockedPrivateApi")
@Nullable
public View findViewByAccessibilityIdTraversal(int accessibilityId) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT < API_LEVELS.API_29) {
return findViewByAccessibilityIdRootedAtCurrentView(accessibilityId, this);
}
// Android Q or later doesn't call this method.
Expand Down Expand Up @@ -1032,8 +1034,8 @@ private void resetWillNotDraw(boolean isAccessibilityEnabled, boolean isTouchExp

// -------- Start: Mouse -------
@Override
@TargetApi(Build.VERSION_CODES.N)
@RequiresApi(Build.VERSION_CODES.N)
@TargetApi(API_LEVELS.API_24)
@RequiresApi(API_LEVELS.API_24)
@NonNull
public PointerIcon getSystemPointerIcon(int type) {
return PointerIcon.getSystemIcon(getContext(), type);
Expand Down Expand Up @@ -1098,7 +1100,7 @@ public void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine) {

// Initialize various components that know how to process Android View I/O
// in a way that Flutter understands.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_24) {
mouseCursorPlugin = new MouseCursorPlugin(this, this.flutterEngine.getMouseCursorChannel());
}
textInputPlugin =
Expand Down Expand Up @@ -1412,7 +1414,7 @@ public void removeFlutterEngineAttachmentListener(
boolean isNativeSpellCheckServiceDefined = false;

if (textServicesManager != null) {
if (Build.VERSION.SDK_INT >= 31) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_31) {
List<SpellCheckerInfo> enabledSpellCheckerInfos =
textServicesManager.getEnabledSpellCheckerInfos();
boolean gboardSpellCheckerEnabled =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package io.flutter.embedding.engine;

import static io.flutter.Build.API_LEVELS;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -566,7 +568,7 @@ public static native void nativeImageHeaderCallback(
@VisibleForTesting
@Nullable
public static Bitmap decodeImage(@NonNull ByteBuffer buffer, long imageGeneratorAddress) {
if (Build.VERSION.SDK_INT >= 28) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_28) {
ImageDecoder.Source source = ImageDecoder.createSource(buffer);
try {
return ImageDecoder.decodeBitmap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package io.flutter.embedding.engine.loader;

import static io.flutter.Build.API_LEVELS;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
Expand Down Expand Up @@ -203,7 +205,7 @@ public InitResult call() {
}

private static boolean areValidationLayersOnByDefault() {
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= API_LEVELS.API_26) {
return Build.SUPPORTED_ABIS[0].equals("arm64-v8a");
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package io.flutter.embedding.engine.loader;

import static io.flutter.Build.API_LEVELS;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
Expand All @@ -28,7 +30,7 @@ class ResourceExtractor {
@SuppressWarnings("deprecation")
static long getVersionCode(@NonNull PackageInfo packageInfo) {
// Linter needs P (28) hardcoded or else it will fail these lines.
if (Build.VERSION.SDK_INT >= 28) {
if (Build.VERSION.SDK_INT >= API_LEVELS.API_28) {
return packageInfo.getLongVersionCode();
} else {
return packageInfo.versionCode;
Expand Down
Loading