package com.cradle.iitc_mobile.fragments;

import android.app.Dialog;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.cradle.iitc_mobile.IITC_AboutDialogPreference;
import com.cradle.iitc_mobile.Log;
import com.cradle.iitc_mobile.R;

public class MainSettings extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);

        // set versions
        String iitcVersion = getArguments().getString("iitc_version");

        String buildVersion = "unknown";

        PackageManager pm = getActivity().getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(getActivity().getPackageName(), 0);
            buildVersion = info.versionName;
        } catch (NameNotFoundException e) {
            Log.w(e);
        }

        IITC_AboutDialogPreference pref_about = (IITC_AboutDialogPreference) findPreference("pref_about");
        pref_about.setVersions(iitcVersion, buildVersion);

        final ListPreference pref_user_location_mode = (ListPreference) findPreference("pref_user_location_mode");
        pref_user_location_mode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                int mode = Integer.parseInt((String) newValue);
                preference.setSummary(getResources().getStringArray(R.array.pref_user_location_titles)[mode]);
                return true;
            }
        });

        String value = getPreferenceManager().getSharedPreferences().getString("pref_user_location_mode", "0");
        int mode = Integer.parseInt(value);
        pref_user_location_mode.setSummary(getResources().getStringArray(R.array.pref_user_location_titles)[mode]);
    }

    // we want a home button + HomeAsUpEnabled in nested preferences
    // for some odd reasons android is not able to do this by default
    // so we need some additional hacks...
    // thx to http://stackoverflow.com/a/16800527/2638486 !!
    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        if (preference.getTitle().toString().equals(getString(R.string.pref_advanced_options))
                || preference.getTitle().toString().equals(getString(R.string.pref_about_title))) {
            initializeActionBar((PreferenceScreen) preference);
        }
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }

    // Apply custom home button area click listener to close the PreferenceScreen
    // because PreferenceScreens are dialogs which swallow
    // events instead of passing to the activity
    // Related Issue: https://code.google.com/p/android/issues/detail?id=4611
    public static void initializeActionBar(PreferenceScreen preferenceScreen) {
        final Dialog dialog = preferenceScreen.getDialog();

        if (dialog != null) {
            if (dialog.getActionBar() != null) dialog.getActionBar().setDisplayHomeAsUpEnabled(true);

            View homeBtn = dialog.findViewById(android.R.id.home);

            if (homeBtn != null) {
                View.OnClickListener dismissDialogClickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                };

                ViewParent homeBtnContainer = homeBtn.getParent();

                // The home button is an ImageView inside a FrameLayout
                if (homeBtnContainer instanceof FrameLayout) {
                    ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();

                    if (containerParent instanceof LinearLayout) {
                        // This view also contains the title text, set the whole view as clickable
                        ((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
                    } else {
                        // Just set it on the home button
                        ((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
                    }
                } else {
                    homeBtn.setOnClickListener(dismissDialogClickListener);
                }
            }
        }
    }
}

@@ -238,54 +238,8 @@
 le;%0A
-import android.preference.EditTextPreference;%0A
 impo
@@ -851,16 +851,22 @@
 nCreate(
+final 
 Bundle s
@@ -1015,24 +1015,30 @@
 ons%0A        
+final 
 String iitcV
@@ -1135,24 +1135,30 @@
 %22;%0A%0A        
+final 
 PackageManag
@@ -1226,16 +1226,22 @@
         
+final 
 PackageI
@@ -1367,16 +1367,22 @@
  catch (
+final 
 NameNotF
@@ -1441,16 +1441,22 @@
         
+final 
 IITC_Abo
@@ -1886,16 +1886,22 @@
 eChange(
+final 
 Preferen
@@ -1914,16 +1914,22 @@
 ference,
+ final
  Object 
@@ -1948,32 +1948,38 @@
                 
+final 
 int mode = Integ
@@ -2181,24 +2181,30 @@
 );%0A%0A        
+final 
 String value
@@ -2303,16 +2303,22 @@
         
+final 
 int mode
@@ -2766,16 +2766,22 @@
 eeClick(
+final 
 Preferen
@@ -2806,16 +2806,22 @@
 eScreen,
+ final
  Prefere
@@ -3499,16 +3499,22 @@
 tionBar(
+final 
 Preferen
@@ -3744,24 +3744,30 @@
             
+final 
 View homeBtn
@@ -3857,24 +3857,30 @@
             
+final 
 View.OnClick
@@ -4016,16 +4016,22 @@
 onClick(
+final 
 View v) 
@@ -4128,24 +4128,30 @@
             
+final 
 ViewParent h
@@ -4345,16 +4345,22 @@
         
+final 
 ViewGrou

Remove warning, add final modifiers