aboutsummaryrefslogtreecommitdiff
path: root/apps/TestingCamera2/src/com/android/testingcamera2/TestingCamera21.java
blob: c15a78c4202809754c30f2960e464be27a39716e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.testingcamera2;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestingCamera21 extends Activity implements CameraControlPane.InfoDisplayer {

    private LinearLayout mMainList;
    private ScrollView mOutputViewScroller;
    private ScrollView mControlScroller;
    private View mMainDivider;

    private PaneLayout<CameraControlPane> mCameraControlList;
    private PaneLayout<TargetControlPane> mTargetControlList;
    private PaneLayout<RequestControlPane> mRequestControlList;
    private PaneLayout<BurstControlPane> mBurstControlList;
    private PaneLayout<UtilControlPane> mUtilControlList;
    private List<PaneLayout<?>> mPaneLayouts = new ArrayList<PaneLayout<?>>();

    private CameraOps2 mCameraOps;
    private PaneTracker mPaneTracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main3);

        mCameraOps = new CameraOps2(this);
        mPaneTracker = new PaneTracker();

        TLog.Logger logger;
        logger = setUpUIAndLog();
        TLog.setLogger(logger);
    }

    private TLog.Logger setUpUIAndLog() {

        mMainList = (LinearLayout) findViewById(R.id.main_list);
        mOutputViewScroller = (ScrollView) findViewById(R.id.output_view_scroller);
        mControlScroller = (ScrollView) findViewById(R.id.control_scroller);
        mMainDivider = (View) findViewById(R.id.main_section_divider);

        onConfigurationChanged(getResources().getConfiguration());

        LogPane logPane = (LogPane) findViewById(R.id.log_pane);

        mTargetControlList = (PaneLayout<TargetControlPane>) findViewById(R.id.target_list);
        mPaneLayouts.add(mTargetControlList);

        mCameraControlList = (PaneLayout<CameraControlPane>) findViewById(R.id.camera_list);
        mPaneLayouts.add(mCameraControlList);

        mRequestControlList = (PaneLayout<RequestControlPane>) findViewById(R.id.request_list);
        mPaneLayouts.add(mRequestControlList);

        mBurstControlList = (PaneLayout<BurstControlPane>) findViewById(R.id.burst_list);
        mPaneLayouts.add(mBurstControlList);

        mUtilControlList = (PaneLayout<UtilControlPane>) findViewById(R.id.util_list);
        mPaneLayouts.add(mUtilControlList);

        return logPane;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        /**
         * In landscape, place target view list left of the control pane list In
         * portrait, place target view list above the control pane list.
         */
        int width = 0;
        int height = 0;
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            mMainList.setOrientation(LinearLayout.HORIZONTAL);
            height = LayoutParams.MATCH_PARENT;
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            mMainList.setOrientation(LinearLayout.VERTICAL);
            width = LayoutParams.MATCH_PARENT;
        }

        int thickness =
                getResources().getDimensionPixelSize(R.dimen.main_section_divider_thickness);
        LinearLayout.LayoutParams mainDividerLayout =
                new LinearLayout.LayoutParams(
                        (width == 0) ? thickness : width,
                        (height == 0) ? thickness : height);
        mMainDivider.setLayoutParams(mainDividerLayout);

        TypedValue outputViewListWeight = new TypedValue();
        getResources().getValue(R.dimen.output_view_list_weight, outputViewListWeight, false);
        LinearLayout.LayoutParams outputScrollerLayout =
                new LinearLayout.LayoutParams(width, height, outputViewListWeight.getFloat());
        mOutputViewScroller.setLayoutParams(outputScrollerLayout);

        TypedValue controlListWeight = new TypedValue();
        getResources().getValue(R.dimen.control_list_weight, controlListWeight, false);
        LinearLayout.LayoutParams controlScrollerLayout =
                new LinearLayout.LayoutParams(width, height, controlListWeight.getFloat());
        mControlScroller.setLayoutParams(controlScrollerLayout);

        WindowManager windowManager =  (WindowManager) getSystemService(WINDOW_SERVICE);
        int rot = windowManager.getDefaultDisplay().getRotation();

        mPaneTracker.notifyOrientationChange(windowManager.getDefaultDisplay().getRotation());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu to the action bar
        getMenuInflater().inflate(R.menu.testing_camera21, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here.
        int id = item.getItemId();
        boolean done = true;

        switch (id) {
        case R.id.action_add_camera:
            mCameraControlList.addPane(this);
            break;
        case R.id.action_add_target:
            mTargetControlList.addPane(this);
            break;
        case R.id.action_add_request:
            mRequestControlList.addPane(this);
            break;
        case R.id.action_add_burst:
            mBurstControlList.addPane(this);
            break;
        case R.id.action_add_utility:
            mUtilControlList.addPane(this);
            break;
        case R.id.action_load_config:
            selectConfig();
            break;
        case R.id.action_save_config:
            TLog.e("Saving a configuration is not yet implemented");
            break;
        default:
            done = false;
            break;
        }

        if (done)
            return true;

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
            int[] grantResults) {
        mCameraOps.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    /**
     * Get shared camera controls
     */
    public CameraOps2 getCameraOps() {
        return mCameraOps;
    }

    /**
     * Get shared pane tracker
     */
    public PaneTracker getPaneTracker() {
        return mPaneTracker;
    }

    /**
     * Select which layout configuration to load, and then load it TODO:
     * Implement selecting something besides the default
     */
    private void selectConfig() {
        XmlPullParser config = getResources().getXml(R.xml.still_camera);
        readConfig(config);
    }

    /**
     * Parse a selected XML configuration
     *
     * @param configParser
     *            the XML parser to read from
     */
    private void readConfig(XmlPullParser configParser) {
        boolean inConfig = false;
        boolean gotConfig = false;
        try {
            while (configParser.getEventType() != XmlPullParser.END_DOCUMENT) {
                int eventType = configParser.next();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (!inConfig) {
                        configParser.require(XmlPullParser.START_TAG, XmlPullParser.NO_NAMESPACE,
                                "testingcamera2_config");
                        inConfig = true;
                    } else {
                        for (PaneLayout<?> paneLayout : mPaneLayouts) {
                            if (configParser.getName().equals(paneLayout.getXmlName())) {
                                paneLayout.readConfig(this, configParser);
                                break;
                            }
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    configParser.require(XmlPullParser.END_TAG, XmlPullParser.NO_NAMESPACE,
                            "testingcamera2_config");
                    inConfig = false;
                    gotConfig = true;
                    break;
                }
            }
        } catch (XmlPullParserException e) {
            TLog.e("Unable to parse new config.", e);
        } catch (IOException e) {
            TLog.e("Unable to read new config.", e);
        }

        if (gotConfig) {
            for (PaneLayout<?> paneLayout : mPaneLayouts) {
                paneLayout.activateConfig();
            }
        } else {
            for (PaneLayout<?> paneLayout : mPaneLayouts) {
                paneLayout.clearConfig();
            }
        }
    }

    @Override
    public void showCameraInfo(String cameraId) {
        final String infoTag = "camera_info_dialog";

        CameraInfoDialogFragment infoDialog = new CameraInfoDialogFragment();
        infoDialog.setCameraId(cameraId);
        infoDialog.show(getFragmentManager(), infoTag);
    }
}