1 /*
2  * Copyright 2021 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.app.settings.SettingsEnums;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.provider.DeviceConfig;
22 import android.provider.Settings;
23 import android.view.Menu;
24 import android.view.MenuInflater;
25 import android.view.MenuItem;
26 
27 import com.android.internal.config.sysui.SystemUiDeviceConfigFlags;
28 import com.android.settings.R;
29 import com.android.settings.dashboard.DashboardFragment;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settingslib.search.SearchIndexable;
32 
33 /** Dashboard Fragment to display all recent location access (apps), sorted by recency. */
34 @SearchIndexable
35 public class RecentLocationAccessSeeAllFragment extends DashboardFragment {
36     private static final String TAG = "RecentLocAccessSeeAll";
37     public static final String PATH =
38             "com.android.settings.location.RecentLocationAccessSeeAllFragment";
39 
40     private static final int MENU_SHOW_SYSTEM = Menu.FIRST + 1;
41     private static final int MENU_HIDE_SYSTEM = Menu.FIRST + 2;
42 
43     private boolean mShowSystem = false;
44     private MenuItem mShowSystemMenu;
45     private MenuItem mHideSystemMenu;
46     private RecentLocationAccessSeeAllPreferenceController mController;
47 
48     @Override
getMetricsCategory()49     public int getMetricsCategory() {
50         return SettingsEnums.LOCATION_RECENT_ACCESS_ALL;
51     }
52 
53     @Override
onAttach(Context context)54     public void onAttach(Context context) {
55         super.onAttach(context);
56         mController = use(RecentLocationAccessSeeAllPreferenceController.class);
57         mController.init(this);
58     }
59 
60     @Override
onCreate(Bundle savedInstanceState)61     public void onCreate(Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63         mShowSystem = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY,
64             SystemUiDeviceConfigFlags.PROPERTY_LOCATION_INDICATORS_SMALL_ENABLED, false)
65             ? Settings.Secure.getInt(getContentResolver(),
66             Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0) == 1
67             : false;
68     }
69 
70     @Override
getPreferenceScreenResId()71     protected int getPreferenceScreenResId() {
72         return R.xml.location_recent_access_see_all;
73     }
74 
75     @Override
getLogTag()76     protected String getLogTag() {
77         return TAG;
78     }
79 
80     @Override
onOptionsItemSelected(MenuItem menuItem)81     public boolean onOptionsItemSelected(MenuItem menuItem) {
82         switch (menuItem.getItemId()) {
83             case MENU_SHOW_SYSTEM:
84             case MENU_HIDE_SYSTEM:
85                 mShowSystem = menuItem.getItemId() == MENU_SHOW_SYSTEM;
86                 Settings.Secure.putInt(getContentResolver(),
87                         Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, mShowSystem ? 1 : 0);
88                 updateMenu();
89                 if (mController != null) {
90                     mController.setShowSystem(mShowSystem);
91                 }
92                 return true;
93             default:
94                 return super.onOptionsItemSelected(menuItem);
95         }
96     }
97 
updateMenu()98     private void updateMenu() {
99         mShowSystemMenu.setVisible(!mShowSystem);
100         mHideSystemMenu.setVisible(mShowSystem);
101     }
102 
103     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)104     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
105         super.onCreateOptionsMenu(menu, inflater);
106         mShowSystemMenu = menu.add(Menu.NONE, MENU_SHOW_SYSTEM, Menu.NONE,
107                 R.string.menu_show_system);
108         mHideSystemMenu = menu.add(Menu.NONE, MENU_HIDE_SYSTEM, Menu.NONE,
109                 R.string.menu_hide_system);
110         updateMenu();
111     }
112 
113     /**
114      * For Search.
115      */
116     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
117             new BaseSearchIndexProvider(R.xml.location_recent_access_see_all);
118 }
119