405 lines
15 KiB
Java
405 lines
15 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.AdapterView;
|
|
import android.widget.ArrayAdapter;
|
|
import android.widget.EditText;
|
|
import android.widget.ImageButton;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.Spinner;
|
|
import android.widget.Toast;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.idlegandalf.ledd.ColorActivity;
|
|
import com.idlegandalf.ledd.ColorApplication;
|
|
import com.idlegandalf.ledd.R;
|
|
import com.idlegandalf.ledd.callbacks.AddStripeCallback;
|
|
import com.idlegandalf.ledd.callbacks.StripesCallback;
|
|
import com.idlegandalf.ledd.components.Controller;
|
|
import com.idlegandalf.ledd.components.LedDDaemon;
|
|
import com.idlegandalf.ledd.components.LedStripe;
|
|
import com.idlegandalf.ledd.helper.LedDHelper;
|
|
|
|
import java.text.NumberFormat;
|
|
import java.text.ParsePosition;
|
|
import java.util.List;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.ButterKnife;
|
|
import butterknife.OnClick;
|
|
|
|
public class AddStripeDialog extends DialogFragment implements DialogInterface.OnShowListener {
|
|
|
|
@BindView(R.id.spinner_daemon)
|
|
Spinner daemonSpinner;
|
|
@BindView(R.id.spinner_controller)
|
|
Spinner controllerSpinner;
|
|
@BindView(R.id.choose_controller_lay)
|
|
RelativeLayout controllerLayout;
|
|
@BindView(R.id.input_stripe_name_lay)
|
|
TextInputLayout stripeNameLay;
|
|
@BindView(R.id.input_stripe_name)
|
|
EditText stripeNameText;
|
|
@BindView(R.id.input_channel_r_lay)
|
|
TextInputLayout channelRLay;
|
|
@BindView(R.id.input_channel_r)
|
|
EditText channelR;
|
|
@BindView(R.id.input_channel_g_lay)
|
|
TextInputLayout channelGLay;
|
|
@BindView(R.id.input_channel_g)
|
|
EditText channelG;
|
|
@BindView(R.id.input_channel_b_lay)
|
|
TextInputLayout channelBLay;
|
|
@BindView(R.id.input_channel_b)
|
|
EditText channelB;
|
|
@BindView(R.id.stripe_mapping_lay)
|
|
LinearLayout stripeMapping;
|
|
@BindView(R.id.imgbuttn_togglechannel_r)
|
|
ImageButton channelRBttn;
|
|
@BindView(R.id.imgbuttn_togglechannel_g)
|
|
ImageButton channelGBttn;
|
|
@BindView(R.id.imgbuttn_togglechannel_b)
|
|
ImageButton channelBBttn;
|
|
ArrayAdapter<LedDDaemon> daemonArrayAdapter;
|
|
ArrayAdapter<Controller> controllerArrayAdapter;
|
|
boolean mDaemonSelected = false;
|
|
boolean mControllerSelected = false;
|
|
|
|
private static boolean isNumeric(String str) {
|
|
NumberFormat formatter = NumberFormat.getInstance();
|
|
ParsePosition pos = new ParsePosition(0);
|
|
formatter.parse(str, pos);
|
|
return str.length() == pos.getIndex();
|
|
}
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
View v = View.inflate(getActivity(), R.layout.fragment_addstripe, null);
|
|
ButterKnife.bind(this, v);
|
|
|
|
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;
|
|
toggleAll(controllerLayout, false);
|
|
channelRBttn.setEnabled(false);
|
|
channelGBttn.setEnabled(false);
|
|
channelBBttn.setEnabled(false);
|
|
|
|
stripeNameText.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) {
|
|
stripeNameLay.setError(null);
|
|
}
|
|
});
|
|
|
|
channelR.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) {
|
|
if (s.toString().isEmpty()) {
|
|
channelRBttn.setEnabled(false);
|
|
} else {
|
|
channelRBttn.setEnabled(true);
|
|
}
|
|
}
|
|
});
|
|
channelG.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) {
|
|
if (s.toString().isEmpty()) {
|
|
channelGBttn.setEnabled(false);
|
|
} else {
|
|
channelGBttn.setEnabled(true);
|
|
}
|
|
}
|
|
});
|
|
channelB.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) {
|
|
if (s.toString().isEmpty()) {
|
|
channelBBttn.setEnabled(false);
|
|
} else {
|
|
channelBBttn.setEnabled(true);
|
|
}
|
|
}
|
|
});
|
|
|
|
ImageButton.OnClickListener imgListener = v -> {
|
|
switch (v.getId()) {
|
|
case R.id.imgbuttn_togglechannel_r:
|
|
testChannel(channelR, (ImageButton) v);
|
|
break;
|
|
case R.id.imgbuttn_togglechannel_g:
|
|
testChannel(channelG, (ImageButton) v);
|
|
break;
|
|
case R.id.imgbuttn_togglechannel_b:
|
|
testChannel(channelB, (ImageButton) v);
|
|
break;
|
|
}
|
|
};
|
|
|
|
channelRBttn.setOnClickListener(imgListener);
|
|
channelGBttn.setOnClickListener(imgListener);
|
|
channelBBttn.setOnClickListener(imgListener);
|
|
|
|
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(v -> alertDialog.dismiss());
|
|
|
|
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
|
|
if (!mDaemonSelected) {
|
|
Toast.makeText(getActivity(), "Please select Daemon and Controller", Toast.LENGTH_LONG).show();
|
|
return;
|
|
}
|
|
|
|
if (stripeNameText.getText().toString().isEmpty()) {
|
|
stripeNameLay.setError("Please set the Stripe name");
|
|
return;
|
|
}
|
|
|
|
if (channelR.getText().toString().isEmpty() || channelG.getText().toString().isEmpty() || channelB.getText().toString().isEmpty()) {
|
|
Toast.makeText(getActivity(), "Please add mapping for all channels", Toast.LENGTH_LONG).show();
|
|
return;
|
|
}
|
|
|
|
LedStripe stripe = new LedStripe(Integer.parseInt(channelR.getText().toString()), Integer.parseInt(channelG.getText().toString()),
|
|
Integer.parseInt(channelB.getText().toString()), stripeNameText.getText().toString());
|
|
stripe.setLedDDaemon((LedDDaemon) daemonSpinner.getSelectedItem());
|
|
stripe.setController((Controller) controllerSpinner.getSelectedItem());
|
|
|
|
LedDHelper helper = ColorApplication.getInstance().getHelperForDaemon((LedDDaemon) daemonSpinner.getSelectedItem());
|
|
|
|
if (helper != null) {
|
|
helper.addStripe(stripe, new AddStripeCallback() {
|
|
@Override
|
|
public void onAddSuccessfully(final LedStripe stripe) {
|
|
ColorActivity activity = ((ColorActivity) getActivity());
|
|
activity.refreshStripes();
|
|
|
|
getActivity().runOnUiThread(() -> Snackbar.make(((ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0), String.format(getActivity().getString(R.string.snackbar_added_stripe_id), stripe.getId()), Snackbar.LENGTH_LONG).show());
|
|
dismiss();
|
|
}
|
|
|
|
@Override
|
|
public void onAddFailed(int code, String msg) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onConnectionFailed(final String message) {
|
|
getActivity().runOnUiThread(() -> Snackbar.make(((ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0), getActivity().getString(R.string.snackbar_failed_add_stripe) + message, Snackbar.LENGTH_LONG).show());
|
|
dismiss();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
daemonArrayAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.spinner_item);
|
|
daemonArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
|
|
daemonArrayAdapter.addAll(ColorApplication.getInstance().getDaemons());
|
|
|
|
controllerArrayAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.spinner_item);
|
|
controllerArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
|
|
|
|
daemonSpinner.setAdapter(daemonArrayAdapter);
|
|
controllerSpinner.setAdapter(controllerArrayAdapter);
|
|
|
|
daemonSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
|
@Override
|
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
|
mDaemonSelected = true;
|
|
toggleAll(controllerLayout, true);
|
|
refreshController((LedDDaemon) daemonSpinner.getSelectedItem());
|
|
}
|
|
|
|
@Override
|
|
public void onNothingSelected(AdapterView<?> parent) {
|
|
toggleAll(controllerLayout, false);
|
|
controllerArrayAdapter.clear();
|
|
mDaemonSelected = false;
|
|
mControllerSelected = false;
|
|
}
|
|
});
|
|
|
|
controllerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
|
@Override
|
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
|
mControllerSelected = true;
|
|
}
|
|
|
|
@Override
|
|
public void onNothingSelected(AdapterView<?> parent) {
|
|
mControllerSelected = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
@OnClick(R.id.imgbuttn_addcontroller)
|
|
public void addController(ImageButton button) {
|
|
AddControllerDialog dialog = new AddControllerDialog();
|
|
Bundle b = new Bundle();
|
|
b.putString("daemon", new Gson().toJson(daemonSpinner.getSelectedItem()));
|
|
dialog.setArguments(b);
|
|
dialog.show(getActivity().getFragmentManager(), "");
|
|
}
|
|
|
|
@OnClick(R.id.imgbuttn_adddaemon)
|
|
public void addDaemon(ImageButton button) {
|
|
new AddDaemonDialog().show(getActivity().getFragmentManager(), "");
|
|
}
|
|
|
|
private void toggleAll(ViewGroup v, boolean status) {
|
|
for (int i = 0; i < v.getChildCount(); i++) {
|
|
v.getChildAt(i).setEnabled(status);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
ColorApplication.getInstance().onResume();
|
|
|
|
getActivity().runOnUiThread(() -> {
|
|
if (daemonArrayAdapter != null) {
|
|
daemonArrayAdapter.clear();
|
|
daemonArrayAdapter.addAll(ColorApplication.getInstance().getDaemons());
|
|
}
|
|
|
|
if (controllerArrayAdapter != null && mDaemonSelected) {
|
|
refreshController((LedDDaemon) daemonSpinner.getSelectedItem());
|
|
}
|
|
});
|
|
}
|
|
|
|
private void testChannel(EditText text, ImageButton button) {
|
|
if (mDaemonSelected && mControllerSelected) {
|
|
if (!text.getText().toString().isEmpty() && isNumeric(text.getText().toString())) {
|
|
int val;
|
|
boolean toggle = false;
|
|
if (text.getTag() != null) toggle = (boolean) text.getTag();
|
|
if (!toggle) {
|
|
val = 1;
|
|
toggle = true;
|
|
button.setImageResource(R.drawable.ic_visibility_black_48dp);
|
|
} else {
|
|
val = 0;
|
|
toggle = false;
|
|
button.setImageResource(R.drawable.ic_visibility_off_black_48dp);
|
|
}
|
|
text.setTag(toggle);
|
|
|
|
LedDHelper helper = ColorApplication.getInstance().getHelperForDaemon((LedDDaemon) daemonSpinner.getSelectedItem());
|
|
|
|
|
|
if (helper != null) {
|
|
helper.testChannel((Controller) controllerSpinner.getSelectedItem(), Integer.parseInt(text.getText().toString()), val);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void refreshController(final LedDDaemon ledDDaemon) {
|
|
controllerArrayAdapter.clear();
|
|
LedDHelper helper = ColorApplication.getInstance().getHelperForDaemon(ledDDaemon);
|
|
|
|
if (helper != null) {
|
|
helper.getStripes(new StripesCallback() {
|
|
@Override
|
|
public void onSuccess(List<LedStripe> stripes) {
|
|
getActivity().runOnUiThread(() -> {
|
|
controllerArrayAdapter.addAll(ledDDaemon.getControllers());
|
|
toggleAll(stripeMapping, true);
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onGetFailed(int code, String message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onConnectionFailed(String message) {
|
|
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|