/*
*  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.storage.Preferences;

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


public class SettingsManager extends PreferenceActivity implements Preference.OnPreferenceChangeListener{
	
	private CheckBoxPreference   mVibrationCheckBox;
	private CheckBoxPreference   mSoundCheckBox;
	private ListPreference	     mColourList;
	
	private Preferences          mSettings;
	
	@Override
	protected void onCreate(Bundle bundle){
		
		super.onCreate(bundle);
		
		addPreferencesFromResource(R.xml.settings);
		
		buildSettings();
	}

	
	@Override
	public boolean onPreferenceChange(Preference preference, Object value) {
		
		if(preference.equals(mColourList))
			mSettings.setValue(Constants.COLOUR_SETTINGS, mColourList.getValue());
		
		return true;
	}
	
	
	private void buildSettings(){
		
		mVibrationCheckBox = (CheckBoxPreference) findPreference(Constants.VIBRATION_CHECKBOX_KEY);
		mSoundCheckBox     = (CheckBoxPreference) findPreference(Constants.SOUND_CHECKBOX_KEY);
		mColourList        = (ListPreference)     findPreference(Constants.COLOUR_LIST_KEY);
		
		mSettings  =  new Preferences(Constants.BATTERY_SETTINGS, this);
		
		mVibrationCheckBox.setChecked(mSettings.getValue(Constants.VIBRATION_SETTINGS, false));
		mSoundCheckBox.setChecked(mSettings.getValue(Constants.SOUND_SETTINGS, false));
		mColourList.setOnPreferenceChangeListener(this);
		
	}
	
	/*
	 * save state and update the widget
	 * @see android.app.Activity#onPause()
	 */
	@Override 
	public void onPause(){
		super.onPause();
		
		mSettings.setValue(Constants.VIBRATION_SETTINGS, mVibrationCheckBox.isChecked());
		mSettings.setValue(Constants.SOUND_SETTINGS, mSoundCheckBox.isChecked());
		mSettings.setValue(Constants.COLOUR_SETTINGS, mColourList.getValue());
		
		getApplicationContext().startService(new Intent(getApplicationContext(), BatteryUpdateService.class));
	}

}



@@ -1136,18 +1136,16 @@
 urList;%0A
-%09%0A
 %09private
@@ -1179,16 +1179,83 @@
 ings;%0A%09%0A
+%09private boolean              mListPrefChanged = false;%0A%09%0A%09/*%0A%09 */%0A
 %09@Overri
@@ -1298,19 +1298,16 @@
 undle)%7B%0A
-%09%09%0A
 %09%09super.
@@ -1382,30 +1382,282 @@
 %09%0A%09%09
-buildSettings(
+mVibrationCheckBox = (CheckBoxPreference) findPreference(Constants.VIBRATION_CHECKBOX_KEY);%0A%09%09mSoundCheckBox     = (CheckBoxPreference) findPreference(Constants.SOUND_CHECKBOX_KEY);%0A%09%09mColourList        = (ListPreference)     findPreference(Constants.COLOUR_LIST_KEY
 );%0A%09%7D%0A%0A%09
 %0A%09@O
@@ -1652,16 +1652,23 @@
 );%0A%09%7D%0A%0A%09
+/*%0A%09 */
 %0A%09@Overr
@@ -1784,16 +1784,17 @@
 urList))
+%7B
 %0A%09%09%09mSet
@@ -1866,333 +1866,123 @@
 ;%0A%09%09
-%0A%09%09return
+%09mListPrefChanged =
  true;%0A
+%09
 %09%7D%0A%09
-%0A
 %09%0A%09
-private void buildSettings()%7B%0A%09%09%0A%09%09mVibrationCheckBox = (CheckBoxPreference) findPreference(Constants.VIBRATION_CHECKBOX_KEY);%0A%09%09mSoundCheckBox     = (CheckBoxPreference) findPreference(Constants.SOUND_CHECKBOX_KEY);%0A%09%09mColourList        = (ListPreference)     findPreference(Constants.COLOUR_LIST_KEY
+%09return true;%0A%09%7D%0A%09%0A%09/*%0A%09 */%0A%09@Override%0A%09public void onResume()%7B%0A%09%09super.onResume(
 );%0A%09
@@ -2276,19 +2276,16 @@
 (this);%0A
-%09%09%0A
 %09%7D%0A%09%0A%09/*
@@ -2289,85 +2289,8 @@
 %09/*%0A
-%09 * save state and update the widget%0A%09 * @see android.app.Activity#onPause()%0A
 %09 */
@@ -2584,16 +2584,41 @@
 ());%0A%09%09%0A
+%09%09if (mListPrefChanged)%0A%09
 %09%09getApp
@@ -2684,16 +2684,65 @@
 text(), 
+%0A%09%09%09                                             
 BatteryU
@@ -2770,9 +2770,7 @@
 %0A%09%7D%0A
-%0A%7D%0A
+%7D
 %0A%0A

update widget from SettingsManager only if the colour list has changed