237 lines
8.9 KiB
Java
237 lines
8.9 KiB
Java
/*
|
|
* LEDD Project
|
|
* Copyright (C) 2015 LEDD Team
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package com.idlegandalf.ledd.fragments;
|
|
|
|
import android.app.Dialog;
|
|
import android.app.DialogFragment;
|
|
import android.content.DialogInterface;
|
|
import android.os.Bundle;
|
|
import android.support.design.widget.Snackbar;
|
|
import android.support.design.widget.TextInputLayout;
|
|
import android.support.v7.app.AlertDialog;
|
|
import android.text.Editable;
|
|
import android.text.TextWatcher;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.EditText;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.idlegandalf.ledd.ColorApplication;
|
|
import com.idlegandalf.ledd.R;
|
|
import com.idlegandalf.ledd.callbacks.AddControllerCallback;
|
|
import com.idlegandalf.ledd.components.Controller;
|
|
import com.idlegandalf.ledd.components.LedDDaemon;
|
|
import com.idlegandalf.ledd.helper.LedDHelper;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import java.io.IOException;
|
|
import java.text.NumberFormat;
|
|
import java.text.ParsePosition;
|
|
|
|
import butterknife.Bind;
|
|
import butterknife.ButterKnife;
|
|
|
|
public class AddControllerDialog extends DialogFragment implements DialogInterface.OnShowListener {
|
|
|
|
@Bind(R.id.input_i2c_layout)
|
|
TextInputLayout i2cLayout;
|
|
@Bind(R.id.input_i2c)
|
|
EditText i2cText;
|
|
@Bind(R.id.input_address_layout)
|
|
TextInputLayout addressLayout;
|
|
@Bind(R.id.input_address)
|
|
EditText addressText;
|
|
@Bind(R.id.input_channel_layout)
|
|
TextInputLayout channelLayout;
|
|
@Bind(R.id.input_channel)
|
|
EditText channelText;
|
|
LedDDaemon dDaemon;
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
View v = View.inflate(getActivity(), R.layout.fragment_addcontroller, null);
|
|
ButterKnife.bind(this, v);
|
|
|
|
dDaemon = new Gson().fromJson(getArguments().getString("daemon"), LedDDaemon.class);
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
builder.setView(v).setPositiveButton("Add", null).setNegativeButton("Cancel", null);
|
|
|
|
AlertDialog dialog = builder.create();
|
|
|
|
dialog.setOnShowListener(this);
|
|
|
|
return dialog;
|
|
}
|
|
|
|
@Override
|
|
public void onShow(final DialogInterface dialog) {
|
|
|
|
final AlertDialog alertDialog = (AlertDialog) dialog;
|
|
|
|
addressText.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
addressLayout.setError(null);
|
|
}
|
|
});
|
|
|
|
channelText.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
channelLayout.setError(null);
|
|
}
|
|
});
|
|
|
|
i2cText.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
i2cLayout.setError(null);
|
|
}
|
|
});
|
|
|
|
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
AddStripeDialog.instance.onResume();
|
|
alertDialog.dismiss();
|
|
}
|
|
});
|
|
|
|
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (i2cText.getText().toString().isEmpty() || !isNumeric(i2cText.getText().toString())) {
|
|
i2cLayout.setError("No valid i2c device number");
|
|
return;
|
|
}
|
|
|
|
if (!addressText.getText().toString().contains("0x") || !addressText.getText().toString().split("x")[1].matches("-?[0-9a-fA-F]+")) {
|
|
addressLayout.setError("No valid hexdecimal address");
|
|
return;
|
|
}
|
|
|
|
if (channelText.getText().toString().isEmpty() || !isNumeric(channelText.getText().toString()) && Integer.parseInt(channelText
|
|
.getText().toString()) <= 0) {
|
|
channelLayout.setError("No valid channel amount");
|
|
return;
|
|
}
|
|
|
|
Controller c = new Controller();
|
|
c.setAddress(addressText.getText().toString());
|
|
c.setI2c_device(Integer.parseInt(i2cText.getText().toString()));
|
|
c.setChannels(Integer.parseInt(channelText.getText().toString()));
|
|
|
|
LedDHelper helper = null;
|
|
try {
|
|
helper = ColorApplication.getInstance().getHelperForDaemon(dDaemon);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if (helper != null) {
|
|
try {
|
|
helper.addController(c, new AddControllerCallback() {
|
|
@Override
|
|
public void onControllerAdded(final Controller controller) {
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Snackbar.make(((ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content))
|
|
.getChildAt(0), "Added Controller (" + controller.getId() + ")", Snackbar.LENGTH_LONG).show();
|
|
}
|
|
});
|
|
|
|
dDaemon.getControllers().add(controller);
|
|
dismiss();
|
|
}
|
|
|
|
@Override
|
|
public void onAddFailed(final String msg, String detail) {
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Snackbar.make(((ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content))
|
|
.getChildAt(0), "Error: " + msg, Snackbar.LENGTH_LONG).show();
|
|
}
|
|
});
|
|
AddStripeDialog.instance.onResume();
|
|
dismiss();
|
|
}
|
|
|
|
@Override
|
|
public void onConnectionFailed(final String message) {
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Snackbar.make(((ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content))
|
|
.getChildAt(0), "Coudn't connect to" +
|
|
" daemon at " + dDaemon + ": " + message, Snackbar.LENGTH_LONG).show();
|
|
}
|
|
});
|
|
AddStripeDialog.instance.onResume();
|
|
dismiss();
|
|
}
|
|
});
|
|
} catch (JSONException | IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private static boolean isNumeric(String str) {
|
|
NumberFormat formatter = NumberFormat.getInstance();
|
|
ParsePosition pos = new ParsePosition(0);
|
|
formatter.parse(str, pos);
|
|
return str.length() == pos.getIndex();
|
|
}
|
|
}
|