|
|
|
@@ -0,0 +1,302 @@
|
|
|
|
|
package com.ando.miboxkeyboard.phone;
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.view.Gravity;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends Activity {
|
|
|
|
|
private static final String TAG = "AndoPhone";
|
|
|
|
|
private EditText ipEdit;
|
|
|
|
|
private EditText portEdit;
|
|
|
|
|
private EditText textEdit;
|
|
|
|
|
private TextView statusText;
|
|
|
|
|
private SharedPreferences prefs;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
prefs = getSharedPreferences("settings", MODE_PRIVATE);
|
|
|
|
|
|
|
|
|
|
LinearLayout root = new LinearLayout(this);
|
|
|
|
|
root.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
|
root.setPadding(32, 32, 32, 32);
|
|
|
|
|
|
|
|
|
|
TextView title = new TextView(this);
|
|
|
|
|
title.setText("Clavier MiBox Téléphone");
|
|
|
|
|
title.setTextSize(24);
|
|
|
|
|
title.setGravity(Gravity.CENTER);
|
|
|
|
|
root.addView(title, new LinearLayout.LayoutParams(-1, -2));
|
|
|
|
|
|
|
|
|
|
ipEdit = new EditText(this);
|
|
|
|
|
ipEdit.setHint("IP Mi Box");
|
|
|
|
|
ipEdit.setSingleLine(true);
|
|
|
|
|
ipEdit.setText(prefs.getString("ip", "192.168.1.8"));
|
|
|
|
|
root.addView(ipEdit, new LinearLayout.LayoutParams(-1, -2));
|
|
|
|
|
|
|
|
|
|
portEdit = new EditText(this);
|
|
|
|
|
portEdit.setHint("Port");
|
|
|
|
|
portEdit.setSingleLine(true);
|
|
|
|
|
portEdit.setText(prefs.getString("port", "8765"));
|
|
|
|
|
root.addView(portEdit, new LinearLayout.LayoutParams(-1, -2));
|
|
|
|
|
|
|
|
|
|
textEdit = new EditText(this);
|
|
|
|
|
textEdit.setHint("Texte à envoyer vers la Mi Box");
|
|
|
|
|
textEdit.setMinLines(5);
|
|
|
|
|
textEdit.setGravity(Gravity.TOP | Gravity.START);
|
|
|
|
|
root.addView(textEdit, new LinearLayout.LayoutParams(-1, 0, 1));
|
|
|
|
|
|
|
|
|
|
Button sendButton = new Button(this);
|
|
|
|
|
sendButton.setText("Envoyer le texte");
|
|
|
|
|
sendButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
saveSettings();
|
|
|
|
|
sendText(textEdit.getText().toString());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
root.addView(sendButton, new LinearLayout.LayoutParams(-1, -2));
|
|
|
|
|
|
|
|
|
|
LinearLayout row = new LinearLayout(this);
|
|
|
|
|
row.setOrientation(LinearLayout.HORIZONTAL);
|
|
|
|
|
|
|
|
|
|
Button enterButton = new Button(this);
|
|
|
|
|
enterButton.setText("Entrée");
|
|
|
|
|
enterButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
saveSettings();
|
|
|
|
|
callPostEndpoint("/enter", "");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
row.addView(enterButton, new LinearLayout.LayoutParams(0, -2, 1));
|
|
|
|
|
|
|
|
|
|
Button backspaceButton = new Button(this);
|
|
|
|
|
backspaceButton.setText("Effacer");
|
|
|
|
|
backspaceButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
saveSettings();
|
|
|
|
|
callPostEndpoint("/backspace", "");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
row.addView(backspaceButton, new LinearLayout.LayoutParams(0, -2, 1));
|
|
|
|
|
|
|
|
|
|
Button pingButton = new Button(this);
|
|
|
|
|
pingButton.setText("Tester");
|
|
|
|
|
pingButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
saveSettings();
|
|
|
|
|
callGetEndpoint("/ping");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
row.addView(pingButton, new LinearLayout.LayoutParams(0, -2, 1));
|
|
|
|
|
|
|
|
|
|
root.addView(row, new LinearLayout.LayoutParams(-1, -2));
|
|
|
|
|
|
|
|
|
|
LinearLayout keyboardRow = new LinearLayout(this);
|
|
|
|
|
keyboardRow.setOrientation(LinearLayout.HORIZONTAL);
|
|
|
|
|
|
|
|
|
|
Button pickerButton = new Button(this);
|
|
|
|
|
pickerButton.setText("Changer clavier Mi Box");
|
|
|
|
|
pickerButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
saveSettings();
|
|
|
|
|
callPostEndpoint("/picker", "");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
keyboardRow.addView(pickerButton, new LinearLayout.LayoutParams(0, -2, 1));
|
|
|
|
|
|
|
|
|
|
Button settingsButton = new Button(this);
|
|
|
|
|
settingsButton.setText("Paramètres clavier");
|
|
|
|
|
settingsButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
saveSettings();
|
|
|
|
|
callGetEndpoint("/settings");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
keyboardRow.addView(settingsButton, new LinearLayout.LayoutParams(0, -2, 1));
|
|
|
|
|
|
|
|
|
|
root.addView(keyboardRow, new LinearLayout.LayoutParams(-1, -2));
|
|
|
|
|
|
|
|
|
|
Button clearButton = new Button(this);
|
|
|
|
|
clearButton.setText("Vider le champ téléphone");
|
|
|
|
|
clearButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
textEdit.setText("");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
root.addView(clearButton, new LinearLayout.LayoutParams(-1, -2));
|
|
|
|
|
|
|
|
|
|
statusText = new TextView(this);
|
|
|
|
|
statusText.setText("Statut : prêt");
|
|
|
|
|
statusText.setPadding(0, 20, 0, 0);
|
|
|
|
|
root.addView(statusText, new LinearLayout.LayoutParams(-1, -2));
|
|
|
|
|
|
|
|
|
|
setContentView(root);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveSettings() {
|
|
|
|
|
prefs.edit()
|
|
|
|
|
.putString("ip", ipEdit.getText().toString().trim())
|
|
|
|
|
.putString("port", portEdit.getText().toString().trim())
|
|
|
|
|
.apply();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendText(String text) {
|
|
|
|
|
callPostEndpoint("/type", text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String baseUrl(String endpoint) throws Exception {
|
|
|
|
|
String ip = ipEdit.getText().toString().trim();
|
|
|
|
|
String port = portEdit.getText().toString().trim();
|
|
|
|
|
if (ip.startsWith("http://")) ip = ip.substring(7);
|
|
|
|
|
if (ip.startsWith("https://")) ip = ip.substring(8);
|
|
|
|
|
if (ip.endsWith("/")) ip = ip.substring(0, ip.length() - 1);
|
|
|
|
|
if (ip.contains(":")) {
|
|
|
|
|
return "http://" + ip + endpoint;
|
|
|
|
|
}
|
|
|
|
|
return "http://" + ip + ":" + port + endpoint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String previewUrl(String endpoint) {
|
|
|
|
|
try {
|
|
|
|
|
return baseUrl(endpoint);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return endpoint;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void callGetEndpoint(final String endpoint) {
|
|
|
|
|
setStatus("GET " + endpoint + " → " + previewUrl(endpoint));
|
|
|
|
|
Log.d(TAG, "GET " + previewUrl(endpoint));
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
|
try {
|
|
|
|
|
URL url = new URL(baseUrl(endpoint));
|
|
|
|
|
Log.d(TAG, "Opening POST " + url);
|
|
|
|
|
Log.d(TAG, "Opening GET " + url);
|
|
|
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
|
connection.setConnectTimeout(4000);
|
|
|
|
|
connection.setReadTimeout(4000);
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
|
|
|
|
|
int code = connection.getResponseCode();
|
|
|
|
|
String response = readResponse(connection);
|
|
|
|
|
if (code >= 200 && code < 300) {
|
|
|
|
|
Log.d(TAG, endpoint + " OK " + code + " " + response);
|
|
|
|
|
setStatus("OK : " + response);
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, endpoint + " HTTP " + code + " " + response);
|
|
|
|
|
setStatus("Erreur HTTP " + code + " : " + response);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, endpoint + " exception", e);
|
|
|
|
|
setStatus("Erreur : " + e.getClass().getSimpleName() + " - " + e.getMessage());
|
|
|
|
|
showToast("Connexion impossible. Vérifie IP, Wi-Fi et serveur Mi Box.");
|
|
|
|
|
} finally {
|
|
|
|
|
if (connection != null) connection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void callPostEndpoint(final String endpoint, final String body) {
|
|
|
|
|
setStatus("POST " + endpoint + " → " + previewUrl(endpoint));
|
|
|
|
|
Log.d(TAG, "POST " + previewUrl(endpoint));
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
|
try {
|
|
|
|
|
URL url = new URL(baseUrl(endpoint));
|
|
|
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
|
connection.setConnectTimeout(4000);
|
|
|
|
|
connection.setReadTimeout(4000);
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
|
|
|
|
|
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
connection.setFixedLengthStreamingMode(bytes.length);
|
|
|
|
|
OutputStream out = connection.getOutputStream();
|
|
|
|
|
out.write(bytes);
|
|
|
|
|
out.flush();
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
|
|
int code = connection.getResponseCode();
|
|
|
|
|
String response = readResponse(connection);
|
|
|
|
|
if (code >= 200 && code < 300) {
|
|
|
|
|
Log.d(TAG, endpoint + " OK " + code + " " + response);
|
|
|
|
|
setStatus("OK : " + response);
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, endpoint + " HTTP " + code + " " + response);
|
|
|
|
|
setStatus("Erreur HTTP " + code + " : " + response);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, endpoint + " exception", e);
|
|
|
|
|
setStatus("Erreur : " + e.getClass().getSimpleName() + " - " + e.getMessage());
|
|
|
|
|
showToast("Connexion impossible. Vérifie IP, Wi-Fi et clavier actif.");
|
|
|
|
|
} finally {
|
|
|
|
|
if (connection != null) connection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String readResponse(HttpURLConnection connection) {
|
|
|
|
|
try {
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
if (sb.length() > 0) sb.append(" ");
|
|
|
|
|
sb.append(line);
|
|
|
|
|
}
|
|
|
|
|
reader.close();
|
|
|
|
|
return sb.toString();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setStatus(final String status) {
|
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
statusText.setText("Statut : " + status);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showToast(final String message) {
|
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|