Flutter Engine
The Flutter Engine
Classes | Static Public Member Functions | List of all members
io.flutter.util.ViewUtils Class Reference

Classes

interface  DisplayUpdater
 
interface  ViewVisitor
 

Static Public Member Functions

static void calculateMaximumDisplayMetrics ( @Nullable Context context, @NonNull DisplayUpdater updater)
 
static Activity getActivity (@Nullable Context context)
 
static boolean childHasFocus (@Nullable View root)
 
static boolean hasChildViewOfType (@Nullable View root, Class<? extends View >[] viewTypes)
 
static boolean traverseHierarchy (@Nullable View root, @NonNull ViewVisitor visitor)
 

Detailed Description

Definition at line 17 of file ViewUtils.java.

Member Function Documentation

◆ calculateMaximumDisplayMetrics()

static void io.flutter.util.ViewUtils.calculateMaximumDisplayMetrics ( @Nullable Context  context,
@NonNull DisplayUpdater  updater 
)
inlinestatic

Calculates the maximum display metrics for the given context, and pushes the metric data to the updater.

Definition at line 27 of file ViewUtils.java.

28 {
29 Activity activity = getActivity(context);
30 if (activity != null) {
31 WindowMetrics metrics =
32 WindowMetricsCalculator.getOrCreate().computeMaximumWindowMetrics(activity);
33 float width = metrics.getBounds().width();
34 float height = metrics.getBounds().height();
35 float density = context.getResources().getDisplayMetrics().density;
36 updater.updateDisplayMetrics(width, height, density);
37 }
38 }
static Activity getActivity(@Nullable Context context)
Definition: ViewUtils.java:47
int32_t height
int32_t width

◆ childHasFocus()

static boolean io.flutter.util.ViewUtils.childHasFocus ( @Nullable View  root)
inlinestatic

Determines if the current view or any descendant view has focus.

Parameters
rootThe root view.
Returns
True if the current view or any descendant view has focus.

Definition at line 67 of file ViewUtils.java.

67 {
68 return traverseHierarchy(root, (View view) -> view.hasFocus());
69 }
static boolean traverseHierarchy(@Nullable View root, @NonNull ViewVisitor visitor)
Definition: ViewUtils.java:109
string root
Definition: scale_cpu.py:20

◆ getActivity()

static Activity io.flutter.util.ViewUtils.getActivity ( @Nullable Context  context)
inlinestatic

Retrieves the Activity from a given Context.

This method will recursively traverse up the context chain if it is a ContextWrapper until it finds the first instance of the base context that is an Activity.

Definition at line 47 of file ViewUtils.java.

47 {
48 if (context == null) {
49 return null;
50 }
51 if (context instanceof Activity) {
52 return (Activity) context;
53 }
54 if (context instanceof ContextWrapper) {
55 // Recurse up chain of base contexts until we find an Activity.
56 return getActivity(((ContextWrapper) context).getBaseContext());
57 }
58 return null;
59 }

◆ hasChildViewOfType()

static boolean io.flutter.util.ViewUtils.hasChildViewOfType ( @Nullable View  root,
Class<? extends View >[]  viewTypes 
)
inlinestatic

Returns true if the root or any child view is an instance of the given types.

Parameters
rootThe root view.
viewTypesThe types of views.
Returns
true if any child view is an instance of any of the given types.

Definition at line 78 of file ViewUtils.java.

78 {
79 return traverseHierarchy(
80 root,
81 (View view) -> {
82 for (int i = 0; i < viewTypes.length; i++) {
83 final Class<? extends View> viewType = viewTypes[i];
84 if (viewType.isInstance(view)) {
85 return true;
86 }
87 }
88 return false;
89 });
90 }

◆ traverseHierarchy()

static boolean io.flutter.util.ViewUtils.traverseHierarchy ( @Nullable View  root,
@NonNull ViewVisitor  visitor 
)
inlinestatic

Traverses the view hierarchy in pre-order and runs the visitor for each child view including the root view.

If the visitor returns true, the traversal stops, and the method returns true.

If the visitor returns false, the traversal continues until all views are visited.

Parameters
rootThe root view.
visitorThe visitor.
Returns
true if the visitor returned true for a given view.

Definition at line 109 of file ViewUtils.java.

109 {
110 if (root == null) {
111 return false;
112 }
113 if (visitor.run(root)) {
114 return true;
115 }
116 if (root instanceof ViewGroup) {
117 final ViewGroup viewGroup = (ViewGroup) root;
118 for (int idx = 0; idx < viewGroup.getChildCount(); idx++) {
119 if (traverseHierarchy(viewGroup.getChildAt(idx), visitor)) {
120 return true;
121 }
122 }
123 }
124 return false;
125 }

The documentation for this class was generated from the following file: