Android Open Source - mitlocate Login Activity






From Project

Back to project page mitlocate.

License

The source code is released under:

MIT License

If you think the Android project mitlocate 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 edu.mit.locate;
/* w ww  .j a  va 2  s  .  c  om*/
import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {

  private SharedPreferences prefs;
  private String deviceID, OTP;
  private int authorization;
  private RelativeLayout mMITLogo;
  private LinearLayout mOTPBox;
  private TextView mMappLabel, mHelpLabel, mOTPLabel, mOTP, mNextButton;
  private final String TAG = "LoginActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.activity_login);

    prefs = getSharedPreferences("MITLOCATE", 0);
    deviceID = prefs.getString("DeviceID", null);
    OTP = prefs.getString("OTP", null);
    authorization = prefs.getInt("Authorization", 0);

    mMITLogo = (RelativeLayout)findViewById(R.id.mit_logo);
    mMappLabel = (TextView)findViewById(R.id.mappLabel);
    mMappLabel.setVisibility(View.VISIBLE);
    mHelpLabel = (TextView)findViewById(R.id.helpLabel);
    mOTPBox = (LinearLayout)findViewById(R.id.OTPBox);
    mOTPLabel = (TextView)findViewById(R.id.OTPLabel);
    mOTP = (TextView)findViewById(R.id.OTP);
    mNextButton = (TextView)findViewById(R.id.nextButton);

    Typeface lumos = Typeface.createFromAsset(getAssets(), "lumos.ttf");
    Typeface quint = Typeface.createFromAsset(getAssets(), "Quintessential.ttf");
    //mHelpLabel.setTypeface(lumos);
    //mOTP.setTypeface(quint);
    //mOTPLabel.setTypeface(lumos);
    mMappLabel.setTypeface(lumos);
    mNextButton.setTypeface(lumos);

    String s = "\n Device: " + android.os.Build.DEVICE;
    s += "\n Model (and Product): " + android.os.Build.MODEL + " ("+ android.os.Build.PRODUCT + ")";
    Log.d("DevInfo",s);
    
    mMITLogo.setOnLongClickListener(new OnLongClickListener(){
      @Override
      public boolean onLongClick(View v) {
        final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://locate.mit.edu:444/otp/"+prefs.getString("OTP", null)));
        Editor editor = prefs.edit();
        editor.putBoolean("CertCheck", true);
        editor.commit();
        startActivity(intent);
        return false;
      }
    });

    mNextButton.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        login_check(true);
      }
    });

    //Should happen only on first run
    if (deviceID == null) {
      deviceID = (UUID.randomUUID().toString()+UUID.randomUUID().toString()).replaceAll("-", "");
      Editor editor = prefs.edit();
      editor.putString("DeviceID",deviceID);
      editor.commit();
      Log.i(TAG,"DeviceID Generated: "+deviceID);
    }

    if(authorization < 0) {
      showDisabledUI();
    }

    if(prefs.getBoolean("CertCheck", false)){
      login_check(false);
      Editor editor = prefs.edit();
      editor.putBoolean("CertCheck", false);
      editor.commit();
    }

    if(OTP != null){
      //mLoadingBar.setVisibility(View.INVISIBLE);
      mHelpLabel.setVisibility(View.VISIBLE);
      mNextButton.setVisibility(View.VISIBLE);
      if (authorization == 0) {
        Log.i(TAG,"Code received not yet attached, assuming cached code is still valid.");
        mOTPLabel.setVisibility(View.VISIBLE);
        mOTP.setText(OTP);
        showCodeUI("Activate");
      } else {
        login_check(false);
      }
      // else {
      //  approve(); //Instant approval once device is attached to account
      //}
    } else {
      login_check(false);
    }
  }

  private void login_check(final boolean clicked) {
    mMappLabel.setVisibility(View.VISIBLE);
    //mHelpLabel.setText("Connecting...");

    Log.w(TAG,"running login_check");
    JSONObject jsonParams = new JSONObject();
    try{
      jsonParams.put("UUID", deviceID);
    }catch(JSONException e1){
      e1.printStackTrace();
    }
    new MITHttpsClient("auth", this, true){
      @Override
      protected void onPostExecute(JSONObject response){
        if(response!=null){
          try {
            final String status = response.getString("status");
            if(status.equals("ok")){
              if(prefs.getString("FullName", null)==null){
                Toast.makeText(getApplicationContext(), "Welcome, "+response.getString("name")+"!", Toast.LENGTH_LONG).show();
              }
              Editor editor = prefs.edit();
              editor.putString("FullName", response.getString("fullname"));
              editor.putString("Email", response.getString("email"));
              editor.putInt("Authorization", 1);
              editor.commit();

              mOTP.setText(prefs.getString("DeviceCode",""));
              showCodeUI("Activate");
              approve();
            }else if(status.equals("unverified")){                            
              Editor editor = prefs.edit();
              editor.putString("OTP", response.getString("otp"));
              editor.putInt("Authorization", 0);
              editor.commit();

              mOTP.setText(response.getString("otp"));
              if(clicked)
                Toast.makeText(LoginActivity.this, "Whoops! You haven't entered the code yet.", Toast.LENGTH_SHORT).show();
              showCodeUI("Activate");
            }else if(status.equals("banned")){
              Editor editor = prefs.edit();
              editor.putInt("Authorization", -1);
              editor.commit();

              showDisabledUI();
            } else {
              showErrorUI("Unknown server response.");
              Log.e(TAG,"Unmatched status: "+status);
            }
          }catch (JSONException e) {
            Toast.makeText(LoginActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
            showErrorUI("Invalid server response.");
            e.printStackTrace();
          }
        } else {
          Toast.makeText(LoginActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
          showErrorUI(response.toString());
        }
      }
    }.execute(jsonParams);
  }

  private void showCodeUI(String btnText){
    //mLoadingBar.setVisibility(View.INVISIBLE);
    mHelpLabel.setText(Html.fromHtml("Enter this code at <b>locate.mit.edu</b>."));
    mHelpLabel.setVisibility(View.VISIBLE);
    mOTPLabel.setVisibility(View.VISIBLE);
    mOTP.setVisibility(View.VISIBLE);
    mNextButton.setText(btnText);
    mNextButton.setVisibility(View.VISIBLE);
  }

  private void showErrorUI(String errorMsg){
    //mLoadingBar.setVisibility(View.INVISIBLE);
    mHelpLabel.setText(Html.fromHtml("<b>Error</b><br>"+errorMsg));
    mHelpLabel.setVisibility(View.VISIBLE);
    //Hide device code/label if there isn't one stored
    mOTPLabel.setVisibility(View.VISIBLE);
    mOTP.setVisibility(View.VISIBLE);
    mNextButton.setText("Retry");
    mNextButton.setVisibility(View.VISIBLE);
  }

  private void showDisabledUI(){
    mMappLabel.setVisibility(View.VISIBLE);
    mOTPLabel.setVisibility(View.INVISIBLE);
    mOTP.setVisibility(View.INVISIBLE);
    mOTPBox.setVisibility(View.GONE);
    mNextButton.setVisibility(View.INVISIBLE);
    mHelpLabel.setText(Html.fromHtml("<b>Your account has been disabled.</b><br>Muggles can't see Hogwarts.<br><br>Send a message to<br><b>locate@mit.edu</b><br>for more information."));
  }

  private void approve(){
    Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
    finish();
  }

  @Override
  public void onClick(View arg0) {
  }
}




Java Source Code List

edu.mit.locate.APEditorActivity.java
edu.mit.locate.BootLocationService.java
edu.mit.locate.LoginActivity.java
edu.mit.locate.MITHttpsClient.java
edu.mit.locate.MITLocationService.java
edu.mit.locate.MainActivity.java
edu.mit.locate.alerts.Alert.java
edu.mit.locate.alerts.AlertsListAdapter.java
edu.mit.locate.alerts.AlertsTab.java
edu.mit.locate.apeditor.APListAdapter.java
edu.mit.locate.apeditor.APScanResult.java
edu.mit.locate.friends.FriendActivity.java
edu.mit.locate.friends.Friend.java
edu.mit.locate.friends.FriendsListAdapter.java
edu.mit.locate.friends.FriendsTab.java
edu.mit.locate.tabs.IconPagerAdapter.java
edu.mit.locate.tabs.IcsLinearLayout.java
edu.mit.locate.tabs.NavigationTab.java
edu.mit.locate.tabs.PageIndicator.java
edu.mit.locate.tabs.SearchTab.java
edu.mit.locate.tabs.SettingsTab.java
edu.mit.locate.tabs.TabPageIndicator.java
edu.mit.locate.tabs.TabPagerAdapter.java