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

Commit e099277

Browse files
authored
Use io.flutter.Build.API_LEVELS rather than android.os.Build.VERSION_CODES (#51171)
Updates the linting script to ban the use of `VERSION_CODES`. We currently have a mish-mash of using the integers, using `VERSION_CODES`, and even how we import the version codes. This makes it more confusing when doing things like #51070 - I think it is clearer to see `22` than `LOLLIPOP_MR1`. I'd like to get LGTM (or at least no opinion) from all the requested reviewers here.
1 parent 05fdf94 commit e099277

File tree

63 files changed

+390
-269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+390
-269
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39011,6 +39011,7 @@ ORIGIN: ../../../flutter/shell/platform/android/image_external_texture_gl.h + ..
3901139011
ORIGIN: ../../../flutter/shell/platform/android/image_external_texture_vk.h + ../../../flutter/LICENSE
3901239012
ORIGIN: ../../../flutter/shell/platform/android/image_lru.cc + ../../../flutter/LICENSE
3901339013
ORIGIN: ../../../flutter/shell/platform/android/image_lru.h + ../../../flutter/LICENSE
39014+
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/Build.java + ../../../flutter/LICENSE
3901439015
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/BuildConfig.java + ../../../flutter/LICENSE
3901539016
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/FlutterInjector.java + ../../../flutter/LICENSE
3901639017
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/Log.java + ../../../flutter/LICENSE
@@ -41869,6 +41870,7 @@ FILE: ../../../flutter/shell/platform/android/image_external_texture_vk.cc
4186941870
FILE: ../../../flutter/shell/platform/android/image_external_texture_vk.h
4187041871
FILE: ../../../flutter/shell/platform/android/image_lru.cc
4187141872
FILE: ../../../flutter/shell/platform/android/image_lru.h
41873+
FILE: ../../../flutter/shell/platform/android/io/flutter/Build.java
4187241874
FILE: ../../../flutter/shell/platform/android/io/flutter/BuildConfig.java
4187341875
FILE: ../../../flutter/shell/platform/android/io/flutter/FlutterInjector.java
4187441876
FILE: ../../../flutter/shell/platform/android/io/flutter/Log.java

shell/platform/android/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ embedding_sources_jar_filename = "$embedding_artifact_id-sources.jar"
194194
embedding_source_jar_path = "$root_out_dir/$embedding_sources_jar_filename"
195195

196196
android_java_sources = [
197+
"io/flutter/Build.java",
197198
"io/flutter/FlutterInjector.java",
198199
"io/flutter/Log.java",
199200
"io/flutter/app/FlutterActivity.java",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter;
6+
7+
/** A replacement of utilities from android.os.Build. */
8+
public class Build {
9+
/** For use in place of the Android Build.VERSION_CODES class. */
10+
public static class API_LEVELS {
11+
public static final int API_21 = 21;
12+
public static final int API_22 = 22;
13+
public static final int API_23 = 23;
14+
public static final int API_24 = 24;
15+
public static final int API_25 = 25;
16+
public static final int API_26 = 26;
17+
public static final int API_27 = 27;
18+
public static final int API_28 = 28;
19+
public static final int API_29 = 29;
20+
public static final int API_30 = 30;
21+
public static final int API_31 = 31;
22+
public static final int API_32 = 32;
23+
public static final int API_33 = 33;
24+
public static final int API_34 = 34;
25+
public static final int API_35 = 35;
26+
}
27+
}

shell/platform/android/io/flutter/embedding/android/AndroidTouchProcessor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.flutter.embedding.android;
22

3+
import static io.flutter.Build.API_LEVELS;
4+
35
import android.annotation.TargetApi;
46
import android.content.Context;
57
import android.graphics.Matrix;
@@ -427,7 +429,7 @@ private void addPointerForIndex(
427429
}
428430

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

438440
private float getVerticalScrollFactor(@NonNull Context context) {
439-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
441+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_26) {
440442
return getVerticalScrollFactorAbove26(context);
441443
} else {
442444
return getVerticalScrollFactorPre26(context);
443445
}
444446
}
445447

446-
@TargetApi(26)
448+
@TargetApi(API_LEVELS.API_26)
447449
private float getVerticalScrollFactorAbove26(@NonNull Context context) {
448450
return ViewConfiguration.get(context).getScaledVerticalScrollFactor();
449451
}

shell/platform/android/io/flutter/embedding/android/FlutterActivity.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package io.flutter.embedding.android;
66

7+
import static io.flutter.Build.API_LEVELS;
78
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.DART_ENTRYPOINT_META_DATA_KEY;
89
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.DART_ENTRYPOINT_URI_META_DATA_KEY;
910
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.DEFAULT_BACKGROUND_MODE;
@@ -654,7 +655,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
654655
*/
655656
@VisibleForTesting
656657
public void registerOnBackInvokedCallback() {
657-
if (Build.VERSION.SDK_INT >= 33) {
658+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_33) {
658659
getOnBackInvokedDispatcher()
659660
.registerOnBackInvokedCallback(
660661
OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback);
@@ -670,14 +671,14 @@ public void registerOnBackInvokedCallback() {
670671
*/
671672
@VisibleForTesting
672673
public void unregisterOnBackInvokedCallback() {
673-
if (Build.VERSION.SDK_INT >= 33) {
674+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_33) {
674675
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback);
675676
hasRegisteredBackCallback = false;
676677
}
677678
}
678679

679680
private final OnBackInvokedCallback onBackInvokedCallback =
680-
Build.VERSION.SDK_INT >= 33
681+
Build.VERSION.SDK_INT >= API_LEVELS.API_33
681682
? new OnBackInvokedCallback() {
682683
// TODO(garyq): Remove SuppressWarnings annotation. This was added to workaround
683684
// a google3 bug where the linter is not properly running against API 33, causing
@@ -1369,7 +1370,7 @@ public void onFlutterUiDisplayed() {
13691370
// * reportFullyDrawn behavior isn't tested on pre-Q versions.
13701371
// See https://github.com/flutter/flutter/issues/46172, and
13711372
// https://github.com/flutter/flutter/issues/88767.
1372-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
1373+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_29) {
13731374
reportFullyDrawn();
13741375
}
13751376
}

shell/platform/android/io/flutter/embedding/android/FlutterImageView.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package io.flutter.embedding.android;
66

7+
import static io.flutter.Build.API_LEVELS;
8+
79
import android.annotation.SuppressLint;
810
import android.annotation.TargetApi;
911
import android.content.Context;
@@ -109,7 +111,7 @@ private static ImageReader createImageReader(int width, int height) {
109111
logW("ImageReader height must be greater than 0, but given height=%d, set height=1", height);
110112
height = 1;
111113
}
112-
if (android.os.Build.VERSION.SDK_INT >= 29) {
114+
if (android.os.Build.VERSION.SDK_INT >= API_LEVELS.API_29) {
113115
return ImageReader.newInstance(
114116
width,
115117
height,
@@ -251,9 +253,9 @@ private void closeCurrentImage() {
251253
}
252254
}
253255

254-
@TargetApi(29)
256+
@TargetApi(API_LEVELS.API_29)
255257
private void updateCurrentBitmap() {
256-
if (android.os.Build.VERSION.SDK_INT >= 29) {
258+
if (android.os.Build.VERSION.SDK_INT >= API_LEVELS.API_29) {
257259
final HardwareBuffer buffer = currentImage.getHardwareBuffer();
258260
currentBitmap = Bitmap.wrapHardwareBuffer(buffer, ColorSpace.get(ColorSpace.Named.SRGB));
259261
buffer.close();

shell/platform/android/io/flutter/embedding/android/FlutterView.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package io.flutter.embedding.android;
66

7+
import static io.flutter.Build.API_LEVELS;
8+
79
import android.annotation.SuppressLint;
810
import android.annotation.TargetApi;
911
import android.app.Activity;
@@ -385,7 +387,7 @@ private void init() {
385387
// FlutterView needs to be focusable so that the InputMethodManager can interact with it.
386388
setFocusable(true);
387389
setFocusableInTouchMode(true);
388-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
390+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_26) {
389391
setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_YES);
390392
}
391393
}
@@ -532,7 +534,7 @@ protected void onDetachedFromWindow() {
532534
* Refresh {@link androidx.window.layout.WindowInfoTracker} and {@link android.view.DisplayCutout}
533535
* display features. Fold, hinge and cutout areas are populated here.
534536
*/
535-
@TargetApi(28)
537+
@TargetApi(API_LEVELS.API_28)
536538
protected void setWindowInfoListenerDisplayFeatures(WindowLayoutInfo layoutInfo) {
537539
List<DisplayFeature> displayFeatures = layoutInfo.getDisplayFeatures();
538540
List<FlutterRenderer.DisplayFeature> result = new ArrayList<>();
@@ -574,7 +576,7 @@ protected void setWindowInfoListenerDisplayFeatures(WindowLayoutInfo layoutInfo)
574576

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

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

692-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
694+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_30) {
693695
int mask = 0;
694696
if (navigationBarVisible) {
695697
mask = mask | android.view.WindowInsets.Type.navigationBars();
@@ -956,7 +958,7 @@ public AccessibilityNodeProvider getAccessibilityNodeProvider() {
956958
@SuppressLint("SoonBlockedPrivateApi")
957959
@Nullable
958960
public View findViewByAccessibilityIdTraversal(int accessibilityId) {
959-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
961+
if (Build.VERSION.SDK_INT < API_LEVELS.API_29) {
960962
return findViewByAccessibilityIdRootedAtCurrentView(accessibilityId, this);
961963
}
962964
// Android Q or later doesn't call this method.
@@ -1032,8 +1034,8 @@ private void resetWillNotDraw(boolean isAccessibilityEnabled, boolean isTouchExp
10321034

10331035
// -------- Start: Mouse -------
10341036
@Override
1035-
@TargetApi(Build.VERSION_CODES.N)
1036-
@RequiresApi(Build.VERSION_CODES.N)
1037+
@TargetApi(API_LEVELS.API_24)
1038+
@RequiresApi(API_LEVELS.API_24)
10371039
@NonNull
10381040
public PointerIcon getSystemPointerIcon(int type) {
10391041
return PointerIcon.getSystemIcon(getContext(), type);
@@ -1098,7 +1100,7 @@ public void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine) {
10981100

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

14141416
if (textServicesManager != null) {
1415-
if (Build.VERSION.SDK_INT >= 31) {
1417+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_31) {
14161418
List<SpellCheckerInfo> enabledSpellCheckerInfos =
14171419
textServicesManager.getEnabledSpellCheckerInfos();
14181420
boolean gboardSpellCheckerEnabled =

shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package io.flutter.embedding.engine;
66

7+
import static io.flutter.Build.API_LEVELS;
8+
79
import android.content.Context;
810
import android.content.res.AssetManager;
911
import android.graphics.Bitmap;
@@ -566,7 +568,7 @@ public static native void nativeImageHeaderCallback(
566568
@VisibleForTesting
567569
@Nullable
568570
public static Bitmap decodeImage(@NonNull ByteBuffer buffer, long imageGeneratorAddress) {
569-
if (Build.VERSION.SDK_INT >= 28) {
571+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_28) {
570572
ImageDecoder.Source source = ImageDecoder.createSource(buffer);
571573
try {
572574
return ImageDecoder.decodeBitmap(

shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package io.flutter.embedding.engine.loader;
66

7+
import static io.flutter.Build.API_LEVELS;
8+
79
import android.app.ActivityManager;
810
import android.content.Context;
911
import android.content.pm.ApplicationInfo;
@@ -203,7 +205,7 @@ public InitResult call() {
203205
}
204206

205207
private static boolean areValidationLayersOnByDefault() {
206-
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
208+
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= API_LEVELS.API_26) {
207209
return Build.SUPPORTED_ABIS[0].equals("arm64-v8a");
208210
}
209211
return false;

shell/platform/android/io/flutter/embedding/engine/loader/ResourceExtractor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package io.flutter.embedding.engine.loader;
66

7+
import static io.flutter.Build.API_LEVELS;
8+
79
import android.content.pm.PackageInfo;
810
import android.content.pm.PackageManager;
911
import android.content.res.AssetManager;
@@ -28,7 +30,7 @@ class ResourceExtractor {
2830
@SuppressWarnings("deprecation")
2931
static long getVersionCode(@NonNull PackageInfo packageInfo) {
3032
// Linter needs P (28) hardcoded or else it will fail these lines.
31-
if (Build.VERSION.SDK_INT >= 28) {
33+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_28) {
3234
return packageInfo.getLongVersionCode();
3335
} else {
3436
return packageInfo.versionCode;

0 commit comments

Comments
 (0)