Android Open Source - face_rec_android Setup Activity






From Project

Back to project page face_rec_android.

License

The source code is released under:

GNU General Public License

If you think the Android project face_rec_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 in.amolgupta.helpingfaceless.activities;
/* w  w w  . j a v  a 2 s .c  om*/
import in.amolgupta.helpingfaceless.R;
import in.amolgupta.helpingfaceless.common.Constants;
import in.amolgupta.helpingfaceless.utils.ET;
import in.amolgupta.helpingfaceless.utils.RequestUtils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.LoggingBehavior;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;
import com.facebook.model.GraphUser;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.plus.PlusClient;
import com.squareup.okhttp.OkHttpClient;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
public class SetupActivity extends ActionBarActivity implements
    View.OnClickListener, ConnectionCallbacks, OnConnectionFailedListener {
  /**
   * A dummy authentication store containing known user names and passwords.
   * TODO: remove after connecting to a real authentication system.
   */
  private static final String[] DUMMY_CREDENTIALS = new String[] {
      "foo@example.com:hello", "bar@example.com:world" };

  /**
   * The default email to populate the email field with.
   */
  public static final String EXTRA_EMAIL = "in.amolgupta.helpingfaceless.extra.EMAIL";

  /**
   * Keep track of the login task to ensure we can cancel it if requested.
   */
  private UserLoginTask mAuthTask = null;
  private GraphUser user;

  // Values for email and password at the time of the login attempt.
  private String mEmail;
  private String mPassword;

  // UI references.
  private EditText mEmailView;
  private EditText mPasswordView;
  private View mLoginFormView;
  private View mLoginStatusView;
  private TextView mLoginStatusMessageView;
  private SignInButton GPlusSigninButton;
  private static final String TAG = "ExampleActivity";
  private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
  private Button facebookButton;
  private ProgressDialog mConnectionProgressDialog;
  private PlusClient mPlusClient;
  private ConnectionResult mConnectionResult;
  private Session.StatusCallback statusCallback = new SessionStatusCallback();

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

    setContentView(R.layout.activity_setup);

    // Set up the login form.
    mEmail = getIntent().getStringExtra(EXTRA_EMAIL);
    mEmailView = (EditText) findViewById(R.id.email);
    GPlusSigninButton = (SignInButton) findViewById(R.id.plus_sign_in_button);
    facebookButton = (Button) findViewById(R.id.btn_fb);
    facebookButton.setOnClickListener(this);
    mEmailView.setText(mEmail);
    mEmailView.requestFocus();
    InputMethodManager inputManager = (InputMethodManager) this
        .getSystemService(INPUT_METHOD_SERVICE);
    inputManager.restartInput(mEmailView);
    mPasswordView = (EditText) findViewById(R.id.edt_phone_number);
    mPasswordView
        .setOnEditorActionListener(new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView textView, int id,
              KeyEvent keyEvent) {
            if (id == R.id.login || id == EditorInfo.IME_NULL) {
              attemptLogin();
              return true;
            }
            return false;
          }
        });
    getSupportActionBar().hide();
    // Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
    // Account[] accounts = AccountManager.get(this).getAccounts();
    // for (Account account : accounts) {
    // if (emailPattern.matcher(account.name).matches()) {
    // mEmailView.setText(account.name);
    // break;
    // }
    // }
    // TelephonyManager tMgr = (TelephonyManager) this
    // .getSystemService(Context.TELEPHONY_SERVICE);
    // mPasswordView.setText(tMgr.getLine1Number());
    mLoginFormView = findViewById(R.id.login_form);
    mLoginStatusView = findViewById(R.id.login_status);
    mLoginStatusMessageView = (TextView) findViewById(R.id.login_status_message);

    findViewById(R.id.sign_in_button).setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            attemptLogin();
          }
        });

    SharedPreferences pref = getApplicationContext().getSharedPreferences(
        "MyPref", 0); // 0 - for private
                // mode
    if (pref.getBoolean("isLoggedIn", false)) {
      Constants.mIsLoggedIN = true;
      Intent mDashBoardIntent = new Intent(SetupActivity.this,
          HomeActivity.class);
      startActivity(mDashBoardIntent);
      finish();

    }
    GPlusSigninButton.setOnClickListener(this);
    mPlusClient = new PlusClient.Builder(this, this, this)
        .setActions(

        "http://schemas.google.com/BuyActivity")
        .setScopes(Scopes.PLUS_LOGIN,
            "https://www.googleapis.com/auth/userinfo.email")
        .build();
    mConnectionProgressDialog = new ProgressDialog(this);
    mConnectionProgressDialog.setMessage("Signing in...");
    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

    Session session = Session.getActiveSession();
    if (session == null) {
      if (savedInstanceState != null) {
        session = Session.restoreSession(this, null, statusCallback,
            savedInstanceState);
      }
      if (session == null) {
        session = new Session(this);
      }
      Session.setActiveSession(session);
      if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
        session.openForRead(new Session.OpenRequest(this)
            .setCallback(statusCallback));
      }
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.setup, menu);
    return true;
  }

  /**
   * Attempts to sign in or register the account specified by the login form.
   * If there are form errors (invalid email, missing fields, etc.), the
   * errors are presented and no actual login attempt is made.
   */
  public void attemptLogin() {
    if (mAuthTask != null) {
      return;
    }

    // Reset errors.
    mEmailView.setError(null);
    mPasswordView.setError(null);

    // Store values at the time of the login attempt.
    mEmail = mEmailView.getText().toString();
    mPassword = mPasswordView.getText().toString();

    boolean cancel = false;
    View focusView = null;

    // Check for a valid password.
    if (TextUtils.isEmpty(mPassword)) {
      mPasswordView.setError(getString(R.string.error_field_required));
      focusView = mPasswordView;
      cancel = true;
    } else if (mPassword.length() < 4) {
      mPasswordView.setError(getString(R.string.error_invalid_password));
      focusView = mPasswordView;
      cancel = true;
    }

    // Check for a valid email address.
    if (TextUtils.isEmpty(mEmail)) {
      mEmailView.setError(getString(R.string.error_field_required));
      focusView = mEmailView;
      cancel = true;
    } else if (!mEmail.contains("@")) {
      mEmailView.setError(getString(R.string.error_invalid_email));
      focusView = mEmailView;
      cancel = true;
    }

    if (cancel) {
      // There was an error; don't attempt login and focus the first
      // form field with an error.
      focusView.requestFocus();
    } else {
      // Show a progress spinner, and kick off a background task to
      // perform the user login attempt.
      mLoginStatusMessageView.setText(R.string.login_progress_signing_in);
      showProgress(true);
      mAuthTask = new UserLoginTask(user);
      mAuthTask.execute((Void) null);
    }
  }

  /**
   * Shows the progress UI and hides the login form.
   */
  @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  private void showProgress(final boolean show) {
    // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
    // for very easy animations. If available, use these APIs to fade-in
    // the progress spinner.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
      int shortAnimTime = getResources().getInteger(
          android.R.integer.config_shortAnimTime);

      mLoginStatusView.setVisibility(View.VISIBLE);
      mLoginStatusView.animate().setDuration(shortAnimTime)
          .alpha(show ? 1 : 0)
          .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
              mLoginStatusView.setVisibility(show ? View.VISIBLE
                  : View.GONE);
            }
          });

      mLoginFormView.setVisibility(View.VISIBLE);
      mLoginFormView.animate().setDuration(shortAnimTime)
          .alpha(show ? 0 : 1)
          .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
              mLoginFormView.setVisibility(show ? View.GONE
                  : View.VISIBLE);
            }
          });
    } else {
      // The ViewPropertyAnimator APIs are not available, so simply show
      // and hide the relevant UI components.
      mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
      mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
    }
  }

  /**
   * Represents an asynchronous login/registration task used to authenticate
   * the user.
   */
  public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
    OkHttpClient client = new OkHttpClient();
    private GraphUser user;

    public UserLoginTask(GraphUser user) {
      this.user = user;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
      try {
        String email = user.asMap().get("email").toString()!=null?user.asMap().get("email").toString():"user@facebook.com";
        String result = RequestUtils.get(
            new URL(Uri
                .parse(Constants.mAuthUrl)
                .buildUpon()
                .appendQueryParameter("phone_number",
                    user.getUsername())
                .appendQueryParameter(
                    "email",
                    email).build()
                .toString()
                + "%0A"), client);
        Log.d("HF_API", result);
      } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
      }

      for (String credential : DUMMY_CREDENTIALS) {
        String[] pieces = credential.split(":");
        if (pieces[0].equals(mEmail)) {
          // Account exists, return true if the password matches.
          return pieces[1].equals(mPassword);
        }
      }

      // TODO: register the new account here.
      return true;
    }

    @Override
    protected void onPostExecute(final Boolean success) {
      mAuthTask = null;
      showProgress(false);

      if (success) {
        SharedPreferences pref = getApplicationContext()
            .getSharedPreferences("MyPref", 0); // 0 - for private
                              // mode
        Editor editor = pref.edit();

        editor.putString("session", "string value"); // Storing string
        editor.putBoolean("isLoggedIn", true); // Storing string

        editor.commit(); // commit changes

        Constants.mIsLoggedIN = true;
        if (!Constants.mIsProd) {
          Intent mDashBoardIntent = new Intent(SetupActivity.this,
              HomeActivity.class);
          startActivity(mDashBoardIntent);
          finish();
        } else {
          Toast.makeText(
              SetupActivity.this,
              "Please ,ail the the admin/developer to gain access to the application.",
              Toast.LENGTH_LONG).show();
        }
      } else {
        mPasswordView
            .setError(getString(R.string.error_incorrect_password));
        mPasswordView.requestFocus();
      }
    }

    @Override
    protected void onCancelled() {
      mAuthTask = null;
      showProgress(false);
    }
  }

  @Override
  public void onClick(View view) {
    if (view.getId() == R.id.plus_sign_in_button
        && !mPlusClient.isConnected()) {
      if (mConnectionResult == null) {
        mPlusClient.connect();
        mConnectionProgressDialog.show();
      } else {
        try {
          mConnectionResult.startResolutionForResult(this,
              REQUEST_CODE_RESOLVE_ERR);
        } catch (SendIntentException e) {
          // Try connecting again.
          mConnectionResult = null;
          mPlusClient.connect();
        }
      }
    }
    if (view.getId() == R.id.btn_fb) {
      onClickLogin();
    }
  }

  @Override
  public void onConnected(Bundle connectionHint) {
    mConnectionProgressDialog.dismiss();
    mPlusClient.getAccountName();
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
  }

  @Override
  protected void onStart() {
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);

  }

  @Override
  protected void onStop() {
    super.onStop();
    // mPlusClient.disconnect();
    Session.getActiveSession().removeCallback(statusCallback);

  }

  @Override
  public void onConnectionFailed(ConnectionResult result) {
    if (mConnectionProgressDialog.isShowing()) {
      // The user clicked the sign-in button already. Start to resolve
      // connection errors. Wait until onConnected() to dismiss the
      // connection dialog.
      if (result.hasResolution()) {
        try {
          result.startResolutionForResult(this,
              REQUEST_CODE_RESOLVE_ERR);
        } catch (SendIntentException e) {
          mPlusClient.connect();
        }
      }
    }
    // Save the result and resolve the connection failure upon a user click.
    mConnectionResult = result;
  }

  private void updateView() {
    Session session = Session.getActiveSession();
    if (session.isOpened()) {
      SharedPreferences pref = getApplicationContext()
          .getSharedPreferences("MyPref", 0); // 0 - for private
                            // mode
      Editor editor = pref.edit();

      editor.putString("session", "string value"); // Storing string
      editor.putBoolean("isLoggedIn", true); // Storing string

      editor.commit(); // commit changes

      Constants.mIsLoggedIN = true;

      /* Send user details */
      makeMeRequest(session);
      ET.trackFaceBookLogin(session.getActiveSession().getAccessToken());
    


    } else {
      facebookButton.setText("login");
      facebookButton.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
          onClickLogin();
        }
      });
    }
  }

  private void onClickLogout() {
    Session session = Session.getActiveSession();
    if (!session.isClosed()) {
      session.closeAndClearTokenInformation();
    }
  }

  private void onClickLogin() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()) {
      session.openForRead(new Session.OpenRequest(this).setCallback(
          statusCallback).setPermissions(Arrays.asList("email")));
    } else {
      Session.openActiveSession(this, true, statusCallback);
    }
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
  }

  @Override
  protected void onActivityResult(int requestCode, int responseCode,
      Intent intent) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR
        && responseCode == RESULT_OK) {
      mConnectionResult = null;
      mPlusClient.connect();
    }
    Session.getActiveSession().onActivityResult(this, requestCode,
        responseCode, intent);
  }

  @Override
  public void onDisconnected() {
    Log.d(TAG, "disconnected");
  }

  private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state,
        Exception exception) {
      updateView();
    }
  }

  private void makeMeRequest(final Session session) {
    // Make an API call to get user data and define a
    // new callback to handle the response.
    Request request = Request.newMeRequest(session,
        new Request.GraphUserCallback() {

          @Override
          public void onCompleted(GraphUser user, Response response) {
            // If the response is successful
            if (session == Session.getActiveSession()) {
              if (user != null) {
                SetupActivity.this.user = user;
                UserLoginTask task = new UserLoginTask(user);
                task.execute();
              }
            }
            if (response.getError() != null) {
              // Handle errors, will do so later.
            }
          }
        });
    request.executeAsync();
  }
}




Java Source Code List

in.amolgupta.helpingfaceless.Views.Fragment.DashboardFragment.java
in.amolgupta.helpingfaceless.activities.HFBaseActivity.java
in.amolgupta.helpingfaceless.activities.HelpActivity.java
in.amolgupta.helpingfaceless.activities.HelpItemFragment.java
in.amolgupta.helpingfaceless.activities.HomeActivity.java
in.amolgupta.helpingfaceless.activities.ImageFragment.java
in.amolgupta.helpingfaceless.activities.PledgeFragment.java
in.amolgupta.helpingfaceless.activities.SetupActivity.java
in.amolgupta.helpingfaceless.activities.SigningActivity.java
in.amolgupta.helpingfaceless.activities.UploadForm.java
in.amolgupta.helpingfaceless.common.App.java
in.amolgupta.helpingfaceless.common.Constants.java
in.amolgupta.helpingfaceless.entities.ImageData.java
in.amolgupta.helpingfaceless.entities.NavItem.java
in.amolgupta.helpingfaceless.entities.TaskDetails.java
in.amolgupta.helpingfaceless.parser.CrowsourceDataParser.java
in.amolgupta.helpingfaceless.services.SendCSResponse.java
in.amolgupta.helpingfaceless.utils.ET.java
in.amolgupta.helpingfaceless.utils.LocationRequestData.java
in.amolgupta.helpingfaceless.utils.RequestUtils.java