content: package net.somethingdreadful.MAL.dialog;
  
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.app.DialogFragment;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.text.InputType;
  import android.util.Log;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.NumberPicker;
  
  import net.somethingdreadful.MAL.AppLog;
  import net.somethingdreadful.MAL.PrefManager;
  import net.somethingdreadful.MAL.R;
  import net.somethingdreadful.MAL.Theme;
  import net.somethingdreadful.MAL.account.AccountService;
  
  public class NumberPickerDialogFragment extends DialogFragment {
      private NumberPicker numberPicker;
      private EditText numberInput;
      private onUpdateClickListener callback;
      private boolean inputScore = false;
  
      private View makeNumberPicker() {
          View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_episode_picker, null);
          int max = getValue("max");
          int min = getValue("min");
          int current = getValue("current");
  
          numberInput = (EditText) view.findViewById(R.id.numberInput);
          numberPicker = (NumberPicker) view.findViewById(R.id.numberPicker);
  
          if (!inputScore) {
              numberPicker.setMaxValue(max != 0 ? max : 999);
              numberPicker.setMinValue(min != 0 ? min : 0);
              if (!AccountService.isMAL() && isRating()) {
                  String score = Theme.getDisplayScore(current);
                  numberPicker.setValue(score.equals("?") ? 0 : Integer.parseInt(score));
              } else {
                  numberPicker.setValue(current);
              }
              numberInput.setVisibility(View.GONE);
          } else {
              numberInput.setText(Theme.getDisplayScore(current));
              if (PrefManager.getScoreType() == 4)
                  numberInput.setInputType(InputType.TYPE_CLASS_TEXT);
              numberPicker.setVisibility(View.GONE);
          }
          return view;
      }
  
      @Override
      public Dialog onCreateDialog(Bundle state) {
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), getTheme());
          builder.setView(makeNumberPicker());
          builder.setTitle(getArguments().getString("title"));
          builder.setPositiveButton(R.string.dialog_label_update, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int whichButton) {
                  numberPicker.clearFocus();
                  numberInput.clearFocus();
                  if (!AccountService.isMAL() && isRating()) {
                      int value = Theme.getRawScore(!inputScore ? String.valueOf(numberPicker.getValue()) : numberInput.getText().toString());
                      callback.onUpdated(value, getArguments().getInt("id"));
                  } else
                      callback.onUpdated(numberPicker.getValue(), getArguments().getInt("id"));
                  dismiss();
              }
          });
          builder.setNegativeButton(R.string.dialog_label_cancel, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int whichButton) {
                  dismiss();
              }
          });
  
          return builder.create();
      }
  
      private boolean isRating() {
          return getArguments().getInt("id") == R.id.scorePanel;
      }
  
      /**
       * Get the integer from an argument.
       *
       * @param key The argument name
       * @return int The number of the argument
       */
      private int getValue(String key) {
          try {
              if (getArguments().getInt("id") == R.id.scorePanel && PrefManager.getScoreType() == 4 && PrefManager.getScoreType() == 3)
                  inputScore = true;
              return getArguments().getInt(key);
          } catch (Exception e) {
              AppLog.log(Log.ERROR, "Atarashii", "EpisodesPickerDialogFragment.makeNumberPicker(" + key + "): " + e.getMessage());
              return 0;
          }
      }
  
      /**
       * Set the Callback for update purpose.
       *
       * @param callback The activity/fragment where the callback is located
       * @return NumberPickerDialogFragment This will return the dialog itself to make init simple
       */
      public NumberPickerDialogFragment setOnSendClickListener(onUpdateClickListener callback) {
          this.callback = callback;
          return this;
      }
  
      /**
       * The interface for callback
       */
      public interface onUpdateClickListener {
          void onUpdated(int number, int id);
      }
  }