1 /* 2 * Copyright 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.common; 18 19 import static com.android.car.settings.deeplink.DeepLinkHomepageActivity.EXTRA_TARGET_SECONDARY_CONTAINER; 20 21 import android.car.drivingstate.CarUxRestrictions; 22 import android.car.drivingstate.CarUxRestrictionsManager; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentSender; 26 import android.os.Bundle; 27 import android.util.ArrayMap; 28 import android.util.SparseArray; 29 import android.util.TypedValue; 30 import android.view.ContextThemeWrapper; 31 import android.view.LayoutInflater; 32 import android.view.ViewGroup; 33 34 import androidx.annotation.NonNull; 35 import androidx.annotation.Nullable; 36 import androidx.annotation.StringRes; 37 import androidx.annotation.VisibleForTesting; 38 import androidx.annotation.XmlRes; 39 import androidx.fragment.app.DialogFragment; 40 import androidx.fragment.app.Fragment; 41 import androidx.lifecycle.Lifecycle; 42 import androidx.preference.Preference; 43 import androidx.preference.PreferenceScreen; 44 import androidx.recyclerview.widget.RecyclerView; 45 46 import com.android.car.settings.CarSettingsApplication; 47 import com.android.car.settings.R; 48 import com.android.car.settings.activityembedding.ActivityEmbeddingUtils; 49 import com.android.car.ui.baselayout.Insets; 50 import com.android.car.ui.preference.PreferenceFragment; 51 import com.android.car.ui.recyclerview.CarUiRecyclerView; 52 import com.android.car.ui.toolbar.MenuItem; 53 import com.android.car.ui.toolbar.NavButtonMode; 54 import com.android.car.ui.toolbar.ToolbarController; 55 import com.android.car.ui.utils.ViewUtils; 56 import com.android.settingslib.search.Indexable; 57 58 import java.util.ArrayList; 59 import java.util.List; 60 import java.util.Map; 61 62 /** 63 * Base fragment for all settings. Subclasses must provide a resource id via 64 * {@link #getPreferenceScreenResId()} for the XML resource which defines the preferences to 65 * display and controllers to update their state. This class is responsible for displaying the 66 * preferences, creating {@link PreferenceController} instances from the metadata, and 67 * associating the preferences with their corresponding controllers. 68 * 69 * <p>{@code preferenceTheme} must be specified in the application theme, and the parent to which 70 * this fragment attaches must implement {@link UxRestrictionsProvider} and 71 * {@link FragmentController} or an {@link IllegalStateException} will be thrown during 72 * {@link #onAttach(Context)}. Changes to driving state restrictions are propagated to 73 * controllers. 74 */ 75 public abstract class SettingsFragment extends PreferenceFragment implements 76 CarUxRestrictionsManager.OnUxRestrictionsChangedListener, FragmentController, Indexable { 77 78 @VisibleForTesting 79 static final String DIALOG_FRAGMENT_TAG = 80 "com.android.car.settings.common.SettingsFragment.DIALOG"; 81 82 private static final int MAX_NUM_PENDING_ACTIVITY_RESULT_CALLBACKS = 0xff - 1; 83 84 private final Map<String, PreferenceController> mPreferenceControllersLookup = new ArrayMap<>(); 85 private final List<PreferenceController> mPreferenceControllers = new ArrayList<>(); 86 private final SparseArray<ActivityResultCallback> mActivityResultCallbackMap = 87 new SparseArray<>(); 88 89 private CarUxRestrictions mUxRestrictions; 90 private HighlightablePreferenceGroupAdapter mAdapter; 91 private int mCurrentRequestIndex = 0; 92 93 /** 94 * Returns the resource id for the preference XML of this fragment. 95 */ 96 @XmlRes getPreferenceScreenResId()97 protected abstract int getPreferenceScreenResId(); 98 getToolbar()99 protected ToolbarController getToolbar() { 100 return getFragmentHost().getToolbar(); 101 } 102 /** 103 * Returns the MenuItems to display in the toolbar. Subclasses should override this to 104 * add additional buttons, switches, ect. to the toolbar. 105 */ getToolbarMenuItems()106 protected List<MenuItem> getToolbarMenuItems() { 107 return null; 108 } 109 110 /** 111 * Returns the controller of the given {@code clazz} for the given {@code 112 * preferenceKeyResId}. Subclasses may use this method in {@link #onAttach(Context)} to call 113 * setters on controllers to pass additional arguments after construction. 114 * 115 * <p>For example: 116 * <pre>{@code 117 * @Override 118 * public void onAttach(Context context) { 119 * super.onAttach(context); 120 * use(MyPreferenceController.class, R.string.pk_my_key).setMyArg(myArg); 121 * } 122 * }</pre> 123 * 124 * <p>Important: Use judiciously to minimize tight coupling between controllers and fragments. 125 */ 126 @SuppressWarnings("unchecked") // PreferenceKey is the map key use(Class<T> clazz, @StringRes int preferenceKeyResId)127 protected <T extends PreferenceController> T use(Class<T> clazz, 128 @StringRes int preferenceKeyResId) { 129 String preferenceKey = getString(preferenceKeyResId); 130 return (T) mPreferenceControllersLookup.get(preferenceKey); 131 } 132 133 /** 134 * Enables rotary scrolling for the {@link CarUiRecyclerView} in this fragment. 135 * <p> 136 * Rotary scrolling should be enabled for scrolling views which contain content which the user 137 * may want to see but can't interact with, either alone or along with interactive (focusable) 138 * content. 139 */ enableRotaryScroll()140 protected void enableRotaryScroll() { 141 CarUiRecyclerView recyclerView = getView().findViewById(R.id.settings_recycler_view); 142 if (recyclerView != null) { 143 ViewUtils.setRotaryScrollEnabled(recyclerView.getView(), /* isVertical= */ true); 144 } 145 } 146 147 @Override onAttach(Context context)148 public void onAttach(Context context) { 149 super.onAttach(context); 150 if (!(getActivity() instanceof UxRestrictionsProvider)) { 151 throw new IllegalStateException("Must attach to a UxRestrictionsProvider"); 152 } 153 if (!(getActivity() instanceof FragmentHost)) { 154 throw new IllegalStateException("Must attach to a FragmentHost"); 155 } 156 157 TypedValue tv = new TypedValue(); 158 getActivity().getTheme().resolveAttribute(androidx.preference.R.attr.preferenceTheme, tv, 159 true); 160 int theme = tv.resourceId; 161 if (theme == 0) { 162 throw new IllegalStateException("Must specify preferenceTheme in theme"); 163 } 164 // Construct a context with the theme as controllers may create new preferences. 165 Context styledContext = new ContextThemeWrapper(getActivity(), theme); 166 167 mUxRestrictions = ((UxRestrictionsProvider) requireActivity()).getCarUxRestrictions(); 168 mPreferenceControllers.clear(); 169 mPreferenceControllers.addAll( 170 PreferenceControllerListHelper.getPreferenceControllersFromXml(styledContext, 171 getPreferenceScreenResId(), /* fragmentController= */ this, 172 mUxRestrictions)); 173 174 Lifecycle lifecycle = getLifecycle(); 175 mPreferenceControllers.forEach(controller -> { 176 lifecycle.addObserver(controller); 177 mPreferenceControllersLookup.put(controller.getPreferenceKey(), controller); 178 }); 179 } 180 181 /** 182 * Inflates the preferences from {@link #getPreferenceScreenResId()} and associates the 183 * preference with their corresponding {@link PreferenceController} instances. 184 */ 185 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)186 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 187 @XmlRes int resId = getPreferenceScreenResId(); 188 if (resId <= 0) { 189 throw new IllegalStateException( 190 "Fragment must specify a preference screen resource ID"); 191 } 192 addPreferencesFromResource(resId); 193 PreferenceScreen screen = getPreferenceScreen(); 194 for (PreferenceController controller : mPreferenceControllers) { 195 Preference pref = screen.findPreference(controller.getPreferenceKey()); 196 197 controller.setPreference(pref); 198 } 199 } 200 201 @Override onCreateCarUiRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)202 public CarUiRecyclerView onCreateCarUiRecyclerView(LayoutInflater inflater, ViewGroup parent, 203 Bundle savedInstanceState) { 204 inflater.inflate(R.layout.settings_recyclerview_default, parent, /* attachToRoot= */ true); 205 return parent.findViewById(R.id.settings_recycler_view); 206 } 207 208 @Override setupToolbar(@onNull ToolbarController toolbar)209 protected void setupToolbar(@NonNull ToolbarController toolbar) { 210 List<MenuItem> items = getToolbarMenuItems(); 211 if (items != null) { 212 if (items.size() == 1) { 213 items.get(0).setId(R.id.toolbar_menu_item_0); 214 } else if (items.size() == 2) { 215 items.get(0).setId(R.id.toolbar_menu_item_0); 216 items.get(1).setId(R.id.toolbar_menu_item_1); 217 } 218 } 219 toolbar.setTitle(getPreferenceScreen().getTitle()); 220 toolbar.setMenuItems(items); 221 toolbar.setLogo(null); 222 if (shouldDisableBackButton()) { 223 toolbar.setNavButtonMode(NavButtonMode.DISABLED); 224 } else { 225 toolbar.setNavButtonMode(NavButtonMode.BACK); 226 } 227 } 228 shouldDisableBackButton()229 private boolean shouldDisableBackButton() { 230 if (getActivity() == null || getActivity().getIntent() == null) { 231 return false; 232 } 233 if (getFragmentManager().getBackStackEntryCount() > 1 234 || getActivity().getIntent().getBooleanExtra(EXTRA_TARGET_SECONDARY_CONTAINER, 235 false)) { 236 return false; 237 } 238 boolean startedBySettings = getActivity().getIntent().getAction() == null 239 || CarSettingsApplication.CAR_SETTINGS_PACKAGE_NAME.equals( 240 getActivity().getLaunchedFromPackage()); 241 return startedBySettings && ActivityEmbeddingUtils.isEmbeddingSplitActivated(getActivity()); 242 } 243 244 @Override onDetach()245 public void onDetach() { 246 super.onDetach(); 247 Lifecycle lifecycle = getLifecycle(); 248 mPreferenceControllers.forEach(lifecycle::removeObserver); 249 mActivityResultCallbackMap.clear(); 250 } 251 252 @Override onCreateAdapter(PreferenceScreen preferenceScreen)253 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) { 254 mAdapter = createHighlightableAdapter(preferenceScreen); 255 return mAdapter; 256 } 257 258 /** 259 * Returns a HighlightablePreferenceGroupAdapter to be used as the RecyclerView.Adapter for 260 * this fragment. Subclasses can override this method to return their own 261 * HighlightablePreferenceGroupAdapter instance. 262 */ createHighlightableAdapter( PreferenceScreen preferenceScreen)263 protected HighlightablePreferenceGroupAdapter createHighlightableAdapter( 264 PreferenceScreen preferenceScreen) { 265 return new HighlightablePreferenceGroupAdapter(preferenceScreen); 266 } 267 requestPreferenceHighlight(String key)268 protected void requestPreferenceHighlight(String key) { 269 if (mAdapter != null) { 270 mAdapter.requestHighlight(getView(), getListView(), key); 271 } 272 } 273 clearPreferenceHighlight()274 protected void clearPreferenceHighlight() { 275 if (mAdapter != null) { 276 mAdapter.clearHighlight(getView()); 277 } 278 } 279 280 /** 281 * Notifies {@link PreferenceController} instances of changes to {@link CarUxRestrictions}. 282 */ 283 @Override onUxRestrictionsChanged(CarUxRestrictions uxRestrictions)284 public void onUxRestrictionsChanged(CarUxRestrictions uxRestrictions) { 285 if (!uxRestrictions.isSameRestrictions(mUxRestrictions)) { 286 mUxRestrictions = uxRestrictions; 287 for (PreferenceController controller : mPreferenceControllers) { 288 controller.onUxRestrictionsChanged(uxRestrictions); 289 } 290 } 291 } 292 293 /** 294 * {@inheritDoc} 295 * 296 * <p>Settings needs to launch custom dialog types in order to extend the Device Default theme. 297 * 298 * @param preference The Preference object requesting the dialog. 299 */ 300 @Override onDisplayPreferenceDialog(Preference preference)301 public void onDisplayPreferenceDialog(Preference preference) { 302 // check if dialog is already showing 303 if (findDialogByTag(DIALOG_FRAGMENT_TAG) != null) { 304 return; 305 } 306 307 if (preference instanceof ValidatedEditTextPreference) { 308 DialogFragment dialogFragment = preference instanceof PasswordEditTextPreference 309 ? PasswordEditTextPreferenceDialogFragment.newInstance(preference.getKey()) 310 : ValidatedEditTextPreferenceDialogFragment.newInstance(preference.getKey()); 311 312 dialogFragment.setTargetFragment(/* fragment= */ this, /* requestCode= */ 0); 313 showDialog(dialogFragment, DIALOG_FRAGMENT_TAG); 314 } else { 315 super.onDisplayPreferenceDialog(preference); 316 } 317 } 318 319 @Override launchFragment(Fragment fragment)320 public void launchFragment(Fragment fragment) { 321 getFragmentHost().launchFragment(fragment); 322 } 323 324 @Override goBack()325 public void goBack() { 326 getFragmentHost().goBack(); 327 } 328 329 @Override showDialog(DialogFragment dialogFragment, @Nullable String tag)330 public void showDialog(DialogFragment dialogFragment, @Nullable String tag) { 331 dialogFragment.show(getFragmentManager(), tag); 332 } 333 334 @Override showProgressBar(boolean visible)335 public void showProgressBar(boolean visible) { 336 if (getToolbar() != null && getToolbar().getProgressBar() != null) { 337 getToolbar().getProgressBar().setVisible(visible); 338 } 339 } 340 341 @Nullable 342 @Override findDialogByTag(String tag)343 public DialogFragment findDialogByTag(String tag) { 344 Fragment fragment = getFragmentManager().findFragmentByTag(tag); 345 if (fragment instanceof DialogFragment) { 346 return (DialogFragment) fragment; 347 } 348 return null; 349 } 350 351 @NonNull 352 @Override getSettingsLifecycle()353 public Lifecycle getSettingsLifecycle() { 354 return getLifecycle(); 355 } 356 357 @Override startActivityForResult(Intent intent, int requestCode, ActivityResultCallback callback)358 public void startActivityForResult(Intent intent, int requestCode, 359 ActivityResultCallback callback) { 360 validateRequestCodeForPreferenceController(requestCode); 361 int requestIndex = allocateRequestIndex(callback); 362 super.startActivityForResult(intent, ((requestIndex + 1) << 8) + (requestCode & 0xff)); 363 } 364 365 @Override startIntentSenderForResult(IntentSender intent, int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues, Bundle options, ActivityResultCallback callback)366 public void startIntentSenderForResult(IntentSender intent, int requestCode, 367 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, Bundle options, 368 ActivityResultCallback callback) 369 throws IntentSender.SendIntentException { 370 validateRequestCodeForPreferenceController(requestCode); 371 int requestIndex = allocateRequestIndex(callback); 372 super.startIntentSenderForResult(intent, ((requestIndex + 1) << 8) + (requestCode & 0xff), 373 fillInIntent, flagsMask, flagsValues, /* extraFlags= */ 0, options); 374 } 375 376 @Override onActivityResult(int requestCode, int resultCode, Intent data)377 public void onActivityResult(int requestCode, int resultCode, Intent data) { 378 super.onActivityResult(requestCode, resultCode, data); 379 int requestIndex = (requestCode >> 8) & 0xff; 380 if (requestIndex != 0) { 381 requestIndex--; 382 ActivityResultCallback callback = mActivityResultCallbackMap.get(requestIndex); 383 mActivityResultCallbackMap.remove(requestIndex); 384 if (callback != null) { 385 callback.processActivityResult(requestCode & 0xff, resultCode, data); 386 } 387 } 388 } 389 390 @Override getPreferenceToolbar(@onNull Fragment fragment)391 protected ToolbarController getPreferenceToolbar(@NonNull Fragment fragment) { 392 return getToolbar(); 393 } 394 395 @Override getPreferenceInsets(@onNull Fragment fragment)396 protected Insets getPreferenceInsets(@NonNull Fragment fragment) { 397 return null; 398 } 399 400 // Allocates the next available startActivityForResult request index. allocateRequestIndex(ActivityResultCallback callback)401 private int allocateRequestIndex(ActivityResultCallback callback) { 402 // Check that we haven't exhausted the request index space. 403 if (mActivityResultCallbackMap.size() >= MAX_NUM_PENDING_ACTIVITY_RESULT_CALLBACKS) { 404 throw new IllegalStateException( 405 "Too many pending activity result callbacks."); 406 } 407 408 // Find an unallocated request index in the mPendingFragmentActivityResults map. 409 while (mActivityResultCallbackMap.indexOfKey(mCurrentRequestIndex) >= 0) { 410 mCurrentRequestIndex = 411 (mCurrentRequestIndex + 1) % MAX_NUM_PENDING_ACTIVITY_RESULT_CALLBACKS; 412 } 413 414 mActivityResultCallbackMap.put(mCurrentRequestIndex, callback); 415 return mCurrentRequestIndex; 416 } 417 418 /** 419 * Checks whether the given request code is a valid code by masking it with 0xff00. Throws an 420 * {@link IllegalArgumentException} if the code is not valid. 421 */ validateRequestCodeForPreferenceController(int requestCode)422 private static void validateRequestCodeForPreferenceController(int requestCode) { 423 if ((requestCode & 0xff00) != 0) { 424 throw new IllegalArgumentException("Can only use lower 8 bits for requestCode"); 425 } 426 } 427 getFragmentHost()428 private FragmentHost getFragmentHost() { 429 return (FragmentHost) requireActivity(); 430 } 431 } 432