Android Open Source - RelayRemote Widget Config






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 ww w  .ja  v a  2s.  co 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.app.ListActivity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class WidgetConfig extends ListActivity {
        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setResult(RESULT_CANCELED);
        
        final int appWidgetId = getIntent().getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
        
        ListView relayList = getListView();
        
        // Load all relays from the db
        Database db = new Database(this);
        final ArrayList<Relay> relays = db.selectAllRelays();
        final ArrayList<RelayGroup> groups = db.selectAllRelayGroups();
        
        // Create a list of the names of all relays and groups for the list adapter
        ArrayList<String> names = new ArrayList<String>();
        for(Relay relay : relays) {
            names.add(relay.getName());
        }
        for(RelayGroup group : groups) {
            names.add(group.getName());
        }
        
        relayList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_selectable_list_item, names));
        
        // Create a textview if no relays/groups exist yet
        TextView emptyText = new TextView(this);
        emptyText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        emptyText.setText(R.string.emptyText);
        relayList.setEmptyView(emptyText);
                
        relayList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // If the position is in the first [relays.size()-1] elements, it is a relay, not a group
                int type = (position < relays.size()) ? Constants.WIDGET_RELAY : Constants.WIDGET_GROUP;
                
                // Get the rid or gid of the selected relay/group
                int _id;
                if(type == Constants.WIDGET_RELAY) {
                    _id = relays.get(position).getRid(); 
                } else {
                    _id = groups.get(position-relays.size()).getGid();
                }
                
                // Prepare the result value intent and call onUpdate() on the new widget if successfully added
                Intent intent = new Intent(WidgetConfig.this, Widget.class);
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {appWidgetId});
                
                // Insert the widget in the db
                if(new Database(WidgetConfig.this).insertWidget(appWidgetId, type, _id) == Constants.SUCCESS) {
                    setResult(RESULT_OK, intent);
                    sendBroadcast(intent);
                } else {
                    setResult(RESULT_CANCELED, intent);
                }
                
                finish();
            }
        });
    }
}




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