1 /*
2  * Copyright (C) 2023 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.systemui.tv.hdmi;
18 
19 import android.os.UserHandle;
20 import android.provider.Settings;
21 
22 import com.android.internal.app.LocalePicker;
23 import com.android.systemui.dagger.SysUISingleton;
24 import com.android.systemui.dagger.qualifiers.Background;
25 import com.android.systemui.util.settings.SecureSettings;
26 
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.Locale;
31 import java.util.concurrent.Executor;
32 
33 import javax.inject.Inject;
34 
35 /**
36  * Helper class to separate model and view for system language change initiated by HDMI CEC.
37  */
38 @SysUISingleton
39 public class HdmiCecSetMenuLanguageHelper {
40     private static final String TAG = HdmiCecSetMenuLanguageHelper.class.getSimpleName();
41     private static final String SEPARATOR = ",";
42 
43     private final Executor mBackgroundExecutor;
44     private final SecureSettings mSecureSettings;
45 
46     private Locale mLocale;
47     private HashSet<String> mDenylist;
48 
49     @Inject
HdmiCecSetMenuLanguageHelper(@ackground Executor executor, SecureSettings secureSettings)50     public HdmiCecSetMenuLanguageHelper(@Background Executor executor,
51             SecureSettings secureSettings) {
52         mBackgroundExecutor = executor;
53         mSecureSettings = secureSettings;
54         String denylist = mSecureSettings.getStringForUser(
55                 Settings.Secure.HDMI_CEC_SET_MENU_LANGUAGE_DENYLIST, UserHandle.USER_CURRENT);
56         mDenylist = new HashSet<>(denylist == null
57                 ? Collections.EMPTY_SET
58                 : Arrays.asList(denylist.split(SEPARATOR)));
59     }
60 
61     /**
62      * Set internal locale based on given language tag.
63      */
setLocale(String languageTag)64     public void setLocale(String languageTag) {
65         mLocale = Locale.forLanguageTag(languageTag);
66     }
67 
68     /**
69      * Returns the locale from {@code <Set Menu Language>} CEC message.
70      */
getLocale()71     public Locale getLocale() {
72         return mLocale;
73     }
74 
75     /**
76      * Returns whether the locale from {@code <Set Menu Language>} CEC message was already
77      * denylisted.
78      */
isLocaleDenylisted()79     public boolean isLocaleDenylisted() {
80         return mDenylist.contains(mLocale.toLanguageTag());
81     }
82 
83     /**
84      * Accepts the new locale and updates system language.
85      */
acceptLocale()86     public void acceptLocale() {
87         mBackgroundExecutor.execute(() -> LocalePicker.updateLocale(mLocale));
88     }
89 
90     /**
91      * Declines the locale and puts it on the denylist.
92      */
declineLocale()93     public void declineLocale() {
94         mDenylist.add(mLocale.toLanguageTag());
95         mSecureSettings.putStringForUser(Settings.Secure.HDMI_CEC_SET_MENU_LANGUAGE_DENYLIST,
96                 String.join(SEPARATOR, mDenylist), UserHandle.USER_CURRENT);
97     }
98 }
99