package com.codekidlabs.storagechooserdemo;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.codekidlabs.storagechooser.StorageChooser;
import com.codekidlabs.storagechooser.StorageChooserView;
import com.codekidlabs.storagechooserdemo.utils.TypefaceUtil;

public class MainActivity extends AppCompatActivity {

    Context context;
    private static FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(DialogFragment.STYLE_NO_TITLE);
        setContentView(R.layout.activity_main);
        context = getApplicationContext();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        fragmentManager = getSupportFragmentManager();


        getFragmentManager().beginTransaction().replace(R.id.container, new DemoPreference()).commit();

    }

    public static int getColorFromResource(Context context, int id) {
        return ContextCompat.getColor(context, id);
    }

    public static class DemoPreference extends PreferenceFragment {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_demo);

            PreferenceScreen preferenceScreen = getPreferenceScreen();
            Preference overviewPref = preferenceScreen.findPreference("pref_overview");
            Preference customPref = preferenceScreen.findPreference("pref_overview");
            Preference filePickerPref = preferenceScreen.findPreference("pref_overview");

            overviewPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    StorageChooser chooser = new StorageChooser.Builder()
                            .withActivity(getActivity())
                            .withFragmentManager(fragmentManager)
                            .withMemoryBar(true)
                            .build();

                    chooser.show();
                    return true;
                }
            });

            customPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    StorageChooser chooser = new StorageChooser.Builder()
                            .withActivity(getActivity())
                            .withFragmentManager(fragmentManager)
                            .withMemoryBar(true)
                            .build();

                    chooser.show();
                    return true;
                }
            });


            filePickerPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    StorageChooser chooser = new StorageChooser.Builder()
                            .withActivity(getActivity())
                            .withFragmentManager(fragmentManager)
                            .withMemoryBar(true)
                            .build();

                    chooser.show();
                    return true;
                }
            });
        }
    }
}

@@ -74,50 +74,8 @@
 xt;%0A
-import android.content.SharedPreferences;%0A
 impo
@@ -220,181 +220,13 @@
 ence
-Manager;%0Aimport android.preference.PreferenceScreen;%0Aimport android.support.design.widget.BottomSheetDialogFragment;%0Aimport android.support.design.widget.FloatingActionButto
+Scree
 n;%0Ai
@@ -486,269 +486,61 @@
 og;%0A
-import android.view.View;%0Aimport android.widget.Button;%0Aimport android.widget.TextView;%0A%0Aimport com.codekidlabs.storagechooser.StorageChooser;%0Aimport com.codekidlabs.storagechooser.StorageChooserView;%0Aimport com.codekidlabs.storagechooserdemo.utils.TypefaceUtil
+%0Aimport com.codekidlabs.storagechooser.StorageChooser
 ;%0A%0Ap
@@ -1880,32 +1880,30 @@
 rence(%22pref_
-overview
+custom
 %22);%0A        
@@ -1968,32 +1968,35 @@
 rence(%22pref_
-overview
+file_picker
 %22);%0A%0A       
@@ -2896,32 +2896,83 @@
 (getActivity())%0A
+                            .allowCustomPath(true)%0A
                 
@@ -3013,32 +3013,103 @@
 ragmentManager)%0A
+                            .setType(StorageChooser.DIRECTORY_CHOOSER)%0A
                 
@@ -3716,63 +3716,466 @@
    .
-withMemoryBar(true)%0A                            .build(
+allowCustomPath(true)%0A                            .setType(StorageChooser.FILE_PICKER)%0A                            .withMemoryBar(true)%0A                            .build();%0A%0A                    chooser.setOnSelectListener(new StorageChooser.OnSelectListener() %7B%0A                        @Override%0A                        public void onSelect(String path) %7B%0A                            Log.e(%22File name%22, path);%0A                        %7D%0A                    %7D
 );%0A%0A

update demo with new preference key