Android Open Source - WifiCallingControls Toggle Widget Provider






From Project

Back to project page WifiCallingControls.

License

The source code is released under:

MIT License

If you think the Android project WifiCallingControls listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ajhodges.wificallingcontrols.widget;
//from   w ww. j av  a  2 s . co  m
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.bundle.PluginBundleManager;
import com.ajhodges.wificallingcontrols.ipphone.WifiCallingManager;

/**
 * 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;
            Boolean widgetUpdating = context.getSharedPreferences("ToggleWidgetProvider", Context.MODE_PRIVATE).getBoolean("widgetUpdating", false);
            if(widgetUpdating) {
                return;
            }
            else 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);

                context.getSharedPreferences("ToggleWidgetProvider", Context.MODE_PRIVATE).edit().putBoolean("widgetUpdating", true);
                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);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, updateWidgets, PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setOnClickPendingIntent(R.id.toggle_button_view, pendingIntent);

            //set the widget background to reflect the current Wifi Calling State
            views.setImageViewResource(R.id.toggle_button_view, (ipphoneEnabled ? R.drawable.ic_toggle_on : R.drawable.ic_toggle_off));

            manager.updateAppWidget(i, views);
        }
    }
}




Java Source Code List

com.ajhodges.wificallingcontrols.Constants.java
com.ajhodges.wificallingcontrols.PluginApplication.java
com.ajhodges.wificallingcontrols.bundle.BundleScrubber.java
com.ajhodges.wificallingcontrols.bundle.PluginBundleManager.java
com.ajhodges.wificallingcontrols.ipphone.MovialCallingManager.java
com.ajhodges.wificallingcontrols.ipphone.SamsungCallingManager.java
com.ajhodges.wificallingcontrols.ipphone.WifiCallingManager.java
com.ajhodges.wificallingcontrols.receiver.QueryReceiver.java
com.ajhodges.wificallingcontrols.receiver.ToggleReceiver.java
com.ajhodges.wificallingcontrols.receiver.UpdateReceiver.java
com.ajhodges.wificallingcontrols.ui.AbstractPluginActivity.java
com.ajhodges.wificallingcontrols.ui.EditConditionActivity.java
com.ajhodges.wificallingcontrols.ui.EditSettingActivity.java
com.ajhodges.wificallingcontrols.ui.InfoActivity.java
com.ajhodges.wificallingcontrols.widget.ToggleWidgetProvider.java
com.twofortyfouram.locale.BreadCrumber.java
com.twofortyfouram.locale.Constants.java
com.twofortyfouram.locale.Intent.java
com.twofortyfouram.locale.PackageUtilities.java
com.twofortyfouram.locale.api.BuildConfig.java
com.twofortyfouram.locale.api.BuildConfig.java