Search in sources :

Example 1 with LbryNotification

use of com.odysee.app.model.lbryinc.LbryNotification in project odysee-android by OdyseeTeam.

the class MainActivity method updateLocalNotifications.

private void updateLocalNotifications(List<LbryNotification> notifications) {
    findViewById(R.id.notification_list_empty_container).setVisibility(notifications.isEmpty() ? View.VISIBLE : View.GONE);
    findViewById(R.id.notifications_progress).setVisibility(View.GONE);
    loadUnseenNotificationsCount();
    if (notificationListAdapter == null) {
        notificationListAdapter = new NotificationListAdapter(notifications, MainActivity.this);
        notificationListAdapter.setSelectionModeListener(MainActivity.this);
        ((RecyclerView) findViewById(R.id.notifications_list)).setAdapter(notificationListAdapter);
    } else {
        notificationListAdapter.addNotifications(notifications);
    }
    resolveCommentAuthors(notificationListAdapter.getAuthorUrls());
    notificationListAdapter.setClickListener(new NotificationListAdapter.NotificationClickListener() {

        @Override
        public void onNotificationClicked(LbryNotification notification) {
            // set as seen and read
            Map<String, String> options = new HashMap<>();
            options.put("notification_ids", String.valueOf(notification.getRemoteId()));
            options.put("is_seen", "true");
            // so let's not mark the notification as read
            if (!notification.getTargetUrl().equalsIgnoreCase("lbry://?subscriptions")) {
                options.put("is_read", "true");
            } else {
                options.put("is_read", "false");
            }
            AccountManager am = AccountManager.get(getApplicationContext());
            if (am != null) {
                String at = am.peekAuthToken(Helper.getOdyseeAccount(am.getAccounts()), "auth_token_type");
                if (at != null)
                    options.put("auth_token", at);
            }
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                Supplier<Boolean> supplier = new NotificationUpdateSupplier(options);
                CompletableFuture.supplyAsync(supplier);
            } else {
                Thread t = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Lbryio.call("notification", "edit", options, null);
                        } catch (LbryioResponseException | LbryioRequestException e) {
                            e.printStackTrace();
                        }
                    }
                });
                t.start();
            }
            if (!notification.getTargetUrl().equalsIgnoreCase("lbry://?subscriptions")) {
                markNotificationReadAndSeen(notification.getId());
            }
            String targetUrl = notification.getTargetUrl();
            if (targetUrl.startsWith(SPECIAL_URL_PREFIX)) {
                openSpecialUrl(targetUrl, "notification");
            } else {
                LbryUri target = LbryUri.tryParse(notification.getTargetUrl());
                if (target != null) {
                    if (target.isChannel()) {
                        openChannelUrl(notification.getTargetUrl(), "notification");
                    } else {
                        openFileUrl(notification.getTargetUrl(), "notification");
                    }
                }
            }
            hideNotifications(false);
        }
    });
}
Also used : LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) SpannableString(android.text.SpannableString) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification) NotificationUpdateSupplier(com.odysee.app.supplier.NotificationUpdateSupplier) NotificationListAdapter(com.odysee.app.adapter.NotificationListAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) AccountManager(android.accounts.AccountManager) Supplier(java.util.function.Supplier) FetchRewardsSupplier(com.odysee.app.supplier.FetchRewardsSupplier) GetLocalNotificationsSupplier(com.odysee.app.supplier.GetLocalNotificationsSupplier) NotificationListSupplier(com.odysee.app.supplier.NotificationListSupplier) NotificationUpdateSupplier(com.odysee.app.supplier.NotificationUpdateSupplier) UnlockingTipsSupplier(com.odysee.app.supplier.UnlockingTipsSupplier) LbryUri(com.odysee.app.utils.LbryUri) Map(java.util.Map) HashMap(java.util.HashMap) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 2 with LbryNotification

use of com.odysee.app.model.lbryinc.LbryNotification in project odysee-android by OdyseeTeam.

the class LbrynetMessagingService method onMessageReceived.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (firebaseAnalytics == null) {
        firebaseAnalytics = FirebaseAnalytics.getInstance(this);
    }
    Map<String, String> payload = remoteMessage.getData();
    if (payload != null) {
        String type = payload.get("type");
        String url = payload.get("target");
        String title = payload.get("title");
        String body = payload.get("body");
        // notification name
        String name = payload.get("name");
        // comment hash
        String hash = payload.get("hash");
        if (type != null && getEnabledTypes().contains(type) && body != null && body.trim().length() > 0) {
            // only log the receive event for valid notifications received
            if (firebaseAnalytics != null) {
                Bundle bundle = new Bundle();
                bundle.putString("name", name);
                firebaseAnalytics.logEvent(LbryAnalytics.EVENT_LBRY_NOTIFICATION_RECEIVE, bundle);
            }
            if (!Helper.isNullOrEmpty(hash)) {
                url = String.format("%s?comment_hash=%s", url, hash);
            }
            sendNotification(title, body, type, url, name);
        }
        // persist the notification data
        try {
            DatabaseHelper helper = DatabaseHelper.getInstance();
            SQLiteDatabase db = helper.getWritableDatabase();
            LbryNotification lnotification = new LbryNotification();
            lnotification.setTitle(title);
            lnotification.setDescription(body);
            lnotification.setTargetUrl(url);
            lnotification.setTimestamp(new Date());
            DatabaseHelper.createOrUpdateNotification(lnotification, db);
            // send a broadcast
            Intent intent = new Intent(ACTION_NOTIFICATION_RECEIVED);
            intent.putExtra("title", title);
            intent.putExtra("body", body);
            intent.putExtra("url", url);
            intent.putExtra("timestamp", lnotification.getTimestamp().getTime());
            sendBroadcast(intent);
        } catch (Exception ex) {
            // don't fail if any error occurs while saving a notification
            Log.e(TAG, "could not save notification", ex);
        }
    }
}
Also used : DatabaseHelper(com.odysee.app.data.DatabaseHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Bundle(android.os.Bundle) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification) Date(java.util.Date)

Example 3 with LbryNotification

use of com.odysee.app.model.lbryinc.LbryNotification in project odysee-android by OdyseeTeam.

the class GetLocalNotificationsSupplier method get.

@Override
public List<LbryNotification> get() {
    List<LbryNotification> notifications = new ArrayList<>();
    DatabaseHelper dbHelper = DatabaseHelper.getInstance();
    try {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        notifications = DatabaseHelper.getNotifications(db);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return notifications;
}
Also used : DatabaseHelper(com.odysee.app.data.DatabaseHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification)

Example 4 with LbryNotification

use of com.odysee.app.model.lbryinc.LbryNotification in project odysee-android by OdyseeTeam.

the class NotificationListSupplier method get.

@Override
public List<LbryNotification> get() {
    List<LbryNotification> notifications = new ArrayList<>();
    try {
        JSONArray array = (JSONArray) Lbryio.parseResponse(Lbryio.call("notification", "list", options, null));
        if (array != null) {
            for (int i = 0; i < array.length(); i++) {
                JSONObject item = array.getJSONObject(i);
                if (item.has("notification_parameters")) {
                    LbryNotification notification = new LbryNotification();
                    JSONObject notificationParams = item.getJSONObject("notification_parameters");
                    if (notificationParams.has("device")) {
                        JSONObject device = notificationParams.getJSONObject("device");
                        notification.setTitle(Helper.getJSONString("title", null, device));
                        notification.setDescription(Helper.getJSONString("text", null, device));
                        notification.setTargetUrl(Helper.getJSONString("target", null, device));
                    }
                    if (notificationParams.has("dynamic") && !notificationParams.isNull("dynamic")) {
                        JSONObject dynamic = notificationParams.getJSONObject("dynamic");
                        if (dynamic.has("comment_author") || dynamic.has("channel_thumbnail")) {
                            String url = null;
                            if (dynamic.has("comment_author"))
                                url = Helper.getJSONString("comment_author", null, dynamic);
                            else if (dynamic.has("channel_thumbnail"))
                                url = Helper.getJSONString("channel_thumbnail", null, dynamic);
                            notification.setAuthorThumbnailUrl(url);
                        }
                        if (dynamic.has("channelURI")) {
                            String channelUrl = Helper.getJSONString("channelURI", null, dynamic);
                            if (!Helper.isNullOrEmpty(channelUrl)) {
                                notification.setTargetUrl(channelUrl);
                            }
                        }
                        if (dynamic.has("claim_thumbnail")) {
                            String claimThumbnailUrl = Helper.getJSONString("claim_thumbnail", null, dynamic);
                            if (!Helper.isNullOrEmpty(claimThumbnailUrl)) {
                                notification.setClaimThumbnailUrl(claimThumbnailUrl);
                            }
                        }
                        if (dynamic.has("hash") && "comment".equalsIgnoreCase(Helper.getJSONString("notification_rule", null, item))) {
                            notification.setTargetUrl(String.format("%s?comment_hash=%s", notification.getTargetUrl(), dynamic.getString("hash")));
                        }
                    }
                    notification.setRule(Helper.getJSONString("notification_rule", null, item));
                    notification.setRemoteId(Helper.getJSONLong("id", 0, item));
                    notification.setRead(Helper.getJSONBoolean("is_read", false, item));
                    notification.setSeen(Helper.getJSONBoolean("is_seen", false, item));
                    try {
                        SimpleDateFormat dateFormat = new SimpleDateFormat(Helper.ISO_DATE_FORMAT_JSON, Locale.US);
                        notification.setTimestamp(dateFormat.parse(Helper.getJSONString("created_at", dateFormat.format(new Date()), item)));
                    } catch (ParseException ex) {
                        notification.setTimestamp(new Date());
                    }
                    if (notification.getRemoteId() > 0 && !Helper.isNullOrEmpty(notification.getDescription())) {
                        notifications.add(notification);
                    }
                }
            }
        }
    } catch (ClassCastException | LbryioRequestException | LbryioResponseException | JSONException | IllegalStateException ex) {
        ex.printStackTrace();
        return null;
    }
    return notifications;
}
Also used : LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification) Date(java.util.Date) JSONObject(org.json.JSONObject) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 5 with LbryNotification

use of com.odysee.app.model.lbryinc.LbryNotification in project odysee-android by OdyseeTeam.

the class NotificationListAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(NotificationListAdapter.ViewHolder vh, int position) {
    LbryNotification notification = items.get(position);
    vh.layoutView.setBackgroundColor(ContextCompat.getColor(context, notification.isRead() ? android.R.color.transparent : R.color.odyseePinkSemiTransparent));
    vh.selectedOverlayView.setVisibility(isNotificationSelected(notification) ? View.VISIBLE : View.GONE);
    vh.titleView.setVisibility(!Helper.isNullOrEmpty(notification.getTitle()) ? View.VISIBLE : View.GONE);
    vh.titleView.setText(notification.getTitle());
    vh.bodyView.setText(notification.getDescription());
    vh.timeView.setText(DateUtils.getRelativeTimeSpanString(getLocalNotificationTime(notification), System.currentTimeMillis(), 0, DateUtils.FORMAT_ABBREV_RELATIVE));
    vh.authorThumbnailView.setVisibility(notification.getCommentAuthor() == null || notification.getAuthorThumbnailUrl() == null ? View.INVISIBLE : View.VISIBLE);
    if (notification.getAuthorThumbnailUrl() != null)
        vh.authorThumbnailView.setVisibility(View.VISIBLE);
    if (notification.getCommentAuthor() != null || notification.getAuthorThumbnailUrl() != null) {
        String turl;
        if (notification.getCommentAuthor() != null)
            turl = notification.getCommentAuthor().getThumbnailUrl(vh.authorThumbnailView.getLayoutParams().width, vh.authorThumbnailView.getLayoutParams().height, 85);
        else {
            turl = getThumbnailUrl(vh.authorThumbnailView, notification.getAuthorThumbnailUrl());
        }
        Glide.with(context.getApplicationContext()).load(turl).apply(RequestOptions.circleCropTransform()).into(vh.authorThumbnailView);
    }
    if (notification.getClaimThumbnailUrl() != null) {
        vh.bodyView.getLayoutParams().width = (int) (200 * context.getApplicationContext().getResources().getDisplayMetrics().density);
        Configuration config = context.getApplicationContext().getResources().getConfiguration();
        if (config.smallestScreenWidthDp > 359) {
            String turl = getThumbnailUrl(vh.claimThumbnailView, notification.getClaimThumbnailUrl());
            Glide.with(context.getApplicationContext()).asBitmap().load(turl).into(vh.claimThumbnailView);
            vh.claimThumbnailView.setVisibility(View.VISIBLE);
        } else {
            vh.claimThumbnailView.setVisibility(View.GONE);
        }
    } else {
        vh.bodyView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
        vh.claimThumbnailView.setVisibility(View.GONE);
    }
    vh.iconView.setVisibility(notification.getCommentAuthor() != null || notification.getAuthorThumbnailUrl() != null ? View.INVISIBLE : View.VISIBLE);
    vh.iconView.setText(getStringIdForRule(notification.getRule()));
    vh.iconView.setTextColor(getColorForRule(notification.getRule()));
    vh.itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (inSelectionMode) {
                toggleSelectedNotification(notification);
            } else {
                if (clickListener != null) {
                    clickListener.onNotificationClicked(notification);
                }
                notification.setRead(true);
                notification.setSeen(true);
                notifyItemChanged(position);
            }
        }
    });
    vh.itemView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View view) {
            if (!inSelectionMode) {
                inSelectionMode = true;
                if (selectionModeListener != null) {
                    selectionModeListener.onEnterSelectionMode();
                }
            }
            toggleSelectedNotification(notification);
            return true;
        }
    });
}
Also used : Configuration(android.content.res.Configuration) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification) SolidIconView(com.odysee.app.ui.controls.SolidIconView) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView)

Aggregations

LbryNotification (com.odysee.app.model.lbryinc.LbryNotification)12 ArrayList (java.util.ArrayList)7 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)5 SpannableString (android.text.SpannableString)4 ParseException (java.text.ParseException)4 AccountManager (android.accounts.AccountManager)3 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)3 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 DatabaseHelper (com.odysee.app.data.DatabaseHelper)2 NotificationListSupplier (com.odysee.app.supplier.NotificationListSupplier)2 NotificationUpdateSupplier (com.odysee.app.supplier.NotificationUpdateSupplier)2 ExecutorService (java.util.concurrent.ExecutorService)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2