/*
*  Copyright 2012 Erkan Molla
*
*  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.batterywidget;

import com.batterywidget.preferences.Preferences;

import android.os.Bundle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;


public class OptionsManager extends PreferenceActivity implements OnSharedPreferenceChangeListener, 
                                                                  OnPreferenceClickListener{
	
	private CheckBoxPreference   mVibrationCB;
	private CheckBoxPreference   mSoundCB;
	private CheckBoxPreference   mNotifyCB;
	private ListPreference       mThemeList;
	private ListPreference	     mColourList;
	private ListPreference       mTempList;
	private Preference           mAboutPref;
	private Preferences          mPrefSettings;
	private boolean              mPreferencesChanged = false;
	private int                  mWidgetId;
	
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle bundle){
		super.onCreate(bundle);
		
		mWidgetId = getIntent().getExtras().getInt(Constants.WIDGET_ID, 0);
		
		addPreferencesFromResource(R.xml.options);
		
		
		mVibrationCB  = (CheckBoxPreference) getPreferenceScreen().findPreference(
		                                          Constants.VIBRATION_CHECKBOX_KEY);
		mSoundCB      = (CheckBoxPreference) getPreferenceScreen().findPreference(
		                                          Constants.SOUND_CHECKBOX_KEY);
		mNotifyCB     = (CheckBoxPreference) getPreferenceScreen().findPreference(
		                                          Constants.NOTIFY_ICON_CHECKBOX_KEY);
		mThemeList    = (ListPreference)     getPreferenceScreen().findPreference(
		                                          Constants.WIDGET_THEME_KEY);
		mColourList   = (ListPreference)     getPreferenceScreen().findPreference(
		                                          Constants.TEXT_COLOUR_KEY);
		mTempList     = (ListPreference)     getPreferenceScreen().findPreference(
		                                          Constants.TEMPERATURE_TYPE_KEY);
		mAboutPref    = (Preference)         getPreferenceScreen().findPreference(
		                                          Constants.ABOUT_PREF_KEY);
 		
		mPrefSettings = new Preferences(Constants.BATTERY_SETTINGS, this);
	}
	

	@SuppressWarnings("deprecation")
	@Override
	public void onResume(){
		super.onResume();
		
		mVibrationCB.setChecked(mPrefSettings.getValue(Constants.VIBRATION_SETTINGS, false));
		mSoundCB.setChecked(mPrefSettings.getValue(Constants.SOUND_SETTINGS, false));
		mNotifyCB.setChecked(mPrefSettings.getValue(Constants.NOTIFY_ICON_SETTINGS, false));
		mAboutPref.setOnPreferenceClickListener(this);
		
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}
	

	@SuppressWarnings("deprecation")
	@Override 
	public void onPause(){
		super.onPause();
		
		mPrefSettings.setValue(Constants.VIBRATION_SETTINGS,    mVibrationCB.isChecked());
		mPrefSettings.setValue(Constants.SOUND_SETTINGS,        mSoundCB.isChecked());
		mPrefSettings.setValue(Constants.NOTIFY_ICON_SETTINGS,  mNotifyCB.isChecked());
		mPrefSettings.setValue(Constants.WIDGET_THEME_SETTINGS, mThemeList.getValue());
		mPrefSettings.setValue(Constants.TEXT_COLOUR_SETTINGS,  mColourList.getValue());
		mPrefSettings.setValue(Constants.TEMPERATURE_SETTINGS,  mTempList.getValue());
		
		if (mPreferencesChanged)
		{
			Intent intent = new Intent(getApplicationContext(),BatteryUpdateService.class);
			intent.putExtra(Constants.WIDGET_ID, mWidgetId);
			getApplicationContext().startService(intent);
		}

		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}

	
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		
		mPreferencesChanged = true;
	}


	public boolean onPreferenceClick(Preference preference) {

		if (preference.equals(mAboutPref))
		{
			Intent intent = new Intent(getApplicationContext(), AboutTabInfo.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			getApplicationContext().startActivity(intent);
		}
		
		return false;
	}
}

@@ -1177,102 +1177,8 @@
 ener
-, %0A                                                                  OnPreferenceClickListener
 %7B%0A%09%0A
@@ -3389,28 +3389,37 @@
 ickListener(
-this
+clickListener
 );%0A%09%09%0A%09%09getP
@@ -4551,18 +4551,115 @@
 rue;%0A%09%7D%0A
-%0A%0A
+%09%0A%09%0A%09final private OnPreferenceClickListener clickListener = new OnPreferenceClickListener() %7B%0A%09%09%0A%09
 %09public 
@@ -4709,17 +4709,21 @@
 ence) %7B%0A
-%0A
+%09%09%09%0A%09
 %09%09if (pr
@@ -4751,23 +4751,25 @@
 tPref))%0A
+%09
 %09%09%7B%0A%09%09%09
+%09
 Intent i
@@ -4832,24 +4832,25 @@
 .class);%0A%09%09%09
+%09
 intent.addFl
@@ -4885,16 +4885,73 @@
 _TASK);%0A
+%09%09%09%09intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);%0A%09
 %09%09%09getAp
@@ -4998,13 +4998,16 @@
 ;%0A%09%09
+%09
 %7D%0A%09%09
-%0A
+%09%0A%09
 %09%09re
@@ -5019,12 +5019,18 @@
 false;%0A%09
-%7D
+%09%7D%0A%09%7D;%0A
 %0A%7D%0A

Add private listener for settings