getDialogsUsers returns no elements - android

I am trying to create chat in app using sampe-chat codes.
I authorized user, got opponend id, after that successfully created dialog:
ChatService.initIfNeed(this);
QBUsers.getUserByLogin(login, new QBEntityCallbackImpl<QBUser>() {
#Override
public void onSuccess(QBUser qbUser, Bundle args) {
QBPrivateChatManager privateChatManager = QBChatService.getInstance().getPrivateChatManager();
privateChatManager.createDialog(qbUser.getId(), new QBEntityCallbackImpl<QBDialog>() {
#Override
public void onSuccess(QBDialog dialog, Bundle args) {
ChatActivity.this.dialog = dialog;
setContentView(R.layout.activity_chat);
initViews();
if (isSessionActive()) {
initChat();
}
ChatService.getInstance().addConnectionListener(chatConnectionListener);
}
#Override
public void onError(List<String> errors) {
Log.e("chat", errors.toString());
}
});
}
#Override
public void onError(List errors) {
Log.e("chat", errors.toString());
}
});
But at initViews() I've got error at
else if (dialog.getType() == QBDialogType.PRIVATE) {
Integer opponentID = ChatService.getInstance().getOpponentIDForPrivateDialog(dialog);
companionLabel.setText(ChatService.getInstance().getDialogsUsers().get(opponentID).getLogin());
}
The error is because ChatService.getInstance().getDialogsUsers() returns 0 elements: , so ChatService.getInstance().getDialogsUsers().get(opponentID).getLogin() gives exception.
Any explanations of this I have not found in documentation:
Why I have got 0 elements at ChatService.getInstance().getDialogsUsers()? Dialog is successfully created.

The 'getDialogsUsers' is not an SDK method, it's from code sample
You can just look into it, what does it do
https://github.com/QuickBlox/quickblox-android-sdk/blob/master/sample-chat/src/com/quickblox/sample/chat/core/ChatService.java#L195
It returns a map of users, set in L172
https://github.com/QuickBlox/quickblox-android-sdk/blob/master/sample-chat/src/com/quickblox/sample/chat/core/ChatService.java#L172
Try to follow this file and move all needed logic to your app

Related

Braintree PayPal Sandbox keeps redirecting

I have added a simple non drop-in paypal integration in sandbox mode to my app. Here is a test activity with a single "Pay" button:
public class PaypalPaymentAcivity extends Activity implements PaymentMethodNonceCreatedListener {
private BraintreeFragment mBraintreeFragment;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paypal);
findViewById(R.id.payButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startPayment();
}
});
}
private void startPayment() {
try {
mBraintreeFragment = BraintreeFragment.newInstance(this, "...");
PayPalRequest request = new PayPalRequest("1")
.currencyCode("USD")
.intent(PayPalRequest.INTENT_AUTHORIZE);
PayPal.requestOneTimePayment(mBraintreeFragment, request);
} catch (InvalidArgumentException e) {
e.printStackTrace();
}
}
#Override
public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
}
}
However once the PayPal browser window comes up after clicking the buttons it just keeps popping up over and over, and never returns to my activity.
Anyone had a successful integration like this?

receiving error when building an android project with Neura

I'm working with neura sdk in order to detect current data of a user(where he/she is, where were they 10 min ago etc).
I want to login to their api, and authenticate my user, however - when i call NeuraApiClient.authenticate(...) nothing happens.
I followed neura documentations, but still - nothing happens.
Here's my code :
public class MainActivity extends AppCompatActivity {
private ArrayList<Permission> mPermissions;
private AuthenticationRequest mAuthenticateRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Builder builder = new Builder(this);
NeuraApiClient neuraApiClient = builder.build();
neuraApiClient.setAppUid(getResources().getString(R.string.app_uid));
neuraApiClient.setAppSecret(getResources().getString(R.string.app_secret));
neuraApiClient.connect();
fetchPermissions(neuraApiClient);
neuraApiClient.authenticate(100, mAuthenticateRequest);
}
private void fetchPermissions(final NeuraApiClient client) {
client.getAppPermissions(new GetPermissionsRequestCallbacks() {
#Override
public void onSuccess(final List<Permission> permissions) throws RemoteException {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPermissions = new ArrayList<>(permissions);
mAuthenticateRequest = new AuthenticationRequest();
mAuthenticateRequest.setAppId(client.getAppUid());
mAuthenticateRequest.setAppSecret(client.getAppSecret());
mAuthenticateRequest.setPermissions(mPermissions);
}
});
}
#Override
public void onFailure(Bundle resultData, int errorCode) throws RemoteException {
}
#Override
public IBinder asBinder() {
return null;
}
});
}
}
getAppPermissions is an asynchronous call, and the data is fetched on GetPermissionsRequestCallbacks.
in GetPermissionsRequestCallbacks you're initiating mAuthenticateRequest which is in use of authenticate method.
Which means you have to wait untill onSuccess of GetPermissionsRequestCallbacks is called, and only then you can call
neuraApiClient.authenticate(100, mAuthenticateRequest);
Basically, if you don't wait for mAuthenticateRequest to be fetched, you authenticate with mAuthenticateRequest = null, and neuraApiClient.authenticate(..) fails.
You can do something like this : call authenticate when the results are received -
private void fetchPermissions(final NeuraApiClient client) {
client.getAppPermissions(new GetPermissionsRequestCallbacks() {
#Override
public void onSuccess(final List<Permission> permissions) throws RemoteException {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPermissions = new ArrayList<>(permissions);
mAuthenticateRequest = new AuthenticationRequest();
mAuthenticateRequest.setAppId(client.getAppUid());
mAuthenticateRequest.setAppSecret(client.getAppSecret());
mAuthenticateRequest.setPermissions(mPermissions);
client.authenticate(100, mAuthenticateRequest);
}
});
}
...
});
}

Nuera authentication screen isn't opened when calling neuraApiClient.authenticate(...)

I'm working with Neura sdk in order to detect special events of my users( arriving/leaving home)
I'm trying to initiate their authentication, as described below (fetchPermissions() and initNeuraConnection() are the same as in the documentations, and mAuthenticationRequest is initiated on fetchPermissions())
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getMainActivity().initNeuraConnection();
fetchPermissions();
getMainActivity().getClient().authenticate(NEURA_AUTHENTICATION_REQUEST_CODE, mAuthenticateRequest);
}
My issue is that once i call authenticate - nothing happens and the neura login screen isn't opened
There are few things you can check :
Have you declared initNueraConnection() and fetchPermissions() as described in the Neura dev site ?
If so, I suspect you're sending authenticate(...) a nullable mAuthenticateRequest instance.
Since fetchPermissions() is asynchronous(its a network call), you're calling authenticate(...) before the results are fetched from fetchPermissions(), so, mAuthenticateRequest is null, since it's not initiated yet.
You should call authenticate(...) only after you recieve the data on fetchPermissions().
For example, you can do this :
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getMainActivity().initNeuraConnection();
fetchPermissions();
}
private void fetchPermissions() {
loadProgress(true);
getMainActivity().getClient().getAppPermissions(new GetPermissionsRequestCallbacks() {
#Override
public void onSuccess(final List<Permission> permissions) throws RemoteException {
if (getActivity() == null)
return;
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
loadProgress(false);
mPermissions = new ArrayList<>(permissions);
mAuthenticateRequest = new AuthenticationRequest();
mAuthenticateRequest.setAppId(getMainActivity().getClient().getAppUid());
mAuthenticateRequest.setAppSecret(getMainActivity().getClient().getAppSecret());
mAuthenticateRequest.setPermissions(mPermissions);
getMainActivity().getClient().authenticate(NEURA_AUTHENTICATION_REQUEST_CODE, mAuthenticateRequest);
}
});
}
#Override
public void onFailure(Bundle resultData, int errorCode) throws RemoteException {
loadProgress(false);
mRequestPermissions.setEnabled(true);
}
#Override
public IBinder asBinder() {
return null;
}
});
}
Fyi, you can check your logcat for this error : authenticationRequest is nullable, couldn't create authenticate request.

What causes a fragment to get detached from an Activity?

I have a SignupActivity which will go through several fragments as users go through a signup process. On the last fragment, I'm calling
getActivity().setResult(Activity.RESULT_OK)
since SingupActivity intent was started for result. Some users are crashing at this point, because getActivity() is producing a NPE. I'm not able to figure out what is causing this. Screen rotation is disabled, so there is no reason that I know of for the fragment to detach from the Activity.
Any insight as to what may be causing this, and how I can resolve it?
public class SignupConfirmationFragment extends Fragment {
public static final String TAG = SignupConfirmationFragment.class.getSimpleName();
private User mNewUser;
private myAppClient mmyAppClient;
private Animation rotateAnimation;
private ImageView avatar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNewUser = ((SignUpActivity) getActivity()).getNewUser();
mmyAppClient = ((SignUpActivity) getActivity()).getmyAppClient();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_signup_confirmation, null);
((TextView) v.findViewById(R.id.username_textView)).setText(((SignUpActivity) getActivity()).getNewUser().getName());
avatar = (ImageView) v.findViewById(R.id.avatar);
if (mNewUser.getAvatarImage() != null) {
avatar.setImageBitmap(mNewUser.getAvatarImage());
}
rotateAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.progress_rotate);
v.findViewById(R.id.progress_loading).startAnimation(rotateAnimation);
if (mNewUser.getAvatarImage() != null) {
startAvatarUpload();
} else if (mNewUser.getNewsletter()) {
setNewsletterStatus();
} else {
pauseForOneSecond();
}
return v;
}
private void startAvatarUpload() {
mmyAppClient.uploadUserAvatar(mNewUser.getAvatarImage(), new FutureCallback<JsonObject>() {
#Override
public void onCompleted(Exception e, JsonObject result) {
if (mNewUser.getNewsletter()) {
setNewsletterStatus();
} else {
updateFragment();
}
}
},
null,
null);
}
private void setNewsletterStatus() {
mmyAppClient.setNewsletter(mNewUser.getEmail(), mNewUser.getFirstName(), mNewUser.getLastName(), new FutureCallback<String>() {
#Override
public void onCompleted(Exception e, String result) {
//Log.d(TAG, "Result: " + result);
updateFragment();
}
});
}
private void pauseForOneSecond() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
updateFragment();
}
}, 1000);
}
private void updateFragment() {
rotateAnimation.cancel();
if (isAdded()) {
getActivity().setResult(Activity.RESULT_OK);
AnalyticsManager.logUIEvent("sign up completed");
getActivity().finish();
} else {
AnalyticsManager.logUIEvent("sign up failed");
}
}
}
According to Fragment lifecycle in Android OS, you cannot get the Activity associated with the fragment in the onCreateView, because the Activity with which the Fragment is associated will not be created at that stage.
See the figure below:
Also, refer to this link, http://developer.android.com/guide/components/fragments.html
As you can see the Activity is created in onActivityCreated which is after onCreateView, hence you'll get null if you try to call the Activity in the onCreateView. Try to call it in onActivityCreated or in onStart that should solve your problem.
I hope this helps.

Android: Avoid opening the dialog several times, but allow hide and shows the same dialog

I have an Activity with ViewPager PagerSlidingTabStrip for each page of my ViewPager has a fragment, and in each fragment realize an http request (using Volley) to load the data from the page, but when the request ends in error, type timeout or lost connection, I need to display a dialog with the option to redo the call to the server, the problem to prevent multiple dialogs are open for each error is resolved with the snippet:
See this solution here: http://www.jorgecoca.com/android-quick-tip-avoid-opening-multiple-dialogs-when-tapping-an-element/
#Override
public void show(FragmentManager manager, String tag) {
if (manager.findFragmentByTag(tag) == null) {
super.show(manager, tag);
}
}
When the user clicks the dialog button to try again, and the dialog closed and taken to check if there is internet connection, if I'm not, the dialog should be opened again, but the dialog is not displayed again, I believe that the tag does not was released to FragmentManager.
Code in Activity:
final Button mButton = ( Button ) this.findViewById( R.id.btn_opendialog );
final DialogFragmentHelper mDialog = new DialogFragmentHelper();
mDialog.setCallbackListener( new OnCallback() {
#Override
public void onCancel() {
}
#Override
public void onConfirm() {
// verify if network available
mDialog.show( MainActivity.this.getSupportFragmentManager(), DialogFragmentHelper.DIALOG_TAG );
}
} );
mButton.setOnClickListener( new OnClickListener() {
#Override
public void onClick( final View v ) {
mDialog.show( MainActivity.this.getSupportFragmentManager(), DialogFragmentHelper.DIALOG_TAG );
}
} );
Would someone have a suggestion of a workaround?
In order to maintain the structure that is ready in my project, and also keep something closer to my goal, which is to use no flags, nor pass control of a third dialogfragment to manage, arrived at a solution that'll take an hour as a result.
DialogFragmentHelper mDialog = new DialogFragmentHelper();
mDialog.setCallbackListener( new OnCallback() {
#Override
public void onCancel() {}
#Override
public void onConfirm() {
mDialog.dismissAllowingStateLoss();
if(networkAvailable == false){
new Handler().post( new Runnable() {
#Override
public void run() {
mDialog.show( MainActivity.this.getSupportFragmentManager(), DialogFragmentHelper.DIALOG_TAG );
}
} );
}else {
//do something here
}
}
} );
this way I guarantee that while several requests are sent to open the dialogfragment, only the first is executed, and closing the dialogfragment, I can quickly open it again if needed, as can happen in the scenario I'm working.
You could approach it with a singleton controller. E.g.:
package com.example.stackoverflowsandbox;
public class MyDialogController {
private static MyDialogController instance;
public static MyDialogController getInstance() {
if ( MyDialogController.instance == null ) {
MyDialogController.instance = new MyDialogController();
}
return MyDialogController.instance;
}
private boolean dialogOpenned;
private MyDialogController() {}
public void closeDialog() {
if ( this.dialogOpenned ) {
this.dialogOpenned = false;
// you close code...
}
}
public void openDialog() {
if ( !this.dialogOpenned ) {
this.dialogOpenned = true;
// your open code...
}
}
}

Categories

Resources