Android Open Source - RelayRemote Relay Adapter






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 
////from   www.  ja va  2s . 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 java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Switch;
import android.widget.TextView;

public class RelayAdapter extends ArrayAdapter<Relay> {
    private Context context;
    private ArrayList<Relay> relays;
    
    public RelayAdapter(Context context, ArrayList<Relay> relays) {
        super(context, R.layout.relay_adapter, relays);
        this.context = context;
        this.relays = relays;
    }
    
    public View getView(final int position, View view, ViewGroup parent) {
        // Inflate the view if it has not  been created yet
        if(view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.relay_adapter, null);
        }
        
        TextView relayName       = (TextView) view.findViewById(R.id.relayName);
        final Switch relaySwitch = (Switch) view.findViewById(R.id.relaySwitch);
        
        // Set the name on the label
        relayName.setText(relays.get(position).getName() + ":");
        
        // Set the switch to on if the relay state is on
        if(relays.get(position).isOn()) {
            relaySwitch.setChecked(true);
        } else {
            relaySwitch.setChecked(false);
        }
        
        // Use an on click listener so that the bg thread is only start if a user presses the button
        // rather than using an on change listener which can be triggered programmatically when the
        // relays states are checked
        relaySwitch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.startNetworkThreadForRelay(context, relays.get(position), (relaySwitch.isChecked()) ? Constants.CMD_ON : Constants.CMD_OFF);
                
            }
        });
        
        return view;
    }
    
    public void updateRelays(ArrayList<Relay> relays) {
        // Make a copy of the relays list so that the clear operation doesn't
        // clear it as well since may point to the same reference
        ArrayList<Relay> relays_copy = new ArrayList<Relay>();
        for(int i=0; i<relays.size(); i++) {
            relays_copy.add(relays.get(i));
        }
        
        // Clear the list
        clear();

        // Set the new data
        this.relays = relays_copy;
        addAll(this.relays);
        notifyDataSetChanged();
    }
}




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