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.app.Activity;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 
26 import com.android.settings.R;
27 import com.android.settings.dashboard.DashboardFragment;
28 import com.android.settings.search.BaseSearchIndexProvider;
29 import com.android.settings.widget.PreferenceCategoryController;
30 import com.android.settingslib.core.AbstractPreferenceController;
31 import com.android.settingslib.core.lifecycle.Lifecycle;
32 import com.android.settingslib.search.SearchIndexable;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 @SearchIndexable
38 public class LanguageSettings extends DashboardFragment {
39 
40     private static final String KEY_SPEECH_CATEGORY = "speech_category";
41     private static final String KEY_ON_DEVICE_RECOGNITION = "on_device_recognition_settings";
42     private static final String KEY_TEXT_TO_SPEECH = "tts_settings_summary";
43 
44     private static final String TAG = "LanguageSettings";
45 
46     @Override
getMetricsCategory()47     public int getMetricsCategory() {
48         return SettingsEnums.SETTINGS_LANGUAGES_CATEGORY;
49     }
50 
51     @Override
getLogTag()52     protected String getLogTag() {
53         return TAG;
54     }
55 
56     @Override
onResume()57     public void onResume() {
58         super.onResume();
59         // Hack to update action bar title. It's necessary to refresh title because this page user
60         // can change locale from here and fragment won't relaunch. Once language changes, title
61         // must display in the new language.
62         final Activity activity = getActivity();
63         if (activity == null) {
64             return;
65         }
66         activity.setTitle(R.string.languages_settings);
67     }
68 
69     @Override
getPreferenceScreenResId()70     protected int getPreferenceScreenResId() {
71         return R.xml.language_settings;
72     }
73 
createPreferenceControllers(Context context)74     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
75         return buildPreferenceControllers(context, getSettingsLifecycle());
76     }
77 
buildPreferenceControllers( @onNull Context context, @Nullable Lifecycle lifecycle)78     private static List<AbstractPreferenceController> buildPreferenceControllers(
79             @NonNull Context context, @Nullable Lifecycle lifecycle) {
80         final List<AbstractPreferenceController> controllers = new ArrayList<>();
81 
82         final DefaultVoiceInputPreferenceController defaultVoiceInputPreferenceController =
83                 new DefaultVoiceInputPreferenceController(context, lifecycle);
84         final TtsPreferenceController ttsPreferenceController =
85                 new TtsPreferenceController(context, KEY_TEXT_TO_SPEECH);
86         final OnDeviceRecognitionPreferenceController onDeviceRecognitionPreferenceController =
87                 new OnDeviceRecognitionPreferenceController(context, KEY_ON_DEVICE_RECOGNITION);
88 
89         controllers.add(defaultVoiceInputPreferenceController);
90         controllers.add(ttsPreferenceController);
91         List<AbstractPreferenceController> speechCategoryChildren = new ArrayList<>(
92                 List.of(defaultVoiceInputPreferenceController, ttsPreferenceController));
93 
94         if (onDeviceRecognitionPreferenceController.isAvailable()) {
95             controllers.add(onDeviceRecognitionPreferenceController);
96             speechCategoryChildren.add(onDeviceRecognitionPreferenceController);
97         }
98 
99         controllers.add(new PreferenceCategoryController(context, KEY_SPEECH_CATEGORY)
100                 .setChildren(speechCategoryChildren));
101 
102         return controllers;
103     }
104 
105     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
106             new BaseSearchIndexProvider(R.xml.language_settings) {
107                 @Override
108                 public List<AbstractPreferenceController> createPreferenceControllers(
109                         Context context) {
110                     return buildPreferenceControllers(context, null);
111                 }
112                 @Override
113                 protected boolean isPageSearchEnabled(Context context) {
114                     return true;
115                 }
116             };
117 }