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