/*
 * 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 androidx.preference;

import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import java.util.ArrayList;

public class ListPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat {

    private static final String SAVE_STATE_INDEX = "ListPreferenceDialogFragment.index";
    private static final String SAVE_STATE_ENTRIES = "ListPreferenceDialogFragment.entries";
    private static final String SAVE_STATE_ENTRY_VALUES =
            "ListPreferenceDialogFragment.entryValues";

    private int mClickedDialogEntryIndex;
    private CharSequence[] mEntries;
    private CharSequence[] mEntryValues;

    public static ListPreferenceDialogFragmentCompat newInstance(String key) {
        final ListPreferenceDialogFragmentCompat fragment =
                new ListPreferenceDialogFragmentCompat();
        final Bundle b = new Bundle(1);
        b.putString(ARG_KEY, key);
        fragment.setArguments(b);
        return fragment;
    }

    private static void putCharSequenceArray(Bundle out, String key, CharSequence[] entries) {
        final ArrayList<String> stored = new ArrayList<>(entries.length);

        for (final CharSequence cs : entries) {
            stored.add(cs.toString());
        }

        out.putStringArrayList(key, stored);
    }

    private static CharSequence[] getCharSequenceArray(Bundle in, String key) {
        final ArrayList<String> stored = in.getStringArrayList(key);

        return stored == null ? null : stored.toArray(new CharSequence[stored.size()]);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            final ListPreference preference = getListPreference();

            if (preference.getEntries() == null || preference.getEntryValues() == null) {
                throw new IllegalStateException(
                        "ListPreference requires an entries array and an entryValues array.");
            }

            mClickedDialogEntryIndex = preference.findIndexOfValue(preference.getValue());
            mEntries = preference.getEntries();
            mEntryValues = preference.getEntryValues();
        } else {
            mClickedDialogEntryIndex = savedInstanceState.getInt(SAVE_STATE_INDEX, 0);
            mEntries = getCharSequenceArray(savedInstanceState, SAVE_STATE_ENTRIES);
            mEntryValues = getCharSequenceArray(savedInstanceState, SAVE_STATE_ENTRY_VALUES);
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SAVE_STATE_INDEX, mClickedDialogEntryIndex);
        putCharSequenceArray(outState, SAVE_STATE_ENTRIES, mEntries);
        putCharSequenceArray(outState, SAVE_STATE_ENTRY_VALUES, mEntryValues);
    }

    private ListPreference getListPreference() {
        return (ListPreference) getPreference();
    }

    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        super.onPrepareDialogBuilder(builder);

        builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mClickedDialogEntryIndex = which;

                        // Clicking on an item simulates the positive button click, and dismisses
                        // the dialog.
                        ListPreferenceDialogFragmentCompat.this.onClick(dialog,
                                DialogInterface.BUTTON_POSITIVE);
                        dialog.dismiss();
                    }
                });

        // The typical interaction for list-based dialogs is to have click-on-an-item dismiss the
        // dialog instead of the user having to press 'Ok'.
        builder.setPositiveButton(null, null);
    }

    @Override
    public void onDialogClosed(boolean positiveResult) {
        final ListPreference preference = getListPreference();
        if (positiveResult && mClickedDialogEntryIndex >= 0) {
            String value = mEntryValues[mClickedDialogEntryIndex].toString();
            if (preference.callChangeListener(value)) {
                preference.setValue(value);
            }
        }
    }

}

@@ -791,37 +791,8 @@
 g;%0A%0A
-import java.util.ArrayList;%0A%0A
 publ
@@ -1637,573 +1637,8 @@
  %7D%0A%0A
-    private static void putCharSequenceArray(Bundle out, String key, CharSequence%5B%5D entries) %7B%0A        final ArrayList%3CString%3E stored = new ArrayList%3C%3E(entries.length);%0A%0A        for (final CharSequence cs : entries) %7B%0A            stored.add(cs.toString());%0A        %7D%0A%0A        out.putStringArrayList(key, stored);%0A    %7D%0A%0A    private static CharSequence%5B%5D getCharSequenceArray(Bundle in, String key) %7B%0A        final ArrayList%3CString%3E stored = in.getStringArrayList(key);%0A%0A        return stored == null ? null : stored.toArray(new CharSequence%5Bstored.size()%5D);%0A    %7D%0A%0A
     
@@ -2422,24 +2422,43 @@
  mEntries = 
+savedInstanceState.
 getCharSeque
@@ -2466,36 +2466,16 @@
 ceArray(
-savedInstanceState, 
 SAVE_STA
@@ -2514,16 +2514,35 @@
 alues = 
+savedInstanceState.
 getCharS
@@ -2558,28 +2558,8 @@
 ray(
-savedInstanceState, 
 SAVE
@@ -2789,32 +2789,41 @@
 Index);%0A        
+outState.
 putCharSequenceA
@@ -2823,34 +2823,24 @@
 quenceArray(
-outState, 
 SAVE_STATE_E
@@ -2866,16 +2866,25 @@
         
+outState.
 putCharS
@@ -2900,18 +2900,8 @@
 ray(
-outState, 
 SAVE

resolve merge conflicts of 60eb7ea8525b02b95a763beac637f43d2b2dacc4 to master