/*
 * Copyright 2016 huxizhijian
 *
 * 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 org.huxizhijian.hhcomicviewer2.ui.user;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.widget.Toast;

import org.huxizhijian.hhcomicviewer2.R;
import org.huxizhijian.hhcomicviewer2.utils.HHApiProvider;
import org.huxizhijian.sdk.SDKConstant;
import org.huxizhijian.sdk.imageloader.ImageLoaderOptions;
import org.huxizhijian.sdk.sharedpreferences.SharedPreferencesManager;

public class AdvanceSettingFragment extends PreferenceFragment {

    SharedPreferencesManager mPreferencesManager;

    public AdvanceSettingFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.advance_preferences);
        mPreferencesManager = new SharedPreferencesManager(getActivity());
        String cacheSize = mPreferencesManager.getString("reading_cache_size", "160MB");
        Preference preference = getPreferenceManager().findPreference("reading_cache_size");
        preference.setSummary(cacheSize);
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        switch (preference.getKey()) {
            case "reading_cache_size":
                preference.setSummary(((ListPreference) preference).getValue());
                new ImageLoaderOptions(getActivity())
                        .setCacheSize(preference.getKey(), SDKConstant.DEFAULT_CACHE_NAME);
                return true;
            case "clear_cache":
                //清除在线阅读缓存
                new AsyncTask<Context, Void, Void>() {
                    @Override
                    protected Void doInBackground(Context... params) {
                        Context context = params[0];
                        ImageLoaderOptions.getImageLoaderManager().clearDiskCache(context);
                        return null;
                    }
                }.execute(getActivity());
                Toast.makeText(getActivity(), "清除缓存成功", Toast.LENGTH_SHORT).show();
                return true;
            case "update_variable":
                HHApiProvider.getInstance().updateVariable();
                Toast.makeText(getActivity(), "更新完成", Toast.LENGTH_SHORT).show();
                return true;
        }
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }
}

@@ -970,67 +970,8 @@
 .R;%0A
-import org.huxizhijian.hhcomicviewer2.utils.HHApiProvider;%0A
 impo
@@ -2835,24 +2835,26 @@
 _variable%22:%0A
+//
             
@@ -2954,12 +2954,15 @@
 ), %22
-%E6%9B%B4%E6%96%B0%E5%AE%8C%E6%88%90
+%E6%9C%AC%E5%8A%9F%E8%83%BD%E6%9A%82%E6%9C%AA%E5%AE%9E%E8%A3%85
 %22, T

fix bugs