1 /* 2 * Copyright (C) 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 17 package com.android.car.settings.location; 18 19 import android.app.Service; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.location.LocationManager; 25 import android.os.Bundle; 26 import android.os.UserHandle; 27 import android.provider.Settings; 28 import android.widget.Switch; 29 30 import androidx.annotation.LayoutRes; 31 import androidx.annotation.XmlRes; 32 33 import com.android.car.settings.R; 34 import com.android.car.settings.common.SettingsFragment; 35 import com.android.settingslib.Utils; 36 37 /** 38 * Main page that hosts Location related preferences. 39 */ 40 public class LocationSettingsFragment extends SettingsFragment { 41 private static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED = 42 new IntentFilter(LocationManager.MODE_CHANGED_ACTION); 43 44 private LocationManager mLocationManager; 45 private Switch mLocationSwitch; 46 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 47 @Override 48 public void onReceive(Context context, Intent intent) { 49 mLocationSwitch.setChecked(mLocationManager.isLocationEnabled()); 50 } 51 }; 52 53 @Override 54 @XmlRes getPreferenceScreenResId()55 protected int getPreferenceScreenResId() { 56 return R.xml.location_settings_fragment; 57 } 58 59 @Override 60 @LayoutRes getActionBarLayoutId()61 protected int getActionBarLayoutId() { 62 return R.layout.action_bar_with_toggle; 63 } 64 65 @Override onAttach(Context context)66 public void onAttach(Context context) { 67 super.onAttach(context); 68 mLocationManager = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE); 69 } 70 71 @Override onActivityCreated(Bundle savedInstanceState)72 public void onActivityCreated(Bundle savedInstanceState) { 73 super.onActivityCreated(savedInstanceState); 74 mLocationSwitch = requireActivity().findViewById(R.id.toggle_switch); 75 } 76 77 @Override onStart()78 public void onStart() { 79 super.onStart(); 80 requireContext().registerReceiver(mReceiver, INTENT_FILTER_LOCATION_MODE_CHANGED); 81 updateLocationSwitch(); 82 } 83 84 @Override onStop()85 public void onStop() { 86 super.onStop(); 87 requireContext().unregisterReceiver(mReceiver); 88 } 89 90 // Update the location master switch's state upon starting the fragment. updateLocationSwitch()91 private void updateLocationSwitch() { 92 mLocationSwitch.setChecked(mLocationManager.isLocationEnabled()); 93 mLocationSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> 94 Utils.updateLocationEnabled(requireContext(), isChecked, UserHandle.myUserId(), 95 Settings.Secure.LOCATION_CHANGER_SYSTEM_SETTINGS)); 96 } 97 } 98