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 @VisibleForTesting 39 static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED = 40 new IntentFilter(LocationManager.MODE_CHANGED_ACTION); 41 42 private final LocationManager mLocationManager; 43 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 44 @Override 45 public void onReceive(Context context, Intent intent) { 46 refreshUi(); 47 } 48 }; 49 RecentLocationRequestsEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)50 public RecentLocationRequestsEntryPreferenceController(Context context, String preferenceKey, 51 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 52 super(context, preferenceKey, fragmentController, uxRestrictions); 53 mLocationManager = getContext().getSystemService(LocationManager.class); 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