From 4e3da78e8d1dd3f7673ceb71199315511b23810d Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Fri, 19 Nov 2010 21:02:48 +0000 Subject: Prevented the connection message being repeatedly sent to IRC. Added functionality of a notification being sent when the phone regains service. --- .../BlaMail/src/com/blatech/blamail/BlaMail.java | 72 ++++++++++++++++------ 1 file changed, 53 insertions(+), 19 deletions(-) (limited to 'workspace/BlaMail/src/com') diff --git a/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java b/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java index cb40c34..1fe08ca 100644 --- a/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java +++ b/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java @@ -50,6 +50,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.telephony.PhoneStateListener; +import android.telephony.ServiceState; import android.telephony.SmsManager; import android.telephony.TelephonyManager; import android.view.View; @@ -75,22 +76,60 @@ public class BlaMail extends Activity { TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); - PhoneStateListener listener = new PhoneStateListener() { - @Override + PhoneStateListener serviceListener = new PhoneStateListener() { + Boolean serviceDisconnected = true; + @Override + public void onServiceStateChanged(ServiceState serviceState){ + try { + if(serviceState.getState() == 0 && serviceDisconnected == true){ + sendSMS("0000000000000000000", "07927278978", "!blamail Notification from blamail: The phone is registered with an operator."); + serviceDisconnected = false; + } + else { + serviceDisconnected = true; + } + /*switch(serviceState.getState()){ + case 0: //serviceState.STATE_IN_SERVICE + if (serviceDisconnected == true) { + sendSMS("0000000000000000000", "07927278978", "!blamail Notification from blamail: The phone is registered with an operator."); + } + serviceDisconnected = false; + break; + case 1: //serviceState.STATE_OUT_OF_SERVICE + serviceDisconnected = true; + break; + }*/ + } catch (AddressException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (MessagingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }; + + PhoneStateListener dataListener = new PhoneStateListener() { + Boolean dataDisconnected = true; + @Override public void onDataConnectionStateChanged(int state){ - Thread t = null; + Thread t = null; //Thread s = null; try { switch (state) { - case TelephonyManager.DATA_CONNECTED: - if(t != null){ - t.destroy(); //not good practice. TODO: fix this! + case TelephonyManager.DATA_CONNECTED: + if(dataDisconnected == true){ + dataDisconnected = false; + if(t != null){ + t.destroy(); //not good practice. TODO: fix this! + } + sendSMS("0000000000000000000", "07927278978", "!blamail Notification from blamail: Connected. IP traffic should be available."); + t = new Thread(new Monitor()); + t.start(); } - sendSMS("0000000000000000000", "07927278978", "!blamail Notification from blamail: Connected. IP traffic should be available."); - t = new Thread(new Monitor()); - t.start(); break; case TelephonyManager.DATA_DISCONNECTED: + dataDisconnected = true; sendSMS("0000000000000000000", "07927278978", "!blamail Notification from blamail: Disconnected. IP traffic not available."); if(t != null){ t.destroy(); //not good practice. TODO: fix this! @@ -101,6 +140,7 @@ public class BlaMail extends Activity { break; case TelephonyManager.DATA_ACTIVITY_DORMANT: if(t != null){ + dataDisconnected = true; sendSMS("0000000000000000000", "07927278978", "!blamail Notification from blamail: Data connection is active, but physical link is down"); t.destroy(); //not good practice. TODO: fix this! //t.interrupt(); @@ -110,6 +150,7 @@ public class BlaMail extends Activity { break; case TelephonyManager.DATA_SUSPENDED: if(t != null){ + dataDisconnected = true; sendSMS("0000000000000000000", "07927278978", "!blamail Notification from blamail: Suspended. The connection is up, but IP traffic is temporarily unavailable. For example, in a 2G network, data activity may be suspended when a voice call arrives."); t.destroy(); //not good practice. TODO: fix this! //t.interrupt(); @@ -117,17 +158,9 @@ public class BlaMail extends Activity { //s.start(); } break; - case TelephonyManager.DATA_CONNECTING: - if(t != null){ - sendSMS("0000000000000000000", "07927278978", "!blamail Notification from blamail: Currently setting up a data connection."); - t.destroy(); //not good practice. TODO: fix this! - //t.interrupt(); - //s = new Thread(new StopIdle()); - //s.start(); - } - break; default: if(t != null){ + dataDisconnected = true; sendSMS("0000000000000000000", "07927278978", "!blamail Notification from blamail: Current state is \"" + state + "\""); t.destroy(); //not good practice. TODO: fix this! } @@ -144,7 +177,8 @@ public class BlaMail extends Activity { }; // Register the listener wit the telephony manager - tm.listen(listener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); + tm.listen(serviceListener, PhoneStateListener.LISTEN_SERVICE_STATE); + tm.listen(dataListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); Button send = (Button) findViewById(R.id.send); -- cgit v1.2.3