Created
September 10, 2012 03:31
-
-
Save sodapanda/3688684 to your computer and use it in GitHub Desktop.
MapCall
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.qishui.mapcall.service; | |
| import org.jivesoftware.smack.Chat; | |
| import org.jivesoftware.smack.ChatManager; | |
| import org.jivesoftware.smack.ConnectionConfiguration; | |
| import org.jivesoftware.smack.MessageListener; | |
| import org.jivesoftware.smack.PacketListener; | |
| import org.jivesoftware.smack.Roster; | |
| import org.jivesoftware.smack.XMPPConnection; | |
| import org.jivesoftware.smack.XMPPException; | |
| import org.jivesoftware.smack.filter.PacketFilter; | |
| import org.jivesoftware.smack.filter.PacketTypeFilter; | |
| import org.jivesoftware.smack.packet.Message; | |
| import org.jivesoftware.smack.packet.Packet; | |
| import com.baidu.mapapi.GeoPoint; | |
| import com.qishui.mapcall.R; | |
| import com.qishui.mapcall.application.MapCallApplication; | |
| import com.qishui.mapcall.ui.Contact; | |
| import com.qishui.mapcall.ui.IncomingCall; | |
| import com.qishui.mapcall.ui.MapCall; | |
| import android.app.Activity; | |
| import android.app.Notification; | |
| import android.app.NotificationManager; | |
| import android.app.PendingIntent; | |
| import android.app.Service; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.content.SharedPreferences; | |
| import android.os.Binder; | |
| import android.os.IBinder; | |
| import android.util.Log; | |
| import android.widget.Toast; | |
| /** | |
| * MapCall网络通讯的后台服务,包括后台监听,通讯流程控制,是MapCall应用程序的核心 | |
| * @author 王潇 | |
| * | |
| */ | |
| public class MapCallService extends Service { | |
| XMPPConnection con; | |
| String tag = "test"; | |
| private final IBinder mBinder = new MapCallServiceBinder(); | |
| SharedPreferences settings; | |
| public Chat mapChat; | |
| ChatManager chatManager; | |
| Roster roster; | |
| Notification incomingCallNotification; | |
| NotificationManager mNotificationManager; | |
| public String partner; | |
| public String myNumber; | |
| String request; | |
| String response; | |
| String isAgreeProperty; | |
| String stopcall; | |
| String geoPoint; | |
| @Override | |
| public void onCreate() { | |
| super.onCreate(); | |
| initrequestNotification(); | |
| request = "request"; | |
| response = "response"; | |
| isAgreeProperty = "isAgree"; | |
| stopcall = "stopcall"; | |
| geoPoint = "geoPoint"; | |
| } | |
| @Override | |
| public IBinder onBind(Intent arg0) { | |
| return mBinder; | |
| } | |
| @Override | |
| public int onStartCommand(Intent intent, int flags, int startId) { | |
| return START_STICKY; | |
| } | |
| public class MapCallServiceBinder extends Binder { | |
| public MapCallService getService() { | |
| return MapCallService.this; | |
| } | |
| } | |
| /** | |
| * 后台异步完成登录 | |
| * @param phoneNumber | |
| * @param password | |
| * @param activity | |
| */ | |
| public void connectAysnc(String phoneNumber, String password, | |
| Activity activity) { | |
| String phoneNumberLogin = phoneNumber + MapCallApplication.DOMAIN; | |
| ConnectionConfiguration config = new ConnectionConfiguration( | |
| MapCallApplication.SERVER, 5222); | |
| con = new XMPPConnection(config); | |
| new ConnectAsyncTask(activity, con, new AsyncTastDoneListener() { | |
| @Override | |
| public void asyncTaskComplete() { | |
| chatManager = con.getChatManager(); | |
| myNumber = con.getUser(); | |
| Intent intent = new Intent(MapCallService.this, Contact.class); | |
| intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| startActivity(intent); | |
| startListen(); | |
| startForegroundService(); | |
| } | |
| }).execute(phoneNumberLogin, password, MapCallApplication.RESOURCE); | |
| } | |
| /** | |
| * 开始监听 | |
| */ | |
| private void startListen() { | |
| PacketFilter filter = new PacketTypeFilter(Message.class); | |
| PacketListener mPacketListener = new PacketListener() { | |
| @Override | |
| public void processPacket(Packet packet) { | |
| if (packet.getPacketID() != geoPoint | |
| && packet.getError() == null | |
| && packet.getPacketID().equalsIgnoreCase(request)) { | |
| partner = packet.getFrom(); | |
| MapCallApplication.setPartner(partner); | |
| Log.i(tag, partner + "been heard"); | |
| // createNotification(partner); | |
| Intent intent = new Intent(MapCallService.this, | |
| IncomingCall.class); | |
| intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| startActivity(intent); | |
| } | |
| } | |
| }; | |
| con.addPacketListener(mPacketListener, filter); | |
| } | |
| /** | |
| * 后台异步完成请求发送过程 | |
| * @param number | |
| * @param activity | |
| */ | |
| public void sendRequestAsync(String number, Activity activity) { | |
| partner = number + MapCallApplication.DOMAIN + "/" | |
| + MapCallApplication.RESOURCE; | |
| new SendRequestAsyncTask(activity, con, new AsyncTastDoneListener() { | |
| @Override | |
| public void asyncTaskComplete() { | |
| mapChat = chatManager.createChat(partner, chatListener); | |
| Intent intent = new Intent(MapCallService.this, MapCall.class); | |
| intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| startActivity(intent); | |
| } | |
| }).execute(partner, myNumber); | |
| } | |
| /** | |
| * 发送响应消息,同意或者不同意 | |
| * @param isAgree | |
| */ | |
| public void sendResponse(boolean isAgree) { | |
| Message responsePacket = new Message(); | |
| responsePacket.setPacketID(response); | |
| responsePacket.setFrom(myNumber); | |
| responsePacket.setTo(partner); | |
| if (isAgree) { | |
| mapChat = chatManager.createChat(partner, chatListener); | |
| responsePacket.setProperty(isAgreeProperty, true); | |
| } else { | |
| responsePacket.setProperty(isAgreeProperty, false); | |
| } | |
| con.sendPacket(responsePacket); | |
| Log.i(tag, "response sended"); | |
| } | |
| /** | |
| * GPS信息的发送 | |
| * @param lat | |
| * @param lon | |
| */ | |
| public void send(double lat, double lon) { | |
| Message message = new Message(); | |
| message.setPacketID(geoPoint); | |
| message.setProperty("lat", lat); | |
| message.setProperty("lon", lon); | |
| try { | |
| mapChat.sendMessage(message); | |
| } catch (XMPPException e) { | |
| Log.i(tag, "faild"); | |
| } | |
| } | |
| /** | |
| * 停止通讯过程 | |
| */ | |
| public void stopCall() { | |
| mapChat.removeMessageListener(chatListener); | |
| mapChat = null; | |
| Log.i(tag, "chat destroy"); | |
| Message stopCallMessage = new Message(); | |
| stopCallMessage.setPacketID(stopcall); | |
| stopCallMessage.setFrom(myNumber); | |
| stopCallMessage.setTo(partner); | |
| con.sendPacket(stopCallMessage); | |
| MapCallApplication.delePartner(); | |
| Log.i(tag, "stopcall packet sended"); | |
| } | |
| public void logOut() { | |
| con.disconnect(); | |
| stopForeground(true); | |
| stopSelf(); | |
| } | |
| /** | |
| * 后台完成注册过程避免失去响应 | |
| * @param username | |
| * @param password | |
| * @param activity | |
| */ | |
| public void createAccountAsync(String username, String password, | |
| Activity activity) { | |
| new CreateAccountAsyncTask(activity, new AsyncTastDoneListener() { | |
| @Override | |
| public void asyncTaskComplete() { | |
| // 这就开始登录了才对,应该先完成Application的全局变量,这里才能开始 | |
| } | |
| }).execute(username, password); | |
| } | |
| /** | |
| * 通讯过程中对消息报的监听器 | |
| */ | |
| private MessageListener chatListener = new MessageListener() { | |
| @Override | |
| public void processMessage(Chat arg0, Message message) { | |
| Log.i(tag, "chatListener !!" + message.getPacketID()); | |
| if (message.getPacketID().equals(stopcall)) { | |
| Intent stopcallIntent = new Intent(); | |
| stopcallIntent.setAction("MapCall"); | |
| sendBroadcast(stopcallIntent); | |
| Log.i(tag, "stop call heard and boadcastsended by chatListener"); | |
| } else { | |
| double lat = (Double) message.getProperty("lat"); | |
| double lon = (Double) message.getProperty("lon"); | |
| GeoPoint friendPoint = new GeoPoint((int) lat, (int) lon); | |
| MapCall.friend.setPoint(friendPoint); | |
| } | |
| } | |
| }; | |
| private void initrequestNotification() { | |
| mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
| incomingCallNotification = new Notification(R.drawable.icon, | |
| "new MapCall", System.currentTimeMillis()); | |
| incomingCallNotification.defaults |= Notification.DEFAULT_SOUND; | |
| incomingCallNotification.flags |= Notification.FLAG_AUTO_CANCEL; | |
| } | |
| public boolean getConnectState() { | |
| return con.isAuthenticated(); | |
| } | |
| public void sendToast(String words) { | |
| Toast.makeText(getApplicationContext(), words, Toast.LENGTH_LONG) | |
| .show(); | |
| } | |
| public void startForegroundService() { | |
| Notification notification = new Notification(R.drawable.icon, | |
| getText(R.string.ticker_text), System.currentTimeMillis()); | |
| Intent notificationIntent = new Intent(this, Contact.class); | |
| PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, | |
| notificationIntent, 0); | |
| notification.setLatestEventInfo(this, | |
| getText(R.string.notificationtitle), | |
| getText(R.string.notificationMessage), pendingIntent); | |
| startForeground(1, notification); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment