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 /** 188 * Get shared camera controls 189 */ getCameraOps()190 public CameraOps2 getCameraOps() { 191 return mCameraOps; 192 } 193 194 /** 195 * Get shared pane tracker 196 */ getPaneTracker()197 public PaneTracker getPaneTracker() { 198 return mPaneTracker; 199 } 200 201 /** 202 * Select which layout configuration to load, and then load it TODO: 203 * Implement selecting something besides the default 204 */ selectConfig()205 private void selectConfig() { 206 XmlPullParser config = getResources().getXml(R.xml.still_camera); 207 readConfig(config); 208 } 209 210 /** 211 * Parse a selected XML configuration 212 * 213 * @param configParser 214 * the XML parser to read from 215 */ readConfig(XmlPullParser configParser)216 private void readConfig(XmlPullParser configParser) { 217 boolean inConfig = false; 218 boolean gotConfig = false; 219 try { 220 while (configParser.getEventType() != XmlPullParser.END_DOCUMENT) { 221 int eventType = configParser.next(); 222 switch (eventType) { 223 case XmlPullParser.START_TAG: 224 if (!inConfig) { 225 configParser.require(XmlPullParser.START_TAG, XmlPullParser.NO_NAMESPACE, 226 "testingcamera2_config"); 227 inConfig = true; 228 } else { 229 for (PaneLayout<?> paneLayout : mPaneLayouts) { 230 if (configParser.getName().equals(paneLayout.getXmlName())) { 231 paneLayout.readConfig(this, configParser); 232 break; 233 } 234 } 235 } 236 break; 237 case XmlPullParser.END_TAG: 238 configParser.require(XmlPullParser.END_TAG, XmlPullParser.NO_NAMESPACE, 239 "testingcamera2_config"); 240 inConfig = false; 241 gotConfig = true; 242 break; 243 } 244 } 245 } catch (XmlPullParserException e) { 246 TLog.e("Unable to parse new config.", e); 247 } catch (IOException e) { 248 TLog.e("Unable to read new config.", e); 249 } 250 251 if (gotConfig) { 252 for (PaneLayout<?> paneLayout : mPaneLayouts) { 253 paneLayout.activateConfig(); 254 } 255 } else { 256 for (PaneLayout<?> paneLayout : mPaneLayouts) { 257 paneLayout.clearConfig(); 258 } 259 } 260 } 261 262 @Override showCameraInfo(String cameraId)263 public void showCameraInfo(String cameraId) { 264 final String infoTag = "camera_info_dialog"; 265 266 CameraInfoDialogFragment infoDialog = new CameraInfoDialogFragment(); 267 infoDialog.setCameraId(cameraId); 268 infoDialog.show(getFragmentManager(), infoTag); 269 } 270 } 271