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.localepicker;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.util.Log;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.R;
28 import com.android.settings.overlay.FeatureFactory;
29 import com.android.settingslib.HelpUtils;
30 import com.android.settingslib.core.AbstractPreferenceController;
31 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
32 import com.android.settingslib.widget.FooterPreference;
33 
34 /**
35  * A controller to update current locale information of application.
36  */
37 public class LocaleHelperPreferenceController extends AbstractPreferenceController {
38     private static final String TAG = LocaleHelperPreferenceController.class.getSimpleName();
39 
40     private static final String KEY_FOOTER_LANGUAGE_PICKER = "footer_languages_picker";
41 
42     private final MetricsFeatureProvider mMetricsFeatureProvider;
43 
LocaleHelperPreferenceController(Context context)44     public LocaleHelperPreferenceController(Context context) {
45         super(context);
46         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
47     }
48 
49     @Override
isAvailable()50     public boolean isAvailable() {
51         return true;
52     }
53 
54     @Override
getPreferenceKey()55     public String getPreferenceKey() {
56         return KEY_FOOTER_LANGUAGE_PICKER;
57     }
58 
59     @Override
displayPreference(PreferenceScreen screen)60     public void displayPreference(PreferenceScreen screen) {
61         super.displayPreference(screen);
62         FooterPreference footerPreference = screen.findPreference(getPreferenceKey());
63         updateFooterPreference(footerPreference);
64     }
65 
66     @VisibleForTesting
updateFooterPreference(FooterPreference footerPreference)67     void updateFooterPreference(FooterPreference footerPreference) {
68         if (footerPreference != null) {
69             footerPreference.setLearnMoreAction(v -> openLocaleLearnMoreLink());
70             footerPreference.setLearnMoreText(mContext.getString(
71                     R.string.desc_locale_helper_footer_general));
72         }
73     }
74 
openLocaleLearnMoreLink()75     private void openLocaleLearnMoreLink() {
76         Intent intent = HelpUtils.getHelpIntent(
77                 mContext,
78                 mContext.getString(R.string.link_locale_picker_footer_learn_more),
79                 mContext.getClass().getName());
80         if (intent != null) {
81             mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_LANGUAGES_LEARN_MORE);
82             mContext.startActivity(intent);
83         } else {
84             Log.w(TAG, "HelpIntent is null");
85         }
86     }
87 }
88