li.klass.fhem.adapter.devices.FS20ZDRDeviceAdapter.java Source code

Java tutorial

Introduction

Here is the source code for li.klass.fhem.adapter.devices.FS20ZDRDeviceAdapter.java

Source

/*
 * AndFHEM - Open Source Android application to control a FHEM home automation
 * server.
 *
 * Copyright (c) 2011, Matthias Klass or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU GENERAL PUBLIC LICENSE, as published by the Free Software Foundation.
 *
 * 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 GENERAL PUBLIC LICENSE
 * for more details.
 *
 * You should have received a copy of the GNU GENERAL PUBLIC LICENSE
 * along with this distribution; if not, write to:
 *   Free Software Foundation, Inc.
 *   51 Franklin Street, Fifth Floor
 *   Boston, MA  02110-1301  USA
 */

package li.klass.fhem.adapter.devices;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import org.apache.commons.lang3.math.NumberUtils;

import java.util.List;

import li.klass.fhem.R;
import li.klass.fhem.adapter.devices.core.ToggleableAdapter;
import li.klass.fhem.adapter.devices.genericui.DeviceDetailViewAction;
import li.klass.fhem.domain.FS20ZDRDevice;

public class FS20ZDRDeviceAdapter extends ToggleableAdapter<FS20ZDRDevice> {
    public FS20ZDRDeviceAdapter() {
        super(FS20ZDRDevice.class);
    }

    @Override
    protected List<DeviceDetailViewAction<FS20ZDRDevice>> provideDetailActions() {
        List<DeviceDetailViewAction<FS20ZDRDevice>> detailActions = super.provideDetailActions();

        detailActions.add(new DeviceDetailViewAction<FS20ZDRDevice>() {
            @Override
            public View createView(Context context, LayoutInflater inflater, FS20ZDRDevice device,
                    LinearLayout parent) {
                View view = inflater.inflate(R.layout.fs20_zdr_actions, parent, false);

                registerActionHandlerFor(context, view, device, R.id.vol_up, "volume_up");
                registerActionHandlerFor(context, view, device, R.id.vol_down, "volume_down");
                registerActionHandlerFor(context, view, device, R.id.left, "left");
                registerActionHandlerFor(context, view, device, R.id.right, "right");
                registerActionHandlerFor(context, view, device, R.id.slp, "sleep");
                registerActionHandlerFor(context, view, device, R.id.ms, "ms");
                registerActionHandlerFor(context, view, device, R.id.prog_1, "1");
                registerActionHandlerFor(context, view, device, R.id.prog_2, "2");
                registerActionHandlerFor(context, view, device, R.id.prog_3, "3");
                registerActionHandlerFor(context, view, device, R.id.prog_4, "4");
                registerActionHandlerFor(context, view, device, R.id.prog_5, "5");
                registerActionHandlerFor(context, view, device, R.id.prog_6, "6");
                registerActionHandlerFor(context, view, device, R.id.prog_7, "7");
                registerActionHandlerFor(context, view, device, R.id.prog_8, "8");

                return view;
            }
        });

        return detailActions;
    }

    private void registerActionHandlerFor(final Context context, View view, final FS20ZDRDevice device,
            int buttonId, final String state) {
        Button button = (Button) view.findViewById(buttonId);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stateUiService.setState(device, state, context);
            }
        });
        if (NumberUtils.isNumber(state)) {
            button.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    stateUiService.setState(device, "program_" + state, context);
                    Toast.makeText(context, String.format(context.getString(R.string.programChannelSuccess), state),
                            Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
        }
    }
}