import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.content.Context;
import android.util.Log;
import org.json.JSONException;
public class Menu_utama extends AppCompatActivity {
String ip="",kategori="",PESAN="";
JSONParser jParser = new JSONParser();
JSONArray myJSON = null;
int adabaru=0;
Handler mHandler;
private static final String TAG_SUKSES = "sukses";
private static final String TAG_record = "record";
String id="", name_user,email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_utama);
ip=jParser.getIP();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Menu_utama.this);
Boolean Registered = sharedPref.getBoolean("Registered", false);
if (!Registered){
finish();
}else {
id= sharedPref.getString("id", "");
name_user= sharedPref.getString("name_user", "");
email = sharedPref.getString("email", "");
}
this.mHandler = new Handler();
m_Runnable.run();
}
private final Runnable m_Runnable = new Runnable() {
public void run() {
// Toast.makeText(Menu_utama.this,"cek in runnable",Toast.LENGTH_SHORT).show();
// addNotification();
new load().execute();
Menu_utama.this.mHandler.postDelayed(m_Runnable,5000);
}
};
private void addNotification1() {
Toast.makeText(Menu_utama.this,"Info "+PESAN,Toast.LENGTH_SHORT).show();
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_warning2)
.setContentTitle("WARNING, Ada Serangan ! ")
.setContentText(PESAN);
Intent notificationIntent = new Intent(this, Notif.class);//eventList
notificationIntent.putExtra("kategori", kategori);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
private void addNotification2() {
Toast.makeText(Menu_utama.this,"Info "+PESAN,Toast.LENGTH_SHORT).show();
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_kontak)
.setContentTitle("SELAMAT, Syukurillah! ")
.setContentText(PESAN);
Intent notificationIntent = new Intent(this, Notif.class);//eventList
notificationIntent.putExtra("kategori", "Info ok");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Logout");//.setIcon(R.drawable.ic_launcher_background);
// menu.add(0, 1, 0, "Back");//.setIcon(R.drawable.ic_launcher);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
keluarYN();
return true;
// case 1:
// finish();
// return true;
}
return false;
}
public void keluar(){
new AlertDialog.Builder(this)
.setTitle("Menutup Aplikasi")
.setMessage("Terimakasih... Anda Telah Menggunakan Aplikasi Ini")
.setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
finish();
}})
.show();
}
public void keluarYN(){
AlertDialog.Builder ad=new AlertDialog.Builder(Menu_utama.this);
ad.setTitle("Konfirmasi");
ad.setMessage("Apakah benar ingin keluar?");
ad.setPositiveButton("OK",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
keluar();
}});
ad.setNegativeButton("No",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0, int arg1) {
}});
ad.show();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
keluarYN();
return true;
}
return super.onKeyDown(keyCode, event);
}
class load extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(ip+"cek.php", "GET", params);
Log.d("show: ", json.toString());
try {
adabaru= json.getInt("sukses");
if(adabaru==1){
JSONArray myObj = json.getJSONArray(TAG_record); // JSON Array
final JSONObject myJSON = myObj.getJSONObject(0);
runOnUiThread(new Runnable() {
public void run() {
try {
PESAN=myJSON.getString("PESAN");
kategori="TCP";
if(PESAN.indexOf("UDP")>=0){kategori="UDP";}
else if(PESAN.indexOf("ICMP")>=0){kategori="ICMP";}
}
catch (JSONException e) {e.printStackTrace();}
}});
}
}
catch (JSONException e) {e.printStackTrace();}
return null;
}
protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
public void run() {
//Log.v("BACA0", "CEK DATA.....BARU");
if (adabaru==1){
adabaru=0;
//Log.v("BACA", "ADA DATA.....BARU:"+PESAN);
if(PESAN.indexOf("Aman")>0) {
addNotification2();
MediaPlayer mp = MediaPlayer.create(Menu_utama.this, R.raw.notif);
if(!mp.isPlaying()) {
mp.start();
//mp.setLooping(true);
}
}
else{
addNotification1();
MediaPlayer mp = MediaPlayer.create(Menu_utama.this, R.raw.notif);
if(!mp.isPlaying()) {
mp.start();
//mp.setLooping(true);
}
}
Toast.makeText(Menu_utama.this,"Ada Data Baru="+PESAN,Toast.LENGTH_SHORT).show();
}
}
});}
}
//
// public void createNotification(View view) {
// // Prepare intent which is triggered if the
// // notification is selected
// Intent intent = new Intent(this, Notif.class);
// PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
//
// // Build notification
// // Actions are just fake
// Notification noti = new Notification.Builder(this)
// .setContentTitle("New mail from " + "test@gmail.com")
// .setContentText("Subject").setSmallIcon(R.drawable.ic_grafik)
// .setContentIntent(pIntent)
// .addAction(R.drawable.icon, "Call", pIntent)
// .addAction(R.drawable.icon, "More", pIntent)
// .addAction(R.drawable.icon, "And more", pIntent).build();
// NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// // hide the notification after its selected
// noti.flags |= Notification.FLAG_AUTO_CANCEL;
//
// notificationManager.notify(0, noti);
//
// }
}
+++++++++++++++++++++++++
cek.php
<?php
require_once"koneksivar.php";
mysql_connect($DBServer, $DBUser, $DBPass);
mysql_select_db($DBName);
$respon = array();
$sql="select * from `tb_temp` order by `id` desc";
$q=mysql_query($sql);
$jum=mysql_num_rows($q);
if($jum > 0){
$respon["record"] = array();
$d=mysql_fetch_array($q);
//Full texts id name date time ip_src ip_dst action note
$sid=$d["id"];
$cid=$d["name"];
$signature=$d["date"];
$sig_name=$d["note"];
$ip_src=$d["ip_src"];
$ip_dst=$d["ip_dst"];
$ip_proto=$d["action"];
$timestamp=$d["date"] ." ". $d["time"];
$ss="select * from tb_temp2";
$qq=mysql_query($ss);
$ds=mysql_fetch_array($qq);
$waktu=$ds["waktu"];
if($waktu==$timestamp){
$sql="select * from `tb_temp3`";
$q=mysql_query($sql);
$jum=mysql_num_rows($q);
if($jum > 0){
$respon["record"] = array();
$record = array();
$record["PESAN"] = "Kondisi Aman terjaga....";
array_push($respon["record"], $record);
$respon["sukses"] = 1;
$respon["pesan"] = "$jum record";
echo json_encode($respon);
$qq=mysql_query("truncate tb_temp3");
}
else{
$respon["record"]="";
$respon["sukses"] = 0;
$respon["pesan"] = "0 record";
echo json_encode($respon);
}
}
else{
$record = array();
$record["PESAN"] = "Ada serangan masuk ".$sig_name." dari IP: ".$ip_src;
array_push($respon["record"], $record);
$respon["sukses"] = 1;
$respon["pesan"] = "$jum record";
echo json_encode($respon);
$qq=mysql_query("update tb_temp2 set waktu='$timestamp'");
}
}
else {
$sql="select * from `tb_temp3`";
$q=mysql_query($sql);
$jum=mysql_num_rows($q);
if($jum > 0){
$respon["record"] = array();
$record = array();
$record["PESAN"] = "Kondisi Aman terjaga....";
array_push($respon["record"], $record);
$respon["sukses"] = 1;
$respon["pesan"] = "$jum record";
echo json_encode($respon);
$qq=mysql_query("truncate tb_temp3");
}
else{
$respon["record"]="";
$respon["sukses"] = 0;
$respon["pesan"] = "0 pesan";
echo json_encode($respon);
}
}
?>
<?php
function getJum($conn,$sql){
$rs=$conn->query($sql);
$jum= $rs->num_rows;
$rs->free();
return $jum;
}
function getData($conn,$sql){
$rs=$conn->query($sql);
$rs->data_seek(0);
$arr = $rs->fetch_all(MYSQLI_ASSOC);
$rs->free();
return $arr;
}
?>
Tidak ada komentar:
Posting Komentar