1 /*
2  * Copyright (C) 2014 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.fuelgauge.batterysaver;
18 
19 import android.app.settings.SettingsEnums;
20 import android.text.TextUtils;
21 
22 import androidx.annotation.VisibleForTesting;
23 
24 import com.android.settings.R;
25 import com.android.settings.dashboard.DashboardFragment;
26 import com.android.settings.search.BaseSearchIndexProvider;
27 import com.android.settingslib.HelpUtils;
28 import com.android.settingslib.search.SearchIndexable;
29 import com.android.settingslib.widget.FooterPreference;
30 
31 /** Battery saver settings page */
32 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
33 public class BatterySaverSettings extends DashboardFragment {
34     private static final String TAG = "BatterySaverSettings";
35     private static final String KEY_FOOTER_PREFERENCE = "battery_saver_footer_preference";
36     private String mHelpUri;
37 
38     @Override
onStart()39     public void onStart() {
40         super.onStart();
41         setupFooter();
42     }
43 
44     @Override
getMetricsCategory()45     public int getMetricsCategory() {
46         return SettingsEnums.OPEN_BATTERY_SAVER;
47     }
48 
49     @Override
getPreferenceScreenResId()50     protected int getPreferenceScreenResId() {
51         return R.xml.battery_saver_settings;
52     }
53 
54     @Override
getLogTag()55     protected String getLogTag() {
56         return TAG;
57     }
58 
59     @Override
getHelpResource()60     public int getHelpResource() {
61         return R.string.help_url_battery_saver_settings;
62     }
63 
64     /** For Search. */
65     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
66             new BaseSearchIndexProvider(R.xml.battery_saver_settings);
67 
68     // Updates the footer for this page.
69     @VisibleForTesting
setupFooter()70     void setupFooter() {
71         mHelpUri = getString(R.string.help_url_battery_saver_settings);
72         if (!TextUtils.isEmpty(mHelpUri)) {
73             addHelpLink();
74         }
75     }
76 
77     // Changes the text to include a learn more link if possible.
78     @VisibleForTesting
addHelpLink()79     void addHelpLink() {
80         FooterPreference pref = getPreferenceScreen().findPreference(KEY_FOOTER_PREFERENCE);
81         if (pref != null) {
82             pref.setLearnMoreAction(
83                     v -> {
84                         mMetricsFeatureProvider.action(
85                                 getContext(), SettingsEnums.ACTION_APP_BATTERY_LEARN_MORE);
86                         startActivityForResult(
87                                 HelpUtils.getHelpIntent(
88                                         getContext(),
89                                         getString(R.string.help_url_battery_saver_settings),
90                                         /* backupContext= */ ""),
91                                 /* requestCode= */ 0);
92                     });
93             pref.setLearnMoreText(getString(R.string.battery_saver_link_a11y));
94         }
95     }
96 }
97