Android Open Source - mha-android Login Activity






From Project

Back to project page mha-android.

License

The source code is released under:

Copyright (c) 2011-2012 Cameron Porter, Ryan Brown http://github.com/camporter/mha-android Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated...

If you think the Android project mha-android 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 com.teamacra.myhomeaudio.ui;
//from  ww w .j  a v  a2s . co  m
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.teamacra.myhomeaudio.MHAApplication;
import com.teamacra.myhomeaudio.R;
import com.teamacra.myhomeaudio.discovery.DiscoverySearch;
import com.teamacra.myhomeaudio.http.HttpClient;

public class LoginActivity extends SherlockActivity implements View.OnClickListener {

  private Button loginButton;
  private Button newUserButton;
  
  private Bundle intentBundle;

  /**
   * Creates the LoginActivity UI, sets up the click listener.
   */
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setTheme(R.style.Theme_Sherlock);
    setContentView(R.layout.login);

    // Register the click listeners for the buttons
    this.loginButton = (Button) this.findViewById(R.id.loginButton);
    this.loginButton.setOnClickListener(this);
    this.newUserButton = (Button) this.findViewById(R.id.newUserButton);
    this.newUserButton.setOnClickListener(this);
    
    intentBundle = getIntent().getExtras();
  }

  public void onStart() {
    super.onStart();

    checkBluetooth();
    
    new RunDiscovery().execute();
  }

  /**
   * Runs when the LoginActivity resumes. Checks to see if a user is logged
   * in. If they are, forward the user on to the MyHomeAudioActivity instead.
   * 
   * @param savedInstanceState
   */
  public void onResume() {
    super.onResume();

    MHAApplication app = (MHAApplication) this.getApplication();

    // Check to make sure the user is not already logged in and/or configured
    if (app.isLoggedIn() && app.isConfigured()) {
      // Logged in and configured
      startActivity(new Intent(this, MyHomeAudioActivity.class));
      
    } else if (app.isLoggedIn()) {
      // Logged in but not configured
      Bundle extras = getIntent().getExtras();
      
      if (extras != null && extras.getBoolean("finish")) {
        // Tells the activity that we should finish, ending the program
        finish();
        return;
      }
      
      startActivity(new Intent(this, InitialConfigActivity.class));
      
    } else {
      // Not logged in
    }

    // Check wifi, update the server connection status

  }

  @Override
  public void onClick(View view) {

    if (view == this.loginButton) {
      // Begin to log the user in if they press the button

      String username = ((EditText) this.findViewById(R.id.usernameEditText)).getText()
          .toString().trim();
      String password = ((EditText) this.findViewById(R.id.passwordEditText)).getText()
          .toString().trim();

      if (username.length() > 0 && password.length() > 0) {
        new LogInUser().execute(username, password);
      } else {
        Toast.makeText(this, getText(R.string.complete_username_password),
            Toast.LENGTH_SHORT).show();
      }
    } else if (view == this.newUserButton) {
      // Send the user to the RegisterActivity
      Intent registerIntent = new Intent(this, RegisterActivity.class);
      this.startActivity(registerIntent);
    }
  }

  /**
   * Checks if Bluetooth is on. Prompts user to turn it on when off.
   * 
   * @return Whether bluetooth is on or not.
   */
  private boolean checkBluetooth() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
      AlertDialog.Builder noBluetooth = new AlertDialog.Builder(this);
      noBluetooth.setTitle("No Bluetooth Available");
      noBluetooth.setMessage("Your Android device does not support bluetooth.");
      noBluetooth.setNeutralButton("Quit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          LoginActivity.this.finish();
        }
      });
    } else if (!bluetoothAdapter.isEnabled()) {
      Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBTIntent, 3);
      return false;
    }
    return true;
  }

  private class RunDiscovery extends AsyncTask<String, Void, String> {

    private ProgressDialog progressDialog;
    private AlertDialog failureDialog;

    protected void onPreExecute() {
      progressDialog = new ProgressDialog(LoginActivity.this);
      progressDialog.setMessage(getText(R.string.server_find));
      progressDialog.show();
    }

    protected String doInBackground(String... args) {

      WifiManager wifi = (WifiManager) LoginActivity.this
          .getSystemService(android.content.Context.WIFI_SERVICE);

      DhcpInfo dhcp = wifi.getDhcpInfo();
      DiscoverySearch discovery = new DiscoverySearch("myhomeaudio", dhcp);

      String result = discovery.run();
      discovery.end();
      return result;

    }

    protected void onPostExecute(final String serverAddress) {
      MHAApplication app = (MHAApplication) LoginActivity.this.getApplication();
      if (serverAddress == null) {
        // Didn't find the server, don't let the user continue
        // Disable login button
        loginButton.setEnabled(false);

        // Show error dialog
        AlertDialog.Builder failure = new AlertDialog.Builder(LoginActivity.this);
        failure.setTitle(getText(R.string.server_find_failed_title));
        failure.setMessage(getText(R.string.server_find_failed_detail));
        failure.setNegativeButton(getText(R.string.quit),
            new DialogInterface.OnClickListener() {

              public void onClick(DialogInterface dialog, int which) {
                // Close the activity
                failureDialog.dismiss();
                LoginActivity.this.finish();
              }
            });
        failure.setPositiveButton(getText(R.string.retry),
            new DialogInterface.OnClickListener() {

              public void onClick(DialogInterface dialog, int which) {
                // Run the discovery process again
                failureDialog.dismiss();
                new RunDiscovery().execute();
              }
            });

        this.failureDialog = failure.create();
        this.failureDialog.show();
      } else {
        // Found the server, save the address and let the user continue
        app.setServerAddress(serverAddress);

        // Update the address text view for debug purposes
        TextView addrTextView = (TextView) LoginActivity.this
            .findViewById(R.id.serverAddressTextView);
        addrTextView.setText(getText(R.string.login_server_address) + " " + serverAddress);
        // Enable the login button
        LoginActivity.this.loginButton.setEnabled(true);
      }
      this.progressDialog.dismiss();
    }
  }

  /**
   * Does the actual login, and shows a dialog while the user waits, and if
   * the login fails.
   * 
   * @author Cameron
   * 
   */
  private class LogInUser extends AsyncTask<String, Void, String[]> {

    private final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
    private AlertDialog failureDialog;
    MHAApplication app = (MHAApplication) LoginActivity.this.getApplication();

    protected void onPreExecute() {
      this.progressDialog.setMessage(getText(R.string.logging_in_wait));
      this.progressDialog.show();
    }

    protected String[] doInBackground(String... args) {
      String username = args[0];
      String password = args[1];

      // Do actual login here...
      HttpClient client = new HttpClient(app);
      return client.login(username, password);
    }

    protected void onPostExecute(final String[] result) {
      final String sessionId = result[0];
      final boolean configured = Boolean.parseBoolean(result[1]);

      if (sessionId != null) {
        // Login successful, set our app variables
        String username = ((EditText) LoginActivity.this
            .findViewById(R.id.usernameEditText)).getText().toString();
        String password = ((EditText) LoginActivity.this
            .findViewById(R.id.passwordEditText)).getText().toString();

        app.setLoggedIn(username, password, sessionId, configured);
        this.progressDialog.dismiss();

        Intent mhaIntent;
        if (app.isConfigured()) {
          mhaIntent = new Intent(LoginActivity.this, MyHomeAudioActivity.class);
        } else {
          mhaIntent = new Intent(LoginActivity.this, InitialConfigActivity.class);
        }

        LoginActivity.this.startActivity(mhaIntent);
      } else {
        // Login failed, let the user know with an AlertDialog
        this.progressDialog.dismiss();

        app.setLoggedOut();

        AlertDialog.Builder failure = new AlertDialog.Builder(LoginActivity.this);
        failure.setTitle(getText(R.string.login_failed_title));
        failure.setMessage(getText(R.string.login_failed_detail));
        failure.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

          public void onClick(DialogInterface dialog, int which) {
            failureDialog.dismiss();
          }
        });

        this.failureDialog = failure.create();
        this.failureDialog.show();
      }
    }
  }

}




Java Source Code List

com.teamacra.myhomeaudio.MHAApplication.java
com.teamacra.myhomeaudio.bluetooth.BluetoothService.java
com.teamacra.myhomeaudio.discovery.DiscoveryConstants.java
com.teamacra.myhomeaudio.discovery.DiscoveryDescription.java
com.teamacra.myhomeaudio.discovery.DiscoverySearchListener.java
com.teamacra.myhomeaudio.discovery.DiscoverySearch.java
com.teamacra.myhomeaudio.discovery.MDNSDiscovery.java
com.teamacra.myhomeaudio.http.HttpBase.java
com.teamacra.myhomeaudio.http.HttpClient.java
com.teamacra.myhomeaudio.http.HttpNode.java
com.teamacra.myhomeaudio.http.HttpSource.java
com.teamacra.myhomeaudio.http.HttpStream.java
com.teamacra.myhomeaudio.http.StatusCode.java
com.teamacra.myhomeaudio.locations.NodeSignalRange.java
com.teamacra.myhomeaudio.locations.NodeSignature.java
com.teamacra.myhomeaudio.manager.ConfigurationManager.java
com.teamacra.myhomeaudio.manager.LocationManager.java
com.teamacra.myhomeaudio.manager.NodeManager.java
com.teamacra.myhomeaudio.manager.StreamManager.java
com.teamacra.myhomeaudio.media.MediaDescriptor.java
com.teamacra.myhomeaudio.node.Node.java
com.teamacra.myhomeaudio.source.Source.java
com.teamacra.myhomeaudio.stream.StreamAction.java
com.teamacra.myhomeaudio.stream.Stream.java
com.teamacra.myhomeaudio.ui.InitialConfigActivity.java
com.teamacra.myhomeaudio.ui.LoginActivity.java
com.teamacra.myhomeaudio.ui.MyHomeAudioActivity.java
com.teamacra.myhomeaudio.ui.RegisterActivity.java
com.teamacra.myhomeaudio.ui.fragment.SongFragment.java
com.teamacra.myhomeaudio.ui.fragment.SourceFragment.java
com.teamacra.myhomeaudio.ui.fragment.TestFragment.java