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.SettingInjectorService; 25 import android.os.UserHandle; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceGroup; 30 31 import com.android.car.settings.common.FragmentController; 32 import com.android.car.settings.common.Logger; 33 import com.android.car.settings.common.PreferenceController; 34 import com.android.settingslib.location.SettingsInjector; 35 36 import java.util.ArrayList; 37 import java.util.Collections; 38 import java.util.List; 39 import java.util.Map; 40 41 /** 42 * Injects Location Services into a {@link PreferenceGroup} with a matching key. 43 */ 44 public class LocationServicesPreferenceController extends PreferenceController<PreferenceGroup> { 45 private static final Logger LOG = new Logger(LocationServicesPreferenceController.class); 46 private static final IntentFilter INTENT_FILTER_INJECTED_SETTING_CHANGED = new IntentFilter( 47 SettingInjectorService.ACTION_INJECTED_SETTING_CHANGED); 48 private SettingsInjector mSettingsInjector; 49 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 50 @Override 51 public void onReceive(Context context, Intent intent) { 52 LOG.i("Received injected settings change intent: " + intent); 53 mSettingsInjector.reloadStatusMessages(); 54 } 55 }; 56 LocationServicesPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)57 public LocationServicesPreferenceController(Context context, String preferenceKey, 58 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 59 super(context, preferenceKey, fragmentController, uxRestrictions); 60 mSettingsInjector = new CarLocationSettingsInjector(context); 61 } 62 63 @VisibleForTesting setSettingsInjector(SettingsInjector injector)64 void setSettingsInjector(SettingsInjector injector) { 65 mSettingsInjector = injector; 66 } 67 68 @Override getPreferenceType()69 protected Class<PreferenceGroup> getPreferenceType() { 70 return PreferenceGroup.class; 71 } 72 73 /** 74 * Called when the controller is started. 75 */ 76 @Override onStartInternal()77 protected void onStartInternal() { 78 getContext().registerReceiver(mReceiver, INTENT_FILTER_INJECTED_SETTING_CHANGED, 79 Context.RECEIVER_EXPORTED); 80 } 81 82 /** 83 * Called when the controller is stopped. 84 */ 85 @Override onStopInternal()86 protected void onStopInternal() { 87 getContext().unregisterReceiver(mReceiver); 88 } 89 90 @Override updateState(PreferenceGroup preferenceGroup)91 protected void updateState(PreferenceGroup preferenceGroup) { 92 getPreference().removeAll(); 93 List<Preference> injectedSettings = getSortedInjectedPreferences(UserHandle.USER_CURRENT); 94 for (Preference preference : injectedSettings) { 95 getPreference().addPreference(preference); 96 } 97 98 preferenceGroup.setVisible(preferenceGroup.getPreferenceCount() > 0); 99 } 100 getSortedInjectedPreferences(int profileId)101 private List<Preference> getSortedInjectedPreferences(int profileId) { 102 List<Preference> sortedInjections = new ArrayList<>(); 103 Map<Integer, List<Preference>> injections = 104 mSettingsInjector.getInjectedSettings(getContext(), profileId); 105 for (Map.Entry<Integer, List<Preference>> entry : injections.entrySet()) { 106 sortedInjections.addAll(entry.getValue()); 107 } 108 Collections.sort(sortedInjections); 109 return sortedInjections; 110 } 111 } 112