1/*
2 * Copyright (C) 2010 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
17package android.graphics;
18
19import com.android.layoutlib.bridge.impl.DelegateManager;
20import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
21
22import android.graphics.Shader.TileMode;
23
24/**
25 * Delegate implementing the native methods of android.graphics.Shader
26 *
27 * Through the layoutlib_create tool, the original native methods of Shader have been replaced
28 * by calls to methods of the same name in this delegate class.
29 *
30 * This class behaves like the original native implementation, but in Java, keeping previously
31 * native data into its own objects and mapping them to int that are sent back and forth between
32 * it and the original Shader class.
33 *
34 * This also serve as a base class for all Shader delegate classes.
35 *
36 * @see DelegateManager
37 *
38 */
39public abstract class Shader_Delegate {
40
41    // ---- delegate manager ----
42    protected static final DelegateManager<Shader_Delegate> sManager =
43            new DelegateManager<Shader_Delegate>(Shader_Delegate.class);
44
45    // ---- delegate helper data ----
46
47    // ---- delegate data ----
48    private Matrix_Delegate mLocalMatrix = null;
49
50    // ---- Public Helper methods ----
51
52    public static Shader_Delegate getDelegate(long nativeShader) {
53        return sManager.getDelegate(nativeShader);
54    }
55
56    /**
57     * Returns the {@link TileMode} matching the given int.
58     * @param tileMode the tile mode int value
59     * @return the TileMode enum.
60     */
61    public static TileMode getTileMode(int tileMode) {
62        for (TileMode tm : TileMode.values()) {
63            if (tm.nativeInt == tileMode) {
64                return tm;
65            }
66        }
67
68        assert false;
69        return TileMode.CLAMP;
70    }
71
72    public abstract java.awt.Paint getJavaPaint();
73    public abstract boolean isSupported();
74    public abstract String getSupportMessage();
75
76    // ---- native methods ----
77
78    @LayoutlibDelegate
79    /*package*/ static void nativeDestructor(long native_shader) {
80        sManager.removeJavaReferenceFor(native_shader);
81    }
82
83    @LayoutlibDelegate
84    /*package*/ static void nativeSetLocalMatrix(long native_shader, long matrix_instance) {
85        // get the delegate from the native int.
86        Shader_Delegate shaderDelegate = sManager.getDelegate(native_shader);
87        if (shaderDelegate == null) {
88            return;
89        }
90
91        shaderDelegate.mLocalMatrix = Matrix_Delegate.getDelegate(matrix_instance);
92    }
93
94    // ---- Private delegate/helper methods ----
95
96    protected java.awt.geom.AffineTransform getLocalMatrix() {
97        if (mLocalMatrix != null) {
98            return mLocalMatrix.getAffineTransform();
99        }
100
101        return new java.awt.geom.AffineTransform();
102    }
103
104}
105