• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.settings.language;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.util.FeatureFlagUtils;
23 
24 import com.android.settings.Settings;
25 import com.android.settings.core.BasePreferenceController;
26 
27 /**
28  * This is a display controller for new language activity entry.
29  * TODO(b/273642892): When new layout is on board, this class shall be removed.
30  */
31 public class LanguagePreferenceController extends BasePreferenceController {
32     private static final String TAG = LanguagePreferenceController.class.getSimpleName();
33 
34     private boolean mCacheIsFeatureOn = false;
35 
LanguagePreferenceController(Context context, String key)36     public LanguagePreferenceController(Context context, String key) {
37         super(context, key);
38     }
39 
40     @Override
getAvailabilityStatus()41     public int getAvailabilityStatus() {
42         boolean isFeatureOn = FeatureFlagUtils
43                 .isEnabled(mContext, FeatureFlagUtils.SETTINGS_NEW_KEYBOARD_UI);
44 
45         // LanguageSettingsActivity is a new entry page for new language layout.
46         // LanguageAndInputSettingsActivity is existed entry page for current language layout.
47         if (mCacheIsFeatureOn != isFeatureOn) {
48             setActivityEnabled(
49                     mContext, Settings.LanguageAndInputSettingsActivity.class, !isFeatureOn);
50             setActivityEnabled(mContext, Settings.LanguageSettingsActivity.class, isFeatureOn);
51             mCacheIsFeatureOn = isFeatureOn;
52         }
53         return isFeatureOn ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
54     }
55 
setActivityEnabled(Context context, Class klass, final boolean isEnabled)56     private static void setActivityEnabled(Context context, Class klass, final boolean isEnabled) {
57         PackageManager packageManager = context.getPackageManager();
58 
59         ComponentName componentName =
60                 new ComponentName(context, klass);
61         final int flag = isEnabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
62                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
63 
64         packageManager.setComponentEnabledSetting(
65                 componentName, flag, PackageManager.DONT_KILL_APP);
66     }
67 }