1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.datausage;
16 
17 import android.app.settings.SettingsEnums;
18 import android.content.Context;
19 import android.os.Bundle;
20 import android.provider.SearchIndexableResource;
21 import android.view.Menu;
22 import android.view.MenuInflater;
23 import android.view.MenuItem;
24 import android.view.View;
25 
26 import com.android.settings.R;
27 import com.android.settings.dashboard.DashboardFragment;
28 import com.android.settings.search.BaseSearchIndexProvider;
29 import com.android.settings.search.Indexable;
30 import com.android.settingslib.applications.ApplicationsState;
31 import com.android.settingslib.applications.ApplicationsState.AppFilter;
32 import com.android.settingslib.search.SearchIndexable;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 @SearchIndexable
38 public class UnrestrictedDataAccess extends DashboardFragment {
39 
40     private static final String TAG = "UnrestrictedDataAccess";
41 
42     private static final int MENU_SHOW_SYSTEM = Menu.FIRST + 42;
43     private static final String EXTRA_SHOW_SYSTEM = "show_system";
44 
45     private boolean mShowSystem;
46     private AppFilter mFilter;
47 
48     @Override
onCreate(Bundle icicle)49     public void onCreate(Bundle icicle) {
50         super.onCreate(icicle);
51         mShowSystem = icicle != null && icicle.getBoolean(EXTRA_SHOW_SYSTEM);
52 
53         use(UnrestrictedDataAccessPreferenceController.class).setParentFragment(this);
54     }
55 
56     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)57     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
58         menu.add(Menu.NONE, MENU_SHOW_SYSTEM, Menu.NONE,
59                 mShowSystem ? R.string.menu_hide_system : R.string.menu_show_system);
60         super.onCreateOptionsMenu(menu, inflater);
61     }
62 
63     @Override
onOptionsItemSelected(MenuItem item)64     public boolean onOptionsItemSelected(MenuItem item) {
65         switch (item.getItemId()) {
66             case MENU_SHOW_SYSTEM:
67                 mShowSystem = !mShowSystem;
68                 item.setTitle(mShowSystem ? R.string.menu_hide_system : R.string.menu_show_system);
69                 mFilter = mShowSystem ? ApplicationsState.FILTER_ALL_ENABLED
70                         : ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER;
71 
72                 use(UnrestrictedDataAccessPreferenceController.class).setFilter(mFilter);
73                 use(UnrestrictedDataAccessPreferenceController.class).rebuild();
74 
75                 break;
76         }
77         return super.onOptionsItemSelected(item);
78     }
79 
80     @Override
onSaveInstanceState(Bundle outState)81     public void onSaveInstanceState(Bundle outState) {
82         super.onSaveInstanceState(outState);
83         outState.putBoolean(EXTRA_SHOW_SYSTEM, mShowSystem);
84     }
85 
86     @Override
onViewCreated(View view, Bundle savedInstanceState)87     public void onViewCreated(View view, Bundle savedInstanceState) {
88         super.onViewCreated(view, savedInstanceState);
89     }
90 
91     @Override
onAttach(Context context)92     public void onAttach(Context context) {
93         super.onAttach(context);
94         mFilter = mShowSystem ? ApplicationsState.FILTER_ALL_ENABLED
95                 : ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER;
96         use(UnrestrictedDataAccessPreferenceController.class).setSession(getSettingsLifecycle());
97         use(UnrestrictedDataAccessPreferenceController.class).setFilter(mFilter);
98     }
99 
100     @Override
getHelpResource()101     public int getHelpResource() {
102         return R.string.help_url_unrestricted_data_access;
103     }
104 
105     @Override
getLogTag()106     protected String getLogTag() {
107         return TAG;
108     }
109 
110     @Override
getMetricsCategory()111     public int getMetricsCategory() {
112         return SettingsEnums.DATA_USAGE_UNRESTRICTED_ACCESS;
113     }
114 
115     @Override
getPreferenceScreenResId()116     protected int getPreferenceScreenResId() {
117         return R.xml.unrestricted_data_access_settings;
118     }
119 
120     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
121             new BaseSearchIndexProvider() {
122                 @Override
123                 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
124                         boolean enabled) {
125                     final ArrayList<SearchIndexableResource> result = new ArrayList<>();
126 
127                     final SearchIndexableResource sir = new SearchIndexableResource(context);
128                     sir.xmlResId = R.xml.unrestricted_data_access_settings;
129                     result.add(sir);
130                     return result;
131                 }
132             };
133 }
134