/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.car.settings.common;
import static com.android.car.settings.common.BaseCarSettingsActivity.META_DATA_KEY_SINGLE_PANE;
import android.car.drivingstate.CarUxRestrictions;
import android.car.drivingstate.CarUxRestrictionsManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.ArrayMap;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.XmlRes;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.recyclerview.widget.RecyclerView;
import com.android.car.settings.R;
import com.android.car.ui.baselayout.Insets;
import com.android.car.ui.preference.PreferenceFragment;
import com.android.car.ui.recyclerview.CarUiRecyclerView;
import com.android.car.ui.toolbar.MenuItem;
import com.android.car.ui.toolbar.NavButtonMode;
import com.android.car.ui.toolbar.ToolbarController;
import com.android.car.ui.utils.ViewUtils;
import com.android.settingslib.search.Indexable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Base fragment for all settings. Subclasses must provide a resource id via
* {@link #getPreferenceScreenResId()} for the XML resource which defines the preferences to
* display and controllers to update their state. This class is responsible for displaying the
* preferences, creating {@link PreferenceController} instances from the metadata, and
* associating the preferences with their corresponding controllers.
*
*
{@code preferenceTheme} must be specified in the application theme, and the parent to which
* this fragment attaches must implement {@link UxRestrictionsProvider} and
* {@link FragmentController} or an {@link IllegalStateException} will be thrown during
* {@link #onAttach(Context)}. Changes to driving state restrictions are propagated to
* controllers.
*/
public abstract class SettingsFragment extends PreferenceFragment implements
CarUxRestrictionsManager.OnUxRestrictionsChangedListener, FragmentController, Indexable {
@VisibleForTesting
static final String DIALOG_FRAGMENT_TAG =
"com.android.car.settings.common.SettingsFragment.DIALOG";
private static final int MAX_NUM_PENDING_ACTIVITY_RESULT_CALLBACKS = 0xff - 1;
private final Map> mPreferenceControllersLookup =
new ArrayMap<>();
private final List mPreferenceControllers = new ArrayList<>();
private final SparseArray mActivityResultCallbackMap =
new SparseArray<>();
private CarUxRestrictions mUxRestrictions;
private HighlightablePreferenceGroupAdapter mAdapter;
private int mCurrentRequestIndex = 0;
/**
* Returns the resource id for the preference XML of this fragment.
*/
@XmlRes
protected abstract int getPreferenceScreenResId();
protected ToolbarController getToolbar() {
return getFragmentHost().getToolbar();
}
/**
* Returns the MenuItems to display in the toolbar. Subclasses should override this to
* add additional buttons, switches, ect. to the toolbar.
*/
protected List