/*
 * Copyright (C) 2015 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 android.support.v17.preference;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v14.preference.MultiSelectListPreference;
import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class LeanbackSettingsFragment extends Fragment
        implements PreferenceFragment.OnPreferenceStartFragmentCallback,
        PreferenceFragment.OnPreferenceStartScreenCallback,
        PreferenceFragment.OnPreferenceDisplayDialogCallback {

    private static final String PREFERENCE_FRAGMENT_TAG =
            "android.support.v17.preference.LeanbackSettingsFragment.PREFERENCE_FRAGMENT";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.leanback_settings_fragment, container, false);

        // Trap back button presses
        ((LeanbackSettingsRootView) v).setOnBackKeyListener(new RootViewOnKeyListener());

        return v;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (savedInstanceState == null) {
            onPreferenceStartInitialScreen();
        }
    }

    @Override
    public boolean onPreferenceDisplayDialog(PreferenceFragment caller, Preference pref) {
        final Fragment f;
        if (pref instanceof ListPreference) {
            final ListPreference listPreference = (ListPreference) pref;
            f = LeanbackListPreferenceDialogFragment.newInstanceSingle(listPreference.getKey());
            startImmersiveFragment(f);
        } else if (pref instanceof MultiSelectListPreference) {
            MultiSelectListPreference listPreference = (MultiSelectListPreference) pref;
            f = LeanbackListPreferenceDialogFragment.newInstanceMulti(listPreference.getKey());
            startImmersiveFragment(f);
        }
        // TODO
//        else if (pref instanceof EditTextPreference) {
//
//        }
        else {
            return false;
        }
        return true;
    }

    /**
     * Called to instantiate the initial {@link android.support.v14.preference.PreferenceFragment}
     * to be shown in this fragment. Implementations are expected to call
     * {@link #startPreferenceFragment(android.app.Fragment)}.
     */
    public abstract void onPreferenceStartInitialScreen();

    /**
     * Displays a preference fragment to the user. This method can also be used to display
     * list-style fragments on top of the stack of preference fragments.
     *
     * @param fragment Fragment instance to be added.
     */
    public void startPreferenceFragment(@NonNull Fragment fragment) {
        fragment.setTargetFragment(this, 0);
        final FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        final Fragment prevFragment =
                getChildFragmentManager().findFragmentByTag(PREFERENCE_FRAGMENT_TAG);
        if (prevFragment != null) {
            transaction
                    .addToBackStack(null)
                    .replace(R.id.settings_preference_fragment_container, fragment,
                            PREFERENCE_FRAGMENT_TAG);
        } else {
            transaction
                    .add(R.id.settings_preference_fragment_container, fragment,
                            PREFERENCE_FRAGMENT_TAG);
        }
        transaction.commit();
    }

    /**
     * Displays a fragment to the user, temporarily replacing the contents of this fragment.
     *
     * @param fragment Fragment instance to be added.
     */
    public void startImmersiveFragment(@NonNull Fragment fragment) {
        final FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        final Fragment preferenceFragment =
                getChildFragmentManager().findFragmentByTag(PREFERENCE_FRAGMENT_TAG);
        if (preferenceFragment != null) {
            transaction.hide(preferenceFragment);
        }
        transaction
                .add(R.id.settings_dialog_container, fragment)
                .addToBackStack(null)
                .commit();
    }

    private class RootViewOnKeyListener implements View.OnKeyListener {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                return getChildFragmentManager().popBackStackImmediate();
            } else {
                return false;
            }
        }
    }
}

@@ -723,16 +723,41 @@
 action;%0A
+import android.os.Build;%0A
 import a
@@ -817,16 +817,60 @@
 onNull;%0A
+import android.support.annotation.Nullable;%0A
 import a
@@ -1208,16 +1208,45 @@
 ewGroup;
+%0Aimport android.widget.Space;
 %0A%0Apublic
@@ -4965,35 +4965,258 @@
 Fragment != null
-) %7B
+ && !preferenceFragment.isHidden()) %7B%0A            if (Build.VERSION.SDK_INT %3C 23) %7B%0A                // b/22631964%0A                transaction.add(R.id.settings_preference_fragment_container, new DummyFragment());%0A            %7D
 %0A            tra
@@ -5743,32 +5743,32 @@
   return false;%0A
-
             %7D%0A  
@@ -5780,11 +5780,390 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * @hide%0A     */%0A    public static class DummyFragment extends Fragment %7B%0A%0A        @Override%0A        public @Nullable View onCreateView(LayoutInflater inflater, ViewGroup container,%0A                Bundle savedInstanceState) %7B%0A            final View v = new Space(inflater.getContext());%0A            v.setVisibility(View.GONE);%0A            return v;%0A        %7D%0A    %7D
 %0A%7D%0A

Insert dummy fragment to work around transition crash