• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2024 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.provider.Settings;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
30 import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionInfoCompat;
31 import androidx.preference.PreferenceRecyclerViewAccessibilityDelegate;
32 import androidx.recyclerview.widget.RecyclerView;
33 
34 import com.android.settings.R;
35 import com.android.settings.dashboard.DashboardFragment;
36 import com.android.settings.flags.Flags;
37 import com.android.settings.overlay.FeatureFactory;
38 import com.android.settings.search.BaseSearchIndexProvider;
39 import com.android.settingslib.core.AbstractPreferenceController;
40 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
41 import com.android.settingslib.search.SearchIndexable;
42 
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.List;
46 
47 /** Main fragment to display measurement system. */
48 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
49 public class MeasurementSystemItemFragment extends DashboardFragment {
50 
51     private static final String LOG_TAG = "MeasurementSystemItemFragment";
52     private static final String KEY_PREFERENCE_CATEGORY_MEASUREMENT_SYSTEM_ITEM =
53             "measurement_system_item_category";
54 
55     @Override
onAttach(@onNull Context context)56     public void onAttach(@NonNull Context context) {
57         super.onAttach(context);
58 
59         MetricsFeatureProvider metricsFeatureProvider =
60                 FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
61         String action = getIntent() != null ? getIntent().getAction() : "";
62         if (Settings.ACTION_MEASUREMENT_SYSTEM_SETTINGS.equals(action)) {
63             metricsFeatureProvider.action(
64                     context, SettingsEnums.ACTION_OPEN_MEASUREMENT_SYSTEM_OUTSIDE_SETTINGS);
65         }
66     }
67 
68     @NonNull
69     @Override
onCreateRecyclerView( @onNull LayoutInflater inflater, @NonNull ViewGroup parent, @Nullable Bundle savedInstanceState)70     public RecyclerView onCreateRecyclerView(
71             @NonNull LayoutInflater inflater, @NonNull ViewGroup parent,
72             @Nullable Bundle savedInstanceState) {
73 
74         // Talkback shouldn't announce in list numbers
75         final RecyclerView recyclerView =
76                 super.onCreateRecyclerView(inflater, parent, savedInstanceState);
77         recyclerView.setAccessibilityDelegateCompat(
78             new PreferenceRecyclerViewAccessibilityDelegate(recyclerView) {
79                     @Override
80                     public void onInitializeAccessibilityNodeInfo(@NonNull View host,
81                             @NonNull AccessibilityNodeInfoCompat info) {
82                         super.onInitializeAccessibilityNodeInfo(host, info);
83                         int availableCount = (int) getPreferenceControllers()
84                                 .stream()
85                                 .flatMap(Collection::stream)
86                                 .filter(AbstractPreferenceController::isAvailable)
87                                 .count();
88                         info.setCollectionInfo(
89                                 CollectionInfoCompat.obtain(
90                                         /*rowCount=*/availableCount,
91                                         /*columnCount=*/1,
92                                         /*hierarchical=*/false,
93                                         CollectionInfoCompat.SELECTION_MODE_SINGLE)
94                         );
95                     }
96             });
97         return recyclerView;
98     }
99 
100     @Override
getPreferenceScreenResId()101     protected int getPreferenceScreenResId() {
102         return R.xml.regional_preferences_measurement_system;
103     }
104 
105     @Override
getMetricsCategory()106     public int getMetricsCategory() {
107         return SettingsEnums.MEASUREMENT_SYSTEM_PREFERENCE;
108     }
109 
110     @Override
getLogTag()111     protected String getLogTag() {
112         return LOG_TAG;
113     }
114 
115     @Override
createPreferenceControllers(Context context)116     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
117         final List<AbstractPreferenceController> controllers = new ArrayList<>();
118         controllers.add(new MeasurementSystemItemCategoryController(context,
119                 KEY_PREFERENCE_CATEGORY_MEASUREMENT_SYSTEM_ITEM));
120         return controllers;
121     }
122 
123     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
124             new BaseSearchIndexProvider(R.xml.regional_preferences_measurement_system) {
125                 @Override
126                 protected boolean isPageSearchEnabled(Context context) {
127                     if (!Flags.regionalPreferencesApiEnabled()) {
128                         return false;
129                     }
130                     return true;
131                 }
132             };
133 }
134