summaryrefslogtreecommitdiff
path: root/core/java/android/window/SizeConfigurationBuckets.java
blob: 998bec0892ae01ed709f6a7481f20ae3431b3115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.window;

import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.res.Configuration;
import android.os.Parcelable;
import android.util.SparseIntArray;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.DataClass;

import java.util.Arrays;

/**
 * Contains size-configuration buckets used to prevent excessive configuration changes during
 * resize.
 *
 * These configurations are collected from application's resources based on size-sensitive
 * qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
 * and drawable-sw400dp will be added to both as 400.
 *
 * @hide
 */
@DataClass(genAidl = true)
public final class SizeConfigurationBuckets implements Parcelable {

    /** Horizontal (screenWidthDp) buckets */
    @Nullable
    private final int[] mHorizontal;

    /** Vertical (screenHeightDp) buckets */
    @Nullable
    private final int[] mVertical;

    /** Smallest (smallestScreenWidthDp) buckets */
    @Nullable
    private final int[] mSmallest;

    /** Screen Layout Size (screenLayout & SCREENLAYOUT_SIZE_MASK) buckets */
    @Nullable
    private final int[] mScreenLayoutSize;

    /**
     * Screen Layout Long (screenLayout & SCREENLAYOUT_LONG_MASK) boolean. Only need to know if a
     * value is set because only two possible buckets, SCREENLAYOUT_LONG_NO and
     * SCREENLAYOUT_LONG_YES, so if either is set, then any change is a bucket change.
     */
    private final boolean mScreenLayoutLongSet;

    public SizeConfigurationBuckets(Configuration[] sizeConfigurations) {
        SparseIntArray horizontal = new SparseIntArray();
        SparseIntArray vertical = new SparseIntArray();
        SparseIntArray smallest = new SparseIntArray();
        SparseIntArray screenLayoutSize = new SparseIntArray();
        int curScreenLayoutSize;
        boolean screenLayoutLongSet = false;
        for (int i = sizeConfigurations.length - 1; i >= 0; i--) {
            Configuration config = sizeConfigurations[i];
            if (config.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
                vertical.put(config.screenHeightDp, 0);
            }
            if (config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
                horizontal.put(config.screenWidthDp, 0);
            }
            if (config.smallestScreenWidthDp != Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
                smallest.put(config.smallestScreenWidthDp, 0);
            }
            if ((curScreenLayoutSize = config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
                    != Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
                screenLayoutSize.put(curScreenLayoutSize, 0);
            }
            if (!screenLayoutLongSet && (config.screenLayout & Configuration.SCREENLAYOUT_LONG_MASK)
                    != Configuration.SCREENLAYOUT_LONG_UNDEFINED) {
                screenLayoutLongSet = true;
            }
        }
        mHorizontal = horizontal.copyKeys();
        mVertical = vertical.copyKeys();
        mSmallest = smallest.copyKeys();
        mScreenLayoutSize = screenLayoutSize.copyKeys();
        mScreenLayoutLongSet = screenLayoutLongSet;
    }

    /**
     * Get the changes between two configurations but don't count changes in sizes if they don't
     * cross boundaries that are important to the app.
     */
    public static int filterDiff(int diff, @NonNull Configuration oldConfig,
            @NonNull Configuration newConfig, @Nullable SizeConfigurationBuckets buckets) {
        if (buckets == null) {
            return diff;
        }

        final boolean nonSizeLayoutFieldsUnchanged =
                areNonSizeLayoutFieldsUnchanged(oldConfig.screenLayout, newConfig.screenLayout);
        if ((diff & CONFIG_SCREEN_SIZE) != 0) {
            final boolean crosses = buckets.crossesHorizontalSizeThreshold(oldConfig.screenWidthDp,
                    newConfig.screenWidthDp)
                    || buckets.crossesVerticalSizeThreshold(oldConfig.screenHeightDp,
                    newConfig.screenHeightDp);
            if (!crosses) {
                diff &= ~CONFIG_SCREEN_SIZE;
            }
        }
        if ((diff & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
            final int oldSmallest = oldConfig.smallestScreenWidthDp;
            final int newSmallest = newConfig.smallestScreenWidthDp;
            if (!buckets.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
                diff &= ~CONFIG_SMALLEST_SCREEN_SIZE;
            }
        }
        if ((diff & CONFIG_SCREEN_LAYOUT) != 0 && nonSizeLayoutFieldsUnchanged) {
            if (!buckets.crossesScreenLayoutSizeThreshold(oldConfig, newConfig)
                    && !buckets.crossesScreenLayoutLongThreshold(oldConfig.screenLayout,
                    newConfig.screenLayout)) {
                diff &= ~CONFIG_SCREEN_LAYOUT;
            }
        }
        return diff;
    }

    private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
        return crossesSizeThreshold(mHorizontal, firstDp, secondDp);
    }

    private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
        return crossesSizeThreshold(mVertical, firstDp, secondDp);
    }

    private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
        return crossesSizeThreshold(mSmallest, firstDp, secondDp);
    }

    /**
     * Returns whether a screen layout size threshold has been crossed.
     */
    @VisibleForTesting
    public boolean crossesScreenLayoutSizeThreshold(@NonNull Configuration firstConfig,
            @NonNull Configuration secondConfig) {
        // If both the old and new screen layout are equal (both can be undefined), then no
        // threshold is crossed.
        if ((firstConfig.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
                == (secondConfig.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)) {
            return false;
        }
        // Any time the new layout size is smaller than the old layout size, the activity has
        // crossed a size threshold because layout size represents the smallest possible size the
        // activity can occupy.
        if (!secondConfig.isLayoutSizeAtLeast(firstConfig.screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)) {
            return true;
        }
        // If the new layout size is at least as large as the old layout size, then check if the new
        // layout size has crossed a threshold.
        if (mScreenLayoutSize != null) {
            for (int screenLayoutSize : mScreenLayoutSize) {
                if (firstConfig.isLayoutSizeAtLeast(screenLayoutSize)
                        != secondConfig.isLayoutSizeAtLeast(screenLayoutSize)) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean crossesScreenLayoutLongThreshold(int firstScreenLayout,
            int secondScreenLayout) {
        final int firstScreenLayoutLongValue = firstScreenLayout
                & Configuration.SCREENLAYOUT_LONG_MASK;
        final int secondScreenLayoutLongValue = secondScreenLayout
                & Configuration.SCREENLAYOUT_LONG_MASK;
        return mScreenLayoutLongSet && firstScreenLayoutLongValue != secondScreenLayoutLongValue;
    }

    /**
     * Returns whether non-size related screen layout attributes have changed. If true, then
     * {@link ActivityInfo#CONFIG_SCREEN_LAYOUT} should not be filtered out in
     * {@link SizeConfigurationBuckets#filterDiff()} because the non-size related attributes
     * do not have a bucket range like the size-related attributes of screen layout.
     */
    @VisibleForTesting
    public static boolean areNonSizeLayoutFieldsUnchanged(int oldScreenLayout,
            int newScreenLayout) {
        final int nonSizeRelatedFields = Configuration.SCREENLAYOUT_LAYOUTDIR_MASK
                | Configuration.SCREENLAYOUT_ROUND_MASK | Configuration.SCREENLAYOUT_COMPAT_NEEDED;
        return (oldScreenLayout & nonSizeRelatedFields) == (newScreenLayout & nonSizeRelatedFields);
    }

    /**
     * The purpose of this method is to decide whether the activity needs to be relaunched upon
     * changing its size. In most cases the activities don't need to be relaunched, if the resize
     * is small, all the activity content has to do is relayout itself within new bounds. There are
     * cases however, where the activity's content would be completely changed in the new size and
     * the full relaunch is required.
     *
     * The activity will report to us vertical and horizontal thresholds after which a relaunch is
     * required. These thresholds are collected from the application resource qualifiers. For
     * example, if application has layout-w600dp resource directory, then it needs a relaunch when
     * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
     * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
     * of the threshold.
     */
    @VisibleForTesting
    public static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
            int secondDp) {
        if (thresholds == null) {
            return false;
        }
        for (int i = thresholds.length - 1; i >= 0; i--) {
            final int threshold = thresholds[i];
            if ((firstDp < threshold && secondDp >= threshold)
                    || (firstDp >= threshold && secondDp < threshold)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        return Arrays.toString(mHorizontal) + " " + Arrays.toString(mVertical) + " "
                + Arrays.toString(mSmallest) + " " + Arrays.toString(mScreenLayoutSize) + " "
                + mScreenLayoutLongSet;
    }



    // Code below generated by codegen v1.0.23.
    //
    // DO NOT MODIFY!
    // CHECKSTYLE:OFF Generated code
    //
    // To regenerate run:
    // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/window/SizeConfigurationBuckets.java
    //
    // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
    //   Settings > Editor > Code Style > Formatter Control
    //@formatter:off


    /**
     * Creates a new SizeConfigurationBuckets.
     *
     * @param horizontal
     *   Horizontal (screenWidthDp) buckets
     * @param vertical
     *   Vertical (screenHeightDp) buckets
     * @param smallest
     *   Smallest (smallestScreenWidthDp) buckets
     * @param screenLayoutSize
     *   Screen Layout Size (screenLayout & SCREENLAYOUT_SIZE_MASK) buckets
     * @param screenLayoutLongSet
     *   Screen Layout Long (screenLayout & SCREENLAYOUT_LONG_MASK) boolean. Only need to know if a
     *   value is set because only two possible buckets, SCREENLAYOUT_LONG_NO and
     *   SCREENLAYOUT_LONG_YES, so if either is set, then any change is a bucket change.
     */
    @DataClass.Generated.Member
    public SizeConfigurationBuckets(
            @Nullable int[] horizontal,
            @Nullable int[] vertical,
            @Nullable int[] smallest,
            @Nullable int[] screenLayoutSize,
            boolean screenLayoutLongSet) {
        this.mHorizontal = horizontal;
        this.mVertical = vertical;
        this.mSmallest = smallest;
        this.mScreenLayoutSize = screenLayoutSize;
        this.mScreenLayoutLongSet = screenLayoutLongSet;

        // onConstructed(); // You can define this method to get a callback
    }

    /**
     * Horizontal (screenWidthDp) buckets
     */
    @DataClass.Generated.Member
    public @Nullable int[] getHorizontal() {
        return mHorizontal;
    }

    /**
     * Vertical (screenHeightDp) buckets
     */
    @DataClass.Generated.Member
    public @Nullable int[] getVertical() {
        return mVertical;
    }

    /**
     * Smallest (smallestScreenWidthDp) buckets
     */
    @DataClass.Generated.Member
    public @Nullable int[] getSmallest() {
        return mSmallest;
    }

    /**
     * Screen Layout Size (screenLayout & SCREENLAYOUT_SIZE_MASK) buckets
     */
    @DataClass.Generated.Member
    public @Nullable int[] getScreenLayoutSize() {
        return mScreenLayoutSize;
    }

    /**
     * Screen Layout Long (screenLayout & SCREENLAYOUT_LONG_MASK) boolean. Only need to know if a
     * value is set because only two possible buckets, SCREENLAYOUT_LONG_NO and
     * SCREENLAYOUT_LONG_YES, so if either is set, then any change is a bucket change.
     */
    @DataClass.Generated.Member
    public boolean isScreenLayoutLongSet() {
        return mScreenLayoutLongSet;
    }

    @Override
    @DataClass.Generated.Member
    public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
        // You can override field parcelling by defining methods like:
        // void parcelFieldName(Parcel dest, int flags) { ... }

        byte flg = 0;
        if (mScreenLayoutLongSet) flg |= 0x10;
        if (mHorizontal != null) flg |= 0x1;
        if (mVertical != null) flg |= 0x2;
        if (mSmallest != null) flg |= 0x4;
        if (mScreenLayoutSize != null) flg |= 0x8;
        dest.writeByte(flg);
        if (mHorizontal != null) dest.writeIntArray(mHorizontal);
        if (mVertical != null) dest.writeIntArray(mVertical);
        if (mSmallest != null) dest.writeIntArray(mSmallest);
        if (mScreenLayoutSize != null) dest.writeIntArray(mScreenLayoutSize);
    }

    @Override
    @DataClass.Generated.Member
    public int describeContents() { return 0; }

    /** @hide */
    @SuppressWarnings({"unchecked", "RedundantCast"})
    @DataClass.Generated.Member
    /* package-private */ SizeConfigurationBuckets(@NonNull android.os.Parcel in) {
        // You can override field unparcelling by defining methods like:
        // static FieldType unparcelFieldName(Parcel in) { ... }

        byte flg = in.readByte();
        boolean screenLayoutLongSet = (flg & 0x10) != 0;
        int[] horizontal = (flg & 0x1) == 0 ? null : in.createIntArray();
        int[] vertical = (flg & 0x2) == 0 ? null : in.createIntArray();
        int[] smallest = (flg & 0x4) == 0 ? null : in.createIntArray();
        int[] screenLayoutSize = (flg & 0x8) == 0 ? null : in.createIntArray();

        this.mHorizontal = horizontal;
        this.mVertical = vertical;
        this.mSmallest = smallest;
        this.mScreenLayoutSize = screenLayoutSize;
        this.mScreenLayoutLongSet = screenLayoutLongSet;

        // onConstructed(); // You can define this method to get a callback
    }

    @DataClass.Generated.Member
    public static final @NonNull Parcelable.Creator<SizeConfigurationBuckets> CREATOR
            = new Parcelable.Creator<SizeConfigurationBuckets>() {
        @Override
        public SizeConfigurationBuckets[] newArray(int size) {
            return new SizeConfigurationBuckets[size];
        }

        @Override
        public SizeConfigurationBuckets createFromParcel(@NonNull android.os.Parcel in) {
            return new SizeConfigurationBuckets(in);
        }
    };

    @DataClass.Generated(
            time = 1628273704583L,
            codegenVersion = "1.0.23",
            sourceFile = "frameworks/base/core/java/android/window/SizeConfigurationBuckets.java",
            inputSignatures = "private final @android.annotation.Nullable int[] mHorizontal\nprivate final @android.annotation.Nullable int[] mVertical\nprivate final @android.annotation.Nullable int[] mSmallest\nprivate final @android.annotation.Nullable int[] mScreenLayoutSize\nprivate final  boolean mScreenLayoutLongSet\npublic static  int filterDiff(int,android.content.res.Configuration,android.content.res.Configuration,android.window.SizeConfigurationBuckets)\nprivate  boolean crossesHorizontalSizeThreshold(int,int)\nprivate  boolean crossesVerticalSizeThreshold(int,int)\nprivate  boolean crossesSmallestSizeThreshold(int,int)\npublic @com.android.internal.annotations.VisibleForTesting boolean crossesScreenLayoutSizeThreshold(android.content.res.Configuration,android.content.res.Configuration)\nprivate  boolean crossesScreenLayoutLongThreshold(int,int)\npublic static @com.android.internal.annotations.VisibleForTesting boolean areNonSizeLayoutFieldsUnchanged(int,int)\npublic static @com.android.internal.annotations.VisibleForTesting boolean crossesSizeThreshold(int[],int,int)\npublic @java.lang.Override java.lang.String toString()\nclass SizeConfigurationBuckets extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genAidl=true)")
    @Deprecated
    private void __metadata() {}


    //@formatter:on
    // End of generated code

}