it.andreascarpino.forvodroid.SettingsFragment.java Source code

Java tutorial

Introduction

Here is the source code for it.andreascarpino.forvodroid.SettingsFragment.java

Source

/*
  MIT license:
    
    Copyright (c) 2014 Andrea Scarpino <me@andreascarpino.it>
    
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:
    
    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package it.andreascarpino.forvodroid;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;

/**
 * 
 * @author Andrea Scarpino <me@andreascarpino.it>
 *
 */
public class SettingsFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.settings, container, false);

        SharedPreferences preferences = getActivity().getSharedPreferences(MainActivity.CONFIG_FILE,
                Context.MODE_PRIVATE);

        EditText apiKey = (EditText) rootView.findViewById(R.id.api_key);
        String key = preferences.getString(MainActivity.CONFIG_API_KEY, "");
        if (key.length() > 0) {
            apiKey.setText(key);
        }

        EditText languages = (EditText) rootView.findViewById(R.id.languages);
        String langs = preferences.getString(MainActivity.CONFIG_LANGUAGES, "");
        if (langs.length() > 0) {
            languages.setText(langs);
        }

        // By default use OGG
        final RadioButton ogg = (RadioButton) rootView.findViewById(R.id.ogg);
        final RadioButton mp3 = (RadioButton) rootView.findViewById(R.id.mp3);

        ogg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mp3.setChecked(!isChecked);
            }
        });

        mp3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ogg.setChecked(!isChecked);
            }
        });

        boolean oggEnabled = preferences.getBoolean(MainActivity.CONFIG_OGG, true);
        ogg.setChecked(oggEnabled);
        if (oggEnabled) {
            mp3.setChecked(false);
        } else {
            mp3.setChecked(true);
        }

        return rootView;
    }

    @Override
    public void onStop() {
        SharedPreferences preferences = getActivity().getSharedPreferences(MainActivity.CONFIG_FILE,
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();

        EditText apiKey = (EditText) getActivity().findViewById(R.id.api_key);
        editor.putString(MainActivity.CONFIG_API_KEY, apiKey.getText().toString());

        EditText languages = (EditText) getActivity().findViewById(R.id.languages);
        editor.putString(MainActivity.CONFIG_LANGUAGES, languages.getText().toString());

        RadioButton ogg = (RadioButton) getActivity().findViewById(R.id.ogg);
        editor.putBoolean(MainActivity.CONFIG_OGG, ogg.isChecked());

        editor.commit();

        super.onStop();
    }

}