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