src/com/beem/project/beem/BeemService.java
changeset 33 0e65d5f55d2f
parent 32 4d26854143e2
parent 21 b9f4a0ef574a
child 38 3e76846c48a9
equal deleted inserted replaced
32:4d26854143e2 33:0e65d5f55d2f
     8 import java.util.Set;
     8 import java.util.Set;
     9 
     9 
    10 import org.jivesoftware.smack.Roster;
    10 import org.jivesoftware.smack.Roster;
    11 import org.jivesoftware.smack.RosterListener;
    11 import org.jivesoftware.smack.RosterListener;
    12 import org.jivesoftware.smack.packet.Presence;
    12 import org.jivesoftware.smack.packet.Presence;
       
    13 import org.jivesoftware.smack.XMPPConnection;
       
    14 import org.jivesoftware.smack.XMPPException;
    13 
    15 
    14 import android.app.Notification;
    16 import android.app.Notification;
    15 import android.app.NotificationManager;
    17 import android.app.NotificationManager;
    16 import android.app.PendingIntent;
    18 import android.app.PendingIntent;
    17 import android.app.Service;
    19 import android.app.Service;
    23 import android.widget.Toast;
    25 import android.widget.Toast;
    24 
    26 
    25 import com.beem.project.beem.service.XMPPConnectionAdapter;
    27 import com.beem.project.beem.service.XMPPConnectionAdapter;
    26 import com.beem.project.beem.service.aidl.IXMPPConnection;
    28 import com.beem.project.beem.service.aidl.IXMPPConnection;
    27 import com.beem.project.beem.service.aidl.IXMPPFacade;
    29 import com.beem.project.beem.service.aidl.IXMPPFacade;
    28 
       
    29 /**
    30 /**
    30  * @author darisk
    31  * @author darisk
    31  *
    32  * 
    32  */
    33  */
    33 public class BeemService extends Service {
    34 public class BeemService extends Service {
    34 
    35 
    35     private NotificationManager notificationManager;
    36     private NotificationManager notificationManager;
    36 
    37 
    37     private IXMPPConnection connection;
    38     private IXMPPConnection connection;
    38     private SharedPreferences settings;
    39     private SharedPreferences settings;
    39     private String mLogin;
    40     private String mLogin;
    40     private String mPassword;
    41     private String mPassword;
    41     private String mHost;
    42     private String mHost;
       
    43 
    42     private IXMPPFacade.Stub bind = new IXMPPFacade.Stub() {
    44     private IXMPPFacade.Stub bind = new IXMPPFacade.Stub() {
    43 
    45 
    44 	@Override
    46 	@Override
    45 	public IXMPPConnection getXMPPConnection() throws RemoteException {
    47 	public IXMPPConnection getXMPPConnection() throws RemoteException {
    46 	    return connection;
    48 	    return connection;
    47 	}
    49 	}
    48 
       
    49 
    50 
    50 	@Override
    51 	@Override
    51 	public void disconnect() throws RemoteException {
    52 	public void disconnect() throws RemoteException {
    52 	    connection.disconnect();
    53 	    connection.disconnect();
    53 	}
    54 	}
    88 		getString(R.string.PreferencePasswordKey), "");
    89 		getString(R.string.PreferencePasswordKey), "");
    89 	mHost = settings.getString(getString(R.string.PreferenceHostKey), "");
    90 	mHost = settings.getString(getString(R.string.PreferenceHostKey), "");
    90 	notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    91 	notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    91 	connection = new XMPPConnectionAdapter("10.0.2.2"); // address
    92 	connection = new XMPPConnectionAdapter("10.0.2.2"); // address
    92     }
    93     }
       
    94     
       
    95     private void showBasicNotification(int stringResource) {
       
    96         String text = (String) getText(stringResource);
       
    97         Notification notif = new Notification(R.drawable.logo, text, System
       
    98                 .currentTimeMillis());
       
    99         notif.defaults = Notification.DEFAULT_ALL;
       
   100         notif.setLatestEventInfo(this, text, text, PendingIntent.getActivity(
       
   101                 this, 0, new Intent(), 0));
       
   102         notificationManager.notify(stringResource, notif);
       
   103         Toast toast = Toast.makeText(this, R.string.BeemServiceCreated,
       
   104                 Toast.LENGTH_LONG);
       
   105         toast.show();
       
   106     }
    93 
   107 
    94     @Override
   108     @Override
    95     public void onStart(Intent intent, int startId) {
   109     public void onStart(Intent intent, int startId) {
    96 	try {
   110 	try {
    97 	    connection.connectSync(mLogin, mPassword, "BEEM");
   111 	    connection.connectSync(mLogin, mPassword, "BEEM");
   105     public void onDestroy() {
   119     public void onDestroy() {
   106 	closeConnection();
   120 	closeConnection();
   107 	showBasicNotification(R.string.BeemServiceDestroyed);
   121 	showBasicNotification(R.string.BeemServiceDestroyed);
   108     }
   122     }
   109 
   123 
   110     private void showBasicNotification(int stringResource) {
       
   111 	String text = (String) getText(stringResource);
       
   112 	Notification notif = new Notification(R.drawable.logo, text, System
       
   113 		.currentTimeMillis());
       
   114 	notif.defaults = Notification.DEFAULT_ALL;
       
   115 	notif.setLatestEventInfo(this, text, text, PendingIntent.getActivity(
       
   116 		this, 0, new Intent(), 0));
       
   117 	notificationManager.notify(stringResource, notif);
       
   118     }
       
   119 
       
   120     private void closeConnection() {
   124     private void closeConnection() {
   121 	if (connection != null)
   125 	if (connection != null)
   122 	    try {
   126 	    try {
   123 		connection.disconnect();
   127 		connection.disconnect();
   124 	    } catch (RemoteException e) {
   128 	    } catch (RemoteException e) {