Android Open Source - RelayRemote Widget






From Project

Back to project page RelayRemote.

License

The source code is released under:

GNU General Public License

If you think the Android project RelayRemote 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

// Copyright (C) 2012 Shane Tully 
////  w  ww  .j  a v  a2  s  .  c o  m
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.shanet.relayremote;

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

public class Widget extends AppWidgetProvider {
    
    public static final int STATE_UNKNOWN = 0;
    public static final int STATE_OFF     = 1;
    public static final int STATE_ON      = 2;
    
    private static SparseIntArray states = new SparseIntArray();
    
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);        
        
        for(int appWidgetId : appWidgetIds) {
            try {
                // Get the name of relay/group assigned to this widget
                Database db = new Database(context);
                Bundle widgetInfo = db.selectWidget(appWidgetId);
                
                if(widgetInfo == null) continue;
                
                String name = "";
                switch(widgetInfo.getInt("type")) {
                    case Constants.WIDGET_RELAY:
                        Relay relay = db.selectRelay(widgetInfo.getInt("id"));
                        
                        name = relay.getName();
                        
                        // Call the bg thread to get the state from the server
                        new Background(context, Constants.OP_GET, true).execute(getBgInfoBundle(relay, Constants.OP_GET, appWidgetId));
                        
                        break;
                    case Constants.WIDGET_GROUP:
                        RelayGroup group = db.selectRelayGroup(widgetInfo.getInt("id"));
                        
                        name = group.getName();
                        
                        for(int rid : group.getRids()) {
                            // Call the bg thread to get the state from the server
                            new Background(context, Constants.OP_GET, true).execute(getBgInfoBundle(db.selectRelay(rid), Constants.OP_GET, appWidgetId));
                        }
                }
                                    
                // Set the name of the relay/group the widget is for
                views.setTextViewText(R.id.widgetName, name);
                
                // Check if the state is already in the states array
                // If so, set the indicator as such; if not (unknown state), set it as half while the background thread gets the state
                switch(getState(appWidgetId)) {
                    case STATE_UNKNOWN:
                        views.setImageViewResource(R.id.widgetIndicator, R.drawable.widget_half);
                    case STATE_ON:
                        views.setImageViewResource(R.id.widgetIndicator, R.drawable.widget_on);
                    case STATE_OFF:
                        views.setImageViewResource(R.id.widgetIndicator, R.drawable.widget_off);
                }
                
                // Set a pending intent on the button for button clicks
                Intent intent = new Intent(context, Widget.class);
                intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
                intent.setData(Uri.parse(Integer.valueOf(appWidgetId).toString()));
                views.setOnClickPendingIntent(R.id.widgetButton, PendingIntent.getBroadcast(context, 0, intent, 0));
                
                appWidgetManager.updateAppWidget(appWidgetId, views);
            } catch (NullPointerException npe) {
                continue;
            }
        }
    }
    
    
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
                
        if (intent.hasCategory(Intent.CATEGORY_ALTERNATIVE)) {
            Uri data = intent.getData();
            int appWidgetId = Integer.parseInt(data.getSchemeSpecificPart());
                        
            Database db = new Database(context);

            // Get the type and id of the relay associated with this widget from the db
            Bundle widgetInfo = db.selectWidget(appWidgetId);
            int id = widgetInfo.getInt("id");
            switch(widgetInfo.getInt("type")) {
                case Constants.WIDGET_RELAY:
                    // Call the bg thread to send the commands to the server
                    new Background(context, Constants.OP_SET, true).execute(getBgInfoBundle(db.selectRelay(id), Constants.OP_SET, appWidgetId));
                    break;

                case Constants.WIDGET_GROUP:
                    RelayGroup group = db.selectRelayGroup(id);
                    for(int rid : group.getRids()) {
                        // Call the bg thread to send the commands to the server
                        new Background(context, Constants.OP_SET, true).execute(getBgInfoBundle(db.selectRelay(rid), Constants.OP_SET, appWidgetId));
                    }
            }
            
            // Update the indicator image
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setImageViewResource(R.id.widgetIndicator, R.drawable.widget_half);
            AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);
        }      
    }
    
    
    public Bundle getBgInfoBundle(Relay relay, char op, int appWidgetId) {
        Bundle bgInfo = new Bundle();
        bgInfo.putChar("op", op);
        bgInfo.putString("server", relay.getServer());
        bgInfo.putInt("port", relay.getPort());
        bgInfo.putInt("pin", relay.getPin());
        bgInfo.putChar("cmd", (getState(appWidgetId) == STATE_OFF ? Constants.CMD_ON : Constants.CMD_OFF));
        bgInfo.putInt("appWidgetId", appWidgetId);
                                
        return bgInfo;
    }
    
    
    public static void setState(int appWidgetId, int state) {
        states.put(appWidgetId, state);
    }
    
    
    public static int getState(int appWidgetId) {
        return states.get(appWidgetId);
    }
    
    
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        
        // Delete the widgets from the db
        Database db = new Database(context);
        
        for(int appWidgetId : appWidgetIds) {
            db.deleteWidget(appWidgetId);
        }
    }
}




Java Source Code List

com.shanet.relayremote.AddRelayGroup.java
com.shanet.relayremote.AddRelay.java
com.shanet.relayremote.Background.java
com.shanet.relayremote.Constants.java
com.shanet.relayremote.Database.java
com.shanet.relayremote.DialogUtils.java
com.shanet.relayremote.EditRelayGroup.java
com.shanet.relayremote.EditRelay.java
com.shanet.relayremote.Main.java
com.shanet.relayremote.NFC.java
com.shanet.relayremote.RelayAdapter.java
com.shanet.relayremote.RelayGroup.java
com.shanet.relayremote.RelayGroupsAdapter.java
com.shanet.relayremote.RelayGroupsFragment.java
com.shanet.relayremote.Relay.java
com.shanet.relayremote.RelaysFragment.java
com.shanet.relayremote.Server.java
com.shanet.relayremote.Utils.java
com.shanet.relayremote.VersionUtils.java
com.shanet.relayremote.WidgetConfig.java
com.shanet.relayremote.Widget.java