パスワード更新を DialogPreference 継承で書く

パスワード更新を行う 以下のような、Preference 画面を用意したくなって、
使い回せればと思い、DialogPreference 継承を書きました。

f:id:posturan:20160313225516p:plain


以下、ソースコードです。誤り入力や未入力で再表示するようにしてます。

package sample.setting;

import android.content.Context;
import android.preference.DialogPreference;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
 * PasswordDialogPreference
 */

public class PasswordDialogPreference extends DialogPreference{
   private EditText newPasswdEditText;
   private EditText confirmPasswdEditText;

   public PasswordDialogPreference(Context context,AttributeSet attrs){
      super(context,attrs);
   }
   public PasswordDialogPreference(Context context,AttributeSet attrs,int defStyle){
      super(context,attrs,defStyle);
   }
   @Override
   protected View onCreateDialogView(){
      LinearLayout layout = new LinearLayout(getContext());
      layout.setOrientation(LinearLayout.VERTICAL);


      newPasswdEditText = new EditText(getContext());
      newPasswdEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
      newPasswdEditText.setHint(hintNewPasswdEditText());
      newPasswdEditText.setFilters(new InputFilter{ spaceCheckFilter() });
      layout.addView(newPasswdEditText);

      confirmPasswdEditText = new EditText(getContext());
      confirmPasswdEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
      confirmPasswdEditText.setHint(hintConfirmPasswdEditText());
      confirmPasswdEditText.setFilters(new InputFilterspaceCheckFilter() });

      layout.addView(confirmPasswdEditText);

      newPasswdEditText.setFocusable(true);
      return layout;
   }
   @Override
   protected void onDialogClosed(boolean positiveResult){
      if (positiveResult){
         String newPwd = newPasswdEditText.getText().toString();
         String confirmPwd = confirmPasswdEditText.getText().toString();
         if (newPwd.length()==0 || confirmPwd.length()==0){
            showDialog(null);
            return;
         }
         if (newPwd.equals(confirmPwd)==false){
            showDialog(null);
            return;
         }
         if (localCheck(newPwd)==false){
            showDialog(null);
            return;
         }
         persistString(newPasswdEditText.getText().toString());
         setSummary(createSummaryString(newPwd));
      }
      super.onDialogClosed(positiveResult);
   }

   // ========= 以下カスタマイズ =========

   private String hintNewPasswdEditText(){
      return getContext().getResources().getString(R.string.hint_new_password);
   }
   private String hintConfirmPasswdEditText(){
      return getContext().getResources().getString(R.string.hint_confirm_password);
   }
   private boolean localCheck(String newPwd){
      if (newPwd.length() < 4) return false;
      return true;
   }
   private InputFilter spaceCheckFilter(){
      return new InputFilter(){
         @Override
         public CharSequence filter(CharSequence source,int start,int end,Spanned dest,int dstart,int dend){
            if (source.toString().matches(" +")){
               Toast toast = Toast.makeText(getContext(),"空白は入りません",Toast.LENGTH_SHORT);
               toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
               toast.show();
               return "";
            }
            return source;
         }
      };
   }
   private String createSummaryString(String newPwd){
      StringBuffer sb = new StringBuffer();
      int x = newPwd.length();
      for(int i=0;i < x;i++){
         sb.append("*");
      }
      return sb.toString();
   }

}

Preferencs の xml は、以下の様に記述します。

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="設定"></PreferenceCategory>

    <sample.setting.PasswordDialogPreference
        android:key="password" android:title="パスワードの変更"
        android:dialogIcon="@android:drawable/ic_menu_more"
        android:summary="*****" >
    </sample.setting.PasswordDialogPreference>

</PreferenceScreen>