10
import android.app.*; import android.content.*; import android.location.*; import android.os.*; import android.speech.*; import android.support.v4.view.*; import android.telephony.*; import android.view.*; import android.view.View.*; import android.widget.*; import java.util.*; import android.view.GestureDetector.*; import android.graphics.*; import android.graphics.drawable.*; import android.text.*; import android.text.method.*; import android.util.*; import android.widget.SeekBar.*; import android.media.*; import java.io.*; import android.widget.CompoundButton.*; public class MainActivity extends Activity implements GestureDetector.OnGestureL istener,OnDoubleTapListener { MediaPlayer mp; TextView txtm,rmessageval ; String emid,phone,message,message_copy; static int enable_autoapdate=0; String rec_data=""; public GestureDetectorCompat gDetector; static int tr=0,tr1=0,multiply=1,mail_enable=0,inc_mail=0; static int enable_storm=0; EditText edsub; Button start; AutoCompleteTextView edid; LocationManager lm; SeekBar sb; TextView lt,lng,tmre; ArrayList m; TextView tm; String have=""; Paint pt; MessageReceiver rm; IntentFilter i; LocationListener ll; String mydate,subj; Switch mailen; ImageView iv; Button stormbt ; CheckBox check; Button btnvoice,temp,tempobj,pressure; static int val=0; Timer timer; ArrayAdapter idast; final Handler handler = new Handler(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

Android-Smart Storm Hunter

Embed Size (px)

DESCRIPTION

/

Citation preview

import android.app.*;import android.content.*;import android.location.*;import android.os.*;import android.speech.*;import android.support.v4.view.*;import android.telephony.*;import android.view.*;import android.view.View.*;import android.widget.*;import java.util.*;import android.view.GestureDetector.*;import android.graphics.*;import android.graphics.drawable.*;import android.text.*;import android.text.method.*;import android.util.*;import android.widget.SeekBar.*;import android.media.*;import java.io.*;import android.widget.CompoundButton.*;public class MainActivity extends Activity implements GestureDetector.OnGestureListener,OnDoubleTapListener{MediaPlayer mp;TextView txtm,rmessageval ;String emid,phone,message,message_copy;static int enable_autoapdate=0;String rec_data="";public GestureDetectorCompat gDetector;static int tr=0,tr1=0,multiply=1,mail_enable=0,inc_mail=0;static int enable_storm=0;EditText edsub;Button start;AutoCompleteTextView edid;LocationManager lm;SeekBar sb;TextView lt,lng,tmre;ArrayList m;TextView tm;String have="";Paint pt;MessageReceiver rm;IntentFilter i;LocationListener ll;String mydate,subj;Switch mailen;ImageView iv;Button stormbt ;CheckBox check;Button btnvoice,temp,tempobj,pressure;static int val=0;Timer timer;ArrayAdapter idast;final Handler handler = new Handler();

public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.main);

MyPagerAdapter adapter = new MyPagerAdapter();ViewPager myPager = (ViewPager)findViewById(R.id.myfivepanelpager);myPager.setAdapter(adapter);myPager.setCurrentItem(0);lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);ll=new myPositionPhonesamsung();// Log.d("My Activity","Smart Storm Hunte");rm=new MessageReceiver();i=new IntentFilter();mp=MediaPlayer.create(getBaseContext(),R.raw.ic_song);i.addAction("android.provider.Telephony.SMS_RECEIVED");registerReceiver(rm,i);

gDetector=new GestureDetectorCompat(this,this);gDetector.setOnDoubleTapListener(this);

timer = new Timer();String[] ids=new String[]{"[email protected]","[email protected]"};idast=new ArrayAdapter (this,android.R.layout.simple_list_item_1,ids);

}//id from xml//x1//y1//x2//y2public void dry(int id,int x1,int y1,int x2,int y2){pt=new Paint();pt.setColor(Color.GREEN);pt.setStyle(Paint.Style.STROKE);Bitmap bmp=Bitmap.createBitmap(150,150,Bitmap.Config.ARGB_8888);Canvas c=new Canvas(bmp);c.drawRect(x1,y1,x2,y2,pt);c.drawLine(0,16,80,16,pt);c.drawLine(0,32,80,32,pt);c.drawLine(0,48,80,48,pt);c.drawLine(0,48+16,80,48+16,pt);LinearLayout ll=(LinearLayout)findViewById(id);ll.setBackgroundDrawable(new BitmapDrawable(bmp));}

class MyPagerAdapter extends PagerAdapter {//public int getCount() {return 2 ;}public Object instantiateItem(View collection, int position) {LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);View view = null;

int resId = 0;switch (position) {case 1:resId = R.layout.left;view = inflater.inflate(resId, null);

((ViewPager) collection).addView(view, 0);Button btnleft = (Button)findViewById(R.id.sendmail);start=(Button)findViewById(R.id.timerval);tm=(TextView)findViewById(R.id.time);lt=(TextView)findViewById(R.id.latitude);lng=(TextView)findViewById(R.id.longitude);tmre=(TextView)findViewById(R.id.timerview);mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());//codetxtm = (TextView)findViewById(R.id.textm);

sb=(SeekBar)findViewById(R.id.setTimeUpdate);mailen=(Switch)findViewById(R.id.enableswitch);

edid=(AutoCompleteTextView)findViewById(R.id.to);

edsub=(EditText)findViewById(R.id.sub);timer.schedule(timertask, 0, 600);lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,100,0,ll);edid.setAdapter(idast);mailen.setChecked(false);mailen.setOnCheckedChangeListener(new OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton p1, boolean p2){if(p2==true){Toast.makeText(getBaseContext(),"Enable Send Mail",Toast.LENGTH_SHORT).show();mail_enable=1;}else{Toast.makeText(getBaseContext(),"Disable Send Mail",Toast.LENGTH_SHORT).show();mail_enable=0;}// TODO: Implement this method}

});sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){@Overridepublic void onProgressChanged(SeekBar p1, int p2, boolean p3){Toast.makeText(getBaseContext(),"Update Time:"+p2,Toast.LENGTH_SHORT).show();if(p2==0)p2=1;multiply=p2;}@Overridepublic void onStartTrackingTouch(SeekBar p1){// TODO: Implement this method}@Overridepublic void onStopTrackingTouch(SeekBar p1){// TODO: Implement this method}

});start.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View p1){

// TODO: Implement this method}

});btnleft.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {

emid=edid.getText().toString();subj=edsub.getText().toString();// txtm = (TextView)findViewById(R.id.textl);Intent em=new Intent(Intent.ACTION_SEND);em.putExtra(Intent.EXTRA_EMAIL,new String[]{emid});em.putExtra(Intent.EXTRA_SUBJECT,subj);em.setType("plain/text");if(mail_enable==1){Toast.makeText(getBaseContext(),"Please Enable Send Mail Switch",Toast.LENGTH_SHORT).show();em.putExtra(Intent.EXTRA_TEXT," ");}else{em.putExtra(Intent.EXTRA_TEXT,message);}// startActivity(Intent.createChooser(em,"Email"))startActivity(em);//txtm.setText("Changed Left");}});

//code

break;case 0:resId = R.layout.middle;view = inflater.inflate(resId, null);((ViewPager) collection).addView(view, 0);stormbt = (Button)findViewById(R.id.stormev);btnvoice=(Button)findViewById(R.id.voiceButton);//codetemp=(Button)findViewById(R.id.temperature);tempobj=(Button)findViewById(R.id.tempobj);pressure=(Button)findViewById(R.id.humidity);iv=(ImageView)findViewById(R.id.logo);check=(CheckBox)findViewById(R.id.check_b);iv.setImageResource(R.drawable.ic_launcher1);

// rmessageval=(TextView)findViewById(R.id.receivemes);

dry(R.id.midlelayout,0,0,80,90);

check.setOnClickListener(new OnClickListener(){

@Overridepublic void onClick(View p1){

boolean bin=((CheckBox)p1).isChecked();if(bin==true){

Toast.makeText(getBaseContext(),"Enable Timer",Toast.LENGTH_SHORT).show();enable_autoapdate=1;}else{

Toast.makeText(getBaseContext(),"Disable Timer",Toast.LENGTH_SHORT).show();enable_autoapdate=0;

}// TODO: Implement this method}

});btnvoice.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View p1){

// try{//send Message// mp.prepare();// mp.start();//}catch(IOException e){

// }// Toast.makeText(getBaseContext(),"Play",Toast.LENGTH_SHORT).show();// sendMessage("0742416489","Temperature");// TODO: Implement this method}

});

stormbt.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// startVoice();}});//codebreak;

}return view;}public void destroyItem(View arg0, int arg1, Object arg2) {((ViewPager) arg0).removeView((View) arg2);}public boolean isViewFromObject(View arg0, Object arg1) {return arg0 == ((View) arg1);}public Parcelable saveState() {return null;}//public int getItemPosition(Object object) { return POSITION_NONE; }}

//boolean bo=((CheckBox)b).isChecked();//if(bo==true){//enable_autoapdate=1;//}else{// enable_autoapdate=0;

private void startVoice(){Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);i.putExtra(RecognizerIntent.EXTRA_PROMPT,"Android voice");startActivityForResult(i,1234);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data){if(requestCode==1234&&resultCode==RESULT_OK){m=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);// l.setAdapter(new ArrayAdapter (this,android.R.layout.simple_list_item_1,m));Toast.makeText(getBaseContext(),m.get(0),Toast.LENGTH_SHORT).show();if(m.get(0).compareTo("data")==0){sendMessage("0754969310","Request data");Toast.makeText(getBaseContext(),"Message sent",Toast.LENGTH_LONG).show();}}// TODO: Implement this methodsuper.onActivityResult(requestCode, resultCode, data);}@Overrideprotected void onDestroy(){// TODO: Implement this methodsuper.onDestroy();unregisterReceiver(rm);}private void sendMessage(String number,String message){

SmsManager sms=SmsManager.getDefault();sms.sendTextMessage(number,null,message,null,null);}@Overridepublic boolean onSingleTapConfirmed(MotionEvent p1)

{// TODO: Implement this methodreturn true;}@Overridepublic boolean onDoubleTap(MotionEvent p1){// tr1++;// if(tr1==2){

// //}if(tr1>2)tr1=0;// TODO: Implement this methodreturn true;}@Overridepublic boolean onDoubleTapEvent(MotionEvent p1){// TODO: Implement this methodreturn true;}@Overridepublic boolean onDown(MotionEvent p1){// TODO: Implement this methodreturn true;}@Overridepublic void onShowPress(MotionEvent p1){// TODO: Implement this method}@Overridepublic boolean onSingleTapUp(MotionEvent p1){// Toast.makeText(this,"Timer On",Toast.LENGTH_SHORT).show();// TODO: Implement this methodreturn true;}@Overridepublic boolean onScroll(MotionEvent p1, MotionEvent p2, float p3, float p4){// Toast.makeText(getBaseContext(),"Liver",Toast.LENGTH_SHORT).show();// tr++;// if(tr==2){startVoice();// // }//if(tr>2)//tr=0;// TODO: Implement this methodreturn false;}@Overridepublic void onLongPress(MotionEvent p1){

// TODO: Implement this method

}@Overridepublic boolean onFling(MotionEvent p1, MotionEvent p2, float p3, float p4){// Toast.makeText(this,"Timer On",Toast.LENGTH_SHORT).show();

// TODO: Implement this methodreturn true;}@Overridepublic boolean onTouchEvent(MotionEvent event){gDetector.onTouchEvent(event);// TODO: Implement this methodreturn super.onTouchEvent(event);}private class myPositionPhonesamsung implements LocationListener{@Overridepublic void onLocationChanged(Location p1){

Toast.makeText(getBaseContext(),"Longitude:",Toast.LENGTH_SHORT).show();

if(p1!=null){//p1.getTime();// double alt=p1.getAltitude();double a=p1.getLongitude();double b=p1.getLatitude();

// Toast.makeText(getBaseContext(),"Longitude:"+Double.toString(a),Toast.LENGTH_SHORT).show();lt.setText("Longitude:"+Double.toString(a));lng.setText("Latitude:"+Double.toString(b));

// tm.setText(mydate);}// TODO: Implement this method}@Overridepublic void onStatusChanged(String p1, int p2, Bundle p3){// TODO: Implement this method}@Overridepublic void onProviderEnabled(String p1){// TODO: Implement this method}@Overridepublic void onProviderDisabled(String p1){// TODO: Implement this method}

}public class MessageReceiver extends BroadcastReceiver{

String rec;int isc;@Overridepublic void onReceive(Context p1, Intent p2){Bundle b=p2.getExtras();try{Object[] pdus=(Object[])b.get("pdus");for(int i=0;iSmsMessage curmess=SmsMessage.createFromPdu((byte[])pdus[i]);phone=curmess.getDisplayOriginatingAddress();message=curmess.getDisplayMessageBody();// rec+=""+phone+""+message;if( message.substring(0,5).compareTo("Storm")==0){Toast.makeText(p1,message.substring(0,5),Toast.LENGTH_LONG).show();mp.start();enable_storm=1;

}else{// isc=message.length();

// rmessageval.setText("Humidity "+message.substring(82+9,86+9)+"Temperature "+message.substring(92+9,96+9)+"Temp Obj "+message.substring(106+9,109+10)+"Pressure "+message.substring(114+9,119+10));// txtm.setText(message.substring(6,62));

btnvoice.setText("Humidity:"+message.substring(82+13,86+13)+"%");temp.setText("Temperature:"+message.substring(92+13,96+13)+"oC");tempobj.setText("Temp Obj:"+message.substring(106+13,109+14)+"oC");pressure.setText("Pres:"+message.substring(114+13,119+14)+"Pa");

txtm.setText(message.substring(37,37+21));txtm.setText(Html.fromHtml("GPS BOARD LOCATION"));txtm.setMovementMethod(LinkMovementMethod.getInstance());if(mail_enable==1){inc_mail++;start.setText("Mail Buffer is FULL");

}

if(inc_mail>1){

start.setText("Mail Buffer is OVERWRITE");

}

}

}}catch(Exception e){;}

// TODO: Implement this method}

}TimerTask timertask = new TimerTask() {@Override

public void run() {handler.post(new Runnable() {public void run() {val++;if(val>10*multiply)val=0;if(enable_autoapdate==1){if(val==10*multiply){

sendMessage("0754969310","Request data");Toast.makeText(getBaseContext(),"Message sent "+multiply,Toast.LENGTH_LONG).show();

}

}if(enable_storm==1){enable_storm=0;stormbt.setText("\nSTORM EVENT\n\n DETECTED at "+message.substring(message.length()-5,message.length())); // val=0;// while(++val==150);

// stormbt.setText("STORM EVENT\n DETECTED at"); }

tmre.setText("Timer Value:"+val+"");mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

tm.setText(mydate);}});}};////// }

}