com.futerox.flashlight.MsgPrefs.java Source code

Java tutorial

Introduction

Here is the source code for com.futerox.flashlight.MsgPrefs.java

Source

/***
 Copyright (c) 2013-2014 SpirosBond
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program. If not, see http://www.gnu.org/licenses
    
 ***
    
 https://github.com/spirosbond/CallerFlashlight
    
 ***
    
 */

package com.futerox.flashlight;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;

//import com.xcs.flashalerts.R;

/**
 * Created by spiros on 8/24/13.
 */
public class MsgPrefs extends PreferenceActivity
        implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener {

    private static final String TAG = MsgPrefs.class.getSimpleName();
    private ListPreference lp;
    private CallerFlashlight callerFlashlight;
    private SeekBarPreference sbp;
    private Preference appList;
    private int accessibilityEnabled = 0;
    private Preference moreFlashCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        callerFlashlight = (CallerFlashlight) this.getApplication();
        addPreferencesFromResource(R.xml.sms_prefs);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            getActionBar().setDisplayHomeAsUpEnabled(true);
        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);

        appList = findPreference("app_list");
        appList.setOnPreferenceClickListener(this);
        moreFlashCheck = findPreference("app_list_check");
        moreFlashCheck.setOnPreferenceClickListener(this);

        setModeSum(callerFlashlight.getMsgFlashType());

        try {
            accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_ENABLED);
            if (CallerFlashlight.LOG)
                Log.d(TAG, "ACCESSIBILITY: " + accessibilityEnabled);
            if (accessibilityEnabled == 0) {
                appList.setEnabled(false);
                moreFlashCheck.setSummary(getResources().getString(R.string.more_flash_warning_sum));
            } else {
                appList.setEnabled(true);
                moreFlashCheck.setSummary(getResources().getString(R.string.app_list_check_sum));
            }
        } catch (Settings.SettingNotFoundException e) {
            if (CallerFlashlight.LOG)
                Log.d(TAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
            appList.setEnabled(false);

        }

    }

    @Override
    protected void onResume() {
        super.onResume();

        appList = findPreference("app_list");
        moreFlashCheck = findPreference("app_list_check");

        try {
            accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_ENABLED);
            if (CallerFlashlight.LOG)
                Log.d(TAG, "ACCESSIBILITY: " + accessibilityEnabled);
            if (accessibilityEnabled == 0) {
                appList.setEnabled(false);
                moreFlashCheck.setSummary(getResources().getString(R.string.more_flash_warning_sum));
            } else {
                appList.setEnabled(true);
                moreFlashCheck.setSummary(getResources().getString(R.string.app_list_check_sum));
            }
        } catch (Settings.SettingNotFoundException e) {
            if (CallerFlashlight.LOG)
                Log.d(TAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
            appList.setEnabled(false);

        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        if (CallerFlashlight.LOG)
            Log.d(TAG, "onSharedPreferenceChanged: " + s);
        if (s.equals("sms_mode_list")) {
            //         lp = (ListPreference) findPreference("type_list");
            setModeSum(Integer.valueOf(sharedPreferences.getString("sms_mode_list", "")));

        }
    }

    private void setModeSum(int type) {

        lp = (ListPreference) findPreference("sms_mode_list");
        sbp = (SeekBarPreference) findPreference("msgFlashDuration");
        if (CallerFlashlight.LOG)
            Log.d(TAG, "setSMSModeSum");
        //      int type = callerFlashlight.getMsgFlashType();
        if (type == 1) {
            if (CallerFlashlight.LOG)
                Log.d(TAG, "sum type 1");
            lp.setSummary(getResources().getString(R.string.sms_mode_list_1));
            sbp.setmUnitsRight(" seconds");
            sbp.updateView(getListView());
        } else if (type == 2) {
            if (CallerFlashlight.LOG)
                Log.d(TAG, "sum type 2");
            lp.setSummary(getResources().getString(R.string.sms_mode_list_2));
            sbp.setmUnitsRight(" times");
            //         sbp.updateView(getListView());
        }

    }

    @Override
    public boolean onPreferenceClick(Preference preference) {

        if (CallerFlashlight.LOG)
            Log.d(TAG, "preference clicked: " + preference.getKey());
        if (preference.getKey().equals("app_list")) {
            startActivity(new Intent(this, AppList.class));
        } else if (preference.getKey().equals("app_list_check")) {
            //         Toast.makeText(this, "Please enable Notification Service at Accessibility Settings", Toast.LENGTH_LONG).show();
            startActivityForResult(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), 0);
        }

        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}