Advertisement
Techmo

appcontroller

Jan 13th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. package com.oncall.app;
  2.  
  3. import android.app.Application;
  4. import android.text.TextUtils;
  5.  
  6. import com.android.volley.Request;
  7. import com.android.volley.RequestQueue;
  8. import com.android.volley.toolbox.Volley;
  9.  
  10. import java.util.ArrayList;
  11.  
  12. import io.socket.client.IO;
  13. import io.socket.client.Socket;
  14. import io.socket.emitter.Emitter;
  15.  
  16. /**
  17.  * Created by Bradly on 12/29/2017.
  18.  */
  19.  
  20. public class AppController extends Application{
  21.     public static final String TAG = AppController.class.getSimpleName();
  22.     private RequestQueue mRequestQueue;
  23.     private static AppController mInstance;
  24.     private Socket mSocket; // A 2 way connection between the server and the phone running this app
  25.     private ArrayList<Emitter.Listener> socketListeners;
  26.     private ArrayList<String> socketEvents;
  27.  
  28.     @Override
  29.     public void onCreate(){
  30.         super.onCreate();
  31.         socketListeners= new ArrayList<>();
  32.         mInstance = this; // Save reference to initialized object for future reference
  33.  
  34.         // Initialize the on call api socket
  35.         try{
  36.             IO.Options opts = new IO.Options();
  37.             opts.forceNew = true;
  38.             opts.reconnection = true;
  39.             opts.secure = true;
  40.             mSocket = IO.socket(AppConfig.API_SERVER_ADDRESS, opts);
  41.         } catch (Exception e){
  42.             e.printStackTrace();
  43.         }
  44.     }
  45.  
  46.     public static synchronized AppController getInstance() {
  47.         return mInstance;
  48.     }
  49.  
  50.     public RequestQueue getRequestQueue() {
  51.         if (mRequestQueue == null) {
  52.             mRequestQueue = Volley.newRequestQueue(getApplicationContext());
  53.         }
  54.         return mRequestQueue;
  55.     }
  56.  
  57.     public <T> void addToRequestQueue(Request<T> req, String tag) {
  58.         req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
  59.         getRequestQueue().add(req);
  60.     }
  61.  
  62.     public Socket getSocket(){
  63.         return mSocket;
  64.     }
  65.  
  66.     public void connectSocket(){
  67.         if(socketListeners.size() <= 0)
  68.             System.out.println("[ERR] => (AppController) Socket connection requested, but no listeners registered.");
  69.         else
  70.             mSocket.connect();
  71.     }
  72.  
  73.     // Add an event listener and register it with the socket
  74.     public void addListener(String event, Emitter.Listener listener){
  75.         if(!(event.equals("") || event.equals(" "))) {
  76.             socketListeners.add(listener);
  77.             socketEvents.add(event);
  78.             mSocket.on(event, listener);
  79.         }
  80.         else{
  81.             System.out.println("[ERR] => (AppController) Tried to add listener with empty event.");
  82.         }
  83.     }
  84.  
  85.     // Remove listeners, and disconnect the socket
  86.     public void disconnectSocket(){
  87.         for(int i = 0; i < socketListeners.size(); i++){
  88.             mSocket.off(socketEvents.get(i), socketListeners.get(i));
  89.         }
  90.         mSocket.disconnect();
  91.     }
  92.  
  93.     public void shutdown(){
  94.         disconnectSocket();
  95.         System.out.println("[STAT] => (AppController) Shutdown requested, shutting down.");
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement