Home | History | Annotate | Download | only in classifier
      1 /*
      2  * Copyright (C) 2015 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.systemui.classifier;
     18 
     19 import android.util.SparseArray;
     20 import android.view.MotionEvent;
     21 
     22 import java.util.ArrayList;
     23 
     24 /**
     25  * Contains data which is used to classify interaction sequences on the lockscreen. It does, for
     26  * example, provide information on the current touch state.
     27  */
     28 public class ClassifierData {
     29     private SparseArray<Stroke> mCurrentStrokes = new SparseArray<>();
     30     private ArrayList<Stroke> mEndingStrokes = new ArrayList<>();
     31     private final float mDpi;
     32 
     33     public ClassifierData(float dpi) {
     34         mDpi = dpi;
     35     }
     36 
     37     public void update(MotionEvent event) {
     38         mEndingStrokes.clear();
     39         int action = event.getActionMasked();
     40         if (action == MotionEvent.ACTION_DOWN) {
     41             mCurrentStrokes.clear();
     42         }
     43 
     44         for (int i = 0; i < event.getPointerCount(); i++) {
     45             int id = event.getPointerId(i);
     46             if (mCurrentStrokes.get(id) == null) {
     47                 mCurrentStrokes.put(id, new Stroke(event.getEventTimeNano(), mDpi));
     48             }
     49             mCurrentStrokes.get(id).addPoint(event.getX(i), event.getY(i),
     50                     event.getEventTimeNano());
     51 
     52             if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL
     53                     || (action == MotionEvent.ACTION_POINTER_UP && i == event.getActionIndex())) {
     54                 mEndingStrokes.add(getStroke(id));
     55             }
     56         }
     57     }
     58 
     59     public void cleanUp(MotionEvent event) {
     60         mEndingStrokes.clear();
     61         int action = event.getActionMasked();
     62         for (int i = 0; i < event.getPointerCount(); i++) {
     63             int id = event.getPointerId(i);
     64             if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL
     65                     || (action == MotionEvent.ACTION_POINTER_UP && i == event.getActionIndex())) {
     66                 mCurrentStrokes.remove(id);
     67             }
     68         }
     69     }
     70 
     71     /**
     72      * @return the list of Strokes which are ending in the recently added MotionEvent
     73      */
     74     public ArrayList<Stroke> getEndingStrokes() {
     75         return mEndingStrokes;
     76     }
     77 
     78     /**
     79      * @param id the id from MotionEvent
     80      * @return the Stroke assigned to the id
     81      */
     82     public Stroke getStroke(int id) {
     83         return mCurrentStrokes.get(id);
     84     }
     85 }
     86