CookiePermiss.java 12.7 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
package com.wd.common.notify;

import android.app.Activity;
import android.content.Context;
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.Scroller;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wd.fastcoding.base.R;
import com.wd.foundation.wdkitcore.tools.ResUtils;


/**
 *  权限提醒蒙层
 *
 * @author qts
 * @data 2024/05/12.
 */
class CookiePermiss 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 LinearLayout llCookieTips;

    /**
     * 权限提示方案
     */
    private TextView tvPermissionText;
    private long duration;
    private int layoutGravity = Gravity.BOTTOM;

    private OnActionClickListener onActionClickListener;

    private OnDismissListener onDismissListener;

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

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

    public CookiePermiss(@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_permiss, this);
        layoutCookie = findViewById(R.id.cookie);
        placeholderView = findViewById(R.id.cookie_view_placeholder);
        llCookieTips = findViewById(R.id.ll_cookie_tips);
        tvPermissionText = findViewById(R.id.tips_message);

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

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

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

        init();
    }


    /**
     * 5.权限通知提醒
     * @param params
     */
    public void setParams(final CookieBarPermiss.Params params) {
        if (params != null) {
            duration = params.duration;
            layoutGravity = params.layoutGravity;
            //权限提醒通知
            if( 5 == params.pushScene)
            {
                if(!TextUtils.isEmpty(params.title)) {
                    tvPermissionText.setText(params.title);
                }
                llCookieTips.setVisibility(View.VISIBLE);
                createOutAnim();
            }


        }
    }

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

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

    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) {
                    CookiePermiss.this.clearAnimation();
                    ((ViewGroup) parent).removeView(CookiePermiss.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 activity Activity页面
     * @return 状态栏高度
     */
    private int getStatusBarHeight(Activity activity) {
        int statusBarHeight = 0;
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
        }

        if(statusBarHeight == 0)
        {
            statusBarHeight = (int) ResUtils.getDimension(R.dimen.rmrb_dp25);
        }
        return statusBarHeight;

    }

}