ca.zadrox.dota2esportticker.ui.SettingsActivity.java Source code

Java tutorial

Introduction

Here is the source code for ca.zadrox.dota2esportticker.ui.SettingsActivity.java

Source

/*
 * Copyright 2014 Nicholas Liu
 *
 * 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 ca.zadrox.dota2esportticker.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceFragment;
import android.support.v4.content.IntentCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;

import ca.zadrox.dota2esportticker.R;
import ca.zadrox.dota2esportticker.service.UpdateMatchService;
import ca.zadrox.dota2esportticker.util.PrefUtils;

/**
 * Created by Acco on 11/5/2014.
 */
public class SettingsActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_settings);

        Toolbar toolbar = getActionBarToolbar();
        toolbar.setTitle("Settings");
        toolbar.setNavigationIcon(R.drawable.ic_up);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateUpToFromChild(SettingsActivity.this, IntentCompat
                        .makeMainActivity(new ComponentName(SettingsActivity.this, MatchActivity.class)));
            }
        });

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new SettingsFragment()).commit();
        }
    }

    public static class SettingsFragment extends PreferenceFragment
            implements SharedPreferences.OnSharedPreferenceChangeListener {
        public SettingsFragment() {
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settings);
            PrefUtils.registerOnSharedPreferenceChangeListener(getActivity(), this);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            EditTextPreference etf = (EditTextPreference) findPreference(PrefUtils.PREF_SAPI_KEY);

            String summary = PrefUtils.getSAPIKey(getActivity());
            etf.setSummary(summary.equals("") ? "API Key not set." : PrefUtils.getSAPIKey(getActivity()));
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            PrefUtils.unregisterOnSharedPreferenceChangeListener(getActivity(), this);
        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            if (PrefUtils.PREF_AUTOUPDATE.equals(key)) {
                Intent intent;
                if (PrefUtils.shouldAutoUpdate(getActivity())) {
                    intent = new Intent(UpdateMatchService.ACTION_SCHEDULE_AUTO_UPDATE);
                } else {
                    intent = new Intent(UpdateMatchService.ACTION_CANCEL_AUTO_UPDATE);
                }

                intent.setClass(getActivity(), UpdateMatchService.class);
                getActivity().startService(intent);
            } else if (PrefUtils.PREF_SAPI_KEY.equals(key)) {
                EditTextPreference etf = (EditTextPreference) findPreference(PrefUtils.PREF_SAPI_KEY);

                String summary = PrefUtils.getSAPIKey(getActivity());
                etf.setSummary(summary.equals("") ? "API Key not set." : PrefUtils.getSAPIKey(getActivity()));
            }
        }
    }
}