Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2012 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.inputmethod.keyboard.internal;
     18 
     19 import android.graphics.Canvas;
     20 import android.view.View;
     21 
     22 import com.android.inputmethod.keyboard.MainKeyboardView;
     23 import com.android.inputmethod.keyboard.PointerTracker;
     24 
     25 import javax.annotation.Nonnull;
     26 
     27 /**
     28  * Abstract base class for previews that are drawn on DrawingPreviewPlacerView, e.g.,
     29  * GestureFloatingTextDrawingPreview, GestureTrailsDrawingPreview, and
     30  * SlidingKeyInputDrawingPreview.
     31  */
     32 public abstract class AbstractDrawingPreview {
     33     private View mDrawingView;
     34     private boolean mPreviewEnabled;
     35     private boolean mHasValidGeometry;
     36 
     37     public void setDrawingView(@Nonnull final DrawingPreviewPlacerView drawingView) {
     38         mDrawingView = drawingView;
     39         drawingView.addPreview(this);
     40     }
     41 
     42     protected void invalidateDrawingView() {
     43         if (mDrawingView != null) {
     44             mDrawingView.invalidate();
     45         }
     46     }
     47 
     48     protected final boolean isPreviewEnabled() {
     49         return mPreviewEnabled && mHasValidGeometry;
     50     }
     51 
     52     public final void setPreviewEnabled(final boolean enabled) {
     53         mPreviewEnabled = enabled;
     54     }
     55 
     56     /**
     57      * Set {@link MainKeyboardView} geometry and position in the window of input method.
     58      * The class that is overriding this method must call this super implementation.
     59      *
     60      * @param originCoords the top-left coordinates of the {@link MainKeyboardView} in
     61      *        the input method window coordinate-system. This is unused but has a point in an
     62      *        extended class, such as {@link GestureTrailsDrawingPreview}.
     63      * @param width the width of {@link MainKeyboardView}.
     64      * @param height the height of {@link MainKeyboardView}.
     65      */
     66     public void setKeyboardViewGeometry(@Nonnull final int[] originCoords, final int width,
     67             final int height) {
     68         mHasValidGeometry = (width > 0 && height > 0);
     69     }
     70 
     71     public abstract void onDeallocateMemory();
     72 
     73     /**
     74      * Draws the preview
     75      * @param canvas The canvas where the preview is drawn.
     76      */
     77     public abstract void drawPreview(@Nonnull final Canvas canvas);
     78 
     79     /**
     80      * Set the position of the preview.
     81      * @param tracker The new location of the preview is based on the points in PointerTracker.
     82      */
     83     public abstract void setPreviewPosition(@Nonnull final PointerTracker tracker);
     84 }
     85