Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2013 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.os.Message;
     20 
     21 import com.android.inputmethod.keyboard.Key;
     22 import com.android.inputmethod.keyboard.internal.DrawingHandler.Callbacks;
     23 import com.android.inputmethod.latin.SuggestedWords;
     24 import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper;
     25 
     26 // TODO: Separate this class into KeyPreviewHandler and BatchInputPreviewHandler or so.
     27 public class DrawingHandler extends LeakGuardHandlerWrapper<Callbacks> {
     28     public interface Callbacks {
     29         public void dismissKeyPreviewWithoutDelay(Key key);
     30         public void dismissAllKeyPreviews();
     31         public void showGestureFloatingPreviewText(SuggestedWords suggestedWords);
     32     }
     33 
     34     private static final int MSG_DISMISS_KEY_PREVIEW = 0;
     35     private static final int MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT = 1;
     36 
     37     public DrawingHandler(final Callbacks ownerInstance) {
     38         super(ownerInstance);
     39     }
     40 
     41     @Override
     42     public void handleMessage(final Message msg) {
     43         final Callbacks callbacks = getOwnerInstance();
     44         if (callbacks == null) {
     45             return;
     46         }
     47         switch (msg.what) {
     48         case MSG_DISMISS_KEY_PREVIEW:
     49             callbacks.dismissKeyPreviewWithoutDelay((Key)msg.obj);
     50             break;
     51         case MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT:
     52             callbacks.showGestureFloatingPreviewText(SuggestedWords.EMPTY);
     53             break;
     54         }
     55     }
     56 
     57     public void dismissKeyPreview(final long delay, final Key key) {
     58         sendMessageDelayed(obtainMessage(MSG_DISMISS_KEY_PREVIEW, key), delay);
     59     }
     60 
     61     private void cancelAllDismissKeyPreviews() {
     62         removeMessages(MSG_DISMISS_KEY_PREVIEW);
     63         final Callbacks callbacks = getOwnerInstance();
     64         if (callbacks == null) {
     65             return;
     66         }
     67         callbacks.dismissAllKeyPreviews();
     68     }
     69 
     70     public void dismissGestureFloatingPreviewText(final long delay) {
     71         sendMessageDelayed(obtainMessage(MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT), delay);
     72     }
     73 
     74     public void cancelAllMessages() {
     75         cancelAllDismissKeyPreviews();
     76     }
     77 }
     78