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.accessibility;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.os.Bundle;
23 import android.os.Vibrator;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.recyclerview.widget.RecyclerView;
30 
31 import com.android.settings.R;
32 import com.android.settings.dashboard.DashboardFragment;
33 import com.android.settings.search.BaseSearchIndexProvider;
34 import com.android.settingslib.search.SearchIndexable;
35 
36 /** Accessibility settings for the vibration. */
37 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
38 public class VibrationIntensitySettingsFragment extends DashboardFragment {
39 
40     private static final String TAG = "VibrationIntensitySettings";
41 
42     @Override
getMetricsCategory()43     public int getMetricsCategory() {
44         return SettingsEnums.ACCESSIBILITY_VIBRATION;
45     }
46 
47     @Override
getHelpResource()48     public int getHelpResource() {
49         return R.string.help_uri_accessibility_vibration;
50     }
51 
52     @Override
getPreferenceScreenResId()53     protected int getPreferenceScreenResId() {
54         return R.xml.accessibility_vibration_intensity_settings;
55     }
56 
57     @Override
getLogTag()58     protected String getLogTag() {
59         return TAG;
60     }
61 
62     @VisibleForTesting
isPageSearchEnabled(Context context)63     static boolean isPageSearchEnabled(Context context) {
64         final int supportedIntensityLevels = context.getResources().getInteger(
65                 R.integer.config_vibration_supported_intensity_levels);
66         final boolean hasVibrator = context.getSystemService(Vibrator.class).hasVibrator();
67         return hasVibrator && supportedIntensityLevels > 1;
68     }
69 
70     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)71     public View onCreateView(LayoutInflater inflater, ViewGroup container,
72             Bundle savedInstanceState) {
73         final View view = super.onCreateView(inflater, container, savedInstanceState);
74         final Resources res = view.getResources();
75         final RecyclerView rv = getListView();
76         if (rv != null) {
77             final int bottom_padding = res.getDimensionPixelSize(
78                     com.android.settingslib.widget.theme.R.dimen
79                             .settingslib_listPreferredItemPaddingEnd);
80             rv.setPaddingRelative(rv.getPaddingStart(), rv.getPaddingTop(), rv.getPaddingEnd(),
81                     rv.getPaddingBottom() + bottom_padding);
82         }
83         return view;
84     }
85 
86     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
87             new BaseSearchIndexProvider(R.xml.accessibility_vibration_intensity_settings) {
88                 @Override
89                 protected boolean isPageSearchEnabled(Context context) {
90                     return VibrationIntensitySettingsFragment.isPageSearchEnabled(context);
91                 }
92             };
93 }
94