summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/system/LocationFragment.java
blob: 3f4a25d1b75d65fd2d1220670291b935a125e9a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.tv.settings.system;

import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected;
import static com.android.tv.settings.util.InstrumentationUtils.logToggleInteracted;

import android.app.tvsettings.TvSettingsEnums;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Keep;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;

import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.location.RecentLocationApps;
import com.android.tv.settings.R;
import com.android.tv.settings.SettingsPreferenceFragment;
import com.android.tv.settings.device.apps.AppManagementFragment;
import com.android.tv.settings.library.overlay.FlavorUtils;
import com.android.tv.twopanelsettings.SummaryListPreference;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * The location settings screen in TV settings.
 */
@Keep
public class LocationFragment extends SettingsPreferenceFragment implements
        Preference.OnPreferenceChangeListener {

    private static final String TAG = "LocationFragment";

    private static final String LOCATION_MODE_WIFI = "wifi";
    private static final String LOCATION_MODE_OFF = "off";

    private static final String KEY_LOCATION_MODE = "locationMode";
    private static final String KEY_WIFI_ALWAYS_SCAN = "wifi_always_scan";

    private static final String MODE_CHANGING_ACTION =
            "com.android.settings.location.MODE_CHANGING";
    private static final String CURRENT_MODE_KEY = "CURRENT_MODE";
    private static final String NEW_MODE_KEY = "NEW_MODE";

    private SummaryListPreference mLocationMode;
    private RestrictedPreference mRestrictedLocationMode;
    private SwitchPreference mAlwaysScan;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Received location mode change intent: " + intent);
            }
            refreshLocationMode();
        }
    };

    public static LocationFragment newInstance() {
        return new LocationFragment();
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        final Context themedContext = getPreferenceManager().getContext();
        final PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(
                themedContext);
        screen.setTitle(R.string.system_location);

        final RestrictedLockUtils.EnforcedAdmin admin = checkIfUserRestrictionEnforcedByAdmin();
        if (admin == null) {
            mLocationMode = new SummaryListPreference(themedContext);
            screen.addPreference(mLocationMode);
            mLocationMode.setKey(KEY_LOCATION_MODE);
            mLocationMode.setPersistent(false);
            mLocationMode.setTitle(R.string.location_status);
            mLocationMode.setDialogTitle(R.string.location_status);
            mLocationMode.setSummary("%s");
            mLocationMode.setEntries(new CharSequence[] {
                    getString(R.string.location_mode_wifi_description),
                    getString(R.string.off)
            });
            mLocationMode.setEntryValues(new CharSequence[] {
                    LOCATION_MODE_WIFI,
                    LOCATION_MODE_OFF
            });
            mLocationMode.setSummaries(new CharSequence[] {
                    getString(R.string.system_location_summary),
                    null
            });
            mLocationMode.setOnPreferenceChangeListener(this);
            mLocationMode.setOnPreferenceClickListener(
                    preference -> {
                        logEntrySelected(TvSettingsEnums.PRIVACY_LOCATION_STATUS);
                        return false;
                    });

            mLocationMode.setEnabled(!hasUserRestriction());
        } else {
            mRestrictedLocationMode = new RestrictedPreference(themedContext);
            screen.addPreference(mRestrictedLocationMode);
            mRestrictedLocationMode.setPersistent(false);
            mRestrictedLocationMode.setTitle(R.string.location_status);
            mRestrictedLocationMode.setDisabledByAdmin(admin);
        }

        if (FlavorUtils.isTwoPanel(getContext())) {
            mAlwaysScan = new SwitchPreference(themedContext);
            mAlwaysScan.setKey(KEY_WIFI_ALWAYS_SCAN);
            mAlwaysScan.setTitle(R.string.wifi_setting_always_scan);
            mAlwaysScan.setSummary(R.string.wifi_setting_always_scan_context);
            screen.addPreference(mAlwaysScan);
            mAlwaysScan.setOnPreferenceChangeListener(this);
        }
        final PreferenceCategory recentRequests = new PreferenceCategory(themedContext);
        screen.addPreference(recentRequests);
        recentRequests.setTitle(R.string.location_category_recent_location_requests);

        List<RecentLocationApps.Request> recentLocationRequests =
                new RecentLocationApps(themedContext).getAppList(true);
        List<Preference> recentLocationPrefs = new ArrayList<>(recentLocationRequests.size());
        for (final RecentLocationApps.Request request : recentLocationRequests) {
            Preference pref = new Preference(themedContext);
            pref.setIcon(request.icon);
            pref.setTitle(request.label);
            // Most Android TV devices don't have built-in batteries and we ONLY show "High/Low
            // battery use" for devices with built-in batteries when they are not plugged-in.
            final BatteryManager batteryManager = (BatteryManager) getContext()
                    .getSystemService(Context.BATTERY_SERVICE);
            if (batteryManager != null && !batteryManager.isCharging()) {
                if (request.isHighBattery) {
                    pref.setSummary(R.string.location_high_battery_use);
                } else {
                    pref.setSummary(R.string.location_low_battery_use);
                }
            }
            pref.setOnPreferenceClickListener(
                    preference -> {
                        logEntrySelected(TvSettingsEnums.PRIVACY_LOCATION_REQUESTED_APP);
                        return false;
                    });
            pref.setFragment(AppManagementFragment.class.getName());
            AppManagementFragment.prepareArgs(pref.getExtras(), request.packageName);
            recentLocationPrefs.add(pref);
        }

        if (recentLocationRequests.size() > 0) {
            addPreferencesSorted(recentLocationPrefs, recentRequests);
        } else {
            // If there's no item to display, add a "No recent apps" item.
            Preference banner = new Preference(themedContext);
            banner.setTitle(R.string.location_no_recent_apps);
            banner.setSelectable(false);
            recentRequests.addPreference(banner);
        }

        // TODO: are location services relevant on TV?

        setPreferenceScreen(screen);
    }

    private boolean hasUserRestriction() {
        final UserManager um = UserManager.get(getContext());
        return um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)
                || um.hasUserRestriction(UserManager.DISALLOW_CONFIG_LOCATION);
    }

    private RestrictedLockUtils.EnforcedAdmin checkIfUserRestrictionEnforcedByAdmin() {
        final RestrictedLockUtils.EnforcedAdmin admin = RestrictedLockUtilsInternal
                .checkIfRestrictionEnforced(getContext(),
                        UserManager.DISALLOW_SHARE_LOCATION, UserHandle.myUserId());

        if (admin != null) {
            return admin;
        }

        return RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getContext(),
                UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.myUserId());
    }

    // When selecting the location preference, LeanbackPreferenceFragment
    // creates an inner view with the selection options; that's when we want to
    // register our receiver, bacause from now on user can change the location
    // providers.
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().registerReceiver(mReceiver,
                new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
        refreshLocationMode();
    }

    @Override
    public void onResume() {
        super.onResume();
        updateConnectivity();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(mReceiver);
    }

    private void addPreferencesSorted(List<Preference> prefs, PreferenceGroup container) {
        // If there's some items to display, sort the items and add them to the container.
        prefs.sort(Comparator.comparing(lhs -> lhs.getTitle().toString()));
        for (Preference entry : prefs) {
            container.addPreference(entry);
        }
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (TextUtils.equals(preference.getKey(), KEY_LOCATION_MODE)) {
            int mode = Settings.Secure.LOCATION_MODE_OFF;
            if (TextUtils.equals((CharSequence) newValue, LOCATION_MODE_WIFI)) {
                mode = Settings.Secure.LOCATION_MODE_ON;
                logEntrySelected(TvSettingsEnums.PRIVACY_LOCATION_STATUS_USE_WIFI);
            } else if (TextUtils.equals((CharSequence) newValue, LOCATION_MODE_OFF)) {
                mode = Settings.Secure.LOCATION_MODE_OFF;
                logEntrySelected(TvSettingsEnums.PRIVACY_LOCATION_STATUS_OFF);
            } else {
                Log.wtf(TAG, "Tried to set unknown location mode!");
            }

            writeLocationMode(mode);
            refreshLocationMode();
        }
        return true;
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (TextUtils.equals(preference.getKey(), KEY_WIFI_ALWAYS_SCAN)) {
            Settings.Global.putInt(getActivity().getContentResolver(),
                    Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE,
                    mAlwaysScan.isChecked() ? 1 : 0);
            logToggleInteracted(
                    TvSettingsEnums.PRIVACY_LOCATION_ALWAYS_SCANNING_NETWORKS,
                    mAlwaysScan.isChecked());
            updateConnectivity();
        }
        return super.onPreferenceTreeClick(preference);
    }

    private void writeLocationMode(int mode) {
        int currentMode = Settings.Secure.getInt(getActivity().getContentResolver(),
                Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
        Intent intent = new Intent(MODE_CHANGING_ACTION);
        intent.putExtra(CURRENT_MODE_KEY, currentMode);
        intent.putExtra(NEW_MODE_KEY, mode);
        getActivity().sendBroadcast(intent, android.Manifest.permission.WRITE_SECURE_SETTINGS);
        getActivity().getSystemService(LocationManager.class).setLocationEnabledForUser(
                mode != Settings.Secure.LOCATION_MODE_OFF,
                Process.myUserHandle());
    }

    private void refreshLocationMode() {
        boolean locationEnabled = getActivity().getSystemService(LocationManager.class)
                .isLocationEnabled();
        if (mLocationMode != null) {
            if (locationEnabled) {
                mLocationMode.setValue(LOCATION_MODE_WIFI);
            } else {
                mLocationMode.setValue(LOCATION_MODE_OFF);
            }
        }
        if (mRestrictedLocationMode != null) {
            if (locationEnabled) {
                mRestrictedLocationMode
                        .setSummary(getString(R.string.location_mode_wifi_description));
            } else {
                mRestrictedLocationMode.setSummary(getString(R.string.off));
            }
        }
    }

    private void updateConnectivity() {
        if (FlavorUtils.isTwoPanel(getContext())) {
            int scanAlwaysAvailable = Settings.Global.getInt(getActivity().getContentResolver(),
                    Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0);
            mAlwaysScan.setChecked(scanAlwaysAvailable == 1);
        }
    }

    @Override
    protected int getPageId() {
        return TvSettingsEnums.PRIVACY_LOCATION;
    }
}