2013-07-09 38 views
2

我正在尝试清理将在服务器端发送的应用程序上的一些用户输入。根据服务器设置,在创建或访问文件和文件夹时,可能会使用用户输入的数据。显然,卫生服务也将在服务器上处理,但由于应用程序的性质,我不会和应用程序无法控制。因此,我认为我有责任在我或我的应用程序能够控制的地方拥有端到端的卫生设施。Android - 试图根据偏好消毒用户输入

我试图净化的首选项是一个将传递到服务器的相册名称。

根据Log.d输出这是正常工作。但是,当我单击首选项字段进行编辑时,它会显示未分类的输入,并将首选项传递给服务器时,它也会显示未分类的输入。

FileUtil.sanitize(stringValue)是一种简单的方法,用于修剪前导空格和尾随空格,并从字符串中删除../..\,现在是至少。我知道还有其他我需要观看的,只是不是100%他们现在还在。

所以,显而易见的问题是,我错过了什么?在完成这个事件后,是否有事件会覆盖我在这里做出的更改?我感觉有些东西会覆盖我的更改,因为当我在更改后立即获取值时,它会显示正确的消毒数据。

/** 
    * A preference value change listener that updates the preference's summary 
    * to reflect its new value. 
    */ 
    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { 
     @Override 
     public boolean onPreferenceChange(Preference preference, Object value) { 
     String stringValue = value.toString(); 

     if (preference instanceof ListPreference) { 
      // For list preferences, look up the correct display value in 
      // the preference's 'entries' list. 
      ListPreference listPreference = (ListPreference) preference; 
      int index = listPreference.findIndexOfValue(stringValue); 

      // Set the summary to reflect the new value. 
      preference.setSummary(
       index >= 0 
        ? listPreference.getEntries()[index] 
        : null); 

     } else if (preference.getKey().equals("text_default_album")) { 
      Log.d(TAG, "default pref: "+preference.getKey()+" value: "+stringValue); 
      stringValue = FileUtil.sanitize(stringValue); 
      Log.d(TAG, "default pref: "+preference.getKey()+" value: "+stringValue); 
      preference.setSummary(stringValue); 

      SharedPreferences prefs = preference.getSharedPreferences(); 
      String def = prefs.getString("text_default_album", ""); 
      Boolean updated = prefs.edit().putString("text_default_album", stringValue).commit(); 
      String def2 = prefs.getString("text_default_album", ""); 
      Log.d(TAG, "def: "+def); 
      Log.d(TAG, "def2: "+def2); 
      Log.d(TAG, "updated: "+updated); 
     } else { 
      // For all other preferences, set the summary to the value's 
      // simple string representation. 
      preference.setSummary(stringValue); 
     } 
     return true; 
     } 
    }; 
+1

在'其他if'分支,请尝试使用'preference.getSharedPreferences()'代替'getSharedPreferences(上下文)'。 –

+0

所以这个改为:'SharedPreferences prefs = preference.getSharedPreferences();'? – Jayrox

+0

我做出了改变,对我所经历的行为没有任何影响,但它确实看起来更好。 – Jayrox

回答

0

我已经通过扩展EditTextPrefence类和压倒一切的setTextgetText

这解决了我的问题是基于答案我发现here

package com.example.overrides; 

import android.content.Context; 
import android.preference.EditTextPreference; 
import android.util.AttributeSet; 

public class SanitizedEditTextPreference extends EditTextPreference { 
    public SanitizedEditTextPreference(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public SanitizedEditTextPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SanitizedEditTextPreference(Context context) { 
     super(context); 
    } 

    @Override 
    public String getText() { 
     String value = super.getText(); 
     return StringUtil.sanitize(value); 
    } 

    @Override 
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { 
     super.setText(restoreValue ? getPersistedString(null) : (String) defaultValue); 
    } 

    @Override 
    public void setText(String text) { 
     if (StringUtil.isStringBlank(text)) { 
      super.setText(null); 
      return; 
     } 
     super.setText(StringUtil.sanitize(text)); 
    } 
} 

我简化了我以前的代码:

/** 
* A preference value change listener that updates the preference's summary 
* to reflect its new value. 
*/ 
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { 
    @Override 
    public boolean onPreferenceChange(Preference preference, Object value) { 
    String stringValue = value.toString(); 

    if (preference instanceof ListPreference) { 
     // For list preferences, look up the correct display value in 
     // the preference's 'entries' list. 
     ListPreference listPreference = (ListPreference) preference; 
     int index = listPreference.findIndexOfValue(stringValue); 

     // Set the summary to reflect the new value. 
     preference.setSummary(
      index >= 0 
       ? listPreference.getEntries()[index] 
       : null); 

    } else if (preference.getKey().equals("text_default_album")) { 
     stringValue = StringUtil.sanitize(stringValue); 
     preference.setSummary(stringValue); 
    } else { 
     // For all other preferences, set the summary to the value's 
     // simple string representation. 
     preference.setSummary(stringValue); 
    } 
    return true; 
    } 
}; 

,并在pref.xml文件,它使用这样的:

<com.example.overrides.SanitizedEditTextPreference 
      android:key="text_default_album" 
      android:title="@string/pref_title_default_album" 
      android:defaultValue="@string/pref_default_album" 
      android:selectAllOnFocus="true" 
      android:inputType="text" 
      android:capitalize="none" 
      android:singleLine="true" 
      android:maxLines="1" />