public class CuratedFeedsPresenter implements ICuratedFeedsPresenter, OnCuratedFeedsRetrievedListener {
    private ICuratedFeedsView mView;

    public CuratedFeedsPresenter(ICuratedFeedsView mView) {
        this.mView = mView;
    }

    public void attemptCuratedFeedsLoading(Context context) {
        CuratedFeedsInteractor curatedFeedsInteractor = new CuratedFeedsInteractor();
        curatedFeedsInteractor.fetchCuratedFeedsFromServer(this, context);
    }

    @Override
    public void onSuccess(List<SourceItem> sourceItems) {
        mView.onFeedsLoaded(sourceItems);
    }

    @Override
    public void onFailure(String message) {
        mView.onFeedsLoadingFailure(message);
    }
} public interface ICuratedFeedsView {
    void onFeedsLoaded(List<SourceItem> sourceItems);

    void onFeedsLoadingFailure(String message);
} public interface IImportOpmlView {
    void opmlFeedsRetrieved(List<SourceItem> sourceItems);

    void opmlFeedsRetrievalFailed(String message);
} public class ImportOpmlPresenter implements IImportOpmlPresenter, OnOpmlImportListener {
    private static ImportOpmlInteractor sImportOmplInteractor;
    private static Context sContext;
    private static ImportOpmlPresenter sImportOmplPresenter;
    private IImportOpmlView mView;
    private EditText mETxtUrl;

    public ImportOpmlPresenter(IImportOpmlView mView, Context context) {
        this.mView = mView;
        sContext = context;
        sImportOmplInteractor = new ImportOpmlInteractor();
        sImportOmplPresenter = this;
    }

    public static void onFileSelected(File file) {
        Log.e("File", file.getAbsolutePath());
        sImportOmplInteractor.retrieveFeeds(sImportOmplPresenter, sContext, file);
    }

    public void attemptFeedsRetrievalFromOpml(boolean isUrl) {
        if (isUrl) {
            MaterialDialog urlDialog = new MaterialDialog.Builder(sContext)
                    .title(R.string.enter_url)
                    .customView(R.layout.dialog_ompl_url, true)
                    .positiveText(R.string.get_feeds)
                    .negativeText(R.string.cancel)
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            String url = mETxtUrl.getText().toString();
                            //String url="http://scripting.com/feeds/top100.opml";
                            if (NetworkConnectionUtil.isNetworkAvailable(sContext)) {
                                if (url.isEmpty()) {
                                    mView.opmlFeedsRetrievalFailed("Url can't be empty.");
                                } else if (!url.matches(UrlUtil.REGEX_URL)) {
                                    mView.opmlFeedsRetrievalFailed("Invalid Url");
                                } else {
                                    sImportOmplInteractor.retrieveFeed(ImportOpmlPresenter.this, sContext, url);
                                }
                            } else {
                                NetworkConnectionUtil.showNoNetworkDialog(sContext);
                            }
                        }
                    }).build();
            mETxtUrl = (EditText) urlDialog.getView().findViewById(R.id.edit_text_url);
            urlDialog.show();
        } else {
            SettingsActivity.sFileChooserDialog.show((AppCompatActivity) sContext);
        }
    }

    @Override
    public void onSuccess(List<SourceItem> sourceItems) {
        mView.opmlFeedsRetrieved(sourceItems);
    }

    @Override
    public void onFailure(String message) {
        mView.opmlFeedsRetrievalFailed(message);
    }
} public class SettingsPreferences {
    public static int SOURCE_NAME_SIZE;
    public static int SOURCE_CATEGORY_SIZE;
    public static int SOURCE_URL_SIZE;
    public static int FEED_PUBLISH_DATE_SIZE;
    public static int FEED_TITLE_SIZE;

    public static int ARTICLE_TITLE_SIZE;
    public static int ARTICLE_CATEGORY_SIZE;
    public static int ARTICLE_PUBLISH_DATE_SIZE;
    public static int ARTICLE_CONTENT_SIZE;

    public static boolean THEME;
    public static boolean FEED_CACHE;
    public static boolean IN_APP_BROWSER;
    public static boolean FEEDS_RECYCLER_VIEW_ANIMATION;
    public static boolean SOURCES_RECYCLER_VIEW_ANIMATION;
    public static boolean CIRCULAR_REVEAL;
    public static boolean CHANGE_LOG_DIALOG_SHOW;

    private static String SHARED_SP = "DEFAULT_SP";
    private static String NEW_INSTALL = "new_install";
    private static String FEEDS_SORT = "feeds_sort";
    private static String APP_VERSION = "app_version";

    public static void init(Context context) {
        SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        setFeedsFontSize(context, defaultSharedPreferences);
        setArticleFontSize(context, defaultSharedPreferences);
        setFeedsRecyclerViewAnimation(context, defaultSharedPreferences);
        setSourcesRecyclerViewAnimation(context, defaultSharedPreferences);
        setCircularReveal(context, defaultSharedPreferences);
        setTheme(context, defaultSharedPreferences);
        setFeedsCache(context, defaultSharedPreferences);
        setInAppBrowser(context, defaultSharedPreferences);
        isChangeDialogToBeShown(context);
    }

    private static void setFeedsFontSize(Context context, SharedPreferences defaultSharedPreferences) {
        String fontSizeKey = context.getResources().getString(R.string.perf_feeds_font_size_key);
        String fontSize = defaultSharedPreferences.getString(fontSizeKey, "Small");

        switch (fontSize) {
            case "Small":
                SOURCE_NAME_SIZE = 10;
                SOURCE_CATEGORY_SIZE = 10;
                SOURCE_URL_SIZE = 10;
                FEED_PUBLISH_DATE_SIZE = 10;
                FEED_TITLE_SIZE = 15;
                break;
            case "Medium":
                SOURCE_NAME_SIZE = 15;
                SOURCE_CATEGORY_SIZE = 15;
                SOURCE_URL_SIZE = 15;
                FEED_PUBLISH_DATE_SIZE = 15;
                FEED_TITLE_SIZE = 20;
                break;
            case "Large":
                SOURCE_NAME_SIZE = 20;
                SOURCE_CATEGORY_SIZE = 20;
                SOURCE_URL_SIZE = 20;
                FEED_PUBLISH_DATE_SIZE = 20;
                FEED_TITLE_SIZE = 25;
                break;
        }
    }

    private static void setArticleFontSize(Context context, SharedPreferences defaultSharedPreferences) {
        String fontSizeKey = context.getResources().getString(R.string.perf_article_font_size_key);
        String fontSize = defaultSharedPreferences.getString(fontSizeKey, "Medium");

        switch (fontSize) {
            case "Small":
                ARTICLE_TITLE_SIZE = 20;
                ARTICLE_CATEGORY_SIZE = 10;
                ARTICLE_PUBLISH_DATE_SIZE = 10;
                ARTICLE_CONTENT_SIZE = 15;
                break;
            case "Medium":
                ARTICLE_TITLE_SIZE = 25;
                ARTICLE_CATEGORY_SIZE = 15;
                ARTICLE_PUBLISH_DATE_SIZE = 15;
                ARTICLE_CONTENT_SIZE = 20;
                break;
            case "Large":
                ARTICLE_TITLE_SIZE = 30;
                ARTICLE_CATEGORY_SIZE = 20;
                ARTICLE_PUBLISH_DATE_SIZE = 20;
                ARTICLE_CONTENT_SIZE = 25;
                break;
        }
    }

    private static void setFeedsRecyclerViewAnimation(Context context, SharedPreferences defaultSharedPreferences) {
        String feedsRecyclerViewAnimationKey = context.getResources().getString(R.string.perf_feeds_anim_key);
        FEEDS_RECYCLER_VIEW_ANIMATION = defaultSharedPreferences.getBoolean(feedsRecyclerViewAnimationKey, true);
    }

    private static void setSourcesRecyclerViewAnimation(Context context, SharedPreferences defaultSharedPreferences) {
        String sourcesRecyclerViewAnimationKey = context.getResources().getString(R.string.perf_sources_anim_key);
        SOURCES_RECYCLER_VIEW_ANIMATION = defaultSharedPreferences.getBoolean(sourcesRecyclerViewAnimationKey, true);
    }

    private static void setCircularReveal(Context context, SharedPreferences defaultSharedPreferences) {
        String circularRevealKey = context.getResources().getString(R.string.perf_circular_reveal_key);
        CIRCULAR_REVEAL = defaultSharedPreferences.getBoolean(circularRevealKey, true);
    }

    private static void setTheme(Context context, SharedPreferences defaultSharedPreferences) {
        String themeKey = context.getResources().getString(R.string.perf_theme_key);
        String theme = defaultSharedPreferences.getString(themeKey, "Light");

        switch (theme) {
            case "Light":
                THEME = true;
                break;
            case "Dark":
                THEME = false;
                break;
        }
    }

    private static void setFeedsCache(Context context, SharedPreferences defaultSharedPreferences) {
        String feedsCacheKey = context.getResources().getString(R.string.perf_cache_key);
        FEED_CACHE = defaultSharedPreferences.getBoolean(feedsCacheKey, true);
    }

    private static void setInAppBrowser(Context context, SharedPreferences defaultSharedPreferences) {
        String inAppBrowserKey = context.getResources().getString(R.string.perf_in_app_browser_key);
        IN_APP_BROWSER = defaultSharedPreferences.getBoolean(inAppBrowserKey, true);
    }

    public static boolean isNewInstall(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_SP, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(NEW_INSTALL, true);
    }

    public static void setNewInstall(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_SP, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(NEW_INSTALL, false);
        editor.commit();
    }

    public static String getSortingMethod(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_SP, Context.MODE_PRIVATE);
        return sharedPreferences.getString(FEEDS_SORT, "random");
    }

    public static int getSortingMethodPosition(Context context) {
        switch (getSortingMethod(context)) {
            case "random":
                return 0;
            case "feed_title":
                return 1;
            case "feed_category":
                return 2;
            case "feed_pub_date":
                return 3;
            default:
                return -1;
        }
    }

    public static void setSortingMethodPosition(Context context, int position) {
        switch (position) {
            case 0:
                setSortingMethod(context, "random");
                break;
            case 1:
                setSortingMethod(context, "feed_title");
                break;
            case 2:
                setSortingMethod(context, "feed_category");
                break;
            case 3:
                setSortingMethod(context, "feed_pub_date");
                break;
        }
    }

    public static void setSortingMethod(Context context, String type) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_SP, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(FEEDS_SORT, type);
        editor.commit();
    }

    public static void showChangeLog(Context context) {
        MaterialDialog changeLogDialog = new MaterialDialog.Builder(context)
                .title(R.string.change_log)
                .content(R.string.change_log_desc)
                .negativeText(R.string.dismiss)
                .build();
        changeLogDialog.show();
    }

    private static void isChangeDialogToBeShown(Context context) {
        if (!getOldAppVersion(context).equals(getCurrAppVersion(context))) {
            CHANGE_LOG_DIALOG_SHOW = true;
            setAppVersion(context);
        } else {
            CHANGE_LOG_DIALOG_SHOW = false;
        }
    }

    private static void setAppVersion(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_SP, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(APP_VERSION, getCurrAppVersion(context));
        editor.commit();
    }

    private static String getOldAppVersion(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_SP, Context.MODE_PRIVATE);
        return sharedPreferences.getString(APP_VERSION, "");
    }

    private static String getCurrAppVersion(Context context) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = packageInfo.versionName;
        return version;
    }
} public class SourceItem {
    private String sourceName, sourceUrl, sourceCategoryName, sourceDateAdded;
    private int sourceCategoryImgId;

    public int getSourceCategoryImgId() {
        return sourceCategoryImgId;
    }

    public void setSourceCategoryImgId(int sourceCategoryImgId) {
        this.sourceCategoryImgId = sourceCategoryImgId;
    }

    public String getSourceName() {
        return sourceName;
    }

    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    public String getSourceUrl() {
        return sourceUrl;
    }

    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl = sourceUrl;
    }

    public String getSourceDateAdded() {
        return sourceDateAdded;
    }

    public void setSourceDateAdded(String sourceDateAdded) {
        this.sourceDateAdded = sourceDateAdded;
    }

    public String getSourceCategoryName() {
        return sourceCategoryName;
    }

    public void setSourceCategoryName(String sourceCategoryName) {
        this.sourceCategoryName = sourceCategoryName;
    }
} public interface ISourceView {
    void dataSourceSaved(String message);

    void dataSourceSaveFailed(String message);

    void dataSourceLoaded(List<String> sourceNames);

    void dataSourceItemsLoaded(List<SourceItem> sourceItems);

    void dataSourceLoadingFailed(String message);

    void sourceItemModified(SourceItem sourceItem, String oldName);

    void sourceItemModificationFailed(String message);

    void sourceItemDeleted(SourceItem sourceItem);

    void sourceItemDeletionFailed(String message);
} public class SourcesPresenter implements ISourcePresenter, OnSourceSavedListener, OnSourcesLoadedListener, OnSourcesModifyListener {

    private ISourceView mISourceView;
    private SourceInteractor mSourceInteractor;
    private EditText mETxtSourceName, mETxtSourceUrl;
    private TextView mTxtCategory;
    private ImageView mImgCategory;
    private FrameLayout mFrameCategory;

    public SourcesPresenter(ISourceView mISourceView, Context mContext) {
        this.mISourceView = mISourceView;
        this.mSourceInteractor = new SourceInteractor(mContext);
    }

    public void addSource(SourceItem sourceItem) {
        mSourceInteractor.addSourceToDb(this, sourceItem);
    }

    public void getSources() {
        mSourceInteractor.getSourcesFromDb(this);
    }

    public void getSourceItems() {
        mSourceInteractor.getSourceItemsFromDb(this);
    }

    public void modifySources(final Context context, final SourceItem sourceItem) {
        final MaterialDialog modifyDialog = new MaterialDialog.Builder(context)
                .title(R.string.modify_source)
                .customView(R.layout.dialog_modify_source, true)
                .positiveText(R.string.modify)
                .negativeText(R.string.cancel)
                .neutralText(R.string.delete)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        String sourceName = mETxtSourceName.getText().toString();
                        String sourceUrl = mETxtSourceUrl.getText().toString();
                        String sourceCategory = mTxtCategory.getText().toString();
                        int sourceCategoryImgId = new Categories(context).getDrawableId(sourceCategory);

                        SourceItem sourceItemNew = new SourceItem();
                        sourceItemNew.setSourceName(sourceName);
                        sourceItemNew.setSourceUrl(sourceUrl);
                        sourceItemNew.setSourceCategoryName(sourceCategory);
                        sourceItemNew.setSourceCategoryImgId(sourceCategoryImgId);

                        mSourceInteractor.editSourceItemInDb(SourcesPresenter.this, sourceItemNew, sourceItem.getSourceName());
                    }
                })
                .onNeutral(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        mSourceInteractor.deleteSourceItemFromDb(SourcesPresenter.this, sourceItem);
                    }
                })
                .build();

        mETxtSourceName = (EditText) modifyDialog.getView().findViewById(R.id.edit_text_source_name);
        mETxtSourceUrl = (EditText) modifyDialog.getView().findViewById(R.id.edit_text_source_url);
        mFrameCategory = (FrameLayout) modifyDialog.getView().findViewById(R.id.frame_layout_category);
        mTxtCategory = (TextView) modifyDialog.getView().findViewById(R.id.text_view_category);
        mImgCategory = (ImageView) modifyDialog.getView().findViewById(R.id.image_view_category);

        mETxtSourceName.setText(sourceItem.getSourceName());
        mETxtSourceUrl.setText(sourceItem.getSourceUrl());
        mTxtCategory.setText(sourceItem.getSourceCategoryName());
        mImgCategory.setImageResource(new Categories(context).getDrawableId(sourceItem.getSourceCategoryName()));

        //add a white color filter to the images if dark theme is selected
        if (!SettingsPreferences.THEME) {
            mImgCategory.setColorFilter(ContextCompat.getColor(context, R.color.md_grey_100));
        }

        mFrameCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final List<CategoryItem> categoryItems = new Categories(context).getCategoryItems();

                final MaterialDialog categoryDialog = new MaterialDialog.Builder(context)
                        .title(R.string.add_category)
                        .adapter(new CategoryListAdapter(context, categoryItems),
                                new MaterialDialog.ListCallback() {
                                    @Override
                                    public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                        //Toast.makeText(HomeActivity.this, "Clicked item " + which, Toast.LENGTH_SHORT).show();
                                        mImgCategory.setImageDrawable(categoryItems.get(which).getCategoryImg());
                                        mTxtCategory.setText(categoryItems.get(which).getCategoryName());
                                        dialog.dismiss();
                                    }
                                })
                        .build();
                categoryDialog.show();
            }
        });

        modifyDialog.show();
    }

    public void deleteSource(SourceItem sourceItem) {
        mSourceInteractor.deleteSourceItemFromDb(SourcesPresenter.this, sourceItem);
    }

    @Override
    public void onSuccess(String message) {
        mISourceView.dataSourceSaved(message);

    }

    @Override
    public void onFailure(String message) {
        mISourceView.dataSourceSaveFailed(message);
    }

    @Override
    public void onSourceLoaded(List<String> sourceNames) {
        mISourceView.dataSourceLoaded(sourceNames);
    }

    @Override
    public void onSourceItemsLoaded(List<SourceItem> sourceItems) {
        mISourceView.dataSourceItemsLoaded(sourceItems);
    }

    @Override
    public void onSourceLoadingFailed(String message) {
        mISourceView.dataSourceLoadingFailed(message);
    }

    @Override
    public void onSourceModified(SourceItem sourceItem, String oldName) {
        mISourceView.sourceItemModified(sourceItem, oldName);
    }

    @Override
    public void onSourceModifiedFailed(String message) {
        mISourceView.sourceItemModificationFailed(message);
    }

    @Override
    public void onSourceDeleted(SourceItem sourceItem) {
        mISourceView.sourceItemDeleted(sourceItem);
    }

    @Override
    public void onSourceDeletionFailed(String message) {
        mISourceView.sourceItemDeletionFailed(message);
    }
} public class FadeAnimationUtil {

    private Context mContext;

    public FadeAnimationUtil(Context context) {
        this.mContext = context;
    }

    public void fadeInLeft(View view) {
        view.setVisibility(View.INVISIBLE);
        Animation fadeInAnim = AnimationUtils.makeInAnimation(mContext, true);
        view.startAnimation(fadeInAnim);
        view.setVisibility(View.VISIBLE);
        Log.d("fadeIn", "true");
    }

    public void fadeOutRight(View view) {
        Animation fadeOutAnim = AnimationUtils.makeOutAnimation(mContext, true);
        view.startAnimation(fadeOutAnim);
        view.setVisibility(View.INVISIBLE);
        Log.d("fadeOut", "true");
    }

    public void fadeInAlpha(View view, int durationInMillis) {
        view.setVisibility(View.INVISIBLE);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1.0f);
        alphaAnimation.setDuration(durationInMillis);
        view.startAnimation(alphaAnimation);
        view.setVisibility(View.VISIBLE);
    }

    public void fadeOutAlpha(final View view, int durationInMillis) {
        view.setVisibility(View.VISIBLE);
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0f);
        alphaAnimation.setDuration(durationInMillis);
        view.startAnimation(alphaAnimation);
        view.setVisibility(View.INVISIBLE);
        /*new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                view.setVisibility(View.GONE);
            }
        }, 1000);*/
    }
} public class NetworkConnectionUtil {

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
        return isConnected;
    }

    public static void showNoNetworkDialog(final Context context) {
        MaterialDialog noNetworkAvailDialog = new MaterialDialog.Builder(context)
                .title(R.string.no_internet_available)
                .content(R.string.no_internet_available_extnd)
                .iconRes(R.drawable.ic_error_24dp)
                .positiveText(R.string.settings)
                .negativeText(R.string.dismiss)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                        context.startActivity(intent);
                    }
                }).build();
        noNetworkAvailDialog.show();
    }
} public class WebsiteIntentUtil {
    private static final String EXTRA_CUSTOM_TABS_SESSION = "android.support.customtabs.extra.SESSION";
    private static final String EXTRA_CUSTOM_TABS_TOOLBAR_COLOR = "android.support.customtabs.extra.TOOLBAR_COLOR";
    private Context mContext;

    public WebsiteIntentUtil(Context mContext) {
        this.mContext = mContext;
    }

    public void loadCustomChromeTabs(String url) {
        //ANDROID CHROME CUSTOM TABS IMPLEMENTATION

        // Using a VIEW intent for compatibility with any other browsers on device.
        // Caller should not be setting FLAG_ACTIVITY_NEW_TASK or
        // FLAG_ACTIVITY_NEW_DOCUMENT.

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        //  Must have. Extra used to match the session. Its value is an IBinder passed
        //  whilst creating a news session. See newSession() below. Even if the service is not
        //  used and there is no valid session id to be provided, this extra has to be present
        //  with a null value to launch a custom tab.

        //CustomTabsClient

        Bundle extras = new Bundle();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            extras.putBinder(EXTRA_CUSTOM_TABS_SESSION, null);
        }
        intent.putExtra(EXTRA_CUSTOM_TABS_TOOLBAR_COLOR, R.color.colorPrimary);
        intent.putExtras(extras);
        mContext.startActivity(intent);
    }

    public void loadNormalBrowser(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        mContext.startActivity(intent);
    }
}
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.support.v4.app.ActivityCompat;
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.Toast;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.folderselector.FileChooserDialog;
import com.crazyhitty.chdev.ks.munch.R;
import com.crazyhitty.chdev.ks.munch.curatedfeeds.CuratedFeedsPresenter;
import com.crazyhitty.chdev.ks.munch.curatedfeeds.ICuratedFeedsView;
import com.crazyhitty.chdev.ks.munch.importopml.IImportOpmlView;
import com.crazyhitty.chdev.ks.munch.importopml.ImportOpmlPresenter;
import com.crazyhitty.chdev.ks.munch.models.SettingsPreferences;
import com.crazyhitty.chdev.ks.munch.models.SourceItem;
import com.crazyhitty.chdev.ks.munch.sources.ISourceView;
import com.crazyhitty.chdev.ks.munch.sources.SourcesPresenter;
import com.crazyhitty.chdev.ks.munch.utils.FadeAnimationUtil;
import com.crazyhitty.chdev.ks.munch.utils.NetworkConnectionUtil;
import com.crazyhitty.chdev.ks.munch.utils.WebsiteIntentUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import butterknife.Bind;
import butterknife.ButterKnife;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
package com.crazyhitty.chdev.ks.munch.ui.activities;







/**
 * A {@link PreferenceActivity} that presents a set of application settings. On
 * handset devices, settings are presented as a single list. On tablets,
 * settings are split by category, with category headers shown to the left of
 * the list of settings.
 * <p/>
 * See <a href="http://developer.android.com/design/patterns/settings.html">
 * Android Design: Settings</a> for design guidelines and the <a
 * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
 * API Guide</a> for more information on developing a Settings UI.
 */
public class SettingsActivity extends AppCompatActivity implements FileChooserDialog.FileCallback {

    public static FileChooserDialog sFileChooserDialog;
    private static MaterialProgressBar mMaterialProgressBar;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setActivityTheme();
        setContentView(R.layout.activity_settings);
        ButterKnife.bind(this);

        //bind progress bar
        mMaterialProgressBar = (MaterialProgressBar) findViewById(R.id.indeterminate_progress);
        if (!NetworkConnectionUtil.isNetworkAvailable(SettingsActivity.this)) {
            mMaterialProgressBar.setVisibility(View.GONE);
        }

        setToolBar();

        getFragmentManager().beginTransaction().replace(R.id.frame_layout_settings, new SettingsFragment()).commit();

        sFileChooserDialog = new FileChooserDialog.Builder(SettingsActivity.this)
                .cancelButton(R.string.cancel)
                .build();
    }

    private void setActivityTheme() {
        if (!SettingsPreferences.THEME) {
            setTheme(R.style.DarkAppTheme_NoActionBar);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(ContextCompat.getColor(SettingsActivity.this, R.color.darkColorPrimaryDark));
            }
            getWindow().setBackgroundDrawableResource(R.color.darkColorBackground);
        }
    }

    private void setToolBar() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void onFileSelection(FileChooserDialog dialog, File file) {
        ImportOpmlPresenter.onFileSelected(file);
    }

    public static class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener, ICuratedFeedsView, ISourceView, IImportOpmlView {
        private final static int STORAGE_PERMISSION_RC = 1;
        private static String EMAIL_SUBJECT = "Custom curated list - Munch";
        private static String MESSAGE_TYPE = "message/rfc822";
        private static String SOURCE_NAME = "source_name";
        private static String SOURCE_URL = "source_url";
        private static String SOURCE_CATEGORY = "source_category";
        private static String FEEDS_NOT_WORKING_TUTORIAL_URL = "http://crazyhitty.com/blog/2016/january/make-your-feeds-work-with-munch.html";
        private CuratedFeedsPresenter mCuratedFeedsPresenter;
        private SourcesPresenter mSourcesPresenter;