@Override
  public void clear3rdPartySession(Context context, AuthProviderType type) {
    try {
      if (session != null) {
        AuthProvider<AuthProviderInfo> provider = authProviders.getProvider(type);

        if (provider != null) {
          UserProviderCredentials userProviderCredentials =
              session.getUserProviderCredentials(type);

          if (userProviderCredentials != null) {
            AuthProviderInfo authProviderInfo = userProviderCredentials.getAuthProviderInfo();
            if (authProviderInfo != null) {
              provider.clearCache(context, authProviderInfo);
            }
          }
        }

        session.clear(type);
      }
    } finally {
      if (userSystem != null) {
        userSystem.clearSession(type);
      }
    }
  }
  @SuppressWarnings("unchecked")
  public void test_doRegistrationSocialize() throws SocializeException {

    final String registrationId = "foobar";
    final User user = new User();

    DeviceRegistration registration = AndroidMock.createMock(DeviceRegistration.class);
    NotificationRegistrationState notificationRegistrationState =
        AndroidMock.createMock(NotificationRegistrationState.class);

    DeviceRegistrationSystem deviceRegistrationSystem =
        new DeviceRegistrationSystem() {
          @Override
          public void registerDevice(
              SocializeSession session,
              DeviceRegistration registration,
              DeviceRegistrationListener listener) {
            listener.onSuccess();
          }
        };

    IBeanFactory<DeviceRegistration> deviceRegistrationFactory =
        AndroidMock.createMock(IBeanFactory.class);
    SocializeSession session = AndroidMock.createMock(SocializeSession.class);

    AndroidMock.expect(deviceRegistrationFactory.getBean()).andReturn(registration);
    AndroidMock.expect(session.getUser()).andReturn(user);

    registration.setRegistrationId(registrationId);
    notificationRegistrationState.setC2DMRegistrationId(registrationId);
    notificationRegistrationState.setRegisteredSocialize(user);
    notificationRegistrationState.save(getContext());

    AndroidMock.replay(
        deviceRegistrationFactory, registration, notificationRegistrationState, session);

    PublicSocializeNotificationRegistrationSystem system =
        new PublicSocializeNotificationRegistrationSystem();

    system.setDeviceRegistrationFactory(deviceRegistrationFactory);
    system.setDeviceRegistrationSystem(deviceRegistrationSystem);
    system.setNotificationRegistrationState(notificationRegistrationState);

    system.doRegistrationSocialize(getContext(), session, registrationId);

    AndroidMock.verify(
        deviceRegistrationFactory, registration, notificationRegistrationState, session);
  }
  /*
   * (non-Javadoc)
   * @see com.socialize.SocializeService#isAuthenticated(com.socialize.auth.AuthProviderType)
   */
  @Override
  public boolean isAuthenticated(AuthProviderType providerType) {
    if (isAuthenticated()) {

      if (providerType.equals(AuthProviderType.SOCIALIZE)) {
        return true;
      }

      UserProviderCredentials userProviderCredentials =
          session.getUserProviderCredentials(providerType);

      if (userProviderCredentials != null) {
        // Validate the credentials
        AuthProviderInfo authProviderInfo = userProviderCredentials.getAuthProviderInfo();

        if (authProviderInfo != null) {
          AuthProvider<AuthProviderInfo> provider = authProviders.getProvider(providerType);

          boolean valid = provider.validate(authProviderInfo);

          return valid;
        }

        return false;
      }
    }

    return false;
  }
  @Override
  public void clearSessionCache(Context context) {
    try {
      if (session != null) {

        UserProviderCredentialsMap userProviderCredentialsMap =
            session.getUserProviderCredentials();

        if (userProviderCredentialsMap != null) {
          Collection<UserProviderCredentials> values = userProviderCredentialsMap.values();
          for (UserProviderCredentials userProviderCredentials : values) {
            AuthProviderInfo authProviderInfo = userProviderCredentials.getAuthProviderInfo();
            if (authProviderInfo != null) {
              clear3rdPartySession(context, authProviderInfo.getType());
            }
          }
        }

        session = null;
      }
    } finally {
      if (userSystem != null) {
        userSystem.clearSession();
      }
    }
  }
  @SuppressWarnings("deprecation")
  public boolean validateSessionAuthDataLegacy(SocializeSession loaded, AuthProviderData data) {

    if (data.getAuthProviderType().equals(AuthProviderType.SOCIALIZE)) {
      return true;
    }

    if (data.getAuthProviderType() != null && !StringUtils.isEmpty(data.getAppId3rdParty())) {
      AuthProviderType loadedAuthProviderType = loaded.getAuthProviderType();
      String loadedAppId3rdParty = loaded.get3rdPartyAppId();

      if (loadedAuthProviderType != null
          && !StringUtils.isEmpty(loadedAppId3rdParty)
          && loadedAuthProviderType.equals(data.getAuthProviderType())
          && loadedAppId3rdParty.equals(data.getAppId3rdParty())) {
        return true;
      }
    }

    return false;
  }
 public boolean validateSessionAuthData(SocializeSession loaded, AuthProviderInfo info) {
   UserProviderCredentialsMap userProviderCredentialsMap = loaded.getUserProviderCredentials();
   if (userProviderCredentialsMap != null) {
     UserProviderCredentials userProviderCredentials =
         userProviderCredentialsMap.get(info.getType());
     if (userProviderCredentials != null
         && userProviderCredentials.getAuthProviderInfo().matches(info)) {
       return true;
     }
   }
   return false;
 }
 private final String prepareEndpoint(SocializeSession session, String endpoint, boolean secure) {
   return prepareEndpoint(session.getHost(), endpoint, secure);
 }