Search in sources :

Example 1 with AsyncCallback

use of com.backendless.async.callback.AsyncCallback in project Android-SDK by Backendless.

the class ChatActivity method onCreate.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat);
    final String name = getIntent().getStringExtra(Defaults.NAME_TAG);
    final EditText historyField = (EditText) findViewById(R.id.historyField);
    final EditText messageField = (EditText) findViewById(R.id.messageField);
    Backendless.initApp(this, Defaults.APPLICATION_ID, Defaults.SECRET_KEY, Defaults.VERSION);
    Backendless.Messaging.subscribe(Defaults.CHANNEL_NAME, new AsyncCallback<List<Message>>() {

        @Override
        public void handleResponse(List<Message> response) {
            for (Message message : response) historyField.setText(message.getPublisherId() + ": " + message.getData() + "\n" + historyField.getText());
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Toast.makeText(ChatActivity.this, fault.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }, new AsyncCallback<Subscription>() {

        @Override
        public void handleResponse(Subscription response) {
            subscription = response;
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Toast.makeText(ChatActivity.this, fault.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
    messageField.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
            if (keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_UP) {
                String message = messageField.getText().toString();
                if (message == null || message.equals(""))
                    return true;
                final ProgressDialog progressDialog = ProgressDialog.show(ChatActivity.this, "", "Sending", true);
                Backendless.Messaging.publish(Defaults.CHANNEL_NAME, message, new PublishOptions(name), new AsyncCallback<MessageStatus>() {

                    @Override
                    public void handleResponse(MessageStatus response) {
                        progressDialog.cancel();
                        messageField.setText("");
                    }

                    @Override
                    public void handleFault(BackendlessFault fault) {
                        progressDialog.cancel();
                        Toast.makeText(ChatActivity.this, fault.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
                return true;
            }
            return false;
        }
    });
}
Also used : EditText(android.widget.EditText) Message(com.backendless.messaging.Message) AsyncCallback(com.backendless.async.callback.AsyncCallback) PublishOptions(com.backendless.messaging.PublishOptions) ProgressDialog(android.app.ProgressDialog) View(android.view.View) BackendlessFault(com.backendless.exceptions.BackendlessFault) KeyEvent(android.view.KeyEvent) MessageStatus(com.backendless.messaging.MessageStatus) List(java.util.List) Subscription(com.backendless.Subscription)

Example 2 with AsyncCallback

use of com.backendless.async.callback.AsyncCallback in project Android-SDK by Backendless.

the class Persistence method remove.

protected <E> void remove(final E entity, final AsyncCallback<Long> responder) {
    try {
        if (entity == null)
            throw new IllegalArgumentException(ExceptionMessage.NULL_ENTITY);
        AsyncCallback<Object> removalCallback = new AsyncCallback<Object>() {

            @Override
            public void handleResponse(Object response) {
                FootprintsManager.getInstance().Inner.removeFootprintForObject(BackendlessSerializer.serializeToMap(entity), entity);
                if (responder == null)
                    return;
                responder.handleResponse(((Number) response).longValue());
            }

            @Override
            public void handleFault(BackendlessFault fault) {
                if (responder != null)
                    responder.handleFault(fault);
            }
        };
        Map<String, Object> entityMap = BackendlessSerializer.serializeToMap(entity);
        FootprintsManager.getInstance().Inner.putMissingPropsToEntityMap(entity, entityMap);
        Invoker.invokeAsync(PERSISTENCE_MANAGER_SERVER_ALIAS, "remove", new Object[] { BackendlessSerializer.getSimpleName(entity.getClass()), entityMap }, removalCallback);
    } catch (Throwable e) {
        if (responder != null)
            responder.handleFault(new BackendlessFault(e));
    }
}
Also used : AsyncCallback(com.backendless.async.callback.AsyncCallback) BackendlessFault(com.backendless.exceptions.BackendlessFault)

Example 3 with AsyncCallback

use of com.backendless.async.callback.AsyncCallback in project Android-SDK by Backendless.

the class UserService method getUserRoles.

public void getUserRoles(final AsyncCallback<List<String>> responder) {
    try {
        AsyncCallback<String[]> callback = new AsyncCallback<String[]>() {

            @Override
            public void handleResponse(String[] response) {
                if (responder != null)
                    responder.handleResponse(Arrays.asList(response));
            }

            @Override
            public void handleFault(BackendlessFault fault) {
                if (responder != null)
                    responder.handleFault(fault);
            }
        };
        Invoker.invokeAsync(USER_MANAGER_SERVER_ALIAS, "getUserRoles", new Object[] {}, callback);
    } catch (Throwable e) {
        if (responder != null)
            responder.handleFault(new BackendlessFault(e));
    }
}
Also used : AsyncCallback(com.backendless.async.callback.AsyncCallback) BackendlessFault(com.backendless.exceptions.BackendlessFault)

Example 4 with AsyncCallback

use of com.backendless.async.callback.AsyncCallback in project Android-SDK by Backendless.

the class UserServiceAndroidExtra method getSocialDialogResponder.

private AsyncCallback<JSONObject> getSocialDialogResponder(final AsyncCallback<BackendlessUser> responder) {
    return new AsyncCallback<JSONObject>() {

        @Override
        public void handleResponse(JSONObject response) {
            try {
                BackendlessUser result = new BackendlessUser();
                Iterator keys = response.keys();
                while (keys.hasNext()) {
                    String key = String.valueOf(keys.next());
                    result.setProperty(key, JSONObjectConverter.fromJson(response.get(key)));
                }
                if (responder != null)
                    responder.handleResponse(result);
            } catch (Exception e) {
                if (responder != null)
                    responder.handleFault(new BackendlessFault(e));
            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            if (responder != null)
                responder.handleFault(fault);
        }
    };
}
Also used : JSONObject(org.json.JSONObject) AsyncCallback(com.backendless.async.callback.AsyncCallback) JSONException(org.json.JSONException) BackendlessFault(com.backendless.exceptions.BackendlessFault)

Example 5 with AsyncCallback

use of com.backendless.async.callback.AsyncCallback in project Android-SDK by Backendless.

the class EditPreferencesActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editpreference);
    progressDialog = UIFactory.getDefaultProgressDialog(this);
    TextView preferenceName = (TextView) findViewById(R.id.preferenceName);
    preferencesContainer = (LinearLayout) findViewById(R.id.preferencesContainer);
    preferenceTheme = PreferenceTheme.valueOf(getIntent().getStringExtra(Lifecycle.PREFERENCE_TYPE_TAG));
    preferenceStore = Backendless.Persistence.of(UserPreferences.class);
    // Naming the page
    preferenceName.setText(preferenceTheme.getPreference());
    ((TextView) findViewById(R.id.preferenceLabel)).setText(getString(R.string.textfield_preferences_label_base) + " " + preferenceTheme.getPreference().toLowerCase() + ":");
    // Getting preference structure
    BackendlessDataQuery backendlessDataQuery = new BackendlessDataQuery("theme = '" + preferenceTheme.getPreference() + "'");
    QueryOptions queryOptions = new QueryOptions();
    queryOptions.setPageSize(50);
    queryOptions.addSortByOption("name");
    backendlessDataQuery.setQueryOptions(queryOptions);
    Backendless.Persistence.of(PreferencesDefaults.class).find(backendlessDataQuery, new AsyncCallback<Collection<PreferencesDefaults>>() {

        @Override
        public void handleResponse(Collection<PreferencesDefaults> preferencesDefaultsBackendlessCollection) {
            // Creating a map with predefined checkboxes
            List<PreferencesDefaults> preferencesDefaults = preferencesDefaultsBackendlessCollection.getCurrentPage();
            for (PreferencesDefaults preference : preferencesDefaults) {
                CheckBox checkBox = UIFactory.getPreferenceCheckbox(EditPreferencesActivity.this, preference);
                checkBox.setOnClickListener(getCheckboxListener(checkBox));
                preferencesContainer.addView(checkBox);
                preferenceNameToCheckboxMap.put(preference.getName(), checkBox);
            }
            // Getting preferences, which should be enabled for current user
            String whereClause = "email = '" + Backendless.UserService.CurrentUser().getEmail() + "'";
            whereClause += " and theme = '" + preferenceTheme.getPreference() + "'";
            BackendlessDataQuery backendlessDataQuery = new BackendlessDataQuery();
            backendlessDataQuery.setWhereClause(whereClause);
            backendlessDataQuery.setQueryOptions(new QueryOptions(50, 0));
            Backendless.Persistence.of(UserPreferences.class).find(backendlessDataQuery, new AsyncCallback<Collection<UserPreferences>>() {

                @Override
                public void handleResponse(Collection<UserPreferences> userPreferencesBackendlessCollection) {
                    List<UserPreferences> userPreferences = userPreferencesBackendlessCollection.getCurrentPage();
                    for (UserPreferences userPreference : userPreferences) {
                        CheckBox checkBox = preferenceNameToCheckboxMap.get(userPreference.getPreference());
                        preferenceNameToPreferenceMap.put(userPreference.getPreference(), userPreference);
                        checkBox.setChecked(true);
                    }
                    progressDialog.cancel();
                }

                @Override
                public void handleFault(BackendlessFault backendlessFault) {
                    progressDialog.cancel();
                    Toast.makeText(EditPreferencesActivity.this, backendlessFault.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
        }

        @Override
        public void handleFault(BackendlessFault backendlessFault) {
            progressDialog.cancel();
            Toast.makeText(EditPreferencesActivity.this, backendlessFault.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}
Also used : UserPreferences(com.backendless.examples.endless.matchmaker.models.persistent.UserPreferences) ResponseAsyncCallback(com.backendless.examples.endless.matchmaker.controllers.shared.ResponseAsyncCallback) AsyncCallback(com.backendless.async.callback.AsyncCallback) QueryOptions(com.backendless.persistence.QueryOptions) BackendlessFault(com.backendless.exceptions.BackendlessFault) BackendlessDataQuery(com.backendless.persistence.BackendlessDataQuery) CheckBox(android.widget.CheckBox) PreferencesDefaults(com.backendless.examples.endless.matchmaker.models.persistent.PreferencesDefaults) BackendlessCollection(com.backendless.BackendlessCollection) TextView(android.widget.TextView) List(java.util.List)

Aggregations

AsyncCallback (com.backendless.async.callback.AsyncCallback)12 BackendlessFault (com.backendless.exceptions.BackendlessFault)11 View (android.view.View)5 Intent (android.content.Intent)4 TextView (android.widget.TextView)4 Typeface (android.graphics.Typeface)3 Button (android.widget.Button)3 EditText (android.widget.EditText)3 BackendlessUser (com.backendless.BackendlessUser)3 BackendlessCollection (com.backendless.BackendlessCollection)2 ResponseAsyncCallback (com.backendless.examples.endless.matchmaker.controllers.shared.ResponseAsyncCallback)2 BackendlessDataQuery (com.backendless.persistence.BackendlessDataQuery)2 List (java.util.List)2 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 Bitmap (android.graphics.Bitmap)1 KeyEvent (android.view.KeyEvent)1 CheckBox (android.widget.CheckBox)1 ImageView (android.widget.ImageView)1 Subscription (com.backendless.Subscription)1