1 /*
2  * Copyright (C) 2014 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 
17 package com.android.testingcamera2;
18 
19 import android.app.Activity;
20 import android.content.res.Configuration;
21 import android.os.Bundle;
22 import android.util.TypedValue;
23 import android.view.Menu;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.view.ViewGroup.LayoutParams;
27 import android.view.WindowManager;
28 import android.widget.LinearLayout;
29 import android.widget.ScrollView;
30 
31 import org.xmlpull.v1.XmlPullParser;
32 import org.xmlpull.v1.XmlPullParserException;
33 
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 public class TestingCamera21 extends Activity implements CameraControlPane.InfoDisplayer {
39 
40     private LinearLayout mMainList;
41     private ScrollView mOutputViewScroller;
42     private ScrollView mControlScroller;
43     private View mMainDivider;
44 
45     private PaneLayout<CameraControlPane> mCameraControlList;
46     private PaneLayout<TargetControlPane> mTargetControlList;
47     private PaneLayout<RequestControlPane> mRequestControlList;
48     private PaneLayout<BurstControlPane> mBurstControlList;
49     private PaneLayout<UtilControlPane> mUtilControlList;
50     private List<PaneLayout<?>> mPaneLayouts = new ArrayList<PaneLayout<?>>();
51 
52     private CameraOps2 mCameraOps;
53     private PaneTracker mPaneTracker;
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         setContentView(R.layout.main3);
59 
60         mCameraOps = new CameraOps2(this);
61         mPaneTracker = new PaneTracker();
62 
63         TLog.Logger logger;
64         logger = setUpUIAndLog();
65         TLog.setLogger(logger);
66     }
67 
setUpUIAndLog()68     private TLog.Logger setUpUIAndLog() {
69 
70         mMainList = (LinearLayout) findViewById(R.id.main_list);
71         mOutputViewScroller = (ScrollView) findViewById(R.id.output_view_scroller);
72         mControlScroller = (ScrollView) findViewById(R.id.control_scroller);
73         mMainDivider = (View) findViewById(R.id.main_section_divider);
74 
75         onConfigurationChanged(getResources().getConfiguration());
76 
77         LogPane logPane = (LogPane) findViewById(R.id.log_pane);
78 
79         mTargetControlList = (PaneLayout<TargetControlPane>) findViewById(R.id.target_list);
80         mPaneLayouts.add(mTargetControlList);
81 
82         mCameraControlList = (PaneLayout<CameraControlPane>) findViewById(R.id.camera_list);
83         mPaneLayouts.add(mCameraControlList);
84 
85         mRequestControlList = (PaneLayout<RequestControlPane>) findViewById(R.id.request_list);
86         mPaneLayouts.add(mRequestControlList);
87 
88         mBurstControlList = (PaneLayout<BurstControlPane>) findViewById(R.id.burst_list);
89         mPaneLayouts.add(mBurstControlList);
90 
91         mUtilControlList = (PaneLayout<UtilControlPane>) findViewById(R.id.util_list);
92         mPaneLayouts.add(mUtilControlList);
93 
94         return logPane;
95     }
96 
97     @Override
onConfigurationChanged(Configuration newConfig)98     public void onConfigurationChanged(Configuration newConfig) {
99         super.onConfigurationChanged(newConfig);
100         /**
101          * In landscape, place target view list left of the control pane list In
102          * portrait, place target view list above the control pane list.
103          */
104         int width = 0;
105         int height = 0;
106         if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
107             mMainList.setOrientation(LinearLayout.HORIZONTAL);
108             height = LayoutParams.MATCH_PARENT;
109         } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
110             mMainList.setOrientation(LinearLayout.VERTICAL);
111             width = LayoutParams.MATCH_PARENT;
112         }
113 
114         int thickness =
115                 getResources().getDimensionPixelSize(R.dimen.main_section_divider_thickness);
116         LinearLayout.LayoutParams mainDividerLayout =
117                 new LinearLayout.LayoutParams(
118                         (width == 0) ? thickness : width,
119                         (height == 0) ? thickness : height);
120         mMainDivider.setLayoutParams(mainDividerLayout);
121 
122         TypedValue outputViewListWeight = new TypedValue();
123         getResources().getValue(R.dimen.output_view_list_weight, outputViewListWeight, false);
124         LinearLayout.LayoutParams outputScrollerLayout =
125                 new LinearLayout.LayoutParams(width, height, outputViewListWeight.getFloat());
126         mOutputViewScroller.setLayoutParams(outputScrollerLayout);
127 
128         TypedValue controlListWeight = new TypedValue();
129         getResources().getValue(R.dimen.control_list_weight, controlListWeight, false);
130         LinearLayout.LayoutParams controlScrollerLayout =
131                 new LinearLayout.LayoutParams(width, height, controlListWeight.getFloat());
132         mControlScroller.setLayoutParams(controlScrollerLayout);
133 
134         WindowManager windowManager =  (WindowManager) getSystemService(WINDOW_SERVICE);
135         int rot = windowManager.getDefaultDisplay().getRotation();
136 
137         mPaneTracker.notifyOrientationChange(windowManager.getDefaultDisplay().getRotation());
138 
139     }
140 
141     @Override
onCreateOptionsMenu(Menu menu)142     public boolean onCreateOptionsMenu(Menu menu) {
143         // Inflate the menu to the action bar
144         getMenuInflater().inflate(R.menu.testing_camera21, menu);
145         return true;
146     }
147 
148     @Override
onOptionsItemSelected(MenuItem item)149     public boolean onOptionsItemSelected(MenuItem item) {
150         // Handle action bar item clicks here.
151         int id = item.getItemId();
152         boolean done = true;
153 
154         switch (id) {
155         case R.id.action_add_camera:
156             mCameraControlList.addPane(this);
157             break;
158         case R.id.action_add_target:
159             mTargetControlList.addPane(this);
160             break;
161         case R.id.action_add_request:
162             mRequestControlList.addPane(this);
163             break;
164         case R.id.action_add_burst:
165             mBurstControlList.addPane(this);
166             break;
167         case R.id.action_add_utility:
168             mUtilControlList.addPane(this);
169             break;
170         case R.id.action_load_config:
171             selectConfig();
172             break;
173         case R.id.action_save_config:
174             TLog.e("Saving a configuration is not yet implemented");
175             break;
176         default:
177             done = false;
178             break;
179         }
180 
181         if (done)
182             return true;
183 
184         return super.onOptionsItemSelected(item);
185     }
186 
187     @Override
onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)188     public void onRequestPermissionsResult(int requestCode, String[] permissions,
189             int[] grantResults) {
190         mCameraOps.onRequestPermissionsResult(requestCode, permissions, grantResults);
191     }
192 
193     /**
194      * Get shared camera controls
195      */
getCameraOps()196     public CameraOps2 getCameraOps() {
197         return mCameraOps;
198     }
199 
200     /**
201      * Get shared pane tracker
202      */
getPaneTracker()203     public PaneTracker getPaneTracker() {
204         return mPaneTracker;
205     }
206 
207     /**
208      * Select which layout configuration to load, and then load it TODO:
209      * Implement selecting something besides the default
210      */
selectConfig()211     private void selectConfig() {
212         XmlPullParser config = getResources().getXml(R.xml.still_camera);
213         readConfig(config);
214     }
215 
216     /**
217      * Parse a selected XML configuration
218      *
219      * @param configParser
220      *            the XML parser to read from
221      */
readConfig(XmlPullParser configParser)222     private void readConfig(XmlPullParser configParser) {
223         boolean inConfig = false;
224         boolean gotConfig = false;
225         try {
226             while (configParser.getEventType() != XmlPullParser.END_DOCUMENT) {
227                 int eventType = configParser.next();
228                 switch (eventType) {
229                 case XmlPullParser.START_TAG:
230                     if (!inConfig) {
231                         configParser.require(XmlPullParser.START_TAG, XmlPullParser.NO_NAMESPACE,
232                                 "testingcamera2_config");
233                         inConfig = true;
234                     } else {
235                         for (PaneLayout<?> paneLayout : mPaneLayouts) {
236                             if (configParser.getName().equals(paneLayout.getXmlName())) {
237                                 paneLayout.readConfig(this, configParser);
238                                 break;
239                             }
240                         }
241                     }
242                     break;
243                 case XmlPullParser.END_TAG:
244                     configParser.require(XmlPullParser.END_TAG, XmlPullParser.NO_NAMESPACE,
245                             "testingcamera2_config");
246                     inConfig = false;
247                     gotConfig = true;
248                     break;
249                 }
250             }
251         } catch (XmlPullParserException e) {
252             TLog.e("Unable to parse new config.", e);
253         } catch (IOException e) {
254             TLog.e("Unable to read new config.", e);
255         }
256 
257         if (gotConfig) {
258             for (PaneLayout<?> paneLayout : mPaneLayouts) {
259                 paneLayout.activateConfig();
260             }
261         } else {
262             for (PaneLayout<?> paneLayout : mPaneLayouts) {
263                 paneLayout.clearConfig();
264             }
265         }
266     }
267 
268     @Override
showCameraInfo(String cameraId)269     public void showCameraInfo(String cameraId) {
270         final String infoTag = "camera_info_dialog";
271 
272         CameraInfoDialogFragment infoDialog = new CameraInfoDialogFragment();
273         infoDialog.setCameraId(cameraId);
274         infoDialog.show(getFragmentManager(), infoTag);
275     }
276 }
277