Beispiel #1
0
  private void setGResult() {
    Person signedInUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

    if (signedInUser != null) {
      if (signedInUser.hasDisplayName()) {
        guname = signedInUser.getDisplayName();
      }

      guemail = Plus.AccountApi.getAccountName(mGoogleApiClient);

      // Toast.makeText(getApplicationContext(),guemail,Toast.LENGTH_LONG).show();
      if (signedInUser.hasImage()) {
        String[] profileparts = signedInUser.getImage().getUrl().split("\\?");
        guserProfilePicUrl = profileparts[0];
      }
      if (signedInUser.hasBirthday()) {
        bday = signedInUser.getBirthday();
        Toast.makeText(getApplicationContext(), bday, Toast.LENGTH_LONG).show();
      }
      if (signedInUser.hasGender()) {
        gender = String.valueOf(signedInUser.getGender());
        Toast.makeText(getApplicationContext(), gender, Toast.LENGTH_LONG).show();
      }
      pref = getApplicationContext().getSharedPreferences("unid", MODE_APPEND);
      SharedPreferences.Editor editor = pref.edit();
      editor.putString("unid", guemail);
      editor.commit();

      new Makerqust().execute();
    }
  }
  @Override
  public void onConnected(Bundle bundle) {
    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
      Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
      String personName = currentPerson.getDisplayName();
      String personPhoto = currentPerson.getImage().getUrl();
      String firstName = currentPerson.getName().getGivenName();
      String lastName = currentPerson.getName().getFamilyName();
      String personGooglePlusProfile = currentPerson.getUrl();
      String date = currentPerson.getBirthday();
      if (date == null) {
        date = "";
      }
      int gender = currentPerson.getGender();
      String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

      Intent mIntent = new Intent(getActivity(), LoginUserDetails.class);
      mIntent.putExtra("login_method", "google");
      mIntent.putExtra("email", email);
      mIntent.putExtra("first_name", firstName);
      mIntent.putExtra("gender", gender);
      mIntent.putExtra("last_name", lastName);
      mIntent.putExtra("photo", personPhoto);

      mixpanel.track("Login - Google");
      startActivity(mIntent);
    } else {
      Toast.makeText(getContext(), "You do not have a google+ account", Toast.LENGTH_SHORT).show();
      showSocialLoginDialog();
    }

    Log.d(TAG, "onConnected:" + bundle);
    mShouldResolve = false;
  }
  /**
   * onConnected is called when our Activity successfully connects to Google Play services.
   * onConnected indicates that an account was selected on the device, that the selected account has
   * granted any requested permissions to our app and that we were able to establish a service
   * connection to Google Play services.
   */
  @Override
  public void onConnected(Bundle connectionHint) {
    final Person user = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

    // The new cordova permission API's are available since Cordova-Android 5,
    // so if you're using API level 23 and want to deploy to Android 6,
    // make sure you're building with Cordova-Android 5 or higher.
    // .. it was either this or adding an Android Support library.
    try {
      Method hasPermissionMethod = cordova.getClass().getMethod("hasPermission", String.class);
      if (hasPermissionMethod != null) {
        if (!(Boolean) hasPermissionMethod.invoke(cordova, Manifest.permission.GET_ACCOUNTS)) {
          Method requestPermissionMethod =
              cordova
                  .getClass()
                  .getMethod("requestPermission", CordovaPlugin.class, int.class, String.class);
          requestPermissionMethod.invoke(
              cordova, this, REQUEST_GET_ACCOUNTS, Manifest.permission.GET_ACCOUNTS);
        }
      }
    } catch (Exception ignore) {
    }

    this.email = Plus.AccountApi.getAccountName(mGoogleApiClient);
    this.result = new JSONObject();

    try {
      result.put("email", email);
      // in case there was no internet connection, this may be null
      if (user != null) {
        result.put("userId", user.getId());
        result.put("displayName", user.getDisplayName());
        result.put("gender", getGender(user.getGender()));
        if (user.getImage() != null) {
          result.put("imageUrl", user.getImage().getUrl());
        }
        if (user.getName() != null) {
          result.put("givenName", user.getName().getGivenName());
          result.put("middleName", user.getName().getMiddleName());
          result.put("familyName", user.getName().getFamilyName());
          if (user.hasAgeRange()) {
            if (user.getAgeRange().hasMin()) {
              result.put("ageRangeMin", user.getAgeRange().getMin());
            }
            if (user.getAgeRange().hasMax()) {
              result.put("ageRangeMax", user.getAgeRange().getMax());
            }
          }
          if (user.hasBirthday()) {
            result.put("birthday", user.getBirthday());
          }
        }
      }
      resolveToken(email, result);
    } catch (JSONException e) {
      savedCallbackContext.error("result parsing trouble, error: " + e.getMessage());
    }
  }
  /** Fetching user's information name, email, profile picture */
  private void getGGProfileInformation() {
    Log.e(tag, "getGGProfileInformation");
    try {
      if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
        Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
        String personGooglePlusProfile = currentPerson.getUrl();

        Log.e(tag, currentPerson.toString());

        String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
        String id = currentPerson.getId();
        String name = new String(currentPerson.getDisplayName().getBytes("UTF-8"), "UTF-8");
        String birthday = "";
        if (currentPerson.getBirthday() != null) {
          birthday = currentPerson.getBirthday();
        }
        String gender = currentPerson.getGender() == 1 ? "men" : "women";
        String avatar = currentPerson.getImage().getUrl();
        Log.e(
            tag,
            "Name: "
                + name
                + ", plusProfile: "
                + personGooglePlusProfile
                + ", email: "
                + email
                + ", Image: "
                + avatar
                + ", birthday: "
                + birthday);

        new GGLoginTask().execute(name, email, avatar, gender, birthday, id);
      } else {
        Log.e(tag, "Person information is null");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  // Implements ConnectionCallbacks
  @Override
  public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub
    Person user = mPlusClient.getCurrentPerson();

    // show The Image
    new DownloadImageTask(imageView).execute(user.getImage().getUrl());

    textViewName.setText(user.getDisplayName());
    textViewUrl.setText(user.getUrl());
    textViewBday.setText(user.getBirthday());
    textViewEmail.setText(user.getEmails() != null ? user.getEmails().get(0).getValue() : "");
    textViewGender.setText(user.getGender() == 0 ? "Male" : "Female");

    buttonSignIn.setVisibility(View.GONE);
    relativeLayout.setVisibility(View.VISIBLE);

    mPlusClient.loadPeople(this, Person.Collection.VISIBLE);
  }