Advertisement
pedrohcdo

Untitled

Jul 21st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.13 KB | None | 0 0
  1. package com.ecv.socketexample;
  2.  
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.content.SharedPreferences;
  8. import android.os.Bundle;
  9. import android.os.IBinder;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.util.Log;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. import com.ecv.socketexample.socket.ConnectionService;
  16.  
  17. import org.json.JSONObject;
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20.  
  21.     /**
  22.      * Listener
  23.      */
  24.     ConnectionService.SocketListener mListener = new ConnectionService.SocketListener() {
  25.  
  26.         @Override
  27.         public void onDisconnected() {
  28.             startReconnectionIfNeed();
  29.         }
  30.  
  31.         @Override
  32.         public void onReceivedData(String data) {
  33.             if(data.length() > 0) {
  34.                 try {
  35.                     Log.d("LogTest", "Received data: " + data);
  36.  
  37.                     final JSONObject json = new JSONObject(data);
  38.                     Log.d("LogTest", "Received JSON:" + json);
  39.  
  40.  
  41.                     if(!json.isNull("messageType")) {
  42.                         Log.d("LogTest", "JSON have messgae type: " + json.getString("messageType"));
  43.  
  44.                         final String type = json.getString("messageType");
  45.  
  46.                         if(type.equals("logout")) {
  47.                             Log.d("LogTest", "JSON messgae type is logout");
  48.  
  49.                             final String message = json.getString("message");
  50.                             //
  51.                             runOnUiThread(new Runnable() {
  52.                                 @Override
  53.                                 public void run() {
  54.                                     logout(message);
  55.                                 }
  56.                             });
  57.                         }
  58.  
  59.                     }
  60.  
  61.                 } catch (Exception e) {
  62.  
  63.                 }
  64.             }
  65.         }
  66.     };
  67.  
  68.     /**
  69.      * Service connection
  70.      */
  71.     ServiceConnection mServiceConnection = new ServiceConnection() {
  72.  
  73.         @Override
  74.         public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
  75.             mServiceBound = true;
  76.             mBinder = (ConnectionService.ConnectionBinder) iBinder;
  77.             mBinder.getService().setSocketListener(mListener);
  78.             startReconnectionIfNeed();
  79.         }
  80.  
  81.         @Override
  82.         public void onServiceDisconnected(ComponentName componentName) {
  83.             mServiceBound = false;
  84.         }
  85.     };
  86.  
  87.     //
  88.     private boolean mServiceBound = false;
  89.     private ConnectionService.ConnectionBinder mBinder;
  90.     private TextView mTextViewState;
  91.  
  92.     /**
  93.      * In Create
  94.      *
  95.      * @param savedInstanceState
  96.      */
  97.     @Override
  98.     protected void onCreate(Bundle savedInstanceState) {
  99.         super.onCreate(savedInstanceState);
  100.         setContentView(R.layout.activity_main);
  101.         //
  102.         mTextViewState = (TextView) findViewById(R.id.txt_state);
  103.     }
  104.  
  105.     /**
  106.      * Start login if need
  107.      */
  108.     void startReconnectionIfNeed() {
  109.         // Not connected on service
  110.         if(mBinder == null) return;
  111.         //
  112.         if(!mBinder.getService().isConnected()) {
  113.             SharedPreferences prefs = getSharedPreferences("login", MODE_PRIVATE);
  114.             String id = prefs.getString("accountId", "");
  115.             String imei = prefs.getString("accountIMEI", "");
  116.             String pass = prefs.getString("accountPass", "");
  117.             //
  118.             mTextViewState.setText("Socket State: Connecting");
  119.             //
  120.             mBinder.getService().startReconnection(id, imei, pass, new ConnectionService.RequestResult() {
  121.  
  122.                 /**
  123.                  * On result
  124.                  *
  125.                  * @param result
  126.                  * @param message
  127.                  */
  128.                 @Override
  129.                 public void onResult(final int result, final String message) {
  130.                     if(result == -1) {
  131.                         logout("The section has expired.");
  132.                     } else {
  133.                         runOnUiThread(new Runnable() {
  134.  
  135.                             @Override
  136.                             public void run() {
  137.                                 if(result == 1) {
  138.                                     mTextViewState.setText("Socket State: Connected");
  139.                                 } else {
  140.                                     mTextViewState.setText("Socket State: Error");
  141.                                     Log.d("LogTest", "Error on login in MainActivity: " + message);
  142.                                 }
  143.                             }
  144.                         });
  145.                     }
  146.  
  147.  
  148.                 }
  149.             });
  150.         } else {
  151.             mTextViewState.setText("Socket State: Connected");
  152.         }
  153.     }
  154.  
  155.     /**
  156.      * Logout
  157.      *
  158.      */
  159.     void logout(final String message) {
  160.         if(mBinder != null) {
  161.             //
  162.             Log.d("LogTest", "Logout");
  163.             //
  164.             mBinder.getService().logout();
  165.             mBinder.getService().setSocketListener(null);
  166.             // Save logout info
  167.             SharedPreferences.Editor prefs = getSharedPreferences("login", MODE_PRIVATE).edit();
  168.             prefs.putBoolean("account", false);
  169.             prefs.commit();
  170.             //
  171.             Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
  172.             startActivity(new Intent(this, LoginActivity.class));
  173.             finish();
  174.         }
  175.     }
  176.  
  177.     /**
  178.      * On Start
  179.      */
  180.     @Override
  181.     protected void onStart() {
  182.         super.onStart();
  183.         // Bind to service
  184.         Intent intent = new Intent(this, ConnectionService.class);
  185.         bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
  186.     }
  187.  
  188.     /**
  189.      * On Stop
  190.      */
  191.     @Override
  192.     protected void onStop() {
  193.         super.onStop();
  194.         // Unbind from the service
  195.         if (mServiceBound) {
  196.             unbindService(mServiceConnection);
  197.             mServiceBound = false;
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement