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 } 80 81 /** 82 * Called when the controller is stopped. 83 */ 84 @Override onStopInternal()85 protected void onStopInternal() { 86 getContext().unregisterReceiver(mReceiver); 87 } 88 89 @Override updateState(PreferenceGroup preferenceGroup)90 protected void updateState(PreferenceGroup preferenceGroup) { 91 getPreference().removeAll(); 92 List<Preference> injectedSettings = getSortedInjectedPreferences(UserHandle.USER_CURRENT); 93 for (Preference preference : injectedSettings) { 94 getPreference().addPreference(preference); 95 } 96 97 preferenceGroup.setVisible(preferenceGroup.getPreferenceCount() > 0); 98 } 99 getSortedInjectedPreferences(int profileId)100 private List<Preference> getSortedInjectedPreferences(int profileId) { 101 List<Preference> sortedInjections = new ArrayList<>(); 102 Map<Integer, List<Preference>> injections = 103 mSettingsInjector.getInjectedSettings(getContext(), profileId); 104 for (Map.Entry<Integer, List<Preference>> entry : injections.entrySet()) { 105 sortedInjections.addAll(entry.getValue()); 106 } 107 Collections.sort(sortedInjections); 108 return sortedInjections; 109 } 110 } 111