package android.example.com.visualizerpreferences;

/*
 * Copyright (C) 2016 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.
 */

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceScreen;
import android.widget.Toast;

// TODO (1) Implement OnPreferenceChangeListener
public class SettingsFragment extends PreferenceFragmentCompat implements
        OnSharedPreferenceChangeListener {

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {

        // Add visualizer preferences, defined in the XML file in res->xml->pref_visualizer
        addPreferencesFromResource(R.xml.pref_visualizer);

        SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
        PreferenceScreen prefScreen = getPreferenceScreen();
        int count = prefScreen.getPreferenceCount();

        // Go through all of the preferences, and set up their preference summary.
        for (int i = 0; i < count; i++) {
            Preference p = prefScreen.getPreference(i);
            // You don't need to set up preference summaries for checkbox preferences because
            // they are already set up in xml using summaryOff and summary On
            if (!(p instanceof CheckBoxPreference)) {
                String value = sharedPreferences.getString(p.getKey(), "");
                setPreferenceSummary(p, value);
            }
        }
        // TODO (3) Add the OnPreferenceChangeListener specifically to the EditTextPreference
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        // Figure out which preference was changed
        Preference preference = findPreference(key);
        if (null != preference) {
            // Updates the summary for the preference
            if (!(preference instanceof CheckBoxPreference)) {
                String value = sharedPreferences.getString(preference.getKey(), "");
                setPreferenceSummary(preference, value);
            }
        }
    }

    /**
     * Updates the summary for the preference
     *
     * @param preference The preference to be updated
     * @param value      The value that the preference was updated to
     */
    private void setPreferenceSummary(Preference preference, String value) {
        if (preference instanceof ListPreference) {
            // For list preferences, figure out the label of the selected value
            ListPreference listPreference = (ListPreference) preference;
            int prefIndex = listPreference.findIndexOfValue(value);
            if (prefIndex >= 0) {
                // Set the summary to that label
                listPreference.setSummary(listPreference.getEntries()[prefIndex]);
            }
        } else if (preference instanceof EditTextPreference) {
            // For EditTextPreferences, set the summary to the value's simple string representation.
            preference.setSummary(value);
        }
    }

    // TODO (2) Override onPreferenceChange. This method should try to convert the new preference value
    // to a float; if it cannot, show a helpful error message and return false. If it can be converted
    // to a float check that that float is between 0 (exclusive) and 3 (inclusive). If it isn't, show
    // an error message and return false. If it is a valid number, return true.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }
}
@@ -1176,20 +1176,25 @@
 st;%0A%0A// 
-TODO
+COMPLETED
  (1) Imp
@@ -1341,16 +1341,55 @@
 Listener
+, Preference.OnPreferenceChangeListener
  %7B%0A%0A    
@@ -2367,32 +2367,172 @@
      %7D%0A        %7D
+%0A%0A        Preference preference = findPreference(getString(R.string.pref_size_key));%0A        preference.setOnPreferenceChangeListener(this);
 %0A        // TODO
@@ -2527,20 +2527,25 @@
      // 
-TODO
+COMPLETED
  (3) Add
@@ -4114,12 +4114,17 @@
  // 
-TODO
+COMPLETED
  (2)
@@ -4851,32 +4851,32 @@
 edPreferences()%0A
-
                 
@@ -4931,10 +4931,821 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public boolean onPreferenceChange(Preference preference, Object newValue) %7B%0A        Toast error = Toast.makeText(getContext(), %22Please select a number between 0.1 and 3%22, Toast.LENGTH_SHORT);%0A%0A        String sizeKey = getString(R.string.pref_size_key);%0A        if (preference.getKey().equals(sizeKey)) %7B%0A            String stringSize = ((String) (newValue)).trim();%0A            if (stringSize.equals(%22%22)) stringSize = %221%22;%0A            try %7B%0A                float size = Float.parseFloat(stringSize);%0A                if (size %3E 3 %7C%7C size %3C= 0) %7B%0A                    error.show();%0A                    return false;%0A                %7D%0A            %7D catch (NumberFormatException nfe) %7B%0A                error.show();%0A                return false;%0A            %7D%0A        %7D%0A        return true;%0A    %7D
 %0A%7D

Complete Exercise 6.10