• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.emergency.widgets.slider;
18 
19 import android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.util.AttributeSet;
22 import android.view.MotionEvent;
23 import android.widget.SeekBar;
24 
25 /** A custom {@link SeekBar} that has a threshold to send completion event to listeners. */
26 public final class SliderSeekBar extends SeekBar {
27 
28     static final int SLIDE_MAX_SLIDE_SCALE = 1000;
29     private static final float SLIDE_COMPLETE_THRESHOLD = 0.8f;
30 
31     private Drawable mThumb;
32     private OnSlideCompleteListener mListener;
33 
SliderSeekBar(Context context)34     public SliderSeekBar(Context context) {
35         super(context);
36     }
37 
SliderSeekBar(Context context, AttributeSet attrs)38     public SliderSeekBar(Context context, AttributeSet attrs) {
39         super(context, attrs);
40         setMax(SLIDE_MAX_SLIDE_SCALE);
41     }
42 
43     @Override
setThumb(Drawable thumb)44     public void setThumb(Drawable thumb) {
45         this.mThumb = thumb;
46         super.setThumb(thumb);
47     }
48 
49     @Override
onTouchEvent(MotionEvent event)50     public boolean onTouchEvent(MotionEvent event) {
51         switch (event.getAction()) {
52             case MotionEvent.ACTION_DOWN: {
53                 if (!mThumb.getBounds().contains((int) event.getX(), (int) event.getY())) {
54                     // Only handle when within bound
55                     return false;
56                 }
57                 super.onTouchEvent(event);
58             }
59             break;
60             case MotionEvent.ACTION_UP: {
61                 if (getProgress() > SLIDE_COMPLETE_THRESHOLD * SLIDE_MAX_SLIDE_SCALE) {
62                     if (mListener != null) {
63                         mListener.onSlideComplete();
64                     }
65                     // Event completed, snap to end
66                     setProgress(SLIDE_MAX_SLIDE_SCALE);
67                 }else {
68                     // Event not completed, snap back to beginning
69                     setProgress(0);
70                 }
71             }
72             break;
73             default:
74                 super.onTouchEvent(event);
75         }
76         return true;
77     }
78 
setOnSlideCompleteListener(OnSlideCompleteListener listener)79     void setOnSlideCompleteListener(OnSlideCompleteListener listener) {
80         this.mListener = listener;
81     }
82 }