Initial commit from non tracked working directory
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
package com.idlegandalf.ledd.fragments;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.idlegandalf.ledd.R;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class StepOneFragment extends Fragment {
|
||||
@Bind(R.id.step2_back)
|
||||
Button mBack;
|
||||
@Bind(R.id.ipSelect)
|
||||
EditText mAutoIP;
|
||||
@Bind(R.id.step2_next)
|
||||
Button mTest;
|
||||
@Bind(R.id.portSelect)
|
||||
EditText mAutoPort;
|
||||
ArrayList<String> mIPList = new ArrayList<>();
|
||||
boolean isAutoDetected = false;
|
||||
|
||||
|
||||
public static StepOneFragment newInstance(String addrs) {
|
||||
Bundle args = new Bundle();
|
||||
|
||||
args.putString("ips", addrs);
|
||||
|
||||
StepOneFragment fragment = new StepOneFragment();
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View v = inflater.inflate(R.layout.fragment_step1, container, false);
|
||||
ButterKnife.bind(this, v);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
String ips = getArguments().getString("ips");
|
||||
|
||||
if (ips != null) {
|
||||
if (!ips.isEmpty()) {
|
||||
for (String string : ips.split(";")) {
|
||||
if (string.isEmpty()) {
|
||||
System.out.println("added addr " + string);
|
||||
mIPList.add(string);
|
||||
}
|
||||
}
|
||||
if (mIPList.size() > 0) isAutoDetected = true;
|
||||
}
|
||||
}
|
||||
|
||||
mBack.setOnClickListener(nextClickListener);
|
||||
mTest.setOnClickListener(nextClickListener);
|
||||
}
|
||||
|
||||
private OnClickListener nextClickListener = new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
switch (v.getId()) {
|
||||
case R.id.step2_back:
|
||||
getActivity().onBackPressed();
|
||||
break;
|
||||
case R.id.step2_next:
|
||||
System.out.println("forward calling!");
|
||||
testController();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private void testController() {
|
||||
ConnectivityManager connManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||
|
||||
if (mWifi.isConnected()) {
|
||||
setInputState(false);
|
||||
if (!isValidAddrInput(mAutoIP, mAutoPort)) {
|
||||
setInputState(true);
|
||||
return;
|
||||
}
|
||||
getActivity().setProgressBarIndeterminateVisibility(true);
|
||||
getActivity().setProgressBarVisibility(true);
|
||||
new pokeController().execute(mAutoIP.toString(), mAutoPort.toString());
|
||||
} else {
|
||||
Toast.makeText(getActivity().getApplicationContext(), "please connect WiFi", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
private class pokeController extends AsyncTask<String, Void, Boolean> {
|
||||
protected Boolean doInBackground(String... ippo) {
|
||||
try {
|
||||
InetAddress controller = InetAddress.getByName(ippo[0]);
|
||||
int port = Integer.parseInt(ippo[1]);
|
||||
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
String hstr = new JSONObject().put("action", 5).toString();
|
||||
|
||||
DatagramPacket hello = new DatagramPacket(hstr.getBytes(), hstr.getBytes().length);
|
||||
|
||||
hello.setPort(port);
|
||||
socket.setSoTimeout(100);
|
||||
|
||||
if (!controller.isReachable(50)) {
|
||||
socket.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
hello.setAddress(controller);
|
||||
socket.send(hello);
|
||||
|
||||
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
|
||||
try {
|
||||
socket.receive(packet);
|
||||
} catch (SocketTimeoutException e) {
|
||||
socket.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
Toast.makeText(getActivity().getApplicationContext(), new String(packet.getData(), 0, packet.getLength()), Toast.LENGTH_LONG).show();
|
||||
|
||||
JSONObject resjson = new JSONObject(new String(packet.getData(), 0, packet.getLength()));
|
||||
|
||||
if (resjson.getInt("action") != 5) {
|
||||
socket.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
socket.close();
|
||||
} catch (SocketException e1) {
|
||||
return false;
|
||||
} catch (IOException e1) {
|
||||
return false;
|
||||
} catch (JSONException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void onProgressUpdate(Void... progress) {
|
||||
|
||||
}
|
||||
|
||||
protected void onPostExecute(Boolean result) {
|
||||
getActivity().setProgressBarIndeterminateVisibility(false);
|
||||
getActivity().setProgressBarVisibility(false);
|
||||
setInputState(true);
|
||||
if (result) {
|
||||
Snackbar.make(getActivity().findViewById(R.id.linlayp), "found daemon running on " +
|
||||
"address " + mAutoIP.toString() + ":" + mAutoPort.toString(), Snackbar.LENGTH_LONG).show();
|
||||
} else {
|
||||
Snackbar.make(getActivity().findViewById(R.id.linlayp), "No running daemon " + "found", Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidIPFromEditText(EditText atv) {
|
||||
String exip[] = atv.toString().trim().split("\\.");
|
||||
if (exip.length != 4) return false;
|
||||
|
||||
for (String string : exip) {
|
||||
if (!string.isEmpty()) {
|
||||
try {
|
||||
if (Integer.parseInt(string) < 0 || Integer.parseInt(string) > 255) return false;
|
||||
} catch (NumberFormatException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isValidPortFromEditText(EditText atv) {
|
||||
return !atv.toString().isEmpty() && Integer.parseInt(atv.toString()) > 1023 && Integer.parseInt(atv.toString()) <= 65535;
|
||||
}
|
||||
|
||||
private boolean isValidAddrInput(EditText ipatv, EditText portatv) {
|
||||
if (!isValidIPFromEditText(ipatv)) {
|
||||
Snackbar.make(getActivity().findViewById(R.id.linlayp), "Please enter valid IP " + "address", Snackbar.LENGTH_LONG).show();
|
||||
|
||||
return false;
|
||||
}
|
||||
if (!isValidPortFromEditText(portatv)) {
|
||||
Snackbar.make(getActivity().findViewById(R.id.linlayp), "Please enter valid port " + "(1024-65535)", Snackbar.LENGTH_LONG).show();
|
||||
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void setInputState(boolean state) {
|
||||
mAutoIP.setEnabled(state);
|
||||
mAutoPort.setEnabled(state);
|
||||
mBack.setEnabled(state);
|
||||
mTest.setEnabled(state);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user