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.view.View; 29 30 import androidx.annotation.NonNull; 31 import androidx.annotation.Nullable; 32 import androidx.annotation.XmlRes; 33 34 import com.android.car.settings.R; 35 import com.android.car.settings.common.SettingsFragment; 36 import com.android.car.settings.search.CarBaseSearchIndexProvider; 37 import com.android.car.ui.toolbar.MenuItem; 38 import com.android.settingslib.Utils; 39 import com.android.settingslib.search.SearchIndexable; 40 41 import java.util.Collections; 42 import java.util.List; 43 44 /** 45 * Main page that hosts Location related preferences. 46 */ 47 @SearchIndexable 48 public class LocationSettingsFragment extends SettingsFragment { 49 private static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED = 50 new IntentFilter(LocationManager.MODE_CHANGED_ACTION); 51 52 private LocationManager mLocationManager; 53 private MenuItem mLocationSwitch; 54 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 55 @Override 56 public void onReceive(Context context, Intent intent) { 57 mLocationSwitch.setChecked(mLocationManager.isLocationEnabled()); 58 } 59 }; 60 61 @Override getToolbarMenuItems()62 public List<MenuItem> getToolbarMenuItems() { 63 return Collections.singletonList(mLocationSwitch); 64 } 65 66 @Override onCreate(Bundle savedInstanceState)67 public void onCreate(Bundle savedInstanceState) { 68 super.onCreate(savedInstanceState); 69 70 mLocationSwitch = new MenuItem.Builder(getContext()) 71 .setCheckable() 72 .setOnClickListener(menuItem -> 73 Utils.updateLocationEnabled( 74 requireContext(), 75 menuItem.isChecked(), 76 UserHandle.myUserId(), 77 Settings.Secure.LOCATION_CHANGER_SYSTEM_SETTINGS)) 78 .build(); 79 } 80 81 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)82 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 83 super.onViewCreated(view, savedInstanceState); 84 enableRotaryScroll(); 85 } 86 87 @Override 88 @XmlRes getPreferenceScreenResId()89 protected int getPreferenceScreenResId() { 90 return R.xml.location_settings_fragment; 91 } 92 93 @Override onAttach(Context context)94 public void onAttach(Context context) { 95 super.onAttach(context); 96 mLocationManager = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE); 97 } 98 99 @Override onStart()100 public void onStart() { 101 super.onStart(); 102 requireContext().registerReceiver(mReceiver, INTENT_FILTER_LOCATION_MODE_CHANGED); 103 mLocationSwitch.setChecked(mLocationManager.isLocationEnabled()); 104 } 105 106 @Override onStop()107 public void onStop() { 108 super.onStop(); 109 requireContext().unregisterReceiver(mReceiver); 110 } 111 112 public static final CarBaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 113 new CarBaseSearchIndexProvider(R.xml.location_settings_fragment, 114 Settings.ACTION_LOCATION_SOURCE_SETTINGS); 115 } 116