Cookie.java 21.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
package com.wd.common.notify;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.wd.common.imageglide.ImageUtils;
import com.wd.common.viewclick.BaseClickListener;
import com.wd.common.widget.RoundRectImageView;
import com.wd.fastcoding.base.R;
import com.wd.foundation.wdkit.statusbar.StatusBarCompat;
import com.wd.foundation.wdkit.utils.TimeUtil;


class Cookie extends LinearLayout {

    private Scroller scroller;
    private GestureDetector detector;
    private boolean isFling;
    private int currentId = 1;
    private boolean isScrollUp;
    private int minVelocity = 300;

    private void init() {
        scroller = new Scroller(getContext());
        detector = new GestureDetector(getContext(), new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent motionEvent) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent motionEvent) {
            }

            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                destroy();
                if (onActionClickListener != null) {
                    onActionClickListener.onClick(0);
                }
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float distanceX, float distanceY) {
                isScrollUp = false;
                if (Math.abs(distanceY) - Math.abs(distanceX) > 0) {
                    if (getScrollX() == 0) {
                        if (distanceY > 0 || motionEvent1.getY() < motionEvent.getY()) {
                            isScrollUp = true;
                            int scrollY = getScrollY();
                            scrollY = scrollY < 0 ? -scrollY : scrollY;
                            float alpha = 1;
                            if (motionEvent.getY() > 0) {
                                alpha = 1 - scrollY / motionEvent.getY();
                            }
                            if (alpha > 1) {
                                alpha = 1;
                            } else if (alpha < 0) {
                                alpha = 0;
                            }
                            layoutCookie.setAlpha(alpha);
                            scrollBy(0, (int) distanceY);
                        }
                    }
                    return false;
                }
                if (getScrollY() == 0) {
                    int scrollX = getScrollX();
                    float width = motionEvent.getX();
                    if (scrollX < 0) {
                        scrollX = -scrollX;
                        width = getWidth() - width;
                    }
                    float alpha = 1;
                    if (width > 0) {
                        alpha = 1 - scrollX / width;
                    }
                    if (alpha > 1) {
                        alpha = 1;
                    } else if (alpha < 0) {
                        alpha = 0;
                    }
                    layoutCookie.setAlpha(alpha);
                    scrollBy((int) distanceX, 0);
                }
                return false;
            }

            @Override
            public void onLongPress(MotionEvent motionEvent) {
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (Math.abs(velocityY) - Math.abs(velocityX) > 0) {
                    return false;
                }
                isFling = true;

                if (Math.abs(getScrollX()) > minVelocity) {
                    if (velocityX > 0) { // 快速向右滑动
                        currentId--;
                    } else if (velocityX < 0) {// 快速向左滑动
                        currentId++;
                    }
                }

                moveToDest(currentId);
                return false;
            }
        });

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                detector.onTouchEvent(ev);
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    private int firstDownX;
    private int firstDownY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        detector.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                firstDownX = (int) event.getX();
                firstDownY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                if (!isFling) { // 没有快速滑动的情况下
//                    if ((Math.abs(event.getY() - firstDownY) - Math.abs(event.getX() - firstDownX) > 0) && (event.getY() < firstDownY)) {
                    if (isScrollUp) {
                        int distanceY = getHeight() - getScrollY();
                        scroller.startScroll(0, getScrollY(), 0, distanceY);
                        invalidate();
                        destroy();
                        callDismiss(1);
                    } else {
                        if ((Math.abs(event.getY() - firstDownY) - Math.abs(event.getX() - firstDownX) > 0) && event.getY() - firstDownY > 50) {
                            destroy();
                            if (onActionClickListener != null) {
                                onActionClickListener.onClick(1);
                            }
                        } else {
                            int nextId;
                            if (event.getX() - firstDownX > getWidth() / 2) {
                                nextId = (currentId - 1) <= 0 ? 0 : currentId - 1;
                            } else if (firstDownX - event.getX() > getWidth() / 2) {
                                nextId = currentId + 1;
                            } else {
                                nextId = currentId;
                            }
                            moveToDest(nextId);
                        }
                    }
                }

                isFling = false;

                break;
        }
        return true;
    }

    private void moveToDest(int nextId) {
        if (nextId < 0) {
            currentId = 0;
        } else if (nextId > 2) {
            currentId = 2;
        } else {
            currentId = nextId;
        }

        int distanceX = (currentId - 1) * getWidth() - getScrollX();
        scroller.startScroll(getScrollX(), 0, distanceX, 0);
        invalidate();

        if (currentId != 1) {
            destroy();
            callDismiss(1);
        } else {
            layoutCookie.setAlpha(1);
        }
    }

    @Override
    public void computeScroll() {
        if (scroller.computeScrollOffset()) {
            int newX = scroller.getCurrX();
            int newY = scroller.getCurrY();
            scrollTo(newX, newY);
            invalidate();
        }
    }


    private Animation slideInAnimation;
    private Animation slideOutAnimation;

    private Context context;
    private LinearLayout layoutCookie;
    private View placeholderView;
    private TextView tvMessage;

    /**
     * 普通消息通知布局,适配单行不带图片文字
     */
    private LinearLayout llCookieContent;


    /**
     * 倒计时时间,5秒倒计时
     */
    private int runCount;

    private Handler handler;

    private Runnable runnableOrder;

    /**
     * 预约倒计时,或者去查看
     */
    private TextView tvOrderTv;

    /**
     * 顶部直播数据
     */
    private LinearLayout llLiveTop;

    /**
     * 推送数据提醒
     */
    private LinearLayout llCookiePush;

    /**
     * 权限提示说明
     */
    private LinearLayout llCookieTips;

    /**
     * 权限提示方案
     */
    private TextView tvPermissionText;

    /**
     * 直播中 提示
     */
    private TextView tvLiveTv;

    /**
     * 预约时间整体布局
     */
    private RelativeLayout rlOrder;
    private RoundRectImageView pushImage;
    private long duration;
    private int layoutGravity = Gravity.BOTTOM;

    private OnActionClickListener onActionClickListener;

    private OnDismissListener onDismissListener;

    public Cookie(@NonNull final Context context) {
        this(context, null);
    }

    public Cookie(@NonNull final Context context, @Nullable final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Cookie(@NonNull final Context context, @Nullable final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initViews();
    }

    private void initViews() {
        inflate(getContext(), R.layout.layout_cookie, this);
        layoutCookie = findViewById(R.id.cookie);
        placeholderView = findViewById(R.id.cookie_view_placeholder);
        tvMessage = findViewById(R.id.cookie_tv_content);
        pushImage = findViewById(R.id.push_image);
        rlOrder = findViewById(R.id.rl_order);
        tvOrderTv = findViewById(R.id.rl_order_tv);
        tvLiveTv = findViewById(R.id.push_live_txt);
        llLiveTop = findViewById(R.id.ll_live_top);
        llCookieContent = findViewById(R.id.cookie_ll_content);

        llCookiePush = findViewById(R.id.ll_cookie_push);
        llCookieTips = findViewById(R.id.ll_cookie_tips);
        tvPermissionText = findViewById(R.id.tips_message);


        int height = 0;
        if (context instanceof Activity) {
            height = StatusBarCompat.getStatusBarHeight((Activity) context);
        }

        if (height <= 0) {
            height = (int) context.getResources().getDimension(R.dimen.rmrb_dp30);
        }

        LayoutParams params = (LayoutParams) placeholderView.getLayoutParams();
        params.height = height;

        init();
    }

    public void setOrderTime(long time) {
        if (tvOrderTv != null && time > 0) {
            tvOrderTv.setText(time + "s|预约");
        } else {
            rlOrder.setVisibility(View.GONE);
        }
    }

    /**
     * 1.推送查询直播是否预约:
     * <p>
     * 直播id:targetId
     * 直播关系id:targetRelId
     * 推送场景:pushScene:1:直播提醒;2:用户订阅;3:普通站内通知
     * 直播开播时间:planStartTimeLong
     * <p>
     * 1.1 直播类型为直播提醒:
     * 判断开播时间和系统时间比较:已开播,显示去查看
     * 判断开播时间和系统时间比较:未开播,已预约,已预约
     * 判断开播时间和系统时间比较:未开播,未预约,显示 5s|预约
     * <p>
     * 1.2 直播类型为用户订阅:
     * 判断开播时间和系统时间比较:未开播, 你订阅的直播即将开播
     * 判断开播时间和系统时间比较:已开播, 你订阅的直播已开播
     * <p>
     * 1.3 普通站内通知
     * 只显示推送图片和推送内容,如没图片地址,文字居左
     *
     * @param params
     */
    public void setParams(final CookieBar.Params params) {
        if (params != null) {
            duration = params.duration;
            layoutGravity = params.layoutGravity;
            //权限提醒通知
            if( 5 == params.pushScene)
            {
                llCookiePush.setVisibility(View.GONE);
                if(!TextUtils.isEmpty(params.title)) {
                    tvPermissionText.setText(params.title);
                }
                llCookieTips.setVisibility(View.VISIBLE);
                createOutAnim();
//                if (layoutCookie != null) {
//                    layoutCookie.postDelayed(runnable, duration);
//                }
                return;
            }

            //消息推送通知
            llCookiePush.setVisibility(View.VISIBLE);
            //消息通知设置背景
            layoutCookie.setBackgroundColor(ContextCompat.getColor(context,R.color.res_color_common_C8));
            llCookieTips.setVisibility(View.GONE);
            if (1 == params.pushScene) {
                if ("running".equals(params.liveStatus)) {
                    //已开播显示去查看
                    tvOrderTv.setText("去查看");
                    tvLiveTv.setText("直播中");
                } else {
                    TimeUtil.transFormTime4(params.planStartTimeLong);
                    tvLiveTv.setText(TimeUtil.transFormTime4(params.planStartTimeLong) + " 开始直播");
                    // 未开播未预约
                    if (!params.isOrder) {
                        //设置可预约
                        setLiveOrder(false);
                        //预约倒计时
                        setTimer();
                        //预约按钮设置点击请求
                        setRequestOrder(params.targetId, params.targetRelIdId);
                    } else {
                        //设置已预约
                        setLiveOrder(true);

                    }
                }

            } else if (2 == params.pushScene ) {
                if ("running".equals(params.liveStatus)) {
                    //你订阅的直播已开播
                    tvLiveTv.setText("你订阅的直播已开播");

                    tvOrderTv.setVisibility(View.GONE);

                } else {
                    //你订阅的直播将于15-00开始直播
                    tvLiveTv.setText("你订阅的直播已将于" + TimeUtil.transFormTime6(params.planStartTimeLong) + "开播");
                    tvOrderTv.setVisibility(View.GONE);
                }

            } else {
                //普通站内通知,隐藏顶部直播数据
                llLiveTop.setVisibility(View.GONE);
            }


            //Message
            if (!TextUtils.isEmpty(params.message)) {
                tvMessage.setText(params.message);
            }

            // Image
            boolean isShowImg = false;
            if (!TextUtils.isEmpty(params.imgUrl)) {
                isShowImg = true;
            }
            if (isShowImg) {
                pushImage.setVisibility(VISIBLE);
                pushImage.setRadius((int)context.getResources().getDimension(R.dimen.rmrb_dp3));
                ImageUtils.getInstance().loadImage(pushImage, params.imgUrl);
            } else {
                LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT);
                llCookieContent.setLayoutParams(layoutParams);
                pushImage.setVisibility(GONE);
            }

            //Action
            if (params.onActionClickListener != null) {
                this.onActionClickListener = params.onActionClickListener;
            }

            //Dismiss
            if (params.onDismissListener != null) {
                this.onDismissListener = params.onDismissListener;
            }

            //Background
            if (params.backgroundColor != 0) {
                layoutCookie.setBackgroundColor(ContextCompat.getColor(getContext(), params.backgroundColor));
            }

            createInAnim();
            createOutAnim();

            startMyAnimation(slideInAnimation);
        }
    }

    private void startMyAnimation(Animation animation) {
        if (animation != null) {
            startAnimation(animation);
        }
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            startMyAnimation(slideOutAnimation);
        }
    };

    private void createInAnim() {
        slideInAnimation = AnimationUtils.loadAnimation(getContext(),
                layoutGravity == Gravity.BOTTOM ? R.anim.slide_in_from_bottom : R.anim.slide_in_from_top);
        slideInAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (layoutCookie != null) {
                    layoutCookie.postDelayed(runnable, duration);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    private void createOutAnim() {
        slideOutAnimation = AnimationUtils.loadAnimation(getContext(),
                layoutGravity == Gravity.BOTTOM ? R.anim.slide_out_to_bottom : R.anim.slide_out_to_top);
        slideOutAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                destroy();
                callDismiss(0);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    protected void destroy() {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                ViewParent parent = getParent();
                if (parent != null) {
                    Cookie.this.clearAnimation();
                    ((ViewGroup) parent).removeView(Cookie.this);
                }

                if (layoutCookie != null) {
                    layoutCookie.removeCallbacks(runnable);
                }
                slideInAnimation = null;
                slideOutAnimation = null;
            }
        }, 10);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, layoutCookie.getMeasuredHeight());
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (layoutGravity == Gravity.TOP) {
            super.onLayout(changed, l, 0, r, layoutCookie.getMeasuredHeight());
        } else {
            super.onLayout(changed, l, t, r, b);
        }
    }

    public int getLayoutGravity() {
        return layoutGravity;
    }

    /**
     * 0:定时自动消失
     * 1:手动滑动消失
     *
     * @param type see {@link OnDismissListener}
     */
    private void callDismiss(int type) {
        if (onDismissListener != null) {
            onDismissListener.dismiss(type);
        }
    }

    /**
     * 设置直播预约按钮状态
     *
     * @param isOrder
     */
    private void setLiveOrder(boolean isOrder) {
        if (isOrder) {
            tvOrderTv.setText("已预约");
            tvOrderTv.setTextColor(0xFFCCCCCC);
            tvOrderTv.setBackground(ContextCompat.getDrawable(context,R.drawable.push_shape_order));

        } else {
            tvOrderTv.setText("5" + "s|预约");
            tvOrderTv.setTextColor(0xFFFFFFFF);
            tvOrderTv.setBackground(ContextCompat.getDrawable(context,R.drawable.push_shape_red));
        }
    }

    /**
     * 预约倒计时设置
     */
    private void setTimer() {
        runCount = 4;
        handler = new Handler();
        runnableOrder = new Runnable() {
            @Override
            public void run() {
                if (runCount == 0) {// 第一次执行则关闭定时执行操作
                    handler.removeCallbacks(this);
                    setOrderTime(0);
                } else if (runCount > 0) {
                    setOrderTime(runCount);
                    handler.postDelayed(this, 1000);
                    runCount--;
                }
            }
        };
        // 打开定时器,执行操作
        handler.postDelayed(runnableOrder, 1000);
    }

    /**
     * 设置预约按钮点击请求
     */
    private void setRequestOrder(String liveId, String liveRelId) {
        // 预约点击
        tvOrderTv.setOnClickListener(new BaseClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
//                if (!PDUtils.isLogin()) {
//                    ProcessUtils.toOneKeyLoginActivity();
//                    return;
//                }
//                new PreviewDataFetcher(new PreviewDataListener() {
//                    @Override
//                    public void onCheckLiveSubscribeStatusSuccess(boolean mResult) {
//                        if (mResult) {
//                            setLiveOrder(mResult);
//                        }
//                    }
//
//                    @Override
//                    public void onPredictSuccess() {
//                    }
//
//                    @Override
//                    public void onFailed(String error) {
//
//                    }
//                }).predictLive(liveId, true, liveRelId);


            }
        });
    }

}