Android Open Source - Icinga-Mobile Login Activity






From Project

Back to project page Icinga-Mobile.

License

The source code is released under:

GNU General Public License

If you think the Android project Icinga-Mobile listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mhst.dreamteam.UI;
/*  ww w . ja  va  2s.c o  m*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import java.security.InvalidParameterException;

import mhst.dreamteam.IcingaClient.GlobalConfig;
import mhst.dreamteam.IcingaClient.GlobalConst;
import mhst.dreamteam.IcingaClient.Interface.OnCompleteListener;
import mhst.dreamteam.R;
import mhst.dreamteam.IcingaClient.SessionMng.Session;

/**
 * Login activity
 *
 * @author MinhNN
 */
public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // Get control IDs
        final EditText etIp = (EditText) findViewById(R.id.etIp);       // Server address
        final EditText etUser = (EditText) findViewById(R.id.etUser);   // Username
        final EditText etPass = (EditText) findViewById(R.id.etPass);   // Password
        final CheckBox cbRem = (CheckBox) findViewById(R.id.cbPass);    // Remember me?
        final Button btLogin = (Button) findViewById(R.id.btLogIn);     // Log in
        final TextView tvStt = (TextView) findViewById(R.id.tvStt);     // Status

        // Fill data if saved before
        final SharedPreferences pref = getSharedPreferences(GlobalConfig.prefLoginInfo, MODE_PRIVATE);
        String sServer = pref.getString("Server", null);
        String sUser = pref.getString("Username", null);
        String sPw = pref.getString("_Pw", null);

        if (sServer != null && sUser != null && sPw != null) {
            etIp.setText(sServer);
            etUser.setText(sUser);
            etPass.setText(sPw);
            cbRem.setChecked(true);
        }


        // What do we do when "Log in" button clicked
        btLogin.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    OnCompleteListener listener = new OnCompleteListener() {
                        @Override
                        public void onComplete(Object obj, String sender) {
                            int result = (Integer) obj;
                            // Process result
                            switch (result) {
                                case GlobalConst.RETURNCODE_SUCCESS: // Log in successfully
                                    // If user wanna save login information
                                    SharedPreferences.Editor editor = pref.edit();
                                    if (cbRem.isChecked()) {
                                        editor.putString("Server", etIp.getText().toString());
                                        editor.putString("Username", etUser.getText().toString());
                                        editor.putString("_Pw", etPass.getText().toString());
                                        editor.apply();
                                    } else {
                                        editor.clear();
                                        editor.apply();
                                    }
                                    // Set login result and finish activity
                                    Intent i = new Intent("mhst.dreamteam.intent.SESSION_CHANGED");
                                    i.putExtra("LoginState", true);
                                    i.putExtra("Server", Session.getInstance().getWorkingServer());
                                    i.putExtra("Cookie", Session.getInstance().getCookie());
                                    sendBroadcast(i);
                                    setResult(result);
                                    finish();
                                    break;
                                case GlobalConst.ERROR_WRONG_USER_PASS: // Wrong username or password
                                    tvStt.setText(LoginActivity.this.getString(R.string.error_wrong_user_pass));
                                    break;
                                case GlobalConst.ERROR_UNKNOWN_HOST: // Connection error, unknown host or host unreachable
                                    new AlertDialog.Builder(LoginActivity.this)
                                            .setMessage(R.string.error_server_unreachable)
                                            .setTitle(R.string.error)
                                            .create().show();
                                    tvStt.setText(LoginActivity.this.getString(R.string.error_server_unreachable));
                                    break;
                                case GlobalConst.ERROR_CONNECTION_ERROR: // Connection error, IO error
                                    new AlertDialog.Builder(LoginActivity.this)
                                            .setMessage(R.string.error_connection_error)
                                            .setTitle(R.string.error)
                                            .create().show();
                                    tvStt.setText(LoginActivity.this.getString(R.string.error_connection_error));
                                    break;
                                default: // Log in failed, unknown error
                                    tvStt.setText(LoginActivity.this.getString(R.string.error_login_fail));
                            }
                        }
                    };

                    Session.getInstance().doLogin(LoginActivity.this, listener,
                            etIp.getText().toString(), etUser.getText().toString(), etPass.getText().toString());
                } catch (InvalidParameterException ex) {
                    // Empty field
                    tvStt.setText(LoginActivity.this.getString(R.string.error_empty_field));
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }
}




Java Source Code List

mhst.dreamteam.ApplicationContext.java
mhst.dreamteam.ApplicationTest.java
mhst.dreamteam.ApplicationTest.java
mhst.dreamteam.MainActivity.java
mhst.dreamteam.IcingaClient.GlobalConfig.java
mhst.dreamteam.IcingaClient.GlobalConst.java
mhst.dreamteam.IcingaClient.Controller.NetControllerTest.java
mhst.dreamteam.IcingaClient.Controller.NetController.java
mhst.dreamteam.IcingaClient.Icinga.IcingaApiConst.java
mhst.dreamteam.IcingaClient.Icinga.IcingaApi.java
mhst.dreamteam.IcingaClient.Icinga.IcingaConst.java
mhst.dreamteam.IcingaClient.Icinga.IcingaExecutor.java
mhst.dreamteam.IcingaClient.Icinga.IcingaParam.java
mhst.dreamteam.IcingaClient.Icinga.IcingaUdt.java
mhst.dreamteam.IcingaClient.Icinga.package-info.java
mhst.dreamteam.IcingaClient.Interface.OnCompleteListener.java
mhst.dreamteam.IcingaClient.Interface.OnPieChartClickListener.java
mhst.dreamteam.IcingaClient.Json.JsonHelperTest.java
mhst.dreamteam.IcingaClient.Json.JsonHelper.java
mhst.dreamteam.IcingaClient.Misc.CookieMng.java
mhst.dreamteam.IcingaClient.Misc.CookieTest.java
mhst.dreamteam.IcingaClient.SessionMng.LogInTest.java
mhst.dreamteam.IcingaClient.SessionMng.Login.java
mhst.dreamteam.IcingaClient.SessionMng.Logout.java
mhst.dreamteam.IcingaClient.SessionMng.Session.java
mhst.dreamteam.IcingaService.ApplicationContext.java
mhst.dreamteam.IcingaService.DataUpdater.java
mhst.dreamteam.IcingaService.MessageReveicer.java
mhst.dreamteam.IcingaService.NotiBuilder.java
mhst.dreamteam.IcingaService.SQLHelper.java
mhst.dreamteam.IcingaService.SessionProvider.java
mhst.dreamteam.UI.Color.java
mhst.dreamteam.UI.GradientLine.java
mhst.dreamteam.UI.HostDetailsFragment.java
mhst.dreamteam.UI.HostlistAdapter.java
mhst.dreamteam.UI.HostlistFragment.java
mhst.dreamteam.UI.LoginActivity.java
mhst.dreamteam.UI.OverviewFragment.java
mhst.dreamteam.UI.PieGraph.java
mhst.dreamteam.UI.ProgressDialog.java
mhst.dreamteam.UI.ServiceDetailsFragment.java
mhst.dreamteam.UI.ServicelistAdapter.java
mhst.dreamteam.UI.ServicelistFragment.java
org.json.CDL.java
org.json.CookieList.java
org.json.Cookie.java
org.json.HTTPTokener.java
org.json.HTTP.java
org.json.JSONArray.java
org.json.JSONException.java
org.json.JSONML.java
org.json.JSONObject.java
org.json.JSONString.java
org.json.JSONStringer.java
org.json.JSONTokener.java
org.json.JSONWriter.java
org.json.Kim.java
org.json.Property.java
org.json.XMLTokener.java
org.json.XML.java
org.json.zip.BitInputStream.java
org.json.zip.BitOutputStream.java
org.json.zip.BitReader.java
org.json.zip.BitWriter.java
org.json.zip.Huff.java
org.json.zip.JSONzip.java
org.json.zip.Keep.java
org.json.zip.None.java
org.json.zip.PostMortem.java
org.json.zip.Unzipper.java
org.json.zip.Zipper.java