com.givenhansco.keystonenavigation.Activities.PTT.PTTMainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.givenhansco.keystonenavigation.Activities.PTT.PTTMainActivity.java

Source

package com.givenhansco.keystonenavigation.Activities.PTT;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.givenhansco.keystonenavigation.Activities.Core.StatusActivity;
import com.givenhansco.keystonenavigation.Models.Services.VehicleStatus;
import com.givenhansco.keystonenavigation.R;
import com.zello.sdk.Tab;
import com.zello.sdk.Zello;

/**
 * Created by bseverson on 2/13/2017.
 */

public class PTTMainActivity extends AppCompatActivity implements com.zello.sdk.Events {
    //region Lifecycle Methods

    private EditText networkEdit;
    private EditText usernameEdit;
    private EditText passwordEdit;
    private Button loginButton;
    private TextView textView;
    private CheckBox perishableCheckBox;
    private RelativeLayout signingInView;
    private RelativeLayout signInView;
    private Button cancelButton;

    private TextView errorTextView;
    private boolean signInAttempted = false;
    private com.zello.sdk.AppState appState = new com.zello.sdk.AppState();
    public SharedPreferences _settings;
    ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mProgressBar = new ProgressBar(PTTMainActivity.this);

        setContentView(R.layout.activity_signin);
        _settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        signInView = (RelativeLayout) findViewById(R.id.signInView);
        networkEdit = (EditText) findViewById(R.id.networkEdit);
        usernameEdit = (EditText) findViewById(R.id.usernameEdit);
        passwordEdit = (EditText) findViewById(R.id.passwordEdit);
        loginButton = (Button) findViewById(R.id.loginButton);
        textView = (TextView) findViewById(R.id.statusTextView);
        perishableCheckBox = (CheckBox) findViewById(R.id.checkBox);
        signingInView = (RelativeLayout) findViewById(R.id.signingInView);
        cancelButton = (Button) findViewById(R.id.cancelButton);
        errorTextView = (TextView) findViewById(R.id.incorrectPasswordTextView);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mProgressBar.setVisibility(View.VISIBLE);
                String network = networkEdit.getText().toString();
                String username = usernameEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                boolean perishable = perishableCheckBox.isChecked();
                if (appState.isSignedIn()) {
                    updateUI();
                    if (mProgressBar.isShown())
                        mProgressBar.setVisibility(View.GONE);
                }
                signInAttempted = true;
                boolean signin = Zello.getInstance().signIn(network.trim(), username.trim(), password.trim(),
                        perishable);
                Log.e("signin", "" + signin);
                Zello.getInstance().getAppState(appState);
                Log.e("appstate", "" + appState.isSignedIn());
                Log.e("error", "" + appState.getLastError());
                hideKeyboard();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Zello.getInstance().cancelSignIn();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //Zello.getInstance().unsubscribeFromEvents(this);
        // Zello.getInstance().unconfigure();
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Zello.getInstance().leavePowerSavingMode();
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Zello.getInstance().enterPowerSavingMode();
    }

    //endregion

    //region Zello SDK Events

    @Override
    public void onMessageStateChanged() {

    }

    @Override
    public void onSelectedContactChanged() {

    }

    @Override
    public void onAudioStateChanged() {

    }

    @Override
    public void onContactsChanged() {

    }

    @Override
    public void onLastContactsTabChanged(Tab tab) {

    }

    @Override
    public void onAppStateChanged() {
        Zello.getInstance().getAppState(appState);
        Log.e("appstate", "app state changed" + appState.isAvailable());
        updateUI();
    }

    @Override
    public void onMicrophonePermissionNotGranted() {

    }

    //endregion

    private void updateUI() {
        if (appState.isSignedIn()) {
            if (mProgressBar.isShown())
                mProgressBar.setVisibility(View.GONE);
            signInView.setVisibility(View.INVISIBLE);
            signingInView.setVisibility(View.VISIBLE);

            cancelButton.setVisibility(View.INVISIBLE);

            navigateToStatusActivity();
        } else if (appState.isSigningIn()) {
            textView.setText(R.string.signing_in);
            cancelButton.setVisibility(View.VISIBLE);

            signInView.setVisibility(View.INVISIBLE);
            signingInView.setVisibility(View.VISIBLE);
        } else {
            signInView.setVisibility(View.VISIBLE);
            signingInView.setVisibility(View.INVISIBLE);

        }

        if (signInAttempted) {
            String error = getErrorText(appState.getLastError());
            Log.e("error", "" + appState.getLastError());
            if (error == null) {
                errorTextView.setText("");
            } else {
                errorTextView.setText(error);
            }
        }
    }

    private String getErrorText(com.zello.sdk.Error error) {
        switch (error) {
        case UNKNOWN:
            return getResources().getString(R.string.error_unknown);
        case INVALID_CREDENTIALS:
            return getResources().getString(R.string.error_invalid_credentials);
        case INVALID_NETWORK_NAME:
            return getResources().getString(R.string.error_invalid_network_name);
        case NETWORK_SUSPENDED:
            return getResources().getString(R.string.error_network_suspended);
        case SERVER_SECURE_CONNECT_FAILED:
            return getResources().getString(R.string.error_secure_connect_failed);
        case SERVER_SIGNIN_FAILED:
            return getResources().getString(R.string.error_server_signin_failed);
        case NETWORK_SIGNIN_FAILED:
            return getResources().getString(R.string.error_network_signin_failed);
        case KICKED:
            return getResources().getString(R.string.error_kicked);
        case APP_UPDATE_REQUIRED:
            return getResources().getString(R.string.error_update_required);
        case NO_INTERNET_CONNECTION:
            return getResources().getString(R.string.error_no_internet);
        case INTERNET_CONNECTION_RESTRICTED:
            return getResources().getString(R.string.error_internet_restricted);
        case SERVER_LICENSE_PROBLEM:
            return getResources().getString(R.string.error_server_license);
        case TOO_MANY_SIGNIN_ATTEMPTS:
            return getResources().getString(R.string.error_brute_force_protection);
        default:
            return null;
        }
    }

    private void hideKeyboard() {
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    private void navigateToStatusActivity() {
        Log.e("business", "" + _settings.getBoolean("businessSettingsLoaded", false));

        // if (_settings.getBoolean("businessSettingsLoaded", false)) {
        VehicleStatus status = VehicleStatus.LOGIN;
        //sendMessage(status);
        //            SharedPreferences.Editor editor = _settings.edit();
        //            editor.commit();
        Intent s = new Intent(getApplicationContext(), StatusActivity.class);//pre-trip
        if (_settings.getBoolean("preTripEnabled", false)) {
            s.putExtra("motive", "pre-trip");
        } else {
            s.putExtra("motive", "log-in");
        }
        s.putExtra("newStatus", "Clock In");
        startActivity(s);
        finish();
        //progressBarLogin.setVisibility(View.INVISIBLE);
        // }
    }
}