import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;
import android.view.WindowManager;
public class PreferenceScreenTest extends PreferenceActivity implements
OnPreferenceChangeListener, OnPreferenceClickListener,OnSharedPreferenceChangeListener {
private ListPreference mVideoSelect;
private ListPreference mAudioSelect;
PreferenceScreen preferenceScreen=null;
private static final String mVideo="video_select";
private static final String mAudio="audio_select";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
addPreferencesFromResource(R.xml.options);
preferenceScreen = getPreferenceScreen();
mVideoSelect= (ListPreference) preferenceScreen.findPreference(mVideo);
mAudioSelect = (ListPreference) preferenceScreen.findPreference(mAudio);
mVideoSelect.setOnPreferenceChangeListener(this);
mAudioSelect.setOnPreferenceChangeListener(this);
//
setDefault_Value();
}
@Override
protected void onResume()
{
super.onResume();
Log.i("111", "onResume()");
updateState(true);
/*IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(mReceiver, filter);*/
}
private void updateState(boolean force)
{
Log.i("222222", "updateState()");
updateVideoSelectSummary(mVideoSelect.getValue());
updateAudioSelectSummary(mAudioSelect.getValue());
}
public boolean onPreferenceChange(Preference preference, Object objValue) {
final String key = preference.getKey();
// TODO Auto-generated method stub
if ("video_select".equals(key))
{
//int value = Integer.parseInt((String) objValue);
try
{
updateVideoSelectSummary(objValue);
}
catch (NumberFormatException e)
{
//Log.e(TAG, "could not persist screen output select", e);
}
}
else if ("audio_select".equals(key))
{
int value = Integer.parseInt((String) objValue);
try
{
updateAudioSelectSummary(objValue);
}
catch (NumberFormatException e)
{
//Log.e(TAG, "could not persist screen output select", e);
}
}
return true;
}
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
return true;
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
}
private void setDefault_Value() {
CharSequence[] values;
mVideoSelect=(ListPreference)findPreference(mVideo);
values=mVideoSelect.getEntryValues();
for(int i=0;i<=values.length;i++) {
if(i==values.length) {
mVideoSelect.setValue(values[0].toString());
} else if (values[i].equals(mVideoSelect.getValue())) {
break;
}
}
mAudioSelect=(ListPreference)findPreference(mAudio);
values=mAudioSelect.getEntryValues();
for(int i=0;i<values.length;i++) {
if(i==values.length) {
mAudioSelect.setValue(values[0].toString());
} else if (values[i].equals(mAudioSelect.getValue())) {
break;
}
}
}
private void updateVideoSelectSummary(Object value)
{
CharSequence[] summaries = getResources().getTextArray(R.array.video_select_summaries);
CharSequence[] values = mVideoSelect.getEntryValues();
for (int i=0; i<values.length; i++)
{
if (values[i].equals(value))
{
mVideoSelect.setSummary(summaries[i]);
break;
}
}
}
private void updateAudioSelectSummary(Object value)
{
CharSequence[] summaries = getResources().getTextArray(R.array.audio_select_summaries);
CharSequence[] values = mAudioSelect.getEntryValues();
for (int i=0; i<values.length; i++)
{
if (values[i].equals(value))
{
mAudioSelect.setSummary(summaries[i]);
break;
}
}
}
}