gt:           result = getIntent().getStringExtra("e164number");
context: /**
   * Copyright (C) 2011 Whisper Systems
   * Copyright (C) 2014 Securecom
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  package com.securecomcode.messaging;
  
  import android.app.ProgressDialog;
  import android.content.BroadcastReceiver;
  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.content.ServiceConnection;
  import android.graphics.Color;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.IBinder;
  import android.os.Message;
  import android.support.v7.app.ActionBarActivity;
  import android.text.SpannableString;
  import android.text.Spanned;
  import android.text.TextUtils;
  import android.text.method.LinkMovementMethod;
  import android.text.style.ClickableSpan;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.ProgressBar;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  import android.widget.Toast;
  
  
  import com.securecomcode.messaging.crypto.MasterSecret;
  import com.securecomcode.messaging.push.TextSecureCommunicationFactory;
  import com.securecomcode.messaging.service.RegistrationService;
  import com.securecomcode.messaging.util.Dialogs;
  import com.securecomcode.messaging.util.TextSecurePreferences;
  import com.securecomcode.messaging.util.Util;
  import org.whispersystems.textsecure.api.TextSecureAccountManager;
  import org.whispersystems.textsecure.api.push.exceptions.ExpectationFailedException;
  import org.whispersystems.textsecure.api.push.exceptions.RateLimitException;
  import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
  
  import java.io.IOException;
  
  import static com.securecomcode.messaging.service.RegistrationService.RegistrationState;
  
  public class RegistrationProgressActivity extends ActionBarActivity {
  
    private static final int FOCUSED_COLOR   = Color.parseColor("#ff333333");
    private static final int UNFOCUSED_COLOR = Color.parseColor("#ff808080");
  
    private ServiceConnection    serviceConnection        = new RegistrationServiceConnection();
    private Handler              registrationStateHandler = new RegistrationStateHandler();
    private RegistrationReceiver registrationReceiver     = new RegistrationReceiver();
  
    private RegistrationService registrationService;
  
    private LinearLayout registrationLayout;
    private LinearLayout verificationFailureLayout;
    private LinearLayout connectivityFailureLayout;
    private LinearLayout smsVerificationLayout;
    private RelativeLayout timeoutProgressLayout;
  
    private ProgressBar registrationProgress;
    private ProgressBar connectingProgress;
    private ProgressBar verificationProgress;
    private ProgressBar generatingKeysProgress;
    private ProgressBar gcmRegistrationProgress;
  
  
    private ImageView   connectingCheck;
    private ImageView   verificationCheck;
    private ImageView   generatingKeysCheck;
    private ImageView   gcmRegistrationCheck;
  
    private TextView    connectingText;
    private TextView    verificationText;
    private TextView    registrationTimerText;
    private TextView    generatingKeysText;
    private TextView    gcmRegistrationText;
    private TextView    verificationFailureLabel;
    private TextView    verificationFailureWaitingLabel;
    private TextView    voiceVerificationLabel;
    private TextView    telephonyTextLabel;
    private TextView    smsTextLabel;
    private TextView    smsTextDesLabel;
  
    private Button      verificationFailureButton;
    private Button      smsVerificationFailureButton;
    private Button      connectivityFailureButton;
    private Button      callButton;
    private Button      verifyButton;
    private Button      verifySmsButton;
  
    private EditText    codeEditText;
    private EditText    codeSmsEditText;
  
    private ImageView   voiceImage;
  
    private MasterSecret masterSecret;
    private volatile boolean visible;
  
    @Override
    public void onCreate(Bundle bundle) {
      super.onCreate(bundle);
      if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Phone")){
          getSupportActionBar().setTitle(getString(R.string.RegistrationProgressActivity_verifying_number));
      }else if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Email")){
          getSupportActionBar().setTitle(getString(R.string.RegistrationProgressActivity_verifying_email));
      }
  
      setContentView(R.layout.registration_progress_activity);
  
      initializeResources();
      initializeLinks();
      initializeServiceBinding();
    }
  
    @Override
    public void onDestroy() {
      super.onDestroy();
      shutdownServiceBinding();
    }
  
    @Override
    public void onResume() {
      super.onResume();
      handleActivityVisible();
    }
  
    @Override
    public void onPause() {
      super.onPause();
      handleActivityNotVisible();
    }
  
    @Override
    public void onBackPressed() {
  
    }
  
    private void initializeServiceBinding() {
      Intent intent = new Intent(this, RegistrationService.class);
      bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }
  
    private void initializeResources() {
      this.masterSecret              = getIntent().getParcelableExtra("master_secret");
      this.registrationLayout        = (LinearLayout)findViewById(R.id.registering_layout);
      this.verificationFailureLayout = (LinearLayout)findViewById(R.id.verification_failure_layout);
      this.connectivityFailureLayout = (LinearLayout)findViewById(R.id.connectivity_failure_layout);
      this.smsVerificationLayout     = (LinearLayout)findViewById(R.id.sms_verification_layout);
      this.registrationProgress      = (ProgressBar) findViewById(R.id.registration_progress);
      this.connectingProgress        = (ProgressBar) findViewById(R.id.connecting_progress);
      this.verificationProgress      = (ProgressBar) findViewById(R.id.verification_progress);
      this.generatingKeysProgress    = (ProgressBar) findViewById(R.id.generating_keys_progress);
      this.gcmRegistrationProgress   = (ProgressBar) findViewById(R.id.gcm_registering_progress);
      this.connectingCheck           = (ImageView)   findViewById(R.id.connecting_complete);
      this.verificationCheck         = (ImageView)   findViewById(R.id.verification_complete);
      this.generatingKeysCheck       = (ImageView)   findViewById(R.id.generating_keys_complete);
      this.gcmRegistrationCheck      = (ImageView)   findViewById(R.id.gcm_registering_complete);
      this.connectingText            = (TextView)    findViewById(R.id.connecting_text);
      this.verificationText          = (TextView)    findViewById(R.id.verification_text);
      this.registrationTimerText     = (TextView)    findViewById(R.id.registration_timer);
      this.generatingKeysText        = (TextView)    findViewById(R.id.generating_keys_text);
      this.gcmRegistrationText       = (TextView)    findViewById(R.id.gcm_registering_text);
      this.verificationFailureLabel  = (TextView)    findViewById(R.id.registration_progress_activity__sms_verification_failed_label);
      this.voiceVerificationLabel    = (TextView)    findViewById(R.id.registration_progress_activity__voice_verification_label);
      this.telephonyTextLabel        = (TextView)    findViewById(R.id.telephone_text);
      this.smsTextLabel              = (TextView)    findViewById(R.id.sms_text_label);
      this.smsTextDesLabel           = (TextView)    findViewById(R.id.sms_text);
      this.verificationFailureWaitingLabel  = (TextView)    findViewById(R.id.sms_failed_text);
      this.verificationFailureButton = (Button)      findViewById(R.id.verification_failure_edit_button);
      this.smsVerificationFailureButton = (Button)      findViewById(R.id.sms_verification_failure_edit_button);
      this.connectivityFailureButton = (Button)      findViewById(R.id.connectivity_failure_edit_button);
      this.callButton                = (Button)      findViewById(R.id.call_button);
      this.verifyButton              = (Button)      findViewById(R.id.verify_button);
      this.verifySmsButton           = (Button)      findViewById(R.id.sms_verify_button);
      this.codeEditText              = (EditText)    findViewById(R.id.telephone_code);
      this.codeSmsEditText           = (EditText)    findViewById(R.id.sms_code);
      this.timeoutProgressLayout     = (RelativeLayout) findViewById(R.id.timer_progress_layout);
      Button editButton              = (Button)      findViewById(R.id.edit_button);
      this.voiceImage                = (ImageView) findViewById(R.id.telephone);
  
        if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Phone")){
            this.verificationText.setText(R.string.registration_progress_activity__waiting_for_sms_verification);
        }else if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Email")){
            this.verificationText.setText(R.string.registration_progress_activity__waiting_for_email_verification);
        }
  
      editButton.setOnClickListener(new EditButtonListener());
      this.verificationFailureButton.setOnClickListener(new EditButtonListener());
      this.smsVerificationFailureButton.setOnClickListener(new EditButtonListener());
      this.connectivityFailureButton.setOnClickListener(new EditButtonListener());
    }
  
    private void initializeLinks() {
      TextView        failureText     = (TextView) findViewById(R.id.sms_failed_text);
      String          pretext         = getString(R.string.registration_progress_activity__textsecure_timed_out_while_waiting_for_a_verification_sms_message);
      String          link            = getString(R.string.RegistrationProblemsActivity_possible_problems);
      SpannableString spannableString = new SpannableString(pretext + " " + link);
  
      spannableString.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View widget) {
          Intent intent = new Intent(RegistrationProgressActivity.this,
                                     RegistrationProblemsActivity.class);
          startActivity(intent);
        }
      }, pretext.length() + 1, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  
      failureText.setText(spannableString);
      failureText.setMovementMethod(LinkMovementMethod.getInstance());
    }
  
    private void handleActivityVisible() {
      IntentFilter filter = new IntentFilter(RegistrationService.REGISTRATION_EVENT);
      filter.setPriority(1000);
      registerReceiver(registrationReceiver, filter);
      visible = true;
    }
  
    private void handleActivityNotVisible() {
      unregisterReceiver(registrationReceiver);
      visible = false;
    }
  
    private void handleStateIdle() {
      if (hasNumberDirective()) {
        Intent intent = new Intent(this, RegistrationService.class);
        intent.setAction(RegistrationService.REGISTER_NUMBER_ACTION);
          if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Phone")){
              intent.putExtra("e164number", getNumberDirective());
          }else if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Email")){
              intent.putExtra("email_address", getNumberDirective());
          }
        intent.putExtra("master_secret", masterSecret);
        startService(intent);
      } else {
        Intent intent = new Intent(this, RegistrationActivity.class);
        intent.putExtra("master_secret", masterSecret);
        startActivity(intent);
        finish();
      }
    }
  
    private void handleStateConnecting() {
      this.registrationLayout.setVisibility(View.VISIBLE);
      this.verificationFailureLayout.setVisibility(View.GONE);
      this.connectivityFailureLayout.setVisibility(View.GONE);
      this.connectingProgress.setVisibility(View.VISIBLE);
      this.connectingCheck.setVisibility(View.INVISIBLE);
      this.verificationProgress.setVisibility(View.INVISIBLE);
      this.verificationCheck.setVisibility(View.INVISIBLE);
      this.generatingKeysProgress.setVisibility(View.INVISIBLE);
      this.generatingKeysCheck.setVisibility(View.INVISIBLE);
      this.gcmRegistrationProgress.setVisibility(View.INVISIBLE);
      this.gcmRegistrationCheck.setVisibility(View.INVISIBLE);
      this.connectingText.setTextColor(FOCUSED_COLOR);
      this.verificationText.setTextColor(UNFOCUSED_COLOR);
      this.generatingKeysText.setTextColor(UNFOCUSED_COLOR);
      this.gcmRegistrationText.setTextColor(UNFOCUSED_COLOR);
      this.timeoutProgressLayout.setVisibility(View.VISIBLE);
      this.smsVerificationLayout.setVisibility(View.GONE);
    }
  
    private void handleStateVerifying() {
      this.registrationLayout.setVisibility(View.VISIBLE);
      this.verificationFailureLayout.setVisibility(View.GONE);
      this.connectivityFailureLayout.setVisibility(View.GONE);
      this.connectingProgress.setVisibility(View.INVISIBLE);
      this.connectingCheck.setVisibility(View.VISIBLE);
      this.verificationProgress.setVisibility(View.VISIBLE);
      this.verificationCheck.setVisibility(View.INVISIBLE);
      this.generatingKeysProgress.setVisibility(View.INVISIBLE);
      this.generatingKeysCheck.setVisibility(View.INVISIBLE);
      this.gcmRegistrationProgress.setVisibility(View.INVISIBLE);
      this.gcmRegistrationCheck.setVisibility(View.INVISIBLE);
      this.connectingText.setTextColor(UNFOCUSED_COLOR);
      this.verificationText.setTextColor(FOCUSED_COLOR);
      this.generatingKeysText.setTextColor(UNFOCUSED_COLOR);
      this.gcmRegistrationText.setTextColor(UNFOCUSED_COLOR);
      this.registrationProgress.setVisibility(View.VISIBLE);
      this.timeoutProgressLayout.setVisibility(View.VISIBLE);
      this.smsVerificationLayout.setVisibility(View.GONE);
    }
  
    private void handleStateGeneratingKeys() {
      this.registrationLayout.setVisibility(View.VISIBLE);
      this.verificationFailureLayout.setVisibility(View.GONE);
      this.connectivityFailureLayout.setVisibility(View.GONE);
      this.connectingProgress.setVisibility(View.INVISIBLE);
      this.connectingCheck.setVisibility(View.VISIBLE);
      this.verificationProgress.setVisibility(View.INVISIBLE);
      this.verificationCheck.setVisibility(View.VISIBLE);
      this.generatingKeysProgress.setVisibility(View.VISIBLE);
      this.generatingKeysCheck.setVisibility(View.INVISIBLE);
      this.gcmRegistrationProgress.setVisibility(View.INVISIBLE);
      this.gcmRegistrationCheck.setVisibility(View.INVISIBLE);
      this.connectingText.setTextColor(UNFOCUSED_COLOR);
      this.verificationText.setTextColor(UNFOCUSED_COLOR);
      this.generatingKeysText.setTextColor(FOCUSED_COLOR);
      this.gcmRegistrationText.setTextColor(UNFOCUSED_COLOR);
      this.registrationProgress.setVisibility(View.INVISIBLE);
      this.timeoutProgressLayout.setVisibility(View.INVISIBLE);
      this.smsVerificationLayout.setVisibility(View.GONE);
    }
  
    private void handleStateGcmRegistering() {
      this.registrationLayout.setVisibility(View.VISIBLE);
      this.verificationFailureLayout.setVisibility(View.GONE);
      this.connectivityFailureLayout.setVisibility(View.GONE);
      this.connectingProgress.setVisibility(View.INVISIBLE);
      this.connectingCheck.setVisibility(View.VISIBLE);
      this.verificationProgress.setVisibility(View.INVISIBLE);
      this.verificationCheck.setVisibility(View.VISIBLE);
      this.generatingKeysProgress.setVisibility(View.INVISIBLE);
      this.generatingKeysCheck.setVisibility(View.VISIBLE);
      this.gcmRegistrationProgress.setVisibility(View.VISIBLE);
      this.gcmRegistrationCheck.setVisibility(View.INVISIBLE);
      this.connectingText.setTextColor(UNFOCUSED_COLOR);
      this.verificationText.setTextColor(UNFOCUSED_COLOR);
      this.generatingKeysText.setTextColor(UNFOCUSED_COLOR);
      this.gcmRegistrationText.setTextColor(FOCUSED_COLOR);
      this.registrationProgress.setVisibility(View.INVISIBLE);
      this.timeoutProgressLayout.setVisibility(View.INVISIBLE);
      this.smsVerificationLayout.setVisibility(View.GONE);
    }
  
    private void handleGcmTimeout(RegistrationState state) {
      handleConnectivityError(state);
    }
  
    private void handleVerificationRequestedVoice(RegistrationState state) {
      handleVerificationTimeout(state);
      verifyButton.setOnClickListener(new VerifyClickListener(state.number, state.password));
      verifyButton.setEnabled(true);
      codeEditText.setEnabled(true);
    }
  
    private void handleVerificationTimeout(RegistrationState state) {
        this.callButton.setOnClickListener(new CallClickListener(state.number));
        this.verifyButton.setEnabled(false);
        this.codeEditText.setEnabled(false);
        this.registrationLayout.setVisibility(View.GONE);
        this.connectivityFailureLayout.setVisibility(View.GONE);
        this.smsVerificationLayout.setVisibility(View.GONE);
        this.verificationFailureLayout.setVisibility(View.VISIBLE);
      if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Phone")){
          handleSmsVerificationTimeout(state);
      }else if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Email")){
          handleEmailVerificationTimeout(state);
      }
    }
  
    private void handleSmsVerificationTimeout(RegistrationState state){
        verificationFailureLabel.setText(R.string.registration_progress_activity__sms_verification_failed);
        verificationFailureWaitingLabel.setText(R.string.registration_progress_activity__textsecure_timed_out_while_waiting_for_a_verification_sms_message);
        this.verificationFailureButton.setText(String.format(getString(R.string.RegistrationProgressActivity_edit_s),
                PhoneNumberFormatter.formatNumberInternational(state.number)));
    }
  
    private void handleEmailVerificationTimeout(RegistrationState state){
        verificationFailureLabel.setText(R.string.registration_progress_activity__email_verification_failed);
        verificationFailureWaitingLabel.setText(R.string.registration_progress_activity__securecom_messaging_timed_out_while_waiting_for_a_verification_email_message);
        voiceImage.setVisibility(View.GONE);
        voiceVerificationLabel.setVisibility(View.GONE);
        telephonyTextLabel.setVisibility(View.GONE);
        this.verifyButton.setVisibility(View.GONE);
        this.callButton.setVisibility(View.GONE);
        codeEditText.setVisibility(View.GONE);
        this.verificationFailureButton.setText(String.format(getString(R.string.RegistrationProgressActivity_edit_s),
                state.number));
    }
  
    private void handleSmsVerification(RegistrationState state){
  
        if(!org.whispersystems.textsecure.internal.util.Util.isAndroidPhone()){
            if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Phone")){
                this.smsTextLabel.setText(R.string.registration_progress_activity__sms_verification);
                this.smsTextDesLabel.setText(R.string.registration_progress_activity__securecom_messaging_sms_you_to_verify_your_number);
            }
        }
        if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Email")){
            this.smsTextLabel.setText(R.string.registration_progress_activity__email_verification);
            this.smsTextDesLabel.setText( R.string.registration_progress_activity__securecom_messaging_email_you_to_verify_your_number);
        }
  
        if(!org.whispersystems.textsecure.internal.util.Util.isAndroidPhone() || TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Email")) {
            this.verifySmsButton.setEnabled(true);
            this.verifySmsButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    RegistrationProgressActivity self = RegistrationProgressActivity.this;
  
                    if(!com.securecomcode.messaging.util.Util.showAlertOnNoData(self)){
                        return;
                    }
                    String code = RegistrationProgressActivity.this.codeSmsEditText.getText().toString();
                    if (org.whispersystems.textsecure.internal.util.Util.isEmpty(code)) {
                        Toast.makeText(getApplicationContext(),
                                getString(R.string.RegistrationActivity_you_must_specify_your_verification_code),
                                Toast.LENGTH_LONG).show();
                        return;
                    }
                    RegistrationProgressActivity.this.registrationService.challengeReceived(code);
                }
            });
            this.codeSmsEditText.setEnabled(true);
            this.registrationLayout.setVisibility(View.GONE);
            this.smsVerificationLayout.setVisibility(View.VISIBLE);
        }else{
            this.registrationLayout.setVisibility(View.VISIBLE);
        }
        if(!org.whispersystems.textsecure.internal.util.Util.isValidEmail(state.number)){
            this.smsVerificationFailureButton.setText(String.format(getString(R.string.RegistrationProgressActivity_edit_s),
                    PhoneNumberFormatter.formatNumberInternational(state.number)));
        }else{
            this.smsVerificationFailureButton.setText(String.format(getString(R.string.RegistrationProgressActivity_edit_s),
                   state.number));
        }
  
    }
  
    private void handleConnectivityError(RegistrationState state) {
      this.registrationLayout.setVisibility(View.GONE);
      this.verificationFailureLayout.setVisibility(View.GONE);
      this.connectivityFailureLayout.setVisibility(View.VISIBLE);
      this.smsVerificationLayout.setVisibility(View.GONE);
      this.connectivityFailureButton.setText(String.format(getString(R.string.RegistrationProgressActivity_edit_s),
                                                           PhoneNumberFormatter.formatNumberInternational(state.number)));
    }
  
    private void handleMultiRegistrationError(RegistrationState state) {
      handleVerificationTimeout(state);
      Dialogs.showAlertDialog(this,                         getString(R.string.RegistrationProgressActivity_registration_conflict),
                           getString(R.string.RegistrationProgressActivity_this_number_is_already_registered_on_a_different));
    }
  
    private void handleVerificationComplete() {
      if (visible) {
        Toast.makeText(this,
                       R.string.RegistrationProgressActivity_registration_complete,
                       Toast.LENGTH_LONG).show();
      }
  
      shutdownService();
      startActivity(new Intent(this, RoutingActivity.class));
      finish();
    }
  
    private void handleTimerUpdate() {
      if (registrationService == null)
        return;
  
      int totalSecondsRemaining = registrationService.getSecondsRemaining();
      int minutesRemaining      = totalSecondsRemaining / 60;
      int secondsRemaining      = totalSecondsRemaining - (minutesRemaining * 60);
      double percentageComplete = (double)((60 * 2) - totalSecondsRemaining) / (double)(60 * 2);
      int progress              = (int)Math.round(((double)registrationProgress.getMax()) * percentageComplete);
  
      this.registrationProgress.setProgress(progress);
      this.registrationTimerText.setText(String.format("%02d:%02d", minutesRemaining, secondsRemaining));
  
      registrationStateHandler.sendEmptyMessageDelayed(RegistrationState.STATE_TIMER, 1000);
    }
  
    private boolean hasNumberDirective() {
        boolean result = false;
        if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Phone")){
            result = getIntent().getStringExtra("e164number") != null;
        }else if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Email")){
            result = getIntent().getStringExtra("email_address") != null;
        }
       return result;
    }
  
    private String getNumberDirective() {
        String result = "";
        if(TextSecurePreferences.getRegistrationOptionSelected(getApplicationContext()).equalsIgnoreCase("Phone")){
  