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.settings.regionalpreferences;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.util.Log;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.internal.app.LocaleHelper;
27 import com.android.settings.R;
28 import com.android.settings.dashboard.DashboardFragment;
29 import com.android.settingslib.core.AbstractPreferenceController;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Locale;
34 
35 /** Provides options of numbering system to each language. */
36 public class NumberingPreferencesFragment  extends DashboardFragment {
37     /** Initializes variables. */
38     @VisibleForTesting
initTitle()39     String initTitle() {
40         String option = getArguments().getString(
41                 RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE, "");
42         if (option.isEmpty()) {
43             Log.d(getLogTag(), "Option is empty.");
44             return "";
45         }
46         Log.i(getLogTag(), "[NumberingPreferencesFragment] option is " + option);
47 
48         if (option.equals(NumberingSystemItemController.ARG_VALUE_LANGUAGE_SELECT)) {
49             return getContext().getString(R.string.numbers_preferences_title);
50         } else if (option.equals(NumberingSystemItemController.ARG_VALUE_NUMBERING_SYSTEM_SELECT)) {
51             String selectedLanguage = getArguments().getString(
52                     NumberingSystemItemController.KEY_SELECTED_LANGUAGE, "");
53             if (selectedLanguage.isEmpty()) {
54                 Log.w(getLogTag(), "No selected language.");
55                 return "";
56             }
57             Locale locale = Locale.forLanguageTag(selectedLanguage);
58             return LocaleHelper.getDisplayName(locale.stripExtensions(), locale, true);
59         }
60         Log.w(getLogTag(), "Incorrect option : " + option);
61         return "";
62     }
63 
64     @Override
onCreate(Bundle icicle)65     public void onCreate(Bundle icicle) {
66         super.onCreate(icicle);
67         String title = initTitle();
68         if (initTitle().isEmpty()) {
69             finish();
70         } else {
71             getActivity().setTitle(title);
72         }
73     }
74 
75     /**
76      * Get a list of {@link AbstractPreferenceController} for this fragment.
77      */
78     @Override
createPreferenceControllers(Context context)79     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
80         NumberingSystemItemController controller =
81                 new NumberingSystemItemController(context, getArguments());
82         controller.setParentFragment(this);
83         List<AbstractPreferenceController> listControllers = new ArrayList<>();
84         listControllers.add(controller);
85         return listControllers;
86     }
87 
88     @Override
getPreferenceScreenResId()89     protected int getPreferenceScreenResId() {
90         return R.xml.regional_preference_numbering_system_page;
91     }
92 
93     /**
94      * Get the tag string for logging.
95      */
96     @Override
getLogTag()97     protected String getLogTag() {
98         return NumberingPreferencesFragment.class.getSimpleName();
99     }
100 
101     @Override
getMetricsCategory()102     public int getMetricsCategory() {
103         String option = getArguments().getString(
104                 RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE, "");
105         if (option.equals(NumberingSystemItemController.ARG_VALUE_LANGUAGE_SELECT)) {
106             return SettingsEnums.NUMBERING_SYSTEM_LANGUAGE_SELECTION_PREFERENCE;
107         } else {
108             return SettingsEnums.NUMBERING_SYSTEM_NUMBER_FORMAT_SELECTION_PREFERENCE;
109         }
110     }
111 }
112