<commit_before>package com.ajhodges.wificallingcontrols.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;

import com.ajhodges.wificallingcontrols.Constants;
import com.ajhodges.wificallingcontrols.R;
import com.ajhodges.wificallingcontrols.ipphone.WifiCallingManager;
import com.ajhodges.wificallingcontrols.bundle.PluginBundleManager;

/**
 * Created by Adam on 3/13/14.
 */
public class ToggleWidgetProvider extends AppWidgetProvider {
    public final static String EXTRA_WIDGET_IDS = "ToggleWidgetProviderID";
    public final static String EXTRA_WIDGET_TOGGLE = "ToggleWidgetProviderToggle";
    @Override
    public void onReceive(Context context, Intent intent){
        if(intent.hasExtra(EXTRA_WIDGET_IDS)){
            int[] ids = intent.getIntArrayExtra(EXTRA_WIDGET_IDS);
            Boolean ipphoneEnabled = null;
            if(intent.hasExtra(EXTRA_WIDGET_TOGGLE)){
                //This intent is coming from a widget click event! Broadcast toggle pendingintent
                Intent fireIntent = new Intent();
                fireIntent.setAction(com.twofortyfouram.locale.Intent.ACTION_FIRE_SETTING);

                final Bundle resultBundle = PluginBundleManager.generateBundle(context, WifiCallingManager.MODE_TOGGLE);
                fireIntent.putExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE, resultBundle);

                Log.v(Constants.LOG_TAG, "Button clicked, toggling Wifi Calling state.");
                context.sendBroadcast(fireIntent);

                //set the widget background to reflect the new Wifi Calling State
                ipphoneEnabled = !intent.getBooleanExtra(EXTRA_WIDGET_TOGGLE, false);

            }
            //Noticed a change in our settings... update widgets!
            this.update(context, AppWidgetManager.getInstance(context), ids, ipphoneEnabled);
        } else {
            super.onReceive(context, intent);
        }
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
        super.onUpdate(context,appWidgetManager,appWidgetIds);
        update(context,appWidgetManager,appWidgetIds, null);
    }

    private void update(Context context, AppWidgetManager manager, int[] ids, Boolean ipphoneEnabled){
        //Called once per widget to initialize the state
        Log.v(Constants.LOG_TAG, "onUpdate: updating widget state");

        //get the current Wifi Calling state
        if(ipphoneEnabled == null)
            ipphoneEnabled = WifiCallingManager.getInstance(context).getIPPhoneEnabled(context);

        for(int i : ids){
            //set onClick to update the widget and toggle the Wifi Calling state
            Intent updateWidgets = new Intent();
            updateWidgets.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            updateWidgets.putExtra(EXTRA_WIDGET_IDS, ids);
            updateWidgets.putExtra(EXTRA_WIDGET_TOGGLE, ipphoneEnabled);
<commit_msg>Tapping a widget more than once before the onclick event is processed no longer does anything (fixes toggling for impatient people)<commit_after>package com.ajhodges.wificallingcontrols.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;

import com.ajhodges.wificallingcontrols.Constants;
import com.ajhodges.wificallingcontrols.R;
import com.ajhodges.wificallingcontrols.ipphone.WifiCallingManager;
import com.ajhodges.wificallingcontrols.bundle.PluginBundleManager;

/**
 * Created by Adam on 3/13/14.
 */
public class ToggleWidgetProvider extends AppWidgetProvider {
    public final static String EXTRA_WIDGET_IDS = "ToggleWidgetProviderID";
    public final static String EXTRA_WIDGET_TOGGLE = "ToggleWidgetProviderToggle";
    @Override
    public void onReceive(Context context, Intent intent){
        if(intent.hasExtra(EXTRA_WIDGET_IDS)){
            int[] ids = intent.getIntArrayExtra(EXTRA_WIDGET_IDS);
            Boolean ipphoneEnabled = null;
            if(intent.hasExtra(EXTRA_WIDGET_TOGGLE)){
                //set the widget background to reflect the new Wifi Calling State
                ipphoneEnabled = !intent.getBooleanExtra(EXTRA_WIDGET_TOGGLE, false);

                //This intent is coming from a widget click event! Broadcast toggle pendingintent
                Intent fireIntent = new Intent();
                fireIntent.setAction(com.twofortyfouram.locale.Intent.ACTION_FIRE_SETTING);

                final Bundle resultBundle = PluginBundleManager.generateBundle(context, ipphoneEnabled ? 1 : 0);
                fireIntent.putExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE, resultBundle);

                Log.v(Constants.LOG_TAG, "Button clicked, toggling Wifi Calling state.");
                context.sendBroadcast(fireIntent);



            }
            //Noticed a change in our settings... update widgets!
            this.update(context, AppWidgetManager.getInstance(context), ids, ipphoneEnabled);
        } else {
            super.onReceive(context, intent);
        }
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
        super.onUpdate(context,appWidgetManager,appWidgetIds);
        update(context,appWidgetManager,appWidgetIds, null);
    }

    private void update(Context context, AppWidgetManager manager, int[] ids, Boolean ipphoneEnabled){
        //Called once per widget to initialize the state
        Log.v(Constants.LOG_TAG, "onUpdate: updating widget state");

        //get the current Wifi Calling state
        if(ipphoneEnabled == null)
            ipphoneEnabled = WifiCallingManager.getInstance(context).getIPPhoneEnabled(context);

        for(int i : ids){
            //set onClick to update the widget and toggle the Wifi Calling state
            Intent updateWidgets = new Intent();
            updateWidgets.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            updateWidgets.putExtra(EXTRA_WIDGET_IDS, ids);
            updateWidgets.putExtra(EXTRA_WIDGET_TOGGLE, ipphoneEnabled);

{'fraction_non_alphanumeric': 0.07865342848278002, 'fraction_numerical': 0.001861619609059882, 'mean_word_length': 3.3502024291497974, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3440273', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1749, 'n_words': 408}