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.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Paint;
22 import android.graphics.PorterDuff;
23 import android.graphics.PorterDuffXfermode;
24 import android.util.AttributeSet;
25 import android.widget.RelativeLayout;
26 
27 import com.android.inputmethod.latin.common.CoordinateUtils;
28 
29 import java.util.ArrayList;
30 
31 public final class DrawingPreviewPlacerView extends RelativeLayout {
32     private final int[] mKeyboardViewOrigin = CoordinateUtils.newInstance();
33 
34     private final ArrayList<AbstractDrawingPreview> mPreviews = new ArrayList<>();
35 
DrawingPreviewPlacerView(final Context context, final AttributeSet attrs)36     public DrawingPreviewPlacerView(final Context context, final AttributeSet attrs) {
37         super(context, attrs);
38         setWillNotDraw(false);
39     }
40 
setHardwareAcceleratedDrawingEnabled(final boolean enabled)41     public void setHardwareAcceleratedDrawingEnabled(final boolean enabled) {
42         if (!enabled) return;
43         final Paint layerPaint = new Paint();
44         layerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
45         setLayerType(LAYER_TYPE_HARDWARE, layerPaint);
46     }
47 
addPreview(final AbstractDrawingPreview preview)48     public void addPreview(final AbstractDrawingPreview preview) {
49         if (mPreviews.indexOf(preview) < 0) {
50             mPreviews.add(preview);
51         }
52     }
53 
setKeyboardViewGeometry(final int[] originCoords, final int width, final int height)54     public void setKeyboardViewGeometry(final int[] originCoords, final int width,
55             final int height) {
56         CoordinateUtils.copy(mKeyboardViewOrigin, originCoords);
57         final int count = mPreviews.size();
58         for (int i = 0; i < count; i++) {
59             mPreviews.get(i).setKeyboardViewGeometry(originCoords, width, height);
60         }
61     }
62 
deallocateMemory()63     public void deallocateMemory() {
64         final int count = mPreviews.size();
65         for (int i = 0; i < count; i++) {
66             mPreviews.get(i).onDeallocateMemory();
67         }
68     }
69 
70     @Override
onDetachedFromWindow()71     protected void onDetachedFromWindow() {
72         super.onDetachedFromWindow();
73         deallocateMemory();
74     }
75 
76     @Override
onDraw(final Canvas canvas)77     public void onDraw(final Canvas canvas) {
78         super.onDraw(canvas);
79         final int originX = CoordinateUtils.x(mKeyboardViewOrigin);
80         final int originY = CoordinateUtils.y(mKeyboardViewOrigin);
81         canvas.translate(originX, originY);
82         final int count = mPreviews.size();
83         for (int i = 0; i < count; i++) {
84             mPreviews.get(i).drawPreview(canvas);
85         }
86         canvas.translate(-originX, -originY);
87     }
88 }
89