Skip to content
This repository has been archived by the owner on Mar 2, 2018. It is now read-only.

Commit

Permalink
Merge pull request #44 from googlesamples/release-xiaotong
Browse files Browse the repository at this point in the history
Release xiaotong
  • Loading branch information
r4ravi2008 committed Oct 2, 2015
2 parents fc71046 + 6443873 commit 7f7d9b2
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@
import java.io.File;
import java.util.Arrays;

import com.projecttango.experiments.javaarealearning.SetADFNameDialog.SetNameCommunicator;

/**
* This class lets you manage ADFs between this class's Application Package folder and API private
* space. This show cases mainly three things: Import, Export, Delete an ADF file from API private
* space to any known and accessible file path.
*
*/
public class ADFUUIDListViewActivity extends Activity implements SetNameCommunicator {
public class ADFUUIDListViewActivity extends Activity implements SetADFNameDialog.CallbackListener {
private ADFDataSource mADFDataSource;
private ListView mUUIDListView, mAppSpaceUUIDListView;
ADFUUIDArrayAdapter mADFAdapter, mAppSpaceADFAdapter;
Expand Down Expand Up @@ -224,8 +222,11 @@ private void showSetNameDialog(String mCurrentUUID) {
setADFNameDialog.show(manager, "ADFNameDialog");
}

/**
* Implements SetADFNameDialog.CallbackListener.
*/
@Override
public void onSetName(String name, String uuid) {
public void onAdfNameOk(String name, String uuid) {
TangoAreaDescriptionMetaData metadata = new TangoAreaDescriptionMetaData();
metadata = mADFDataSource.getTango().loadAreaDescriptionMetaData(uuid);
byte[] adfNameBytes = metadata.get("name");
Expand All @@ -239,6 +240,15 @@ public void onSetName(String name, String uuid) {
mADFAdapter = new ADFUUIDArrayAdapter(this, mUUIDList, mUUIDNames);
mUUIDListView.setAdapter(mADFAdapter);
}


/**
* Implements SetADFNameDialog.CallbackListener.
*/
@Override
public void onAdfNameCancelled() {
// Nothing to do here.
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@
import java.text.DecimalFormat;
import java.util.ArrayList;

import com.projecttango.experiments.javaarealearning.SetADFNameDialog.SetNameCommunicator;

/**
* Main Activity class for the Area Learning API Sample. Handles the connection to the Tango service
* and propagation of Tango pose data to OpenGL and Layout views. OpenGL rendering logic is
* delegated to the {@link ALRenderer} class.
*/
public class AreaLearningActivity extends Activity implements View.OnClickListener,
SetNameCommunicator {
SetADFNameDialog.CallbackListener, SaveAdfTask.SaveAdfListener {

private static final String TAG = AreaLearningActivity.class.getSimpleName();
private static final int SECONDS_TO_MILLI = 1000;
Expand All @@ -79,7 +77,7 @@ public class AreaLearningActivity extends Activity implements View.OnClickListen
private TextView mAdf2DevicePoseDeltaTextView;
private TextView mAdf2StartPoseDeltaTextView;

private Button mSaveAdf;
private Button mSaveAdfButton;
private Button mFirstPersonButton;
private Button mThirdPersonButton;
private Button mTopDownButton;
Expand Down Expand Up @@ -109,6 +107,8 @@ public class AreaLearningActivity extends Activity implements View.OnClickListen
private TangoPoseData[] mPoses;
private static final int UPDATE_INTERVAL_MS = 100;
private static final DecimalFormat threeDec = new DecimalFormat("00.000");
// Long-running task to save the ADF.
private SaveAdfTask mSaveAdfTask;
public static Object sharedLock = new Object();

@Override
Expand Down Expand Up @@ -145,10 +145,9 @@ protected void onCreate(Bundle savedInstanceState) {
mApplicationVersionTextView = (TextView) findViewById(R.id.appversion);
mGLView = (GLSurfaceView) findViewById(R.id.gl_surface_view);

mSaveAdf = (Button) findViewById(R.id.saveAdf);
mSaveAdfButton = (Button) findViewById(R.id.saveAdf);
mUUIDTextView = (TextView) findViewById(R.id.uuid);

mSaveAdf.setVisibility(View.GONE);
// Set up button click listeners
mFirstPersonButton.setOnClickListener(this);
mThirdPersonButton.setOnClickListener(this);
Expand Down Expand Up @@ -187,8 +186,11 @@ private void setTangoConfig() {
// Set learning mode to config.
mConfig.putBoolean(TangoConfig.KEY_BOOLEAN_LEARNINGMODE, true);
// Set the ADF save button visible.
mSaveAdf.setVisibility(View.VISIBLE);
mSaveAdf.setOnClickListener(this);
mSaveAdfButton.setEnabled(false);
mSaveAdfButton.setOnClickListener(this);
}else {
// Hide to save ADF button if leanring mode is off.
mSaveAdfButton.setVisibility(View.GONE);
}
// Check for Load ADF/Constant Space relocalization mode
if (mIsConstantSpaceRelocalize) {
Expand Down Expand Up @@ -243,7 +245,17 @@ public void onTangoEvent(final TangoEvent event) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// Update the debug UI with information about this event.
mTangoEventTextView.setText(event.eventKey + ": " + event.eventValue);

// When saving an ADF, update the progress bar UI.
if (event.eventType == TangoEvent.EVENT_AREA_LEARNING &&
TangoEvent.KEY_AREA_DESCRIPTION_SAVE_PROGRESS.equals(event.eventKey)) {
int progressPercent = (int) (Double.parseDouble(event.eventValue) * 100);
if (mSaveAdfTask != null) {
mSaveAdfTask.publishProgress(progressPercent);
}
}
}
});
}
Expand Down Expand Up @@ -315,10 +327,8 @@ public void onPoseAvailable(TangoPoseData pose) {
mAdf2StartPreviousPoseTimeStamp = pose.timestamp;
if (pose.statusCode == TangoPoseData.POSE_VALID) {
mIsRelocalized = true;
// Set the color to green
} else {
mIsRelocalized = false;
// Set the color blue
}
}

Expand Down Expand Up @@ -363,35 +373,13 @@ private void showSetNameDialog() {
setADFNameDialog.show(manager, "ADFNameDialog");
}

@Override
public void onSetName(String name, String uuids) {

TangoAreaDescriptionMetaData metadata = new TangoAreaDescriptionMetaData();
try {
mCurrentUUID = mTango.saveAreaDescription();
metadata = mTango.loadAreaDescriptionMetaData(mCurrentUUID);
metadata.set(TangoAreaDescriptionMetaData.KEY_NAME, name.getBytes());
mTango.saveAreaDescriptionMetadata(mCurrentUUID, metadata);
} catch (TangoErrorException e) {
Toast.makeText(getApplicationContext(), getString(R.string.tango_error),
Toast.LENGTH_SHORT).show();
return;
} catch (TangoInvalidException e) {
Toast.makeText(getApplicationContext(), getString(R.string.tango_invalid),
Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(getApplicationContext(), getString(R.string.adf_save) + mCurrentUUID,
Toast.LENGTH_SHORT).show();
}

/**
* Updates the text view in UI screen with the Pose. Each pose is associated with Target and
* Base Frame. We need to check for that pair and update our views accordingly.
*
* @param pose
*/
private void updateTextViews() {
// Allow clicking of the save button only when Tango is localized to the current ADF.
mSaveAdfButton.setEnabled(mIsRelocalized);
if (mPoses[0] != null
&& mPoses[0].baseFrame == TangoPoseData.COORDINATE_FRAME_AREA_DESCRIPTION
&& mPoses[0].targetFrame == TangoPoseData.COORDINATE_FRAME_DEVICE) {
Expand Down Expand Up @@ -456,31 +444,31 @@ protected void onPause() {
try {
mTango.disconnect();
} catch (TangoErrorException e) {
Toast.makeText(getApplicationContext(), R.string.tango_error, Toast.LENGTH_SHORT)
.show();
Toast.makeText(getApplicationContext(), R.string.tango_error, Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onResume() {
super.onResume();
// Clear the relocalization state: we don't know where the device has been since our app was paused
mIsRelocalized = false;
try {
setUpTangoListeners();
} catch (TangoErrorException e) {
Toast.makeText(getApplicationContext(), R.string.tango_error, Toast.LENGTH_SHORT)
.show();
Toast.makeText(getApplicationContext(), R.string.tango_error, Toast.LENGTH_SHORT).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), R.string.no_permissions, Toast.LENGTH_SHORT)
.show();
Toast.makeText(getApplicationContext(), R.string.no_permissions, Toast.LENGTH_SHORT).show();
}
// Connect to the tango service (start receiving pose updates).
try {
mTango.connect(mConfig);
} catch (TangoOutOfDateException e) {
Toast.makeText(getApplicationContext(), R.string.tango_out_of_date_exception,
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), R.string.tango_out_of_date_exception, Toast.LENGTH_SHORT).show();
} catch (TangoErrorException e) {
Toast.makeText(getApplicationContext(), R.string.tango_error, Toast.LENGTH_SHORT)
.show();
Toast.makeText(getApplicationContext(), R.string.tango_error, Toast.LENGTH_SHORT).show();
} catch (TangoInvalidException e) {
Toast.makeText(getApplicationContext(), R.string.tango_invalid, Toast.LENGTH_SHORT).show();
}
}

Expand All @@ -503,7 +491,8 @@ public void onClick(View v) {
mRenderer.setThirdPersonView();
break;
case R.id.saveAdf:
saveAdf();
// Query the user for an ADF name and save if OK was clicked.
showSetADFNameDialog();
break;
default:
Log.w(TAG, "Unknown button click");
Expand Down Expand Up @@ -551,4 +540,70 @@ public void run() {
}
}).start();
}

/**
* Save the current Area Description File.
* Performs saving on a background thread and displays a progress dialog.
*/
private void saveAdf(String adfName) {
mSaveAdfTask = new SaveAdfTask(this, this, mTango, adfName);
mSaveAdfTask.execute();
}

/**
* Handles failed save from mSaveAdfTask.
*/
@Override
public void onSaveAdfFailed(String adfName) {
String toastMessage = String.format(
getResources().getString(R.string.save_adf_failed_toast_format),
adfName);
Toast.makeText(this, toastMessage, Toast.LENGTH_LONG).show();
mSaveAdfTask = null;
}

/**
* Handles successful save from mSaveAdfTask.
*/
@Override
public void onSaveAdfSuccess(String adfName, String adfUuid) {
String toastMessage = String.format(
getResources().getString(R.string.save_adf_success_toast_format),
adfName, adfUuid);
Toast.makeText(this, toastMessage, Toast.LENGTH_LONG).show();
mSaveAdfTask = null;
finish();
}

/**
* Shows a dialog for setting the ADF name.
*/
private void showSetADFNameDialog() {
Bundle bundle = new Bundle();
bundle.putString("name",getResources().getString(R.string.default_adf_name) );
bundle.putString("id", ""); // UUID is generated after the ADF is saved.

FragmentManager manager = getFragmentManager();
SetADFNameDialog setADFNameDialog = new SetADFNameDialog();
setADFNameDialog.setArguments(bundle);
setADFNameDialog.show(manager, "ADFNameDialog");
}


/**
* Implements SetADFNameDialog.CallbackListener.
*/
@Override
public void onAdfNameOk(String name, String uuid) {
saveAdf(name);
}


/**
* Implements SetADFNameDialog.CallbackListener.
*/
@Override
public void onAdfNameCancelled() {
// Continue running.s
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.experiments.javaarealearning;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ProgressBar;

/**
* Displays progress bar and text information while saving an adf.
*/
public class SaveAdfDialog extends AlertDialog {
private static final String TAG = SaveAdfDialog.class.getSimpleName();

private ProgressBar mProgressBar;

public SaveAdfDialog(Context context) {
super(context);
}

public void setProgress(int progress) {
if (mProgressBar != null) {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(progress);
}
}

public void setProgressBarVisibility(int visibility) {
if (mProgressBar != null) {
mProgressBar.setVisibility(visibility);
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.save_adf_dialog);

setCancelable(false);

mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
if (mProgressBar == null) {
Log.e(TAG, "Unable to find view progress_bar.");
}
}
}
Loading

0 comments on commit 7f7d9b2

Please sign in to comment.