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
17package android.support.test.vectordrawable;
18
19import android.app.Activity;
20import android.content.res.Resources;
21import android.graphics.PorterDuff.Mode;
22import android.graphics.drawable.Drawable.ConstantState;
23import android.os.Bundle;
24import android.support.graphics.drawable.VectorDrawableCompat;
25import android.widget.Button;
26import android.widget.LinearLayout;
27import android.widget.ScrollView;
28import android.widget.TextView;
29
30import java.text.DecimalFormat;
31
32public class TestActivity extends Activity {
33    private static final String LOG_TAG = "TestActivity";
34
35    private static final String LOGCAT = "VectorDrawable1";
36    protected int[] icon = {
37            R.drawable.vector_drawable01,
38            R.drawable.vector_drawable02,
39            R.drawable.vector_drawable03,
40            R.drawable.vector_drawable04,
41            R.drawable.vector_drawable05,
42            R.drawable.vector_drawable06,
43            R.drawable.vector_drawable07,
44            R.drawable.vector_drawable08,
45            R.drawable.vector_drawable09,
46            R.drawable.vector_drawable10,
47            R.drawable.vector_drawable11,
48            R.drawable.vector_drawable12,
49            R.drawable.vector_drawable13,
50            R.drawable.vector_drawable14,
51            R.drawable.vector_drawable15,
52            R.drawable.vector_drawable16,
53            R.drawable.vector_drawable17,
54            R.drawable.vector_drawable18,
55            R.drawable.vector_drawable19,
56            R.drawable.vector_drawable20,
57            R.drawable.vector_drawable21,
58            R.drawable.vector_drawable22,
59            R.drawable.vector_drawable23,
60            R.drawable.vector_drawable24,
61            R.drawable.vector_drawable25,
62            R.drawable.vector_drawable26,
63            R.drawable.vector_drawable27,
64            R.drawable.vector_drawable28,
65            R.drawable.vector_drawable29,
66            R.drawable.vector_drawable30,
67    };
68
69    private static final int EXTRA_TESTS = 2;
70
71    @Override
72    protected void onCreate(Bundle savedInstanceState) {
73        super.onCreate(savedInstanceState);
74        ScrollView scrollView = new ScrollView(this);
75        LinearLayout container = new LinearLayout(this);
76        scrollView.addView(container);
77        container.setOrientation(LinearLayout.VERTICAL);
78        Resources res = this.getResources();
79        container.setBackgroundColor(0xFF888888);
80        VectorDrawableCompat []d = new VectorDrawableCompat[icon.length];
81        long time =  android.os.SystemClock.currentThreadTimeMillis();
82        for (int i = 0; i < icon.length; i++) {
83             d[i] = VectorDrawableCompat.create(res, icon[i], getTheme());
84        }
85        time =  android.os.SystemClock.currentThreadTimeMillis()-time;
86
87        // Testing Tint on one particular case.
88        d[3].setTint(0x8000FF00);
89        d[3].setTintMode(Mode.MULTIPLY);
90
91        // Testing Constant State like operation by creating the first 2 icons
92        // from the 3rd one's constant state.
93        VectorDrawableCompat []extras = new VectorDrawableCompat[EXTRA_TESTS];
94        ConstantState state = d[0].getConstantState();
95        extras[0] = (VectorDrawableCompat) state.newDrawable();
96        extras[1] = (VectorDrawableCompat) state.newDrawable();
97
98        // This alpha change is expected to affect both extra 0, 1, and d0.
99        extras[0].setAlpha(128);
100
101        d[0].mutate();
102        d[0].setAlpha(255);
103
104        // Just show the average create time as the first view.
105        TextView t = new TextView(this);
106        DecimalFormat df = new DecimalFormat("#.##");
107        t.setText("avgL=" + df.format(time / (icon.length)) + " ms");
108        container.addView(t);
109
110        addDrawableButtons(container, extras);
111
112        addDrawableButtons(container, d);
113
114        setContentView(scrollView);
115    }
116
117    private void addDrawableButtons(LinearLayout container, VectorDrawableCompat[] d) {
118        // Add the VD into consequent views.
119        for (int i = 0; i < d.length; i++) {
120            Button button = new Button(this);
121            button.setWidth(200);
122            // Note that setBackgroundResource() will fail b/c createFromXmlInner() failed
123            // to recognize <vector> pre-L.
124            button.setBackgroundDrawable(d[i]);
125            container.addView(button);
126        }
127    }
128}
129