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.regionalpreferences;
18 
19 import android.content.Context;
20 import android.provider.Settings;
21 
22 import androidx.core.text.util.LocalePreferences;
23 
24 import com.android.settings.R;
25 import com.android.settings.core.BasePreferenceController;
26 
27 import java.util.Locale;
28 
29 /** A controller for the entry of Temperature units' page */
30 public class TemperatureUnitController extends BasePreferenceController {
31     private static final String TAG = TemperatureUnitController.class.getSimpleName();
TemperatureUnitController(Context context, String preferenceKey)32     public TemperatureUnitController(Context context, String preferenceKey) {
33         super(context, preferenceKey);
34     }
35 
36     /**
37      * @return {@link AvailabilityStatus} for the Setting. This status is used to determine if the
38      * Setting should be shown or disabled in Settings. Further, it can be used to produce
39      * appropriate error / warning Slice in the case of unavailability.
40      * </p>
41      * The status is used for the convenience methods: {@link #isAvailable()}, {@link
42      * #isSupported()}
43      * </p>
44      * The inherited class doesn't need to check work profile if android:forWork="true" is set in
45      * preference xml.
46      */
47     @Override
getAvailabilityStatus()48     public int getAvailabilityStatus() {
49         return AVAILABLE;
50     }
51 
52     @Override
getSummary()53     public CharSequence getSummary() {
54         String record = Settings.System.getString(
55                 mContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
56         String result = "";
57         if (record != null) {
58             result = LocalePreferences.getTemperatureUnit(Locale.forLanguageTag(record), false);
59         }
60 
61         if (result.isEmpty()) {
62             result = LocalePreferences.getTemperatureUnit(false);
63         }
64 
65         return result.isEmpty()
66                 ? mContext.getString(R.string.default_string_of_regional_preference)
67                 : RegionalPreferencesDataUtils.temperatureUnitsConverter(mContext, result);
68     }
69 }
70