2013-07-09 43 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" />