1 /* 2 * Copyright (C) 2017 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 package com.android.car.settings.home; 17 18 19 import static com.android.car.settings.home.ExtraSettingsLoader.DEVICE_CATEGORY; 20 import static com.android.car.settings.home.ExtraSettingsLoader.PERSONAL_CATEGORY; 21 import static com.android.car.settings.home.ExtraSettingsLoader.WIRELESS_CATEGORY; 22 23 import android.bluetooth.BluetoothAdapter; 24 import android.car.user.CarUserManagerHelper; 25 import android.content.BroadcastReceiver; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.IntentFilter; 29 import android.graphics.drawable.Icon; 30 import android.os.Bundle; 31 32 import com.android.car.list.LaunchAppLineItem; 33 import com.android.car.list.TypedPagedListAdapter; 34 import com.android.car.settings.R; 35 import com.android.car.settings.accounts.AccountsListFragment; 36 import com.android.car.settings.applications.ApplicationSettingsFragment; 37 import com.android.car.settings.common.ListSettingsFragment; 38 import com.android.car.settings.common.Logger; 39 import com.android.car.settings.datetime.DatetimeSettingsFragment; 40 import com.android.car.settings.display.DisplaySettingsFragment; 41 import com.android.car.settings.security.SettingsScreenLockActivity; 42 import com.android.car.settings.sound.SoundSettingsFragment; 43 import com.android.car.settings.suggestions.SettingsSuggestionsController; 44 import com.android.car.settings.system.SystemSettingsFragment; 45 import com.android.car.settings.users.UsersListFragment; 46 import com.android.car.settings.wifi.CarWifiManager; 47 import com.android.car.settings.wifi.WifiUtil; 48 49 import java.util.ArrayList; 50 import java.util.Collection; 51 import java.util.Map; 52 53 /** 54 * Homepage for settings for car. 55 */ 56 public class HomepageFragment extends ListSettingsFragment implements 57 CarWifiManager.Listener, 58 SettingsSuggestionsController.Listener { 59 private static final Logger LOG = new Logger(HomepageFragment.class); 60 61 private SettingsSuggestionsController mSettingsSuggestionsController; 62 private CarWifiManager mCarWifiManager; 63 private WifiLineItem mWifiLineItem; 64 private BluetoothLineItem mBluetoothLineItem; 65 private CarUserManagerHelper mCarUserManagerHelper; 66 // This tracks the number of suggestions currently shown in the fragment. This is based off of 67 // the assumption that suggestions are 0 through (num suggestions - 1) in the adapter. Do not 68 // change this assumption without updating the code in onSuggestionLoaded. 69 private int mNumSettingsSuggestions; 70 71 private final BroadcastReceiver mBtStateReceiver = new BroadcastReceiver() { 72 @Override 73 public void onReceive(Context context, Intent intent) { 74 String action = intent.getAction(); 75 76 if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 77 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 78 BluetoothAdapter.ERROR); 79 switch (state) { 80 case BluetoothAdapter.STATE_TURNING_OFF: 81 // TODO show a different status icon? 82 case BluetoothAdapter.STATE_OFF: 83 mBluetoothLineItem.onBluetoothStateChanged(false); 84 break; 85 default: 86 mBluetoothLineItem.onBluetoothStateChanged(true); 87 } 88 } 89 } 90 }; 91 92 private final IntentFilter mBtStateChangeFilter = 93 new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 94 95 /** 96 * Gets an instance of this class. 97 */ newInstance()98 public static HomepageFragment newInstance() { 99 HomepageFragment homepageFragment = new HomepageFragment(); 100 Bundle bundle = ListSettingsFragment.getBundle(); 101 bundle.putInt(EXTRA_TITLE_ID, R.string.settings_label); 102 homepageFragment.setArguments(bundle); 103 return homepageFragment; 104 } 105 106 @Override onActivityCreated(Bundle savedInstanceState)107 public void onActivityCreated(Bundle savedInstanceState) { 108 LOG.v("onActivityCreated: " + savedInstanceState); 109 mSettingsSuggestionsController = 110 new SettingsSuggestionsController( 111 getContext(), 112 getLoaderManager(), 113 /* listener= */ this); 114 mCarWifiManager = new CarWifiManager(getContext(), /* listener= */ this); 115 if (WifiUtil.isWifiAvailable(getContext())) { 116 mWifiLineItem = new WifiLineItem( 117 getContext(), mCarWifiManager, getFragmentController()); 118 } 119 mBluetoothLineItem = new BluetoothLineItem(getContext(), getFragmentController()); 120 mCarUserManagerHelper = new CarUserManagerHelper(getContext()); 121 122 // reset the suggestion count. 123 mNumSettingsSuggestions = 0; 124 125 // Call super after the wifiLineItem and BluetoothLineItem are setup, because 126 // those are needed in super.onCreate(). 127 super.onActivityCreated(savedInstanceState); 128 } 129 130 @Override onAccessPointsChanged()131 public void onAccessPointsChanged() { 132 // don't care 133 } 134 135 @Override onWifiStateChanged(int state)136 public void onWifiStateChanged(int state) { 137 mWifiLineItem.onWifiStateChanged(state); 138 } 139 140 @Override onStart()141 public void onStart() { 142 super.onStart(); 143 mCarWifiManager.start(); 144 mSettingsSuggestionsController.start(); 145 getActivity().registerReceiver(mBtStateReceiver, mBtStateChangeFilter); 146 } 147 148 @Override onStop()149 public void onStop() { 150 super.onStop(); 151 mCarWifiManager.stop(); 152 mSettingsSuggestionsController.stop(); 153 getActivity().unregisterReceiver(mBtStateReceiver); 154 } 155 156 @Override onDestroy()157 public void onDestroy() { 158 super.onDestroy(); 159 mCarWifiManager.destroy(); 160 } 161 162 @Override getLineItems()163 public ArrayList<TypedPagedListAdapter.LineItem> getLineItems() { 164 ExtraSettingsLoader extraSettingsLoader = new ExtraSettingsLoader(getContext()); 165 Map<String, Collection<TypedPagedListAdapter.LineItem>> extraSettings = 166 extraSettingsLoader.load(); 167 ArrayList<TypedPagedListAdapter.LineItem> lineItems = new ArrayList<>(); 168 lineItems.add(new SimpleIconTransitionLineItem( 169 R.string.display_settings, 170 R.drawable.ic_settings_display, 171 getContext(), 172 null, 173 DisplaySettingsFragment.newInstance(), 174 getFragmentController())); 175 lineItems.add(new SimpleIconTransitionLineItem( 176 R.string.sound_settings, 177 R.drawable.ic_settings_sound, 178 getContext(), 179 null, 180 SoundSettingsFragment.newInstance(), 181 getFragmentController())); 182 if (mWifiLineItem != null) { 183 lineItems.add(mWifiLineItem); 184 } 185 lineItems.addAll(extraSettings.get(WIRELESS_CATEGORY)); 186 lineItems.add(mBluetoothLineItem); 187 lineItems.add(new SimpleIconTransitionLineItem( 188 R.string.applications_settings, 189 R.drawable.ic_settings_applications, 190 getContext(), 191 null, 192 ApplicationSettingsFragment.newInstance(), 193 getFragmentController())); 194 lineItems.add(new SimpleIconTransitionLineItem( 195 R.string.date_and_time_settings_title, 196 R.drawable.ic_settings_date_time, 197 getContext(), 198 null, 199 DatetimeSettingsFragment.getInstance(), 200 getFragmentController())); 201 lineItems.add(new SimpleIconTransitionLineItem( 202 R.string.users_list_title, 203 R.drawable.ic_user, 204 getContext(), 205 null, 206 UsersListFragment.newInstance(), 207 getFragmentController())); 208 209 // Guest users can't set screen locks or add/remove accounts. 210 if (!mCarUserManagerHelper.isCurrentProcessGuestUser()) { 211 lineItems.add(new SimpleIconTransitionLineItem( 212 R.string.accounts_settings_title, 213 R.drawable.ic_account, 214 getContext(), 215 null, 216 AccountsListFragment.newInstance(), 217 getFragmentController())); 218 lineItems.add(new LaunchAppLineItem( 219 getString(R.string.security_settings_title), 220 Icon.createWithResource(getContext(), R.drawable.ic_lock), 221 getContext(), 222 null, 223 new Intent(getContext(), SettingsScreenLockActivity.class))); 224 } 225 226 lineItems.add(new SimpleIconTransitionLineItem( 227 R.string.system_setting_title, 228 R.drawable.ic_settings_about, 229 getContext(), 230 null, 231 SystemSettingsFragment.getInstance(), 232 getFragmentController())); 233 234 lineItems.addAll(extraSettings.get(DEVICE_CATEGORY)); 235 lineItems.addAll(extraSettings.get(PERSONAL_CATEGORY)); 236 return lineItems; 237 } 238 239 @Override onSuggestionsLoaded(ArrayList<TypedPagedListAdapter.LineItem> suggestions)240 public void onSuggestionsLoaded(ArrayList<TypedPagedListAdapter.LineItem> suggestions) { 241 LOG.v("onDeferredSuggestionsLoaded"); 242 for (int index = 0; index < mNumSettingsSuggestions; index++) { 243 mPagedListAdapter.remove(0); 244 } 245 mNumSettingsSuggestions = suggestions.size(); 246 mPagedListAdapter.addAll(0, suggestions); 247 } 248 249 @Override onSuggestionDismissed(int adapterPosition)250 public void onSuggestionDismissed(int adapterPosition) { 251 LOG.v("onSuggestionDismissed adapterPosition " + adapterPosition); 252 mPagedListAdapter.remove(adapterPosition); 253 } 254 } 255