Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.globallogic.poctoolsuite.activities;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.Toast;
- import com.globallogic.poctoolsuite.MainActivity;
- import com.globallogic.poctoolsuite.R;
- import com.globallogic.poctoolsuite.utilities.TimeHelper;
- import com.globallogic.rigapi.RigApi;
- import com.globallogic.rigapi.json.connection.ConnectionHandler;
- import com.globallogic.rigapi.json.connection.ConnectionTaskJob;
- /**
- * Created by lukasz.burcon on 2017-07-10.
- */
- public class SplashScreen extends AppCompatActivity implements ConnectionTaskJob {
- /**
- * Reference to NETWORK_LOST
- */
- private static final ConnectionHandler.NetworkStatus NETWORK_LOST = ConnectionHandler.NetworkStatus.LOST;
- /**
- * Reference to current network status
- */
- private ConnectionHandler.NetworkStatus networkStatus;
- /**
- * Reference to timer, how much application has been loading
- */
- private int timer = 0;
- /**
- * Reference to second
- */
- private int second = 1000;
- /**
- * time in seconds after which error will be displayed
- */
- private int secToError = 900;
- /**
- * Reference to handler
- */
- private Handler handler;
- /**
- * Reference to runnable
- */
- private Runnable runnable;
- /**
- * Reference to connectionHandler
- */
- private ConnectionHandler connectionHandler;
- /**
- * Reference to timeHelper
- *
- * @param savedInstanceState
- */
- TimeHelper timeHelper;
- private Toast toast;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_splash_screen);
- initializeScreen();
- runnable = new Runnable() {
- @Override
- public void run() {
- runnable = this;
- if (!checkConnection() && timer < secToError)
- handler.postDelayed(runnable, second);
- else
- doAction();
- }
- };
- handler.postDelayed(runnable, second);
- }
- /**
- * SplashScreen variables initializaiton
- */
- private void initializeScreen() {
- RigApi.getJsonHandler().initialize(SplashScreen.this);
- connectionHandler = RigApi.getJsonHandler().getConnectionHandler();
- networkStatus = connectionHandler.getLastNetworkStatus();
- handler = new Handler();
- timeHelper = new TimeHelper();
- toast = Toast.makeText(this, R.string.toast_exit, Toast.LENGTH_LONG);
- }
- /**
- * Checks internet connection until specified time
- */
- private boolean checkConnection() {
- boolean isConnection = false;
- if (networkStatus == NETWORK_LOST) {
- networkStatus = connectionHandler.getLastNetworkStatus();
- } else {
- isConnection = true;
- }
- timer++;
- return isConnection;
- }
- /**
- * Executes specified action after connection reached or time elapsed
- */
- private void doAction() {
- if (timer < secToError) {
- Intent i = new Intent(SplashScreen.this, MainActivity.class);
- startActivity(i);
- finish();
- } else {
- Toast.makeText(SplashScreen.this, R.string.error_no_server_connection, Toast.LENGTH_LONG).show();
- closeScreen();
- }
- }
- /**
- * Executes finish() after specified time in Thread.sleep()
- */
- private void closeScreen() {
- Thread thread = new Thread() {
- @Override
- public void run() {
- try {
- Thread.sleep(3500);
- SplashScreen.this.finish();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- };
- thread.start();
- }
- @Override
- public void onBackPressed() {
- if (timeHelper.getTimeDifference() < 3.5 * second) {
- handler.removeCallbacks(runnable);
- connectionHandler.endDownloading();
- toast.cancel();
- finishAffinity();
- } else {
- toast.show();
- timeHelper.setTime();
- }
- }
- @Override
- protected void onDestroy() {
- if (timer < secToError)
- super.onDestroy();
- else {
- handler.removeCallbacks(runnable);
- connectionHandler.endDownloading();
- super.onDestroy();
- }
- }
- @Override
- public void postExecuteJob() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement