Search in sources :

Example 1 with RideRequestReceived

use of br.ufrj.caronae.models.RideRequestReceived in project caronae-android by caronae.

the class MyFirebaseMessagingService method onMessageReceived.

@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
    Log.i("onMessageReceived", "onMessageReceived");
    if (App.isUserLoggedIn() && remoteMessage.getData() != null) {
        Map data = remoteMessage.getData();
        String msgType = (String) data.get("msgType");
        if (msgType != null && msgType.equals("alert")) {
            PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString(MSG_TYPE_ALERT, (String) data.get(ALERT_KEY)).apply();
            PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString(MSG_TYPE_ALERT_HEADER, (String) data.get(ALERT_HEADER_KEY)).commit();
        } else {
            String message = (String) data.get("message");
            String senderName = (String) data.get("senderName");
            final String rideId = (String) data.get("rideId");
            Log.i("onMessageReceived", message);
            if (msgType != null && msgType.equals("chat")) {
                String senderId = (String) data.get("senderId");
                // noinspection ConstantConditions
                if (senderId.equals(App.getUser().getDbId() + "")) {
                    return;
                }
                List<ChatMessageReceived> listOldMessages = ChatMessageReceived.find(ChatMessageReceived.class, "ride_id = ?", rideId);
                ChatMessageReceived lastMessage = null;
                if (listOldMessages.size() != 0) {
                    lastMessage = listOldMessages.get(listOldMessages.size() - 1);
                }
                String since;
                if (lastMessage == null) {
                    since = null;
                } else {
                    since = lastMessage.getTime();
                }
                if (!SharedPref.getChatActIsForeground()) {
                    startService(new Intent(this, FetchReceivedMessagesService.class).putExtra("rideId", rideId).putExtra("since", since));
                }
            }
            if (msgType != null && msgType.equals("joinRequest")) {
                new RideRequestReceived(Integer.valueOf(rideId)).save();
            }
            if (msgType != null && msgType.equals("finished")) {
                FirebaseTopicsHandler.unsubscribeFirebaseTopic(rideId);
                App.getBus().post(new RideEndedEvent(rideId));
                ActiveRide.deleteAll(ActiveRide.class, "db_id = ?", rideId);
            }
            // TODO: Carona cancelada Nao esta rebendo notificacao
            if (msgType != null && msgType.equals("cancelled")) {
                FirebaseTopicsHandler.unsubscribeFirebaseTopic(rideId);
                App.getBus().post(new RideEndedEvent(rideId));
                ActiveRide.deleteAll(ActiveRide.class, "db_id = ?", rideId);
            }
            if (msgType != null && msgType.equals("accepted")) {
                FirebaseTopicsHandler.CheckSubFirebaseTopic(rideId);
            // new DeleteConflictingRequests().execute(rideId);
            }
            if (SharedPref.getNotifPref().equals("true"))
                if (msgType != null && msgType.equals("chat")) {
                    if (!SharedPref.getChatActIsForeground()) {
                        createNotification(msgType, senderName, message, rideId);
                    } else {
                        App.getBus().post(rideId);
                    }
                } else
                    createNotification(msgType, senderName, message, rideId);
        }
    }
}
Also used : ChatMessageReceived(br.ufrj.caronae.models.ChatMessageReceived) RideEndedEvent(br.ufrj.caronae.models.RideEndedEvent) RideRequestReceived(br.ufrj.caronae.models.RideRequestReceived) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Map(java.util.Map)

Example 2 with RideRequestReceived

use of br.ufrj.caronae.models.RideRequestReceived in project caronae-android by caronae.

the class StartAct method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (App.isUserLoggedIn()) {
        Intent fetchOfferedRides = new Intent(getApplicationContext(), FetchMyOfferedRidesService.class);
        startService(fetchOfferedRides);
    }
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        for (String key : bundle.keySet()) {
            Object value = bundle.get(key);
            Log.e("DATA_SENT", String.format("%s %s (%s)", key, value.toString(), value.getClass().getName()));
        }
    }
    if (App.isUserLoggedIn())
        if (bundle != null && bundle.get(MSG_TYPE_BUNDLE_KEY) != null && bundle.get(MSG_TYPE_BUNDLE_KEY).equals("chat")) {
            Intent intent = new Intent(this, ChatAct.class);
            intent.putExtra(RIDE_ID_BUNDLE_KEY, (String) bundle.get(RIDE_ID_BUNDLE_KEY));
            intent.putExtras(bundle);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        } else if (bundle != null && bundle.get(MSG_TYPE_BUNDLE_KEY) != null && bundle.get(MSG_TYPE_BUNDLE_KEY).equals("alert")) {
            PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString(MSG_TYPE_ALERT, (String) bundle.get(ALERT_KEY)).commit();
            PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString(MSG_TYPE_ALERT_HEADER, (String) bundle.get(ALERT_HEADER_KEY)).commit();
        } else if (bundle != null && bundle.get(MSG_TYPE_BUNDLE_KEY) != null && (bundle.get(MSG_TYPE_BUNDLE_KEY).equals("joinRequest") || (bundle.get(MSG_TYPE_BUNDLE_KEY).equals("accepted")))) {
            if (bundle.get(MSG_TYPE_BUNDLE_KEY).equals("joinRequest")) {
                new RideRequestReceived(Integer.valueOf((String) bundle.get(RIDE_ID_BUNDLE_KEY))).save();
            }
            Intent intent = new Intent(this, MainAct.class);
            intent.putExtra(SharedPref.MY_RIDE_LIST_KEY, true);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        } else {
            startActivity(new Intent(this, MainAct.class));
        }
    else
        startActivity(new Intent(this, OpeningAct.class));
    finish();
}
Also used : Bundle(android.os.Bundle) RideRequestReceived(br.ufrj.caronae.models.RideRequestReceived) Intent(android.content.Intent)

Example 3 with RideRequestReceived

use of br.ufrj.caronae.models.RideRequestReceived in project caronae-android by caronae.

the class MyRidesAdapter method configureMyOfferRide.

private void configureMyOfferRide(int position, final MyRidesAdapter.ViewHolder holder) {
    final Ride ride = (Ride) rides.get(position);
    int color = Util.getColorbyZone(ride.getZone());
    holder.location_tv.setTextColor(color);
    if (ride.isGoing())
        holder.time_tv.setText(activity.getString(R.string.arrivingAt, Util.formatBadHour(ride.getTime())));
    else
        holder.time_tv.setText(activity.getString(R.string.leavingAt, Util.formatBadHour(ride.getTime())));
    if (ride.getDate().contains("-")) {
        ride.setDate(Util.formatBadDateWithYear(ride.getDate()));
    }
    holder.date_tv.setText(Util.formatGoodDateWithoutYear(ride.getDate()));
    holder.slots_tv.setText(activity.getString(R.string.Xslots, ride.getSlots(), (Integer.parseInt(ride.getSlots()) > 1 ? "s" : "")));
    String location;
    if (ride.isGoing())
        location = ride.getNeighborhood() + " ➜ " + ride.getHub();
    else
        location = ride.getHub() + " ➜ " + ride.getNeighborhood();
    holder.location_tv.setText(location);
    String s = activity.getString(R.string.repeats);
    if (ride.isRoutine()) {
        s += ride.getWeekDays().contains("7") ? activity.getString(R.string.sunday) : "";
        s += ride.getWeekDays().contains("1") ? activity.getString(R.string.monday) : "";
        s += ride.getWeekDays().contains("2") ? activity.getString(R.string.tuesday) : "";
        s += ride.getWeekDays().contains("3") ? activity.getString(R.string.wednesday) : "";
        s += ride.getWeekDays().contains("4") ? activity.getString(R.string.thursday) : "";
        s += ride.getWeekDays().contains("5") ? activity.getString(R.string.friday) : "";
        s += ride.getWeekDays().contains("6") ? activity.getString(R.string.saturday) : "";
        s = s.substring(0, s.length() - 1);
    } else {
        s = activity.getString(R.string.notRoutine);
    }
    holder.routine_tv.setText(s);
    holder.delete_bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (ride.isRoutine()) {
                Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {

                    @Override
                    protected void onBuildDone(Dialog dialog) {
                        dialog.layoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    }

                    @Override
                    public void onPositiveActionClicked(DialogFragment fragment) {
                        RadioGroup radioGroup = (RadioGroup) fragment.getDialog().findViewById(R.id.radioGroup);
                        int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
                        switch(checkedRadioButton) {
                            case R.id.all_rb:
                                deleteAllRidesFromRoutine();
                                break;
                            case R.id.single_rb:
                                deleteSingleRide();
                                break;
                        }
                        super.onPositiveActionClicked(fragment);
                    }

                    @Override
                    public void onNegativeActionClicked(DialogFragment fragment) {
                        super.onNegativeActionClicked(fragment);
                    }
                };
                builder.title(activity.getString(R.string.attention)).positiveAction(activity.getString(R.string.ok)).negativeAction(activity.getString(R.string.cancel)).contentView(R.layout.delete_routine_dialog);
                DialogFragment fragment = DialogFragment.newInstance(builder);
                fragment.show(activity.getSupportFragmentManager(), null);
            } else {
                deleteSingleRide();
            }
        }

        private void deleteAllRidesFromRoutine() {
            Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {

                @Override
                public void onPositiveActionClicked(DialogFragment fragment) {
                    final ProgressDialog pd = ProgressDialog.show(activity, "", activity.getString(R.string.wait), true, true);
                    final String routineId = ride.getRoutineId();
                    CaronaeAPI.service(activity.getApplicationContext()).deleteAllRidesFromRoutine(routineId).enqueue(new Callback<ResponseBody>() {

                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            if (response.isSuccessful()) {
                                pd.dismiss();
                                Util.toast(R.string.ridesDeleted);
                                if (routineId != null) {
                                    Iterator<Object> it = rides.iterator();
                                    while (it.hasNext()) {
                                        Object object = it.next();
                                        if (object.getClass() == Ride.class) {
                                            Ride ride2 = (Ride) object;
                                            if (ride2.getRoutineId().equals(routineId))
                                                it.remove();
                                        }
                                    }
                                }
                                notifyDataSetChanged();
                                Ride.deleteAll(Ride.class, "routine_id = ?", routineId);
                            } else {
                                Util.treatResponseFromServer(response);
                                pd.dismiss();
                                Util.toast(activity.getString(R.string.errorDeleteRide));
                                Log.e("deleteAllFromRoutine", response.message());
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {
                            pd.dismiss();
                            Util.toast(activity.getString(R.string.errorDeleteRide));
                            Log.e("deleteAllFromRoutine", t.getMessage());
                        }
                    });
                    super.onPositiveActionClicked(fragment);
                }

                @Override
                public void onNegativeActionClicked(DialogFragment fragment) {
                    super.onNegativeActionClicked(fragment);
                }
            };
            ((SimpleDialog.Builder) builder).message(activity.getString(R.string.warnDeleteRidesCouldBeActive)).title(activity.getString(R.string.attention)).positiveAction(activity.getString(R.string.ok)).negativeAction(activity.getString(R.string.cancel));
            DialogFragment fragment = DialogFragment.newInstance(builder);
            fragment.show(activity.getSupportFragmentManager(), null);
        }

        private void deleteSingleRide() {
            Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {

                @Override
                public void onPositiveActionClicked(DialogFragment fragment) {
                    final ProgressDialog pd = ProgressDialog.show(activity, "", activity.getString(R.string.wait), true, true);
                    CaronaeAPI.service(activity.getApplicationContext()).deleteRide(ride.getDbId() + "").enqueue(new Callback<ResponseBody>() {

                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            if (response.isSuccessful()) {
                                pd.dismiss();
                                Util.toast(R.string.rideDeleted);
                                rides.remove(ride);
                                notifyItemRemoved(holder.getAdapterPosition());
                                for (int rideCounter = 0; rideCounter < rides.size(); rideCounter++) {
                                    if (rides.get(rideCounter).getClass() == RideForJson.class) {
                                        RideForJson rideForJson = (RideForJson) rides.get(rideCounter);
                                        if (rideForJson.getId() == ride.getId()) {
                                            rides.remove(rideForJson);
                                            notifyItemRemoved(rideCounter);
                                        }
                                    }
                                }
                                ride.delete();
                            } else {
                                Util.treatResponseFromServer(response);
                                pd.dismiss();
                                Util.toast(activity.getString(R.string.errorDeleteRide));
                                Log.e("deleteRide", response.message());
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {
                            pd.dismiss();
                            Util.toast(activity.getString(R.string.errorDeleteRide));
                            Log.e("deleteRide", t.getMessage());
                        }
                    });
                    super.onPositiveActionClicked(fragment);
                }

                @Override
                public void onNegativeActionClicked(DialogFragment fragment) {
                    super.onNegativeActionClicked(fragment);
                }
            };
            ((SimpleDialog.Builder) builder).message(activity.getString(R.string.warnDeleteRideCouldBeActive)).title(activity.getString(R.string.attention)).positiveAction(activity.getString(R.string.ok)).negativeAction(activity.getString(R.string.cancel));
            DialogFragment fragment = DialogFragment.newInstance(builder);
            fragment.show(activity.getSupportFragmentManager(), null);
        }
    });
    final int colorToSend = color;
    final boolean[] isOpen = { false, false };
    holder.myRideLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
    holder.delete_bt.setBackgroundColor(color);
    holder.myRideLayout.addSwipeListener(new SwipeLayout.SwipeListener() {

        @Override
        public void onStartOpen(SwipeLayout layout) {
            isOpen[0] = true;
            isOpen[1] = true;
        }

        @Override
        public void onOpen(SwipeLayout layout) {
        }

        @Override
        public void onStartClose(SwipeLayout layout) {
        }

        @Override
        public void onClose(SwipeLayout layout) {
            isOpen[1] = false;
        }

        @Override
        public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
        }

        @Override
        public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
        }
    });
    holder.share_bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, Util.getTextToShareRide(ride));
            intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Check out this site!");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent.createChooser(intent, "Compartilhar Carona"));
        }
    });
    holder.myRideLayout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (!isOpen[0]) {
                final ProgressDialog pd = ProgressDialog.show(activity, "", activity.getResources().getString(R.string.wait), true, true);
                RideRequestReceived.deleteAll(RideRequestReceived.class, "db_id = ?", ride.getDbId() + "");
                holder.newRequest_iv.setVisibility(View.INVISIBLE);
                CaronaeAPI.service(activity.getApplicationContext()).getRequesters(ride.getDbId() + "").enqueue(new Callback<List<User>>() {

                    @Override
                    public void onResponse(Call<List<User>> call, Response<List<User>> response) {
                        if (response.isSuccessful()) {
                            pd.dismiss();
                            List<User> users = response.body();
                            if (users.isEmpty()) {
                                Util.toast(R.string.noRequesters);
                            } else {
                                Intent intent = new Intent(activity, RequestersListAct.class);
                                intent.putParcelableArrayListExtra("users", (ArrayList<User>) users);
                                intent.putExtra("rideId", ride.getDbId());
                                intent.putExtra("color", colorToSend);
                                activity.startActivity(intent);
                            }
                        } else {
                            Util.treatResponseFromServer(response);
                            pd.dismiss();
                            Util.toast(R.string.errorGetRequesters);
                            Log.e("getRequesters", response.message());
                        }
                    }

                    @Override
                    public void onFailure(Call<List<User>> call, Throwable t) {
                        pd.dismiss();
                        Util.toast(R.string.errorGetRequesters);
                        Log.e("getRequesters", t.getMessage());
                    }
                });
            } else if (isOpen[0] && !isOpen[1]) {
                isOpen[0] = false;
            }
        }
    });
    boolean found = false;
    for (RideRequestReceived requestReceived : rideRequestReceivedList) {
        if (requestReceived.getDbId() == ride.getDbId()) {
            holder.newRequest_iv.setVisibility(View.VISIBLE);
            found = true;
            break;
        }
    }
    if (!found) {
        CaronaeAPI.service(activity.getApplicationContext()).getRequesters(ride.getDbId() + "").enqueue(new Callback<List<User>>() {

            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {
                if (response.isSuccessful()) {
                    List<User> requesters = response.body();
                    if (requesters.isEmpty()) {
                        holder.newRequest_iv.setVisibility(View.INVISIBLE);
                    } else {
                        holder.newRequest_iv.setVisibility(View.VISIBLE);
                        new RideRequestReceived(Integer.valueOf(ride.getDbId())).save();
                    }
                } else {
                    Util.toast(R.string.errorGetRequesters);
                    Log.e("getRequesters", response.message());
                }
            }

            @Override
            public void onFailure(Call<List<User>> call, Throwable t) {
                Util.toast(R.string.errorGetRequesters);
                Log.e("getRequesters", t.getMessage());
            }
        });
    }
}
Also used : User(br.ufrj.caronae.models.User) RadioGroup(android.widget.RadioGroup) DialogFragment(com.rey.material.app.DialogFragment) ProgressDialog(android.app.ProgressDialog) SimpleDialog(com.rey.material.app.SimpleDialog) ProgressDialog(android.app.ProgressDialog) SimpleDialog(com.rey.material.app.SimpleDialog) Dialog(com.rey.material.app.Dialog) SwipeLayout(com.daimajia.swipe.SwipeLayout) RideRequestReceived(br.ufrj.caronae.models.RideRequestReceived) ArrayList(java.util.ArrayList) List(java.util.List) Call(retrofit2.Call) RideForJson(br.ufrj.caronae.models.modelsforjson.RideForJson) Intent(android.content.Intent) ImageView(android.widget.ImageView) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) Response(retrofit2.Response) Callback(retrofit2.Callback) Ride(br.ufrj.caronae.models.Ride)

Aggregations

Intent (android.content.Intent)3 RideRequestReceived (br.ufrj.caronae.models.RideRequestReceived)3 PendingIntent (android.app.PendingIntent)1 ProgressDialog (android.app.ProgressDialog)1 Bundle (android.os.Bundle)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 RadioGroup (android.widget.RadioGroup)1 TextView (android.widget.TextView)1 ChatMessageReceived (br.ufrj.caronae.models.ChatMessageReceived)1 Ride (br.ufrj.caronae.models.Ride)1 RideEndedEvent (br.ufrj.caronae.models.RideEndedEvent)1 User (br.ufrj.caronae.models.User)1 RideForJson (br.ufrj.caronae.models.modelsforjson.RideForJson)1 SwipeLayout (com.daimajia.swipe.SwipeLayout)1 Dialog (com.rey.material.app.Dialog)1 DialogFragment (com.rey.material.app.DialogFragment)1 SimpleDialog (com.rey.material.app.SimpleDialog)1 CircleImageView (de.hdodenhof.circleimageview.CircleImageView)1