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.car.drivingstate.CarUxRestrictions;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.location.LocationManager;
26 
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 static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED =
39             new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
40 
41     private final LocationManager mLocationManager;
42     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
43         @Override
44         public void onReceive(Context context, Intent intent) {
45             refreshUi();
46         }
47     };
48 
RecentLocationRequestsEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)49     public RecentLocationRequestsEntryPreferenceController(Context context, String preferenceKey,
50             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
51         super(context, preferenceKey, fragmentController, uxRestrictions);
52         mLocationManager = (LocationManager) getContext().getSystemService(
53                 Service.LOCATION_SERVICE);
54     }
55 
56 
57     @Override
getPreferenceType()58     protected Class<Preference> getPreferenceType() {
59         return Preference.class;
60     }
61 
62     @Override
updateState(Preference preference)63     protected void updateState(Preference preference) {
64         preference.setEnabled(mLocationManager.isLocationEnabled());
65     }
66 
67     @Override
onStartInternal()68     protected void onStartInternal() {
69         getContext().registerReceiver(mReceiver, INTENT_FILTER_LOCATION_MODE_CHANGED);
70     }
71 
72     @Override
onStopInternal()73     protected void onStopInternal() {
74         getContext().unregisterReceiver(mReceiver);
75     }
76 }
77