Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 KB | None | 0 0
  1. package com.globallogic.poctoolsuite.activities;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.widget.Toast;
  8.  
  9. import com.globallogic.poctoolsuite.MainActivity;
  10. import com.globallogic.poctoolsuite.R;
  11. import com.globallogic.poctoolsuite.utilities.TimeHelper;
  12. import com.globallogic.rigapi.RigApi;
  13. import com.globallogic.rigapi.json.connection.ConnectionHandler;
  14. import com.globallogic.rigapi.json.connection.ConnectionTaskJob;
  15.  
  16.  
  17. /**
  18.  * Created by lukasz.burcon on 2017-07-10.
  19.  */
  20.  
  21. public class SplashScreen extends AppCompatActivity implements ConnectionTaskJob {
  22.  
  23.     /**
  24.      * Reference to NETWORK_LOST
  25.      */
  26.     private static final ConnectionHandler.NetworkStatus NETWORK_LOST = ConnectionHandler.NetworkStatus.LOST;
  27.  
  28.     /**
  29.      * Reference to current network status
  30.      */
  31.     private ConnectionHandler.NetworkStatus networkStatus;
  32.  
  33.     /**
  34.      * Reference to timer, how much application has been loading
  35.      */
  36.     private int timer = 0;
  37.  
  38.     /**
  39.      * Reference to second
  40.      */
  41.     private int second = 1000;
  42.  
  43.     /**
  44.      * time in seconds after which error will be displayed
  45.      */
  46.     private int secToError = 900;
  47.  
  48.     /**
  49.      * Reference to handler
  50.      */
  51.     private Handler handler;
  52.  
  53.     /**
  54.      * Reference to runnable
  55.      */
  56.     private Runnable runnable;
  57.  
  58.     /**
  59.      * Reference to connectionHandler
  60.      */
  61.     private ConnectionHandler connectionHandler;
  62.  
  63.     /**
  64.      * Reference to timeHelper
  65.      *
  66.      * @param savedInstanceState
  67.      */
  68.     TimeHelper timeHelper;
  69.     private Toast toast;
  70.  
  71.     @Override
  72.     protected void onCreate(Bundle savedInstanceState) {
  73.         super.onCreate(savedInstanceState);
  74.         setContentView(R.layout.activity_splash_screen);
  75.         initializeScreen();
  76.  
  77.         runnable = new Runnable() {
  78.             @Override
  79.             public void run() {
  80.                 runnable = this;
  81.  
  82.                 if (!checkConnection() && timer < secToError)
  83.                     handler.postDelayed(runnable, second);
  84.                 else
  85.                     doAction();
  86.             }
  87.         };
  88.         handler.postDelayed(runnable, second);
  89.     }
  90.  
  91.     /**
  92.      * SplashScreen variables initializaiton
  93.      */
  94.     private void initializeScreen() {
  95.         RigApi.getJsonHandler().initialize(SplashScreen.this);
  96.         connectionHandler = RigApi.getJsonHandler().getConnectionHandler();
  97.         networkStatus = connectionHandler.getLastNetworkStatus();
  98.         handler = new Handler();
  99.         timeHelper = new TimeHelper();
  100.         toast = Toast.makeText(this, R.string.toast_exit, Toast.LENGTH_LONG);
  101.     }
  102.  
  103.     /**
  104.      * Checks internet connection until specified time
  105.      */
  106.     private boolean checkConnection() {
  107.         boolean isConnection = false;
  108.  
  109.         if (networkStatus == NETWORK_LOST) {
  110.             networkStatus = connectionHandler.getLastNetworkStatus();
  111.         } else {
  112.             isConnection = true;
  113.         }
  114.         timer++;
  115.  
  116.         return isConnection;
  117.     }
  118.  
  119.     /**
  120.      * Executes specified action after connection reached or time elapsed
  121.      */
  122.     private void doAction() {
  123.         if (timer < secToError) {
  124.             Intent i = new Intent(SplashScreen.this, MainActivity.class);
  125.             startActivity(i);
  126.             finish();
  127.         } else {
  128.             Toast.makeText(SplashScreen.this, R.string.error_no_server_connection, Toast.LENGTH_LONG).show();
  129.             closeScreen();
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Executes finish() after specified time in Thread.sleep()
  135.      */
  136.     private void closeScreen() {
  137.         Thread thread = new Thread() {
  138.             @Override
  139.             public void run() {
  140.                 try {
  141.                     Thread.sleep(3500);
  142.                     SplashScreen.this.finish();
  143.                 } catch (Exception e) {
  144.                     e.printStackTrace();
  145.                 }
  146.             }
  147.         };
  148.         thread.start();
  149.     }
  150.  
  151.     @Override
  152.     public void onBackPressed() {
  153.         if (timeHelper.getTimeDifference() < 3.5 * second) {
  154.             handler.removeCallbacks(runnable);
  155.             connectionHandler.endDownloading();
  156.             toast.cancel();
  157.             finishAffinity();
  158.         } else {
  159.             toast.show();
  160.             timeHelper.setTime();
  161.         }
  162.     }
  163.  
  164.     @Override
  165.     protected void onDestroy() {
  166.         if (timer < secToError)
  167.             super.onDestroy();
  168.         else {
  169.             handler.removeCallbacks(runnable);
  170.             connectionHandler.endDownloading();
  171.             super.onDestroy();
  172.         }
  173.     }
  174.  
  175.     @Override
  176.     public void postExecuteJob() {
  177.  
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement