package com.example.appsmartmoobile;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Formatter;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class App extends Activity {
db_rute helper=null;
double total;
String totalgab="";
EditText txtasal,txtket,txtjarak,txtrute;
double jarakA,jarakB;
int jd=10;
String[]arR;
Button btnGo;
int sukses;
Spinner spinTujuan;
String[]pilKat={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app);
helper=new db_rute(this);
txtasal= (EditText) findViewById(R.id.txtasal);
txtket= (EditText) findViewById(R.id.txtket);
txtjarak= (EditText) findViewById(R.id.txtjarak);
txtrute= (EditText) findViewById(R.id.txtrute);
spinTujuan= (Spinner) findViewById(R.id.spinTujuan);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, pilKat);
spinTujuan.setAdapter(adapter);
btnGo=(Button)findViewById(R.id.btnGo);
btnGo.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String ip2 = getLocalIpAddress1();Log.v("IP2",ip2);
String ip3 = getLocalIpAddress2();Log.v("IP3",ip3);
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip1 = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
String asal=txtasal.getText().toString();
String tujuan=spinTujuan.getSelectedItem().toString();
sendMessage(ip1+"="+ip2+"="+ip3+"="+ asal+" "+tujuan);
// mulai(asal,tujuan);
}});
Button btnClear=(Button)findViewById(R.id.btnClear);
btnClear.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
txtasal.setText("");
txtket.setText("");
txtjarak.setText("");
txtrute.setText("");
spinTujuan.setSelection(0);
}});
runUdpServer();
}
public String getLocalIpAddress1(){
WifiManager wifiMan = (WifiManager)this.getSystemService(this.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
int ipAddress = wifiInf.getIpAddress();
String ip = String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
return ip;
}
public String getLocalIpAddress2(){
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (Exception ex) {
Log.e("IP Address", ex.toString());
}
return null;
}
void sendMessage(final String message) {
final Handler handler = new Handler();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
InetAddress serverAddr = InetAddress.getByName("192.168.1.2");
int port = 8888;//8888; SENT 8888
ds= new DatagramSocket(port);
DatagramPacket dp;
dp = new DatagramPacket(message.getBytes(), message.length(), serverAddr, port);
ds.send(dp);
// Toast.makeText(getApplicationContext(),stringData,Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
handler.post(new Runnable() {
@Override
public void run() {
// String s =spinTujuan.getSelectedItem().toString();
// if (stringData.trim().length() != 0){
// // Toast.makeText(getApplicationContext(),s + "\nFrom Server : " + stringData,Toast.LENGTH_LONG).show();
// }
}
});
}
});
thread.start();
}
private void runUdpServer() {
new Thread(new Runnable() {
public void run() {
String lText;
try {
int port = 8889;//WAIT 8889
DatagramSocket dsocket = new DatagramSocket(port);
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// int rec=packet.getPort();
// String addr=packet.getAddress().getHostName();
// Log.v("UDPQ", addr+"+"+String.valueOf(rec));
while (true) {
dsocket.receive(packet);
lText = new String(buffer, 0, packet.getLength());
Log.v("UDP packet received", lText);
txtrute.setText(lText);
packet.setLength(buffer.length);
int rec2=packet.getPort();
String addr2=packet.getAddress().getHostName();
Log.v("UDPQ2", addr2+"+"+String.valueOf(rec2));
// DatagramPacket dp = new DatagramPacket(lText.getBytes(), lText.length());
// dsocket.send(dp);
}
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
Log.v("UDPQ2", "ERR");
}
}
}).start();
}
public void gagal(){
new AlertDialog.Builder(this)
.setTitle("Gagal Login")
.setMessage("Silakan Cek Account Anda Kembali")
.setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public void sukses(String item,String ex){
new AlertDialog.Builder(this)
.setTitle("Sukses "+ex)
.setMessage(ex+" data "+item+" Berhasil")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
// Intent i = new Intent(login.this,MainActivity.class);
// i.putExtra("xmlbio", xmlbio);
// startActivity(i);
// finish();
}})
.show();
}
public void lengkapi(String item){
new AlertDialog.Builder(this)
.setTitle("Lengkapi Data")
.setMessage("Silakan lengkapi data "+item)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public void gagal(String item){
new AlertDialog.Builder(this)
.setTitle("Gagal Login")
.setMessage("Login "+item+" ,, Gagal")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
void mulai(String posAwal,String posAhir) {
Node A = new Node("A",0);
Node B = new Node("B",0);
Node C = new Node("C",0);
Node D = new Node("D",0);
Node E = new Node("E",0);
Node F = new Node("F",0);
Node G = new Node("G",0);
Node H = new Node("H",0);
Node I = new Node("I",0);
Node J = new Node("J",0);
Node K = new Node("K",0);
Node L = new Node("L",0);
Node M = new Node("M",0);
Node N = new Node("N",0);
Node O = new Node("O",0);
A.connect(new Edge(D,25));
B.connect(new Edge(F,25));
C.connect(new Edge(D,25));
D.connect(new Edge(A,25));
D.connect(new Edge(C,25));
D.connect(new Edge(E,25));
D.connect(new Edge(J,25));
E.connect(new Edge(F,25));
E.connect(new Edge(H,12));//12.5
E.connect(new Edge(D,25));
F.connect(new Edge(B,25));
F.connect(new Edge(E,25));
F.connect(new Edge(G,25));
F.connect(new Edge(L,25));
G.connect(new Edge(F,25));
H.connect(new Edge(E,12));
H.connect(new Edge(K,12));
I.connect(new Edge(J,25));
J.connect(new Edge(D,25));
J.connect(new Edge(I,25));
J.connect(new Edge(K,25));
J.connect(new Edge(N,100));
K.connect(new Edge(J,25));
K.connect(new Edge(H,12));
K.connect(new Edge(L,25));
L.connect(new Edge(F,25));
L.connect(new Edge(K,25));
L.connect(new Edge(M,25));
L.connect(new Edge(O,100));
M.connect(new Edge(L,25));
N.connect(new Edge(J,100));
O.connect(new Edge(L,100));
//=======================================================
jd=31;
arR=new String[jd];
int n=0;
arR[n]="AD";n++;
arR[n]="BF";n++;
arR[n]="CD";n++;
arR[n]="DA";n++;
arR[n]="DC";n++;
arR[n]="DE";n++;
arR[n]="DJ";n++;
arR[n]="EF";n++;
arR[n]="EH";n++;
arR[n]="ED";n++;
arR[n]="FB";n++;
arR[n]="FE";n++;
arR[n]="FG";n++;
arR[n]="FL";n++;
arR[n]="GF";n++;
arR[n]="HE";n++;
arR[n]="HK";n++;
arR[n]="IJ";n++;
arR[n]="JD";n++;
arR[n]="JI";n++;
arR[n]="JK";n++;
arR[n]="JN";n++;
arR[n]="KJ";n++;
arR[n]="KH";n++;
arR[n]="KL";n++;
arR[n]="LF";n++;
arR[n]="LK";n++;
arR[n]="LM";n++;
arR[n]="ML";n++;
arR[n]="NJ";n++;
arR[n]="OL";n++;
Node n1=A;
if(posAwal.equals("A")){n1=A;}
else if(posAwal.equals("B")){n1=B;}
else if(posAwal.equals("C")){n1=C;}
else if(posAwal.equals("D")){n1=D;}
else if(posAwal.equals("E")){n1=E;}
else if(posAwal.equals("F")){n1=F;}
else if(posAwal.equals("G")){n1=G;}
else if(posAwal.equals("H")){n1=H;}
else if(posAwal.equals("I")){n1=I;}
else if(posAwal.equals("J")){n1=J;}
else if(posAwal.equals("K")){n1=K;}
else if(posAwal.equals("L")){n1=L;}
else if(posAwal.equals("M")){n1=M;}
else if(posAwal.equals("N")){n1=N;}
else if(posAwal.equals("O")){n1=O;}
Node n2=B;
if(posAhir.equals("A")){n2=A;}
else if(posAhir.equals("B")){n2=B;}
else if(posAhir.equals("C")){n2=C;}
else if(posAhir.equals("D")){n2=D;}
else if(posAhir.equals("E")){n2=E;}
else if(posAhir.equals("F")){n2=F;}
else if(posAhir.equals("G")){n2=G;}
else if(posAhir.equals("H")){n2=H;}
else if(posAhir.equals("I")){n2=I;}
else if(posAhir.equals("J")){n2=J;}
else if(posAhir.equals("K")){n2=K;}
else if(posAhir.equals("L")){n2=L;}
else if(posAhir.equals("M")){n2=M;}
else if(posAhir.equals("N")){n2=N;}
else if(posAhir.equals("O")){n2=O;}
//=================================================
appDijkstra app=new appDijkstra();
String gab=app.Dijkstrasearch(n1, n2);
total=app.getCosttotal();
totalgab=app.getCostgab();
String sjaraknya="+- "+String.valueOf(total)+" M #"+totalgab;
String[]ar=gab.split("#");
int p=ar.length;
String mrute="Posisi Awal: "+posAwal+" s/d "+posAhir+" =>RUTE:\n";
for(int i=0;i<p;i++){
String kota=ar[i];
mrute+=kota+" -> ";
}//i
mrute=mrute.substring(0,mrute.length()-3);
cetak("MRUTE1:"+gab);
cetak("MRUTE2:"+mrute);
cetak("MRUTE3:"+sjaraknya);
txtjarak.setText(String.valueOf(total)+" cm");
txtrute.setText(mrute);
txtket.setText(totalgab);
String asal=txtasal.getText().toString();
String tujuan=spinTujuan.getSelectedItem().toString();
//insertrute(String asal, String tujuan, String rute, String jarak, String keterangan ){
helper.insertrute(asal,tujuan, totalgab,String.valueOf(total),mrute);
sukses(asal+" Menuju "+tujuan);
}//void
void cetak(String info){
Log.v("Cetak","Info "+info);
}
public void sukses(String psn){
new AlertDialog.Builder(this)
.setTitle("Sukses terkirim")
.setMessage("Posisi "+psn+" sudah terkirim dan tersimpan ....")
.setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Tidak ada komentar:
Posting Komentar