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.car.drivingstate.CarUxRestrictions;
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 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.Preference;
28 
29 import com.android.car.settings.common.FragmentController;
30 import com.android.car.settings.common.PreferenceController;
31 
32 /**
33  * Disables Recent Location Requests entry when location is off.
34  */
35 public class RecentLocationRequestsEntryPreferenceController extends
36         PreferenceController<Preference> {
37 
38     private final LocationManager mLocationManager;
39 
40     @VisibleForTesting
41     static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED =
42             new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
43 
44     @VisibleForTesting
45     final BroadcastReceiver mReceiver = new BroadcastReceiver() {
46         @Override
47         public void onReceive(Context context, Intent intent) {
48             refreshUi();
49         }
50     };
51 
RecentLocationRequestsEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)52     public RecentLocationRequestsEntryPreferenceController(Context context, String preferenceKey,
53             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
54         super(context, preferenceKey, fragmentController, uxRestrictions);
55         mLocationManager = getContext().getSystemService(LocationManager.class);
56     }
57 
58 
59     @Override
getPreferenceType()60     protected Class<Preference> getPreferenceType() {
61         return Preference.class;
62     }
63 
64     @Override
updateState(Preference preference)65     protected void updateState(Preference preference) {
66         preference.setEnabled(mLocationManager.isLocationEnabled());
67     }
68 
69     @Override
onStartInternal()70     protected void onStartInternal() {
71         getContext().registerReceiver(mReceiver, INTENT_FILTER_LOCATION_MODE_CHANGED);
72     }
73 
74     @Override
onStopInternal()75     protected void onStopInternal() {
76         getContext().unregisterReceiver(mReceiver);
77     }
78 }
79