am 875d8474: am 1e683829: Making sure "release()" is called on data buffers where needed

* commit '875d84747ec509096692a6f7f176f5428d3508d3':
  Making sure "release()" is called on data buffers where needed
diff --git a/wearable/wear/AgendaData/Application/src/main/java/com/example/android/wearable/agendadata/MainActivity.java b/wearable/wear/AgendaData/Application/src/main/java/com/example/android/wearable/agendadata/MainActivity.java
index 8e4063d..62dc349 100644
--- a/wearable/wear/AgendaData/Application/src/main/java/com/example/android/wearable/agendadata/MainActivity.java
+++ b/wearable/wear/AgendaData/Application/src/main/java/com/example/android/wearable/agendadata/MainActivity.java
@@ -96,28 +96,33 @@
                     .setResultCallback(new ResultCallback<DataItemBuffer>() {
                         @Override
                         public void onResult(DataItemBuffer result) {
-                            if (result.getStatus().isSuccess()) {
-                                deleteDataItems(result);
-                            } else {
-                                if (Log.isLoggable(TAG, Log.DEBUG)) {
-                                    Log.d(TAG, "onDeleteEventsClicked(): failed to get Data Items");
+                            try {
+                                if (result.getStatus().isSuccess()) {
+                                    // Store the DataItem URIs in a List and release the buffer.
+                                    // Then use these URIs to delete the DataItems.
+                                    List<DataItem> dataItemList = FreezableUtils
+                                            .freezeIterable(result);
+                                    deleteDataItems(dataItemList);
+                                } else {
+                                    if (Log.isLoggable(TAG, Log.DEBUG)) {
+                                        Log.d(TAG,"onDeleteEventsClicked(): failed to get Data "
+                                                + "Items");
+
+                                    }
                                 }
+                            } finally {
+                                result.release();
                             }
-                            result.close();
                         }
                     });
         } else {
             Log.e(TAG, "Failed to delete data items"
-                     + " - Client disconnected from Google Play Services");
+                    + " - Client disconnected from Google Play Services");
         }
     }
 
-    private void deleteDataItems(DataItemBuffer dataItems) {
+    private void deleteDataItems(final List<DataItem> dataItemList) {
         if (mGoogleApiClient.isConnected()) {
-            // Store the DataItem URIs in a List and close the buffer. Then use these URIs
-            // to delete the DataItems.
-            final List<DataItem> dataItemList = FreezableUtils.freezeIterable(dataItems);
-            dataItems.close();
             for (final DataItem dataItem : dataItemList) {
                 final Uri dataItemUri = dataItem.getUri();
                 // In a real calendar application, this might delete the corresponding calendar
diff --git a/wearable/wear/FindMyPhone/Wearable/src/main/java/com/example/android/wearable/findphone/FindPhoneService.java b/wearable/wear/FindMyPhone/Wearable/src/main/java/com/example/android/wearable/findphone/FindPhoneService.java
index 97204b4..c6c6d67 100644
--- a/wearable/wear/FindMyPhone/Wearable/src/main/java/com/example/android/wearable/findphone/FindPhoneService.java
+++ b/wearable/wear/FindMyPhone/Wearable/src/main/java/com/example/android/wearable/findphone/FindPhoneService.java
@@ -73,19 +73,22 @@
             if (intent.getAction().equals(ACTION_TOGGLE_ALARM)) {
                 // Get current state of the alarm.
                 DataItemBuffer result = Wearable.DataApi.getDataItems(mGoogleApiClient).await();
-                if (result.getStatus().isSuccess()) {
-                    if (result.getCount() == 1) {
-                        alarmOn = DataMap.fromByteArray(result.get(0).getData())
-                                .getBoolean(FIELD_ALARM_ON, false);
-                    } else {
-                        Log.e(TAG, "Unexpected number of DataItems found.\n"
-                                + "\tExpected: 1\n"
-                                + "\tActual: " + result.getCount());
+                try {
+                    if (result.getStatus().isSuccess()) {
+                        if (result.getCount() == 1) {
+                            alarmOn = DataMap.fromByteArray(result.get(0).getData())
+                                    .getBoolean(FIELD_ALARM_ON, false);
+                        } else {
+                            Log.e(TAG, "Unexpected number of DataItems found.\n"
+                                    + "\tExpected: 1\n"
+                                    + "\tActual: " + result.getCount());
+                        }
+                    } else if (Log.isLoggable(TAG, Log.DEBUG)) {
+                        Log.d(TAG, "onHandleIntent: failed to get current alarm state");
                     }
-                } else if (Log.isLoggable(TAG, Log.DEBUG)) {
-                    Log.d(TAG, "onHandleIntent: failed to get current alarm state");
+                } finally {
+                    result.release();
                 }
-                result.close();
                 // Toggle alarm.
                 alarmOn = !alarmOn;
                 // Change notification text based on new value of alarmOn.
diff --git a/wearable/wear/Quiz/Application/src/main/java/com/example/android/wearable/quiz/MainActivity.java b/wearable/wear/Quiz/Application/src/main/java/com/example/android/wearable/quiz/MainActivity.java
index 1e58935..855fcd6 100644
--- a/wearable/wear/Quiz/Application/src/main/java/com/example/android/wearable/quiz/MainActivity.java
+++ b/wearable/wear/Quiz/Application/src/main/java/com/example/android/wearable/quiz/MainActivity.java
@@ -445,16 +445,19 @@
                     .setResultCallback(new ResultCallback<DataItemBuffer>() {
                         @Override
                         public void onResult(DataItemBuffer result) {
-                            if (result.getStatus().isSuccess()) {
-                                List<DataItem> dataItemList = FreezableUtils.freezeIterable(result);
-                                result.close();
-                                resetDataItems(dataItemList);
-                            } else {
-                                if (Log.isLoggable(TAG, Log.DEBUG)) {
-                                    Log.d(TAG, "Reset quiz: failed to get Data Items to reset");
+                            try {
+                                if (result.getStatus().isSuccess()) {
+                                    List<DataItem> dataItemList = FreezableUtils
+                                            .freezeIterable(result);
+                                    resetDataItems(dataItemList);
+                                } else {
+                                    if (Log.isLoggable(TAG, Log.DEBUG)) {
+                                        Log.d(TAG, "Reset quiz: failed to get Data Items to reset");
+                                    }
                                 }
+                            } finally {
+                                result.release();
                             }
-                            result.close();
                         }
                     });
         } else {
@@ -521,19 +524,23 @@
                     .setResultCallback(new ResultCallback<DataItemBuffer>() {
                         @Override
                         public void onResult(DataItemBuffer result) {
-                            if (result.getStatus().isSuccess()) {
-                                List<Uri> dataItemUriList = new ArrayList<Uri>();
-                                for (final DataItem dataItem : result) {
-                                    dataItemUriList.add(dataItem.getUri());
+                            try {
+                                if (result.getStatus().isSuccess()) {
+                                    List<Uri> dataItemUriList = new ArrayList<Uri>();
+                                    for (final DataItem dataItem : result) {
+                                        dataItemUriList.add(dataItem.getUri());
+                                    }
+                                    deleteDataItems(dataItemUriList);
+                                } else {
+                                    if (Log.isLoggable(TAG, Log.DEBUG)) {
+                                        Log.d(TAG, "Clear quiz: failed to get Data Items for "
+                                                + "deletion");
+
+                                    }
                                 }
-                                result.close();
-                                deleteDataItems(dataItemUriList);
-                            } else {
-                                if (Log.isLoggable(TAG, Log.DEBUG)) {
-                                    Log.d(TAG, "Clear quiz: failed to get Data Items for deletion");
-                                }
+                            } finally {
+                                result.release();
                             }
-                            result.close();
                         }
                     });
         } else {