1aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne/*
2aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * Copyright (C) 2011 The Android Open Source Project
3aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *
4aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * Licensed under the Apache License, Version 2.0 (the "License");
5aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * you may not use this file except in compliance with the License.
6aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * You may obtain a copy of the License at
7aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *
8aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *      http://www.apache.org/licenses/LICENSE-2.0
9aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *
10aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * Unless required by applicable law or agreed to in writing, software
11aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * distributed under the License is distributed on an "AS IS" BASIS,
12aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * See the License for the specific language governing permissions and
14aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * limitations under the License.
15aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */
16aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
17aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milnepackage com.android.test.layout;
18aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
19aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.app.Activity;
20aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.content.Context;
21aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.os.Bundle;
22aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.view.View;
23a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport android.view.View.OnClickListener;
24aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.view.ViewGroup;
25aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.widget.Button;
26aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.widget.EditText;
27aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.widget.GridLayout;
28a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport android.widget.GridLayout.Alignment;
29a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport android.widget.GridLayout.LayoutParams;
30aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.widget.TextView;
31aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
32a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport static android.widget.GridLayout.BASELINE;
33a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport static android.widget.GridLayout.BOTTOM;
34a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport static android.widget.GridLayout.CENTER;
35a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport static android.widget.GridLayout.FILL;
36a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport static android.widget.GridLayout.LEFT;
37a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport static android.widget.GridLayout.RIGHT;
38a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport static android.widget.GridLayout.TOP;
39a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyeimport static android.widget.GridLayout.spec;
40aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
41a7726271f756dfc6f2ff3bb10fc4167e750fb606Tor Norbyepublic class AlignmentTest extends Activity {
42aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
437fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne    public static final String[] HORIZONTAL_NAMES = {"LEFT", "center", "east", "fill"};
447fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne    public static final Alignment[] HORIZONTAL_ALIGNMENTS = {LEFT, CENTER, RIGHT, FILL};
457fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne    public static final String[] VERTICAL_NAMES = {"north", "center", "baseline", "south", "fill"};
467fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne    public static final Alignment[] VERTICAL_ALIGNMENTS = {TOP, CENTER, BASELINE, BOTTOM, FILL};
47aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    private static Context CONTEXT;
48aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
49aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static interface ViewFactory {
50aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        View create(String name, int size);
51aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
52aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
53aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static final ViewFactory BUTTON_FACTORY = new ViewFactory() {
54aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        public View create(String name, int size) {
55aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            Button result = new Button(CONTEXT);
56aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            result.setText(name);
57aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne           result.setOnClickListener(new OnClickListener() {
58aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne               @Override
59aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne               public void onClick(View v) {
60aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne                    animate(v);
61aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne               }
62aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne           });
63aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            return result;
64aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
65aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    };
66aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
67aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static final ViewFactory LABEL_FACTORY = new ViewFactory() {
68aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        public View create(String name, int size) {
69aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            TextView result = new TextView(CONTEXT);
70aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            result.setText(name);
71aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            result.setTextSize(40);
72aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            return result;
73aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
74aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    };
75aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
76aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static final ViewFactory TEXT_FIELD_FACTORY = new ViewFactory() {
77aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        public View create(String name, int size) {
78aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            EditText result = new EditText(CONTEXT);
79aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            result.setText(name);
80aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            return result;
81aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
82aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    };
83aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
847fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne    public static final ViewFactory[] FACTORIES =
857fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne                            {BUTTON_FACTORY, LABEL_FACTORY, TEXT_FIELD_FACTORY};
86aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
87aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static ViewGroup create(Context context1) {
88aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        CONTEXT = context1;
89aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        GridLayout container = new GridLayout(context1);
90aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        container.setUseDefaultMargins(true);
91aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
92aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        for (int i = 0; i < VERTICAL_ALIGNMENTS.length; i++) {
93aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            Alignment va = VERTICAL_ALIGNMENTS[i];
94aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            for (int j = 0; j < HORIZONTAL_ALIGNMENTS.length; j++) {
95aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne                Alignment ha = HORIZONTAL_ALIGNMENTS[j];
9693cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne                LayoutParams layoutParams = new LayoutParams(spec(i, va), spec(j, ha));
977fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne                String name = VERTICAL_NAMES[i] + "-" + HORIZONTAL_NAMES[j];
987fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne                ViewFactory factory = FACTORIES[(i + j) % FACTORIES.length];
997fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne                container.addView(factory.create(name, 20), layoutParams);
100aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            }
101aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
102aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
103aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        return container;
104aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
105aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
106aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static void animate(View v) {
107aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
108aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        long start = System.currentTimeMillis();
109aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        int N = 1000;
110aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        for (int i = 0; i < N; i++) {
111aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            ViewGroup.LayoutParams lp = v.getLayoutParams();
112aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            lp.width += 1; // width;
113aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            lp.height += 1; // height;
114aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            v.requestLayout();
115aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            GridLayout p = (GridLayout) v.getParent();
116aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            p.layout(0, 0, 1000 + (i % 2), 500 + (i % 2));
117aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
1187fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne        float time = (float) (System.currentTimeMillis() - start) / N * 1000;
1197fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne        System.out.println("Time: " + time + "mics");
120aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
121aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
122aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    protected void onCreate(Bundle savedInstanceState) {
123aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        super.onCreate(savedInstanceState);
124aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        setContentView(create(getBaseContext()));
125aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
126aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
127aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne}