Android Open Source - app_list_widget Settings Fragment






From Project

Back to project page app_list_widget.

License

The source code is released under:

Apache License

If you think the Android project app_list_widget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2013 acbelter/*from  w  ww .j a  v a  2 s. c  o  m*/
 *
 * 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 com.acbelter.applistwidget.ui;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import com.acbelter.applistwidget.R;
import com.acbelter.applistwidget.widget.Pref;

public class SettingsFragment extends PreferenceFragment {
    private SharedPreferences mPref;
    private ListPreference mIconsSize;
    private ListPreference mIconsAlign;
    private CheckBoxPreference mShowSettingsIcon;
    private CheckBoxPreference mShowBackground;
    private ListPreference mColor;
    private SeekBarPreference mTransparency;
    private int mWidgetId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.widget_settings);
        mWidgetId = getArguments().getInt(ConfigActivity.KEY_WIDGET_ID);

        boolean isLargeWidget = getArguments().getBoolean(ConfigActivity.KEY_WIDGET_LARGE_FLAG);
        mPref = getActivity().getSharedPreferences(Pref.APP_LIST_WIDGET_PREF_ + mWidgetId,
                Context.MODE_PRIVATE);

        mIconsSize = (ListPreference) getPreferenceScreen().findPreference("icons_size");
        if (isLargeWidget) {
            mIconsSize.setEntries(R.array.widget_large_icons_size);
            mIconsSize.setEntryValues(R.array.widget_large_icons_size_values);
        }

        mIconsAlign = (ListPreference) getPreferenceScreen().findPreference("icons_align");
        mShowSettingsIcon = (CheckBoxPreference) getPreferenceScreen()
                .findPreference("show_settings_icon");
        mShowBackground = (CheckBoxPreference) getPreferenceScreen()
                .findPreference("show_background");
        mColor = (ListPreference) getPreferenceScreen().findPreference("color");
        mTransparency = (SeekBarPreference) getPreferenceScreen().findPreference("transparency");

        Resources res = getResources();
        if (isLargeWidget) {
            mIconsSize.setValue(Integer.toString(mPref.getInt(Pref.WIDGET_ICONS_SIZE_
                    + mWidgetId, res.getInteger(R.integer.default_large_icons_size))));
        } else {
            mIconsSize.setValue(Integer.toString(mPref.getInt(Pref.WIDGET_ICONS_SIZE_
                    + mWidgetId, res.getInteger(R.integer.default_icons_size))));
        }
        mIconsAlign.setValue(mPref.getString(Pref.WIDGET_ICONS_ALIGN_ + mWidgetId,
                res.getString(R.string.default_icons_align)));
        mShowSettingsIcon.setChecked(mPref.getBoolean(Pref.WIDGET_SHOW_SETTINGS_ICON_
                + mWidgetId, res.getBoolean(R.bool.default_show_settings_icon)));
        mShowBackground.setChecked(mPref.getBoolean(Pref.WIDGET_SHOW_BACKGROUND_
                + mWidgetId, res.getBoolean(R.bool.default_show_background)));
        mColor.setValue(mPref.getString(Pref.WIDGET_COLOR_ + mWidgetId,
                res.getString(R.string.default_color)));
        mTransparency.setValue(mPref.getInt(Pref.WIDGET_TRANSPARENCY_ + mWidgetId,
                res.getInteger(R.integer.default_transparency)));

        mIconsSize.setSummary(mIconsSize.getEntry());
        mIconsAlign.setSummary(mIconsAlign.getEntry());
        mColor.setSummary(mColor.getEntry());

        mIconsSize.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                int index = mIconsSize.findIndexOfValue((String) newValue);
                mIconsSize.setSummary(mIconsSize.getEntries()[index]);
                return true;
            }
        });

        mIconsAlign.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                int index = mIconsAlign.findIndexOfValue((String) newValue);
                mIconsAlign.setSummary(mIconsAlign.getEntries()[index]);
                return true;
            }
        });

        mColor.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                int index = mColor.findIndexOfValue((String) newValue);
                mColor.setSummary(mColor.getEntries()[index]);
                return true;
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        Editor editor = mPref.edit();
        editor.putInt(Pref.WIDGET_ICONS_SIZE_ + mWidgetId,
                Integer.parseInt(mIconsSize.getValue()));
        editor.putString(Pref.WIDGET_ICONS_ALIGN_ + mWidgetId,
                mIconsAlign.getValue());
        editor.putBoolean(Pref.WIDGET_SHOW_SETTINGS_ICON_ + mWidgetId,
                mShowSettingsIcon.isChecked());
        editor.putBoolean(Pref.WIDGET_SHOW_BACKGROUND_ + mWidgetId,
                mShowBackground.isChecked());
        editor.putString(Pref.WIDGET_COLOR_ + mWidgetId,
                mColor.getValue());
        editor.putInt(Pref.WIDGET_TRANSPARENCY_ + mWidgetId,
                mTransparency.getValue());
        editor.commit();
    }
}




Java Source Code List

com.acbelter.applistwidget.ui.AppItem.java
com.acbelter.applistwidget.ui.AppListOrderAdapter.java
com.acbelter.applistwidget.ui.AppListSelectAdapter.java
com.acbelter.applistwidget.ui.AppOrderFragment.java
com.acbelter.applistwidget.ui.AppSelectFragment.java
com.acbelter.applistwidget.ui.ConfigActivity.java
com.acbelter.applistwidget.ui.SeekBarPreference.java
com.acbelter.applistwidget.ui.SettingsActivity.java
com.acbelter.applistwidget.ui.SettingsFragment.java
com.acbelter.applistwidget.widget.AppListLargeWidgetProvider.java
com.acbelter.applistwidget.widget.AppListWidgetFactory.java
com.acbelter.applistwidget.widget.AppListWidgetProvider.java
com.acbelter.applistwidget.widget.AppListWidgetService.java
com.acbelter.applistwidget.widget.Pref.java