<commit_before>package com.twilio.notify.quickstart;

import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.twilio.notify.quickstart.notifyapi.TwilioSDKStarterAPI;
import com.twilio.notify.quickstart.notifyapi.model.Binding;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.twilio.notify.quickstart.BindingSharedPreferences.ENDPOINT;
import static com.twilio.notify.quickstart.BindingSharedPreferences.IDENTITY;
import static com.twilio.notify.quickstart.BindingSharedPreferences.ADDRESS;

public class BindingIntentService extends IntentService {

    private static final String TAG = "BindingIntentService";
    private static final String FCM_BINDING_TYPE = "fcm";

    private SharedPreferences sharedPreferences;
    private Intent bindingResultIntent;

    public BindingIntentService() {
        super(TAG);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        bindingResultIntent = new Intent(MainActivity.BINDING_REGISTRATION);
        // Load the old binding values from shared preferences if they exist
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        String identity = sharedPreferences.getString(IDENTITY, null);
        String endpoint = sharedPreferences.getString(ENDPOINT, null);
        String address = sharedPreferences.getString(ADDRESS, null);

        // Set the new binding identity
        String newIdentity = intent.getStringExtra(IDENTITY);
        if (newIdentity == null) {
            // If no identity was provided to us then we use the identity stored in shared preferences.
            newIdentity = identity;
        }

        /*
         * Generate a new endpoint based on the new identity and the instanceID. This ensures that we
         * maintain stability of the endpoint even if the instanceID changes without the identity
         * changing.
         */
        String newEndpoint = newIdentity + "@" + FirebaseInstanceId.getInstance().getId();

        /*
         * Obtain the new address based off the Firebase instance token
         */
        String newAddress = FirebaseInstanceId.getInstance().getToken();

        /*
         * Check whether a new binding registration is required by comparing the prior values that
         * were stored in shared preferences after the last successful binding registration.
         */
        boolean sameBinding =
                newIdentity.equals(identity) && newEndpoint.equals(endpoint) && newAddress.equals(address);

        if (newIdentity == null) {
            Log.i(TAG, "A new binding registration was not performed because" +
                    " the identity cannot be null.");
            bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, false);
            bindingResultIntent.putExtra(MainActivity.BINDING_RESPONSE, "Binding identity was null");
            // Notify the MainActivity that the registration ended
            LocalBroadcastManager.getInstance(BindingIntentService.this)
                    .sendBroadcast(bindingResultIntent);
        } else if (sameBinding) {
            Log.i(TAG, "A new binding registration was not performed because" +
                    "the binding values are the same as the last registered binding.");
            bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, true);
            bindingResultIntent.putExtra(MainActivity.BINDING_RESPONSE, "Binding already registered");
            // Notify the MainActivity that the registration ended
            LocalBroadcastManager.getInstance(BindingIntentService.this)
                    .sendBroadcast(bindingResultIntent);
        } else {
            /*
             * Clear all the existing bindings from SharedPreferences and attempt to register
             * the new binding values.
             */
            sharedPreferences.edit().clear().commit();
            final Binding binding = new Binding(newIdentity, newEndpoint, newAddress, FCM_BINDING_TYPE);
            registerBinding(binding);
        }
    }

    /**
     * Register the binding with Twilio Notify via your application server.
     */
    private void registerBinding(final Binding binding) {
        /*
         * Issue the request to your application server and wait for the result in the callback
         */
        Log.i(TAG, "Binding with" +
                " identity: " + binding.identity +
                " endpoint: " + binding.endpoint +
                " address: " + binding.Address);

        /*
         * Make sure the Twilio SDK Starter URL has been updated
         */
        if (TwilioSDKStarterAPI.BASE_SERVER_URL.equals(getString(R.string.base_url_string))) {
            String message = "Set the BASE_SERVER_URL in TwilioSDKStarterAPI.java";
            Log.e(TAG, message);
            bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, false);
            bindingResultIntent.putExtra(MainActivity.BINDING_RESPONSE, message);
            // Notify the MainActivity that the registration ended
            LocalBroadcastManager.getInstance(BindingIntentService.this)
                    .sendBroadcast(bindingResultIntent);
            return;
        }

        Call<Void> call = TwilioSDKStarterAPI.registerBinding(binding);
        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                if(response.isSuccess()) {
                    // Store binding in SharedPreferences upon success
                    sharedPreferences.edit().putString(IDENTITY, binding.identity).commit();
                    sharedPreferences.edit().putString(ENDPOINT, binding.endpoint).commit();
                    sharedPreferences.edit().putString(ADDRESS, binding.Address).commit();

                    bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, true);
                } else {
                    String message = "Binding failed " + response.code() + " " + response.message();
                    Log.e(TAG, message);
                    bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, false);
                    bindingResultIntent.putExtra(MainActivity.BINDING_RESPONSE, message);
                }

                // Notify the MainActivity that the registration ended
                LocalBroadcastManager.getInstance(BindingIntentService.this)
                        .sendBroadcast(bindingResultIntent);
            }

            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                String message = "Binding failed " + t.getMessage();
                Log.e(TAG, message);
                bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, false);
                bindingResultIntent.putExtra(MainActivity.BINDING_RESPONSE, message);
                // Notify the MainActivity that the registration ended
                LocalBroadcastManager.getInstance(BindingIntentService.this)
                        .sendBroadcast(bindingResultIntent);
            }
        });
    }

}
<commit_msg>Selectively clear binding shared preferences<commit_after>package com.twilio.notify.quickstart;

import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.twilio.notify.quickstart.notifyapi.TwilioSDKStarterAPI;
import com.twilio.notify.quickstart.notifyapi.model.Binding;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.twilio.notify.quickstart.BindingSharedPreferences.ENDPOINT;
import static com.twilio.notify.quickstart.BindingSharedPreferences.IDENTITY;
import static com.twilio.notify.quickstart.BindingSharedPreferences.ADDRESS;

public class BindingIntentService extends IntentService {

    private static final String TAG = "BindingIntentService";
    private static final String FCM_BINDING_TYPE = "fcm";

    private SharedPreferences sharedPreferences;
    private Intent bindingResultIntent;

    public BindingIntentService() {
        super(TAG);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        bindingResultIntent = new Intent(MainActivity.BINDING_REGISTRATION);
        // Load the old binding values from shared preferences if they exist
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        String identity = sharedPreferences.getString(IDENTITY, null);
        String endpoint = sharedPreferences.getString(ENDPOINT, null);
        String address = sharedPreferences.getString(ADDRESS, null);

        // Set the new binding identity
        String newIdentity = intent.getStringExtra(IDENTITY);
        if (newIdentity == null) {
            // If no identity was provided to us then we use the identity stored in shared preferences.
            newIdentity = identity;
        }

        /*
         * Generate a new endpoint based on the new identity and the instanceID. This ensures that we
         * maintain stability of the endpoint even if the instanceID changes without the identity
         * changing.
         */
        String newEndpoint = newIdentity + "@" + FirebaseInstanceId.getInstance().getId();

        /*
         * Obtain the new address based off the Firebase instance token
         */
        String newAddress = FirebaseInstanceId.getInstance().getToken();

        /*
         * Check whether a new binding registration is required by comparing the prior values that
         * were stored in shared preferences after the last successful binding registration.
         */
        boolean sameBinding =
                newIdentity.equals(identity) && newEndpoint.equals(endpoint) && newAddress.equals(address);

        if (newIdentity == null) {
            Log.i(TAG, "A new binding registration was not performed because" +
                    " the identity cannot be null.");
            bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, false);
            bindingResultIntent.putExtra(MainActivity.BINDING_RESPONSE, "Binding identity was null");
            // Notify the MainActivity that the registration ended
            LocalBroadcastManager.getInstance(BindingIntentService.this)
                    .sendBroadcast(bindingResultIntent);
        } else if (sameBinding) {
            Log.i(TAG, "A new binding registration was not performed because" +
                    "the binding values are the same as the last registered binding.");
            bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, true);
            bindingResultIntent.putExtra(MainActivity.BINDING_RESPONSE, "Binding already registered");
            // Notify the MainActivity that the registration ended
            LocalBroadcastManager.getInstance(BindingIntentService.this)
                    .sendBroadcast(bindingResultIntent);
        } else {
            /*
             * Clear all the existing bindings from SharedPreferences and attempt to register
             * the new binding values.
             */
            sharedPreferences.edit().remove(BindingSharedPreferences.IDENTITY).commit();
            sharedPreferences.edit().remove(BindingSharedPreferences.ENDPOINT).commit();
            sharedPreferences.edit().remove(BindingSharedPreferences.ADDRESS).commit();
            final Binding binding = new Binding(newIdentity, newEndpoint, newAddress, FCM_BINDING_TYPE);
            registerBinding(binding);
        }
    }

    /**
     * Register the binding with Twilio Notify via your application server.
     */
    private void registerBinding(final Binding binding) {
        /*
         * Issue the request to your application server and wait for the result in the callback
         */
        Log.i(TAG, "Binding with" +
                " identity: " + binding.identity +
                " endpoint: " + binding.endpoint +
                " address: " + binding.Address);

        /*
         * Make sure the Twilio SDK Starter URL has been updated
         */
        if (TwilioSDKStarterAPI.BASE_SERVER_URL.equals(getString(R.string.base_url_string))) {
            String message = "Set the BASE_SERVER_URL in TwilioSDKStarterAPI.java";
            Log.e(TAG, message);
            bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, false);
            bindingResultIntent.putExtra(MainActivity.BINDING_RESPONSE, message);
            // Notify the MainActivity that the registration ended
            LocalBroadcastManager.getInstance(BindingIntentService.this)
                    .sendBroadcast(bindingResultIntent);
            return;
        }

        Call<Void> call = TwilioSDKStarterAPI.registerBinding(binding);
        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                if(response.isSuccess()) {
                    // Store binding in SharedPreferences upon success
                    sharedPreferences.edit().putString(IDENTITY, binding.identity).commit();
                    sharedPreferences.edit().putString(ENDPOINT, binding.endpoint).commit();
                    sharedPreferences.edit().putString(ADDRESS, binding.Address).commit();

                    bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, true);
                } else {
                    String message = "Binding failed " + response.code() + " " + response.message();
                    Log.e(TAG, message);
                    bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, false);
                    bindingResultIntent.putExtra(MainActivity.BINDING_RESPONSE, message);
                }

                // Notify the MainActivity that the registration ended
                LocalBroadcastManager.getInstance(BindingIntentService.this)
                        .sendBroadcast(bindingResultIntent);
            }

            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                String message = "Binding failed " + t.getMessage();
                Log.e(TAG, message);
                bindingResultIntent.putExtra(MainActivity.BINDING_SUCCEEDED, false);
                bindingResultIntent.putExtra(MainActivity.BINDING_RESPONSE, message);
                // Notify the MainActivity that the registration ended
                LocalBroadcastManager.getInstance(BindingIntentService.this)
                        .sendBroadcast(bindingResultIntent);
            }
        });
    }

}

{'fraction_non_alphanumeric': 0.07726430084745763, 'fraction_numerical': 0.0005296610169491525, 'mean_word_length': 2.4916782246879334, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6273843', 'n_tokens_mistral': 3628, 'n_tokens_neox': 3519, 'n_words': 912}