1 /*
2  * Copyright 2018 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 package com.android.settings.location;
17 
18 import android.content.Context;
19 import android.provider.SearchIndexableResource;
20 import android.view.Menu;
21 import android.view.MenuInflater;
22 import android.view.MenuItem;
23 
24 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
25 import com.android.settings.R;
26 import com.android.settings.dashboard.DashboardFragment;
27 import com.android.settings.search.BaseSearchIndexProvider;
28 import com.android.settings.search.Indexable;
29 import com.android.settingslib.core.AbstractPreferenceController;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 import com.android.settingslib.search.SearchIndexable;
32 
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 
37 /** Dashboard Fragment to display all recent location requests, sorted by recency. */
38 @SearchIndexable
39 public class RecentLocationRequestSeeAllFragment extends DashboardFragment {
40     private static final String TAG = "RecentLocationReqAll";
41     public static final String PATH =
42             "com.android.settings.location.RecentLocationRequestSeeAllFragment";
43 
44     private static final int MENU_SHOW_SYSTEM = Menu.FIRST + 1;
45     private static final int MENU_HIDE_SYSTEM = Menu.FIRST + 2;
46 
47     private boolean mShowSystem = false;
48     private MenuItem mShowSystemMenu;
49     private MenuItem mHideSystemMenu;
50     private RecentLocationRequestSeeAllPreferenceController mController;
51 
52     @Override
getMetricsCategory()53     public int getMetricsCategory() {
54         return MetricsEvent.RECENT_LOCATION_REQUESTS_ALL;
55     }
56 
57     @Override
getPreferenceScreenResId()58     protected int getPreferenceScreenResId() {
59         return R.xml.location_recent_requests_see_all;
60     }
61 
62     @Override
getLogTag()63     protected String getLogTag() {
64         return TAG;
65     }
66 
67     @Override
createPreferenceControllers(Context context)68     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
69         return buildPreferenceControllers(context, getSettingsLifecycle(), this);
70     }
71 
72     @Override
onOptionsItemSelected(MenuItem menuItem)73     public boolean onOptionsItemSelected(MenuItem menuItem) {
74         switch (menuItem.getItemId()) {
75             case MENU_SHOW_SYSTEM:
76             case MENU_HIDE_SYSTEM:
77                 mShowSystem = menuItem.getItemId() == MENU_SHOW_SYSTEM;
78                 updateMenu();
79                 if (mController != null) {
80                     mController.setShowSystem(mShowSystem);
81                 }
82                 return true;
83             default:
84                 return super.onOptionsItemSelected(menuItem);
85         }
86     }
87 
updateMenu()88     private void updateMenu() {
89         mShowSystemMenu.setVisible(!mShowSystem);
90         mHideSystemMenu.setVisible(mShowSystem);
91     }
92 
buildPreferenceControllers( Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment)93     private static List<AbstractPreferenceController> buildPreferenceControllers(
94             Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment) {
95         final List<AbstractPreferenceController> controllers = new ArrayList<>();
96         final RecentLocationRequestSeeAllPreferenceController controller =
97                 new RecentLocationRequestSeeAllPreferenceController(context, lifecycle, fragment);
98         controllers.add(controller);
99         if (fragment != null) {
100             fragment.mController = controller;
101         }
102         return controllers;
103     }
104 
105     /**
106      * For Search.
107      */
108     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
109             new BaseSearchIndexProvider() {
110                 @Override
111                 public List<SearchIndexableResource> getXmlResourcesToIndex(
112                         Context context, boolean enabled) {
113                     final SearchIndexableResource sir = new SearchIndexableResource(context);
114                     sir.xmlResId = R.xml.location_recent_requests_see_all;
115                     return Arrays.asList(sir);
116                 }
117 
118                 @Override
119                 public List<AbstractPreferenceController> getPreferenceControllers(Context
120                         context) {
121                     return buildPreferenceControllers(
122                             context, /* lifecycle = */ null, /* fragment = */ null);
123                 }
124             };
125 
126     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)127     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
128         super.onCreateOptionsMenu(menu, inflater);
129         mShowSystemMenu = menu.add(Menu.NONE, MENU_SHOW_SYSTEM, Menu.NONE,
130                 R.string.menu_show_system);
131         mHideSystemMenu = menu.add(Menu.NONE, MENU_HIDE_SYSTEM, Menu.NONE,
132                 R.string.menu_hide_system);
133         updateMenu();
134     }
135 }
136