1030d619aaab044491381e72f222cc9ba8dabcc19John Reck/*
2030d619aaab044491381e72f222cc9ba8dabcc19John Reck * Copyright (C) 2014 The Android Open Source Project
3030d619aaab044491381e72f222cc9ba8dabcc19John Reck *
4030d619aaab044491381e72f222cc9ba8dabcc19John Reck * Licensed under the Apache License, Version 2.0 (the "License");
5030d619aaab044491381e72f222cc9ba8dabcc19John Reck * you may not use this file except in compliance with the License.
6030d619aaab044491381e72f222cc9ba8dabcc19John Reck * You may obtain a copy of the License at
7030d619aaab044491381e72f222cc9ba8dabcc19John Reck *
8030d619aaab044491381e72f222cc9ba8dabcc19John Reck *      http://www.apache.org/licenses/LICENSE-2.0
9030d619aaab044491381e72f222cc9ba8dabcc19John Reck *
10030d619aaab044491381e72f222cc9ba8dabcc19John Reck * Unless required by applicable law or agreed to in writing, software
11030d619aaab044491381e72f222cc9ba8dabcc19John Reck * distributed under the License is distributed on an "AS IS" BASIS,
12030d619aaab044491381e72f222cc9ba8dabcc19John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13030d619aaab044491381e72f222cc9ba8dabcc19John Reck * See the License for the specific language governing permissions and
14030d619aaab044491381e72f222cc9ba8dabcc19John Reck * limitations under the License.
15030d619aaab044491381e72f222cc9ba8dabcc19John Reck */
16030d619aaab044491381e72f222cc9ba8dabcc19John Reck
17030d619aaab044491381e72f222cc9ba8dabcc19John Reckpackage com.android.test.hwui;
18030d619aaab044491381e72f222cc9ba8dabcc19John Reck
19030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.app.Activity;
20030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.app.Dialog;
21030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.content.Context;
22030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.content.DialogInterface;
23030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.graphics.Canvas;
24030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.os.Bundle;
25030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.os.Looper;
26030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.view.View;
27030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.view.ViewGroup.LayoutParams;
28030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.webkit.WebChromeClient;
29030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.webkit.WebView;
30030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.webkit.WebViewClient;
31030d619aaab044491381e72f222cc9ba8dabcc19John Reckimport android.widget.LinearLayout;
32030d619aaab044491381e72f222cc9ba8dabcc19John Reck
33030d619aaab044491381e72f222cc9ba8dabcc19John Reckpublic class LooperAcceleration extends Activity {
34030d619aaab044491381e72f222cc9ba8dabcc19John Reck
35030d619aaab044491381e72f222cc9ba8dabcc19John Reck    static final boolean INCLUDE_WEBVIEW = false;
36030d619aaab044491381e72f222cc9ba8dabcc19John Reck
37030d619aaab044491381e72f222cc9ba8dabcc19John Reck    static class IsAcceleratedView extends View {
38030d619aaab044491381e72f222cc9ba8dabcc19John Reck
39030d619aaab044491381e72f222cc9ba8dabcc19John Reck        public IsAcceleratedView(Context context) {
40030d619aaab044491381e72f222cc9ba8dabcc19John Reck            super(context);
41030d619aaab044491381e72f222cc9ba8dabcc19John Reck        }
42030d619aaab044491381e72f222cc9ba8dabcc19John Reck
43030d619aaab044491381e72f222cc9ba8dabcc19John Reck        @Override
44030d619aaab044491381e72f222cc9ba8dabcc19John Reck        protected void onDraw(Canvas canvas) {
45030d619aaab044491381e72f222cc9ba8dabcc19John Reck            super.onDraw(canvas);
46030d619aaab044491381e72f222cc9ba8dabcc19John Reck            if (canvas.isHardwareAccelerated()) {
47030d619aaab044491381e72f222cc9ba8dabcc19John Reck                canvas.drawARGB(0xFF, 0x00, 0xFF, 0x00);
48030d619aaab044491381e72f222cc9ba8dabcc19John Reck            } else {
49030d619aaab044491381e72f222cc9ba8dabcc19John Reck                canvas.drawARGB(0xFF, 0xFF, 0x00, 0x00);
50030d619aaab044491381e72f222cc9ba8dabcc19John Reck            }
51030d619aaab044491381e72f222cc9ba8dabcc19John Reck        }
52030d619aaab044491381e72f222cc9ba8dabcc19John Reck
53030d619aaab044491381e72f222cc9ba8dabcc19John Reck    }
54030d619aaab044491381e72f222cc9ba8dabcc19John Reck
55030d619aaab044491381e72f222cc9ba8dabcc19John Reck    private View makeView() {
56030d619aaab044491381e72f222cc9ba8dabcc19John Reck        LinearLayout layout = new LinearLayout(this);
57030d619aaab044491381e72f222cc9ba8dabcc19John Reck        layout.addView(new IsAcceleratedView(this), LayoutParams.MATCH_PARENT, 60);
58030d619aaab044491381e72f222cc9ba8dabcc19John Reck
59030d619aaab044491381e72f222cc9ba8dabcc19John Reck        if (INCLUDE_WEBVIEW) {
60030d619aaab044491381e72f222cc9ba8dabcc19John Reck            WebView wv = new WebView(this);
61030d619aaab044491381e72f222cc9ba8dabcc19John Reck            wv.setWebViewClient(new WebViewClient());
62030d619aaab044491381e72f222cc9ba8dabcc19John Reck            wv.setWebChromeClient(new WebChromeClient());
63030d619aaab044491381e72f222cc9ba8dabcc19John Reck            wv.loadUrl("http://www.webkit.org/blog-files/3d-transforms/poster-circle.html");
64030d619aaab044491381e72f222cc9ba8dabcc19John Reck            layout.addView(wv, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
65030d619aaab044491381e72f222cc9ba8dabcc19John Reck        }
66030d619aaab044491381e72f222cc9ba8dabcc19John Reck        return layout;
67030d619aaab044491381e72f222cc9ba8dabcc19John Reck    }
68030d619aaab044491381e72f222cc9ba8dabcc19John Reck
69030d619aaab044491381e72f222cc9ba8dabcc19John Reck    @Override
70030d619aaab044491381e72f222cc9ba8dabcc19John Reck    protected void onCreate(Bundle savedInstanceState) {
71030d619aaab044491381e72f222cc9ba8dabcc19John Reck        super.onCreate(savedInstanceState);
72030d619aaab044491381e72f222cc9ba8dabcc19John Reck
73030d619aaab044491381e72f222cc9ba8dabcc19John Reck        setContentView(makeView());
74030d619aaab044491381e72f222cc9ba8dabcc19John Reck
75030d619aaab044491381e72f222cc9ba8dabcc19John Reck        new Thread() {
76030d619aaab044491381e72f222cc9ba8dabcc19John Reck            @Override
77030d619aaab044491381e72f222cc9ba8dabcc19John Reck            public void run() {
78030d619aaab044491381e72f222cc9ba8dabcc19John Reck                Looper.prepare();
79030d619aaab044491381e72f222cc9ba8dabcc19John Reck                final Context context = LooperAcceleration.this;
80030d619aaab044491381e72f222cc9ba8dabcc19John Reck                Dialog dlg = new Dialog(context);
81030d619aaab044491381e72f222cc9ba8dabcc19John Reck                dlg.addContentView(makeView(), new LayoutParams(300, 400));
82030d619aaab044491381e72f222cc9ba8dabcc19John Reck                dlg.setCancelable(true);
83030d619aaab044491381e72f222cc9ba8dabcc19John Reck                dlg.setCanceledOnTouchOutside(true);
84030d619aaab044491381e72f222cc9ba8dabcc19John Reck                dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {
85030d619aaab044491381e72f222cc9ba8dabcc19John Reck                    @Override
86030d619aaab044491381e72f222cc9ba8dabcc19John Reck                    public void onDismiss(DialogInterface dialog) {
87030d619aaab044491381e72f222cc9ba8dabcc19John Reck                        Looper.myLooper().quit();
88030d619aaab044491381e72f222cc9ba8dabcc19John Reck                    }
89030d619aaab044491381e72f222cc9ba8dabcc19John Reck                });
90030d619aaab044491381e72f222cc9ba8dabcc19John Reck                dlg.setTitle("Not Looper.getMainLooper() check");
91030d619aaab044491381e72f222cc9ba8dabcc19John Reck                dlg.show();
92030d619aaab044491381e72f222cc9ba8dabcc19John Reck                Looper.loop();
93030d619aaab044491381e72f222cc9ba8dabcc19John Reck            }
94030d619aaab044491381e72f222cc9ba8dabcc19John Reck        }.start();
95030d619aaab044491381e72f222cc9ba8dabcc19John Reck    }
96030d619aaab044491381e72f222cc9ba8dabcc19John Reck}
97