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 #51 from googlesamples/release-xiaotong
Browse files Browse the repository at this point in the history
release-xiaotong
  • Loading branch information
jguomoto committed Oct 2, 2015
2 parents e8e96ff + 312afd5 commit f9235ba
Show file tree
Hide file tree
Showing 22 changed files with 605 additions and 135 deletions.
6 changes: 3 additions & 3 deletions area-description-jni-example/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
<activity
android:label="@string/menu_name"
android:name="com.projecttango.experiments.nativearealearning.StartActivity"
android:screenOrientation="landscape">
android:screenOrientation="nosensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.projecttango.experiments.nativearealearning.AreaDescriptionActivity"
android:screenOrientation="landscape">
android:screenOrientation="nosensor">
</activity>
<activity
android:name="com.projecttango.experiments.nativearealearning.ADFUUIDListViewActivity"
android:screenOrientation="landscape">
android:screenOrientation="nosensor">
</activity>
</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
import android.widget.TextView;
import android.widget.Toast;

// Data container for store ADF's UUID and name, these date is used in the
// list view.
/**
* Contains an ADF Name and its UUID.
*/
class AdfData {
public String mUuid = new String();
public String mName = new String();
Expand All @@ -53,8 +54,9 @@ public AdfData(String uuid, String name) {
}
}

// This is an adapter class which maps the ListView with a Data Source(Array of
// strings).
/**
* Maps AdfData to Strings for display in the ListView.
*/
class AdfUuidArrayAdapter extends ArrayAdapter<String> {
Context mContext;
private String[] mUuidArray, mNameArray;
Expand Down Expand Up @@ -99,12 +101,15 @@ public View getView(int position, View convertView, ViewGroup parent) {
}
}

// This class lets you manage ADFs between this class's Application Package
// folder and API private space. This showcases three things:
// Import, Export, Delete an ADF file from API private space to any known and
// accessible file path.
/**
* Creates a ListVIew to manage ADFs owned by the Tango Service.
* Showcases:
* - Importing an ADF to the Tango Service from this class's Application Package folder.
* - Exporting an ADF from the Tango Service to this class's Application Package folder.
* - Deleting an ADF owned by the Tango Service.
*/
public class ADFUUIDListViewActivity extends Activity implements
SetADFNameDialog.SetNameAndUUIDCommunicator {
SetADFNameDialog.CallbackListener {
private static final String INTENT_CLASS_PACKAGE = "com.projecttango.tango";
private static final String INTENT_REQUEST_PERMISSION_CLASSNAME =
"com.google.atap.tango.RequestPermissionActivity";
Expand Down Expand Up @@ -227,13 +232,25 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
updateList();
}

/**
* Implementation of callback listener interface in SetADFNameDialog.
*/
@Override
public void setAdfNameAndUUID(String name, String uuid) {
public void onAdfNameOk(String name, String uuid) {
TangoJNINative.setAdfMetadataValue(uuid, "name", name);
updateList();
}

// Import an ADF from app space to Tango space.
/**
* Implementation of callback listener interface in SetADFNameDialog.
*/
@Override
public void onAdfNameCancelled() {
}

/**
* Import an ADF from app space to Tango space.
*/
private void importAdf(String uuid) {
String filepath = mAppSpaceADFFolder + File.separator + uuid;
Intent importIntent = new Intent();
Expand All @@ -242,7 +259,9 @@ private void importAdf(String uuid) {
startActivityForResult(importIntent, TANGO_INTENT_ACTIVITY_CODE);
}

// Export an ADF from Tango space to app space.
/**
* Export an ADF from Tango space to app space.
*/
private void exportAdf(String uuid) {
Intent exportIntent = new Intent();
exportIntent.setClassName(INTENT_CLASS_PACKAGE, INTENT_IMPORT_EXPORT_CLASSNAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
Expand All @@ -33,10 +34,13 @@
import android.widget.Toast;
import android.widget.ToggleButton;

import java.lang.Override;

// The AreaDescriptionActivity of the application which shows debug information
// and a glSurfaceView that renders graphic content.
public class AreaDescriptionActivity extends Activity implements
View.OnClickListener, SetADFNameDialog.SetNameAndUUIDCommunicator {
View.OnClickListener, SetADFNameDialog.CallbackListener, SaveAdfTask.SaveAdfListener {

// The user has not given permission to use Motion Tracking functionality.
private static final int TANGO_NO_MOTION_TRACKING_PERMISSION = -3;
// The input argument is invalid.
Expand All @@ -62,6 +66,9 @@ public class AreaDescriptionActivity extends Activity implements
// service for pose and event information.
private static final int kUpdateIntervalMs = 100;

// The flag to check if the surface is created.
public boolean mIsSurfaceCreated = false;

// Debug information text.
// Current frame's pose information.
private TextView mStartServiceTDevicePoseData;
Expand All @@ -81,10 +88,10 @@ public class AreaDescriptionActivity extends Activity implements
private Renderer mRenderer;
private GLSurfaceView mGLView;

// Flag that controls wether user wants to run area learning mode.
// Flag that controls whether user wants to run area learning mode.
private boolean mIsAreaLearningEnabled = false;

// Flag that controls wether user wants to load the latest ADF file.
// Flag that controls whether user wants to load the latest ADF file.
private boolean mIsLoadingADF = false;

// A flag to check if the Tango Service is connected. This flag avoids the
Expand All @@ -96,6 +103,9 @@ public class AreaDescriptionActivity extends Activity implements
// Screen size for normalizing the touch input for orbiting the render camera.
private Point mScreenSize = new Point();

// Long-running task to save an ADF.
private SaveAdfTask mSaveAdfTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -107,7 +117,7 @@ protected void onCreate(Bundle savedInstanceState) {
// Initialize Tango Service, this function starts the communication
// between the application and Tango Service.
// The activity object is used for checking if the API version is outdated.
TangoJNINative.initialize((Activity)this);
TangoJNINative.initialize(this);

// UI thread handles the task of updating all debug text.
startUIThread();
Expand Down Expand Up @@ -139,27 +149,34 @@ protected void onResume() {
// Connect the onPoseAvailable callback.
TangoJNINative.connectCallbacks();

// Connect to Tango Service.
// This function will start the Tango Service pipeline, in this case,
// it will start Motion Tracking.
TangoJNINative.connect();
// Connect to Tango Service (returns true on success).
// Starts Motion Tracking and Area Learning.
if (TangoJNINative.connect()) {
// Take the TangoCore version number from Tango Service.
mVersion.setText(TangoJNINative.getVersionNumber());

// Take the TangoCore version number from Tango Service.
mVersion.setText(TangoJNINative.getVersionNumber());
// Display loaded ADF's UUID.
mAdfUuidTextView.setText(TangoJNINative.getLoadedADFUUIDString());

// Display loaded ADF's UUID.
mAdfUuidTextView.setText(TangoJNINative.getLoadedADFUUIDString());
// Set the connected service flag to true.
mIsConnectedService = true;

// Set the connected service flag to true.
mIsConnectedService = true;
} else {
// End the activity and let the user know something went wrong.
Toast.makeText(this, R.string.tango_cant_initialize, Toast.LENGTH_LONG).show();
finish();
}
}
}

@Override
protected void onPause() {
super.onPause();
mGLView.onPause();
TangoJNINative.freeContent();
if (mIsSurfaceCreated) {
TangoJNINative.freeContent();
mIsSurfaceCreated = false;
}

// If the service is connected, we disconnect it here.
if (mIsConnectedService) {
Expand Down Expand Up @@ -193,12 +210,8 @@ public void onClick(View v) {
TangoJNINative.setCamera(1);
break;
case R.id.save_adf_button:
String adfName = TangoJNINative.saveAdf();
if (!adfName.isEmpty()) {
showSetNameDialog(adfName);
} else {
Toast.makeText(this, "Cannot Save ADF", Toast.LENGTH_LONG).show();
}
// Ask the user for an ADF name, then save if OK was clicked.
showSetADFNameDialog();
break;
default:
Log.w(TAG, "Unknown button click");
Expand Down Expand Up @@ -231,7 +244,7 @@ public boolean onTouchEvent(MotionEvent event) {
float normalizedX1 = event.getX(1) / mScreenSize.x;
float normalizedY1 = event.getY(1) / mScreenSize.y;
TangoJNINative.onTouchEvent(2, event.getActionMasked(),
normalizedX0, normalizedY0, normalizedX1, normalizedY1);
normalizedX0, normalizedY0, normalizedX1, normalizedY1);
}
}
return true;
Expand All @@ -253,11 +266,66 @@ protected void onActivityResult (int requestCode, int resultCode, Intent data) {
}
}

// This callback is called when save button is clicked from the
// SetADFNameDialog fragment.
/**
* Implementation of callback listener interface in SetADFNameDialog.
*/
@Override
public void setAdfNameAndUUID(String name, String uuid) {
TangoJNINative.setAdfMetadataValue(uuid, "name", name);
public void onAdfNameOk(String adfName, String adfUuid) {
saveAdf(adfName);
}

/**
* Implementation of callback listener interface in SetADFNameDialog.
*/
@Override
public void onAdfNameCancelled() {
// Continue running.
}

/**
* 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, 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();
}

/**
* Updates the save progress dialog (called from area_learning_app.cc).
*/
public void updateSavingAdfProgress(int progress) {
// Note: this method is not called from the UI thread. We read mSaveAdfTask into
// a local variable because the UI thread may null-out the member variable at any time.
SaveAdfTask saveAdfTask = mSaveAdfTask;
if (saveAdfTask != null) {
saveAdfTask.publishProgress(progress);
}
}

// Query user's input for the Tango Service configuration.
Expand Down Expand Up @@ -309,8 +377,11 @@ private void setupUIComponents() {
findViewById(R.id.top_down_button).setOnClickListener(this);
findViewById(R.id.save_adf_button).setOnClickListener(this);

// Hide to save ADF button if leanring mode is off.
if (!mIsAreaLearningEnabled) {
if (mIsAreaLearningEnabled) {
// Disable save ADF button until Tango relocalizes to the current ADF.
findViewById(R.id.save_adf_button).setEnabled(false);
} else {
// Hide to save ADF button if leanring mode is off.
findViewById(R.id.save_adf_button).setVisibility(View.GONE);
}

Expand All @@ -319,16 +390,14 @@ private void setupUIComponents() {

// Configure OpenGL renderer
mRenderer = new Renderer();
mRenderer.mAreaDescriptionActivity = this;
mGLView.setRenderer(mRenderer);
}

private void showSetNameDialog(String mCurrentUUID) {
private void showSetADFNameDialog() {
Bundle bundle = new Bundle();
String name = TangoJNINative.getAdfMetadataValue(mCurrentUUID, "name");
if (name != null) {
bundle.putString("name", name);
}
bundle.putString("id", mCurrentUUID);
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();
Expand Down Expand Up @@ -377,10 +446,17 @@ public void run() {
}

private void updateUI() {
// Update the UI debug displays.
mEvent.setText(TangoJNINative.getEventString());
mStartServiceTDevicePoseData.setText(
TangoJNINative.getStartServiceTDeviceString());
mADFTDevicePoseData.setText(TangoJNINative.getAdfTDeviceString());
mADFTStartServicePoseData.setText(TangoJNINative.getAdfTStartServiceString());

// If Tango has relocalized, allow saving the ADF.
// Note: Tango returns TANGO_INVALID if saveAdf() is called before relocalization.
if (TangoJNINative.isRelocalized()) {
findViewById(R.id.save_adf_button).setEnabled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
// Renderer renders graphic content. This includes the ground grid, camera
// frustum, camera axis, and trajectory based on the Tango device's pose.
public class Renderer implements GLSurfaceView.Renderer {
public AreaDescriptionActivity mAreaDescriptionActivity;

// Render loop of the Gl context.
public void onDrawFrame(GL10 gl) {
TangoJNINative.render();
Expand All @@ -36,5 +38,6 @@ public void onSurfaceChanged(GL10 gl, int width, int height) {
// Called when the surface is created or recreated.
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
TangoJNINative.initGlContent();
mAreaDescriptionActivity.mIsSurfaceCreated = true;
}
}
Loading

0 comments on commit f9235ba

Please sign in to comment.