@Override
  protected void onStart() {
    super.onStart();

    if (mKey == null) {
      PersonalKeyRunnable action =
          new PersonalKeyRunnable() {
            public void run(PersonalKey key) {
              mKey = key;
              if (mValidator != null)
                // this will release the waiting lock
                mValidator.setKey(mKey);
            }
          };

      // random passphrase (40 characters!!!!)
      mPassphrase = StringUtils.randomString(40);

      mKeyReceiver = new KeyGeneratedReceiver(mHandler, action);

      IntentFilter filter = new IntentFilter(KeyPairGeneratorService.ACTION_GENERATE);
      lbm.registerReceiver(mKeyReceiver, filter);

      Toast.makeText(this, R.string.msg_generating_keypair, Toast.LENGTH_LONG).show();

      Intent i = new Intent(this, KeyPairGeneratorService.class);
      i.setAction(KeyPairGeneratorService.ACTION_GENERATE);
      startService(i);
    }
  }
 @Override
 protected void onSaveInstanceState(Bundle state) {
   super.onSaveInstanceState(state);
   state.putString("name", mName);
   state.putString("phoneNumber", mPhoneNumber);
   state.putParcelable("key", mKey);
   state.putString("passphrase", mPassphrase);
 }
  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    mName = savedInstanceState.getString("name");
    mPhoneNumber = savedInstanceState.getString("phoneNumber");
    mKey = savedInstanceState.getParcelable("key");
    mPassphrase = savedInstanceState.getString("passphrase");
  }
  @Override
  protected void onStop() {
    super.onStop();
    keepScreenOn(false);

    if (mKeyReceiver != null) lbm.unregisterReceiver(mKeyReceiver);

    if (mProgress != null) {
      if (isFinishing()) mProgress.cancel();
      else mProgress.dismiss();
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.number_validation);

    mAccountManager = AccountManager.get(this);
    mHandler = new Handler();

    lbm = LocalBroadcastManager.getInstance(getApplicationContext());

    final Intent intent = getIntent();
    mFromInternal = intent.getBooleanExtra(PARAM_FROM_INTERNAL, false);

    mNameText = (EditText) findViewById(R.id.name);
    mCountryCode = (Spinner) findViewById(R.id.phone_cc);
    mPhone = (EditText) findViewById(R.id.phone_number);
    mValidateButton = (Button) findViewById(R.id.button_validate);
    mInsertCode = (Button) findViewById(R.id.button_validation_code);

    // populate country codes
    final CountryCodesAdapter ccList =
        new CountryCodesAdapter(this, R.layout.country_item, R.layout.country_dropdown_item);
    PhoneNumberUtil util = PhoneNumberUtil.getInstance();
    Set<String> ccSet = getSupportedRegions(util);
    for (String cc : ccSet) ccList.add(cc);

    ccList.sort(
        new Comparator<CountryCodesAdapter.CountryCode>() {
          public int compare(
              CountryCodesAdapter.CountryCode lhs, CountryCodesAdapter.CountryCode rhs) {
            return lhs.regionName.compareTo(rhs.regionName);
          }
        });
    mCountryCode.setAdapter(ccList);
    mCountryCode.setOnItemSelectedListener(
        new AdapterView.OnItemSelectedListener() {
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ccList.setSelected(position);
          }

          public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
          }
        });

    // FIXME this doesn't consider creation because of configuration change
    PhoneNumber myNum = NumberValidator.getMyNumber(this);
    if (myNum != null) {
      mPhone.setText(String.valueOf(myNum.getNationalNumber()));
      Log.d(TAG, "selecting country " + util.getRegionCodeForNumber(myNum));
      CountryCode cc = new CountryCode();
      cc.regionCode = util.getRegionCodeForNumber(myNum);
      cc.countryCode = myNum.getCountryCode();
      mCountryCode.setSelection(ccList.getPositionForId(cc));
    } else {
      final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
      final String regionCode = tm.getSimCountryIso().toUpperCase(Locale.US);
      CountryCode cc = new CountryCode();
      cc.regionCode = regionCode;
      cc.countryCode = util.getCountryCodeForRegion(regionCode);
      mCountryCode.setSelection(ccList.getPositionForId(cc));
    }

    // configuration change??
    RetainData data = (RetainData) getLastCustomNonConfigurationInstance();
    if (data != null) {
      synchronized (this) {
        // sync starter was queued, we can exit
        if (data.syncing) {
          delayedSync();
        }

        mValidator = data.validator;
        if (mValidator != null) mValidator.setListener(this);
      }
      if (data.progressMessage != null) {
        setProgressMessage(data.progressMessage, true);
      }
    }
  }