1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.settingslib.applications;
18 
19 import android.annotation.SuppressLint;
20 import android.content.pm.ActivityInfo;
21 import android.content.res.Configuration;
22 import android.content.res.Resources;
23 
24 import androidx.annotation.NonNull;
25 
26 /**
27  * A class for applying config changes and determing if doing so resulting in any "interesting"
28  * changes.
29  */
30 public class InterestingConfigChanges {
31     private final Configuration mLastConfiguration = new Configuration();
32     private final int mFlags;
33 
InterestingConfigChanges()34     public InterestingConfigChanges() {
35         this(ActivityInfo.CONFIG_LOCALE | ActivityInfo.CONFIG_LAYOUT_DIRECTION
36                 | ActivityInfo.CONFIG_UI_MODE | ActivityInfo.CONFIG_ASSETS_PATHS
37                 | ActivityInfo.CONFIG_DENSITY);
38     }
39 
InterestingConfigChanges(int flags)40     public InterestingConfigChanges(int flags) {
41         mFlags = flags;
42     }
43 
44     /**
45      * Applies the given config change and returns whether an "interesting" change happened.
46      *
47      * @param res The source of the new config to apply
48      *
49      * @return Whether interesting changes occurred
50      */
51     @SuppressLint("NewApi")
applyNewConfig(Resources res)52     public boolean applyNewConfig(Resources res) {
53         return applyNewConfig(res.getConfiguration());
54     }
55 
56     /**
57      * Applies the given config change and returns whether an "interesting" change happened.
58      */
applyNewConfig(@onNull Configuration configuration)59     public boolean applyNewConfig(@NonNull Configuration configuration) {
60         int configChanges = mLastConfiguration.updateFrom(
61                 Configuration.generateDelta(mLastConfiguration, configuration));
62         return (configChanges & (mFlags)) != 0;
63     }
64 }
65