/*
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * 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.projecttango.quickstartjava;

import java.util.ArrayList;

import com.google.atap.tangoservice.Tango;
import com.google.atap.tangoservice.Tango.OnTangoUpdateListener;
import com.google.atap.tangoservice.TangoConfig;
import com.google.atap.tangoservice.TangoCoordinateFramePair;
import com.google.atap.tangoservice.TangoEvent;
import com.google.atap.tangoservice.TangoPoseData;
import com.google.atap.tangoservice.TangoXyzIjData;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

/**
 * Main Activity for the Tango Java Quickstart.  Demonstrates establishing a connection to the
 * {@link Tango} service and printing the {@link TangoPose} data to the LogCat.  Also demonstrates
 * Tango lifecycle management through {@link TangoConfig}.
 */
public class MainActivity extends Activity {

	private static final String TAG = MainActivity.class.getSimpleName();
	private static final String sTranslationFormat = "Translation: %f, %f, %f";
	private static final String sRotationFormat = "Rotation: %f, %f, %f, %f";
	
	private TextView mTranslationTextView;
	private TextView mRotationTextView;
	
	private Tango mTango;
	private TangoConfig mConfig;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mTranslationTextView = (TextView) findViewById(R.id.translation_text_view);
		mRotationTextView = (TextView) findViewById(R.id.rotation_text_view);
	
		// Instantiate Tango client
		mTango = new Tango(this);
		
		// Set up Tango configuration for motion tracking
		// 	If you want to use other APIs, add more appropriate to the config like:
		// 	mConfig.putBoolean(TangoConfig.KEY_BOOLEAN_DEPTH, true)
		mConfig = new TangoConfig();
		mTango.getConfig(TangoConfig.CONFIG_TYPE_CURRENT, mConfig);
		mConfig.putBoolean(TangoConfig.KEY_BOOLEAN_MOTIONTRACKING, true);
		
	    // Select coordinate frame pairs
	    ArrayList<TangoCoordinateFramePair> framePairs = new ArrayList<TangoCoordinateFramePair>();
	    framePairs.add(new TangoCoordinateFramePair(TangoPoseData.COORDINATE_FRAME_START_OF_SERVICE,
	                TangoPoseData.COORDINATE_FRAME_DEVICE));
		
		// Add a listener for Tango pose data
		int statusCode = mTango.connectListener(framePairs, new OnTangoUpdateListener() {

			@SuppressLint("DefaultLocale") 
			@Override
			public void onPoseAvailable(TangoPoseData pose) {
				// Format Translation and Rotation data
				final String translationMsg = String.format(sTranslationFormat, pose.translation[0],
						pose.translation[1], pose.translation[2]);
				final String rotationMsg = String.format(sRotationFormat, pose.rotation[0],
						pose.rotation[1], pose.rotation[2], pose.rotation[3]);
				
				// Output to LogCat
				String logMsg = translationMsg + " | " + rotationMsg;
				Log.i(TAG, logMsg);
				
				// Display data in TextViews.  This must be done inside a runOnUiThread call because
				// 	it affects the UI, which will cause an error if performed from the Tango
				//	service thread
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mTranslationTextView.setText(translationMsg);
						mRotationTextView.setText(rotationMsg);
					}	
				});
			}

			@Override
			public void onXyzIjAvailable(TangoXyzIjData arg0) {
				// Ignoring XyzIj data
			}

			@Override
			public void onTangoEvent(TangoEvent arg0) {
				// Ignoring TangoEvents
			}
			
		});
		
		Log.i(TAG, "Status Code: " + statusCode);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		// Lock the Tango configuration and reconnect to the service each time the app
		//	is brought to the foreground.
		mTango.lockConfig(mConfig);
		mTango.connect();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		// When the app is pushed to the background, unlock the Tango configuration and disconnect
		//	from the service so that other apps will behave properly.
		mTango.unlockConfig();
		mTango.disconnect();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		// When the app is pushed to the background, unlock the Tango configuration and disconnect
		//	from the service so that other apps will behave properly.
		mTango.unlockConfig();
		mTango.disconnect();
	}
	
}

@@ -2454,24 +2454,34 @@
 Config();%0A%09%09
+mConfig = 
 mTango.getCo
@@ -2520,17 +2520,8 @@
 RENT
-, mConfig
 );%0A%09
@@ -2930,25 +2930,8 @@
 a%0A%09%09
-int statusCode = 
 mTan
@@ -4127,55 +4127,8 @@
 %7D);%0A
-%09%09%0A%09%09Log.i(TAG, %22Status Code: %22 + statusCode);%0A
 %09%7D%0A%09
@@ -4197,42 +4197,9 @@
 %09// 
-Lock the Tango configuration and r
+R
 econ
@@ -4273,39 +4273,38 @@
 ound
-.%0A%09%09mTango.lockConfig(mConfig);
+ with the given configuration.
 %0A%09%09m
@@ -4309,32 +4309,39 @@
 %09mTango.connect(
+mConfig
 );%0A%09%7D%0A%09%0A%09@Overri
@@ -4439,44 +4439,8 @@
 ound
-, unlock the Tango configuration and
  dis
@@ -4514,33 +4514,8 @@
 ly.%0A
-%09%09mTango.unlockConfig();%0A
 %09%09mT
@@ -4604,212 +4604,8 @@
 ();%0A
-%09%09// When the app is pushed to the background, unlock the Tango configuration and disconnect%0A%09%09//%09from the service so that other apps will behave properly.%0A%09%09mTango.unlockConfig();%0A%09%09mTango.disconnect();%0A
 %09%7D%0A%09

Modify QUick Start java to match the new samples.