1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package com.android.test.dynamic;
15
16import android.app.Activity;
17import android.content.res.Resources;
18import android.graphics.drawable.AnimatedVectorDrawable;
19import android.graphics.drawable.VectorDrawable;
20import android.os.Bundle;
21import android.util.AttributeSet;
22import android.util.Log;
23import android.util.Xml;
24import android.widget.Button;
25import android.widget.GridLayout;
26import android.widget.ScrollView;
27import android.widget.TextView;
28
29import org.xmlpull.v1.XmlPullParser;
30import org.xmlpull.v1.XmlPullParserException;
31
32import java.io.IOException;
33import java.text.DecimalFormat;
34
35
36@SuppressWarnings({"UnusedDeclaration"})
37public class AnimatedVectorDrawableDupPerf extends Activity {
38
39    private static final String LOGTAG = "AnimatedVectorDrawableDupPerf";
40    protected int[] icon = {
41            R.drawable.animation_vector_linear_progress_bar,
42            R.drawable.animation_vector_linear_progress_bar,
43            R.drawable.animation_vector_linear_progress_bar,
44            R.drawable.animation_vector_linear_progress_bar,
45            R.drawable.animation_vector_linear_progress_bar,
46            R.drawable.animation_vector_linear_progress_bar,
47            R.drawable.animation_vector_linear_progress_bar,
48            R.drawable.animation_vector_linear_progress_bar,
49            R.drawable.animation_vector_linear_progress_bar,
50            R.drawable.animation_vector_linear_progress_bar,
51            R.drawable.animation_vector_linear_progress_bar,
52            R.drawable.animation_vector_linear_progress_bar,
53            R.drawable.animation_vector_linear_progress_bar,
54            R.drawable.animation_vector_linear_progress_bar,
55            R.drawable.animation_vector_linear_progress_bar,
56            R.drawable.animation_vector_linear_progress_bar,
57            R.drawable.animation_vector_linear_progress_bar,
58            R.drawable.animation_vector_linear_progress_bar,
59            R.drawable.animation_vector_linear_progress_bar,
60            R.drawable.animation_vector_linear_progress_bar,
61   };
62
63    /** @hide */
64    public static AnimatedVectorDrawable create(Resources resources, int rid) {
65        try {
66            final XmlPullParser parser = resources.getXml(rid);
67            final AttributeSet attrs = Xml.asAttributeSet(parser);
68            int type;
69            while ((type=parser.next()) != XmlPullParser.START_TAG &&
70                    type != XmlPullParser.END_DOCUMENT) {
71                // Empty loop
72            }
73            if (type != XmlPullParser.START_TAG) {
74                throw new XmlPullParserException("No start tag found");
75            }
76
77            final AnimatedVectorDrawable drawable = new AnimatedVectorDrawable();
78            drawable.inflate(resources, parser, attrs);
79
80            return drawable;
81        } catch (XmlPullParserException e) {
82            Log.e(LOGTAG, "parser error", e);
83        } catch (IOException e) {
84            Log.e(LOGTAG, "parser error", e);
85        }
86        return null;
87    }
88
89    @Override
90    protected void onCreate(Bundle savedInstanceState) {
91        super.onCreate(savedInstanceState);
92        ScrollView scrollView = new ScrollView(this);
93        GridLayout container = new GridLayout(this);
94        scrollView.addView(container);
95        container.setColumnCount(5);
96        Resources res = this.getResources();
97        container.setBackgroundColor(0xFF888888);
98        AnimatedVectorDrawable []d = new AnimatedVectorDrawable[icon.length];
99        long time =  android.os.SystemClock.elapsedRealtimeNanos();
100        for (int i = 0; i < icon.length; i++) {
101             d[i] = create(res,icon[i]);
102        }
103        time =  android.os.SystemClock.elapsedRealtimeNanos()-time;
104        TextView t = new TextView(this);
105        DecimalFormat df = new DecimalFormat("#.##");
106        t.setText("avgL=" + df.format(time / (icon.length * 1000000.)) + " ms");
107        container.addView(t);
108        time =  android.os.SystemClock.elapsedRealtimeNanos();
109        for (int i = 0; i < icon.length; i++) {
110            Button button = new Button(this);
111            button.setWidth(200);
112            button.setBackgroundResource(icon[i]);
113            container.addView(button);
114        }
115        setContentView(scrollView);
116        time =  android.os.SystemClock.elapsedRealtimeNanos()-time;
117        t = new TextView(this);
118        t.setText("avgS=" + df.format(time / (icon.length * 1000000.)) + " ms");
119        container.addView(t);
120    }
121
122}
123