1/*
2 * Copyright (C) 2012 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 com.android.test.hwuicompare;
18
19import com.android.test.hwuicompare.R;
20
21import android.graphics.Bitmap;
22import android.graphics.Color;
23import android.os.Bundle;
24import android.util.Log;
25import android.view.View;
26import android.view.View.OnClickListener;
27import android.widget.AdapterView;
28import android.widget.ArrayAdapter;
29import android.widget.Button;
30import android.widget.ImageButton;
31import android.widget.ImageView;
32import android.widget.LinearLayout;
33import android.widget.Spinner;
34import android.widget.TextView;
35
36public class ManualActivity extends CompareActivity {
37    private static final String LOG_TAG = "ManualActivity";
38    private ImageView mCompareImageView;
39    private Bitmap mCompareBitmap;
40    private TextView mErrorTextView;
41    private MainView mSoftwareView;
42
43    private static final int COMPARE_VIEW_UNINITIALIZED = -1;
44    private static final int COMPARE_VIEW_HARDWARE = 0;
45    private static final int COMPARE_VIEW_SOFTWARE = 1;
46    private static final int COMPARE_VIEW_HEATMAP = 2; // TODO: add more like this? any ideas?
47
48    private int mCompareImageViewState = COMPARE_VIEW_UNINITIALIZED;
49    private int mLastCompareImageViewState = COMPARE_VIEW_UNINITIALIZED;
50
51    Runnable mRunnable = new Runnable() {
52        @Override
53        public void run() {
54            Log.d(LOG_TAG, "mRunnable running, mRedrewFlag = " + mRedrewFlag);
55
56            if (mRedrewFlag) {
57                loadBitmaps();
58                // recalculate error
59                float error = mErrorCalculator.calcErrorRS(mSoftwareBitmap, mHardwareBitmap);
60                String modname = "";
61                for (String s : DisplayModifier.getLastAppliedModifications()) {
62                    modname = modname.concat(s + ".");
63                }
64
65                Log.d(LOG_TAG, "error for " + modname + " is " + error);
66                mErrorTextView.setText(String.format("%.4f", error));
67            }
68
69            if (mCompareImageViewState != mLastCompareImageViewState || mRedrewFlag) {
70                switch (mCompareImageViewState) {
71                    case COMPARE_VIEW_UNINITIALIZED:
72                        // set to hardware
73                    case COMPARE_VIEW_HARDWARE:
74                        mCompareImageView.setImageBitmap(mHardwareBitmap);
75                        break;
76                    case COMPARE_VIEW_SOFTWARE:
77                        mCompareImageView.setImageBitmap(mSoftwareBitmap);
78                        break;
79                    case COMPARE_VIEW_HEATMAP:
80                        mErrorCalculator.calcErrorHeatmapRS(mSoftwareBitmap, mHardwareBitmap,
81                                mCompareBitmap);
82                        mCompareImageView.setImageBitmap(mCompareBitmap);
83                        break;
84                }
85                mCompareImageView.getDrawable().setFilterBitmap(false);
86                mCompareImageView.invalidate();
87            }
88
89            mLastCompareImageViewState = mCompareImageViewState;
90            mRedrewFlag = false;
91            mHandler.removeCallbacks(mRunnable);
92        }
93    };
94
95    private void redrawViews() {
96        mHardwareView.invalidate();
97        mSoftwareView.invalidate();
98    }
99
100    @Override
101    protected void onCreate(Bundle savedInstanceState) {
102        super.onCreate(savedInstanceState);
103        setContentView(R.layout.manual_layout);
104        onCreateCommon(mRunnable);
105
106        mSoftwareView = (MainView) findViewById(R.id.software_view);
107        mSoftwareView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
108        mSoftwareView.setBackgroundColor(Color.WHITE);
109        mSoftwareView.addDrawCallback(mDrawCallback);
110
111        mCompareImageView = (ImageView) findViewById(R.id.compare_image_view);
112
113        int width = getResources().getDimensionPixelSize(R.dimen.layer_width);
114        int height = getResources().getDimensionPixelSize(R.dimen.layer_height);
115        mCompareBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
116
117        mErrorTextView = (TextView) findViewById(R.id.current_error);
118        ((ImageButton) findViewById(R.id.next)).setOnClickListener(new OnClickListener() {
119            @Override
120            public void onClick(View v) {
121                DisplayModifier.step();
122                updateSpinners();
123                redrawViews();
124            }
125        });
126        ((ImageButton) findViewById(R.id.previous)).setOnClickListener(new OnClickListener() {
127            @Override
128            public void onClick(View v) {
129                DisplayModifier.stepBack();
130                updateSpinners();
131                redrawViews();
132            }
133        });
134        ((Button) findViewById(R.id.show_hardware_version))
135                .setOnClickListener(new OnClickListener() {
136                    @Override
137                    public void onClick(View v) {
138                        mCompareImageViewState = COMPARE_VIEW_HARDWARE;
139                        mHandler.post(mRunnable);
140                    }
141                });
142        ((Button) findViewById(R.id.show_software_version))
143                .setOnClickListener(new OnClickListener() {
144                    @Override
145                    public void onClick(View v) {
146                        mCompareImageViewState = COMPARE_VIEW_SOFTWARE;
147                        mHandler.post(mRunnable);
148                    }
149                });
150        ((Button) findViewById(R.id.show_error_heatmap)).setOnClickListener(new OnClickListener() {
151            @Override
152            public void onClick(View v) {
153                mCompareImageViewState = COMPARE_VIEW_HEATMAP;
154                mHandler.post(mRunnable);
155            }
156        });
157
158        buildSpinnerLayout();
159    }
160
161    private class DisplayModifierSpinner extends Spinner {
162        private final int mIndex;
163
164        public DisplayModifierSpinner(int index) {
165            super(ManualActivity.this);
166            mIndex = index;
167            setOnItemSelectedListener(new OnItemSelectedListener() {
168
169                @Override
170                public void onItemSelected(AdapterView<?> parentView, View selectedItem,
171                        int position, long id) {
172                    DisplayModifier.setIndex(mIndex, position);
173                    redrawViews();
174                }
175
176                @Override
177                public void onNothingSelected(AdapterView<?> parentView) {
178                }
179            });
180        }
181    }
182
183    private Spinner[] mSpinners;
184
185    private void buildSpinnerLayout() {
186        LinearLayout layout = (LinearLayout) findViewById(R.id.spinner_layout);
187        String[][] mapsStrings = DisplayModifier.getStrings();
188        mSpinners = new Spinner[mapsStrings.length];
189        int index = 0;
190        for (String[] spinnerValues : mapsStrings) {
191            mSpinners[index] = new DisplayModifierSpinner(index);
192            mSpinners[index].setAdapter(new ArrayAdapter<String>(this,
193                    android.R.layout.simple_spinner_dropdown_item, spinnerValues));
194            layout.addView(mSpinners[index]);
195            index++;
196        }
197        Log.d(LOG_TAG, "created " + index + " spinners");
198    }
199
200    private void updateSpinners() {
201        int[] indices = DisplayModifier.getIndices();
202        for (int i = 0; i < mSpinners.length; i++) {
203            mSpinners[i].setSelection(indices[i]);
204        }
205    }
206
207    @Override
208    protected boolean forceRecreateBitmaps() {
209        // continually recreate bitmaps to avoid modifying bitmaps currently being drawn
210        return true;
211    }
212}
213