<commit_before>/*
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.firebase.ui.auth.ui.phone;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.firebase.ui.auth.R;
import com.firebase.ui.auth.ui.ExtraConstants;
import com.firebase.ui.auth.ui.FlowParameters;
import com.firebase.ui.auth.ui.FragmentBase;
import com.firebase.ui.auth.util.GoogleApiHelper;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.credentials.Credential;
import com.google.android.gms.auth.api.credentials.CredentialPickerConfig;
import com.google.android.gms.auth.api.credentials.HintRequest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.Locale;

/**
 * Displays country selector and phone number input form for users
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class VerifyPhoneNumberFragment extends FragmentBase implements View.OnClickListener {
    public static final String TAG = "VerifyPhoneFragment";

    private CountryListSpinner countryListSpinner;
    private EditText mPhoneEditText;
    private TextView errorEditText;
    private Button sendCodeButton;
    private PhoneVerificationActivity mVerifier;
    private TextView mSmsTermsText;

    private static final int RC_PHONE_HINT = 22;

    public static VerifyPhoneNumberFragment newInstance(FlowParameters flowParameters,
                                                        String phone) {
        VerifyPhoneNumberFragment fragment = new VerifyPhoneNumberFragment();

        Bundle args = new Bundle();
        args.putParcelable(ExtraConstants.EXTRA_FLOW_PARAMS, flowParameters);
        args.putString(ExtraConstants.EXTRA_PHONE, phone);

        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.phone_layout, container, false);

        countryListSpinner = (CountryListSpinner) v.findViewById(R.id.country_list);
        mPhoneEditText = (EditText) v.findViewById(R.id.phone_number);
        errorEditText = (TextView) v.findViewById(R.id.phone_number_error);
        sendCodeButton = (Button) v.findViewById(R.id.send_code);
        mSmsTermsText = (TextView) v.findViewById(R.id.send_sms_tos);

        FragmentActivity parentActivity = getActivity();
        parentActivity.setTitle(getString(R.string.verify_phone_number_title));
        setUpCountrySpinner();
        setupSendCodeButton();
        setupTerms();

        return v;
    }

    private void setupTerms() {
        final String verifyPhoneButtonText = getString(R.string.verify_phone_number);
        final String terms = getString(R.string.sms_terms_of_service, verifyPhoneButtonText);
        mSmsTermsText.setText(terms);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Set listener
        if (!(getActivity() instanceof PhoneVerificationActivity)) {
            throw new IllegalStateException("Activity must implement PhoneVerificationHandler");
        }
        mVerifier = (PhoneVerificationActivity) getActivity();

        if (savedInstanceState != null) {
            return;
        }

        // Check for phone
        // It is assumed that the phone number that are being wired in via Credential Selector
        // are e164 since we store it.
        String phone = getArguments().getString(ExtraConstants.EXTRA_PHONE);
        if (!TextUtils.isEmpty(phone)) {
            // Use phone passed in
            PhoneNumber phoneNumber = PhoneNumberUtils.getPhoneNumber(phone);
            setPhoneNumber(phoneNumber);
            setCountryCode(phoneNumber);
        } else if (getFlowParams().enableHints) {
            // Try SmartLock phone autocomplete hint
            showPhoneAutoCompleteHint();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_PHONE_HINT) {
            if (data != null) {
                Credential cred = data.getParcelableExtra(Credential.EXTRA_KEY);
                if (cred != null) {
                    // Hint selector does not always return phone numbers in e164 format.
                    // To accommodate either case, we normalize to e164 with best effort
                    final String unformattedPhone = cred.getId();
                    final String formattedPhone =
                            PhoneNumberUtils
                                    .formatPhoneNumberUsingCurrentCountry(unformattedPhone,
                                                                          getContext());
                    if (formattedPhone == null) {
                        Log.e(TAG, "Unable to normalize phone number from hint selector:"
                                + unformattedPhone);
                        return;
                    }
                    final PhoneNumber phoneNumberObj =
                            PhoneNumberUtils.getPhoneNumber(formattedPhone);
                    setPhoneNumber(phoneNumberObj);
                    setCountryCode(phoneNumberObj);
                }
            }
        }
    }
<commit_msg>Fix Fragment getContext NPE (#781)<commit_after>/*
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.firebase.ui.auth.ui.phone;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.firebase.ui.auth.R;
import com.firebase.ui.auth.ui.ExtraConstants;
import com.firebase.ui.auth.ui.FlowParameters;
import com.firebase.ui.auth.ui.FragmentBase;
import com.firebase.ui.auth.util.GoogleApiHelper;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.credentials.Credential;
import com.google.android.gms.auth.api.credentials.CredentialPickerConfig;
import com.google.android.gms.auth.api.credentials.HintRequest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.Locale;

/**
 * Displays country selector and phone number input form for users
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class VerifyPhoneNumberFragment extends FragmentBase implements View.OnClickListener {
    public static final String TAG = "VerifyPhoneFragment";

    private Context mAppContext;

    private CountryListSpinner countryListSpinner;
    private EditText mPhoneEditText;
    private TextView errorEditText;
    private Button sendCodeButton;
    private PhoneVerificationActivity mVerifier;
    private TextView mSmsTermsText;

    private static final int RC_PHONE_HINT = 22;

    public static VerifyPhoneNumberFragment newInstance(FlowParameters flowParameters,
                                                        String phone) {
        VerifyPhoneNumberFragment fragment = new VerifyPhoneNumberFragment();

        Bundle args = new Bundle();
        args.putParcelable(ExtraConstants.EXTRA_FLOW_PARAMS, flowParameters);
        args.putString(ExtraConstants.EXTRA_PHONE, phone);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mAppContext = context.getApplicationContext();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.phone_layout, container, false);

        countryListSpinner = (CountryListSpinner) v.findViewById(R.id.country_list);
        mPhoneEditText = (EditText) v.findViewById(R.id.phone_number);
        errorEditText = (TextView) v.findViewById(R.id.phone_number_error);
        sendCodeButton = (Button) v.findViewById(R.id.send_code);
        mSmsTermsText = (TextView) v.findViewById(R.id.send_sms_tos);

        FragmentActivity parentActivity = getActivity();
        parentActivity.setTitle(getString(R.string.verify_phone_number_title));
        setUpCountrySpinner();
        setupSendCodeButton();
        setupTerms();

        return v;
    }

    private void setupTerms() {
        final String verifyPhoneButtonText = getString(R.string.verify_phone_number);
        final String terms = getString(R.string.sms_terms_of_service, verifyPhoneButtonText);
        mSmsTermsText.setText(terms);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Set listener
        if (!(getActivity() instanceof PhoneVerificationActivity)) {
            throw new IllegalStateException("Activity must implement PhoneVerificationHandler");
        }
        mVerifier = (PhoneVerificationActivity) getActivity();

        if (savedInstanceState != null) {
            return;
        }

        // Check for phone
        // It is assumed that the phone number that are being wired in via Credential Selector
        // are e164 since we store it.
        String phone = getArguments().getString(ExtraConstants.EXTRA_PHONE);
        if (!TextUtils.isEmpty(phone)) {
            // Use phone passed in
            PhoneNumber phoneNumber = PhoneNumberUtils.getPhoneNumber(phone);
            setPhoneNumber(phoneNumber);
            setCountryCode(phoneNumber);
        } else if (getFlowParams().enableHints) {
            // Try SmartLock phone autocomplete hint
            showPhoneAutoCompleteHint();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_PHONE_HINT) {
            if (data != null) {
                Credential cred = data.getParcelableExtra(Credential.EXTRA_KEY);
                if (cred != null) {
                    // Hint selector does not always return phone numbers in e164 format.
                    // To accommodate either case, we normalize to e164 with best effort
                    final String unformattedPhone = cred.getId();
                    final String formattedPhone =
                            PhoneNumberUtils
                                    .formatPhoneNumberUsingCurrentCountry(unformattedPhone,
                                                                          mAppContext);
                    if (formattedPhone == null) {
                        Log.e(TAG, "Unable to normalize phone number from hint selector:"
                                + unformattedPhone);
                        return;
                    }
                    final PhoneNumber phoneNumberObj =
                            PhoneNumberUtils.getPhoneNumber(formattedPhone);
                    setPhoneNumber(phoneNumberObj);
                    setCountryCode(phoneNumberObj);
                }
            }
        }
    }

{'fraction_non_alphanumeric': 0.07846536519182935, 'fraction_numerical': 0.0032411245948594258, 'mean_word_length': 2.6865796054459574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6723475', 'n_tokens_mistral': 3423, 'n_tokens_neox': 3279, 'n_words': 851}