Skip to content

Commit 419a0f1

Browse files
committed
Update sample prebuilts for mnc-docs
Synced to //developers/samples/android commit 243feb49e8d1753b746f69ae5519eaace0e50605. Change-Id: I9255d2ad8f68669d77124b7840184171fb5a801b
1 parent f90e19f commit 419a0f1

File tree

113 files changed

+3036
-38
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+3036
-38
lines changed

samples/browseable/AgendaData/Application/src/com.example.android.wearable.agendadata/CalendarQueryService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ private static class Event {
250250
public PutDataMapRequest toPutDataMapRequest(){
251251
final PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(
252252
makeDataItemPath(eventId, begin));
253+
/* In most cases (as in this one), you don't need your DataItem appear instantly. By
254+
default, delivery of normal DataItems to the Wear network might be delayed in order to
255+
improve battery life for user devices. However, if you can't tolerate a delay in the
256+
sync of your DataItems, you can mark them as urgent via setUrgent().
257+
*/
253258
DataMap data = putDataMapRequest.getDataMap();
254259
data.putString(DATA_ITEM_URI, putDataMapRequest.getUri().toString());
255260
data.putLong(ID, id);

samples/browseable/Camera2Basic/src/com.example.android.camera2basic/Camera2BasicFragment.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import android.os.HandlerThread;
4949
import android.support.annotation.NonNull;
5050
import android.support.v13.app.FragmentCompat;
51+
import android.support.v4.content.ContextCompat;
5152
import android.util.Log;
5253
import android.util.Size;
5354
import android.util.SparseIntArray;
@@ -269,6 +270,11 @@ public void onImageAvailable(ImageReader reader) {
269270
*/
270271
private Semaphore mCameraOpenCloseLock = new Semaphore(1);
271272

273+
/**
274+
* Whether the current camera device supports Flash or not.
275+
*/
276+
private boolean mFlashSupported;
277+
272278
/**
273279
* A {@link CameraCaptureSession.CaptureCallback} that handles events related to JPEG capture.
274280
*/
@@ -568,6 +574,10 @@ private void setUpCameraOutputs(int width, int height) {
568574
mPreviewSize.getHeight(), mPreviewSize.getWidth());
569575
}
570576

577+
// Check if the flash is supported.
578+
Boolean available = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
579+
mFlashSupported = available == null ? false : available;
580+
571581
mCameraId = cameraId;
572582
return;
573583
}
@@ -585,7 +595,7 @@ private void setUpCameraOutputs(int width, int height) {
585595
* Opens the camera specified by {@link Camera2BasicFragment#mCameraId}.
586596
*/
587597
private void openCamera(int width, int height) {
588-
if (getActivity().checkSelfPermission(Manifest.permission.CAMERA)
598+
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
589599
!= PackageManager.PERMISSION_GRANTED) {
590600
requestCameraPermission();
591601
return;
@@ -691,8 +701,7 @@ public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
691701
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
692702
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
693703
// Flash is automatically enabled when necessary.
694-
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
695-
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
704+
setAutoFlash(mPreviewRequestBuilder);
696705

697706
// Finally, we start displaying the camera preview.
698707
mPreviewRequest = mPreviewRequestBuilder.build();
@@ -808,8 +817,7 @@ private void captureStillPicture() {
808817
// Use the same AE and AF modes as the preview.
809818
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
810819
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
811-
captureBuilder.set(CaptureRequest.CONTROL_AE_MODE,
812-
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
820+
setAutoFlash(captureBuilder);
813821

814822
// Orientation
815823
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
@@ -844,8 +852,7 @@ private void unlockFocus() {
844852
// Reset the auto-focus trigger
845853
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
846854
CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
847-
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
848-
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
855+
setAutoFlash(mPreviewRequestBuilder);
849856
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
850857
mBackgroundHandler);
851858
// After this, the camera will go back to the normal state of preview.
@@ -877,6 +884,13 @@ public void onClick(View view) {
877884
}
878885
}
879886

887+
private void setAutoFlash(CaptureRequest.Builder requestBuilder) {
888+
if (mFlashSupported) {
889+
requestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
890+
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
891+
}
892+
}
893+
880894
/**
881895
* Saves a JPEG {@link Image} into the specified {@link File}.
882896
*/

samples/browseable/DataLayer/Application/src/com.example.android.wearable.datalayer/MainActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ private class DataItemGenerator implements Runnable {
381381
public void run() {
382382
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(COUNT_PATH);
383383
putDataMapRequest.getDataMap().putInt(COUNT_KEY, count++);
384+
putDataMapRequest.setUrgent();
384385
PutDataRequest request = putDataMapRequest.asPutDataRequest();
386+
request.setUrgent();
385387

386388
LOGD(TAG, "Generating DataItem: " + request);
387389
if (!mGoogleApiClient.isConnected()) {
@@ -442,6 +444,8 @@ private void sendPhoto(Asset asset) {
442444
dataMap.getDataMap().putAsset(IMAGE_KEY, asset);
443445
dataMap.getDataMap().putLong("time", new Date().getTime());
444446
PutDataRequest request = dataMap.asPutDataRequest();
447+
request.setUrgent();
448+
445449
Wearable.DataApi.putDataItem(mGoogleApiClient, request)
446450
.setResultCallback(new ResultCallback<DataItemResult>() {
447451
@Override

samples/browseable/DisplayingBitmaps/res/layout/image_detail_fragment.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
android:layout_height="fill_parent" >
2121

2222
<ProgressBar
23+
android:id="@+id/progressbar"
2324
style="?android:attr/progressBarStyleLarge"
2425
android:layout_width="wrap_content"
2526
android:layout_height="wrap_content"

samples/browseable/DisplayingBitmaps/src/com.example.android.displayingbitmaps/ui/ImageDetailFragment.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.view.View.OnClickListener;
2424
import android.view.ViewGroup;
2525
import android.widget.ImageView;
26+
import android.widget.ProgressBar;
2627

2728
import com.example.android.displayingbitmaps.R;
2829
import com.example.android.displayingbitmaps.util.ImageFetcher;
@@ -32,10 +33,11 @@
3233
/**
3334
* This fragment will populate the children of the ViewPager from {@link ImageDetailActivity}.
3435
*/
35-
public class ImageDetailFragment extends Fragment {
36+
public class ImageDetailFragment extends Fragment implements ImageWorker.OnImageLoadedListener {
3637
private static final String IMAGE_DATA_EXTRA = "extra_image_data";
3738
private String mImageUrl;
3839
private ImageView mImageView;
40+
private ProgressBar mProgressBar;
3941
private ImageFetcher mImageFetcher;
4042

4143
/**
@@ -75,6 +77,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
7577
// Inflate and locate the main ImageView
7678
final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
7779
mImageView = (ImageView) v.findViewById(R.id.imageView);
80+
mProgressBar = (ProgressBar) v.findViewById(R.id.progressbar);
7881
return v;
7982
}
8083

@@ -86,7 +89,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
8689
// cache can be used over all pages in the ViewPager
8790
if (ImageDetailActivity.class.isInstance(getActivity())) {
8891
mImageFetcher = ((ImageDetailActivity) getActivity()).getImageFetcher();
89-
mImageFetcher.loadImage(mImageUrl, mImageView);
92+
mImageFetcher.loadImage(mImageUrl, mImageView, this);
9093
}
9194

9295
// Pass clicks on the ImageView to the parent activity to handle
@@ -104,4 +107,11 @@ public void onDestroy() {
104107
mImageView.setImageDrawable(null);
105108
}
106109
}
110+
111+
@Override
112+
public void onImageLoaded(boolean success) {
113+
// Set loading spinner to gone once image has loaded. Cloud also show
114+
// an error view here if needed.
115+
mProgressBar.setVisibility(View.GONE);
116+
}
107117
}

samples/browseable/DisplayingBitmaps/src/com.example.android.displayingbitmaps/util/ImageWorker.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ protected ImageWorker(Context context) {
7171
*
7272
* @param data The URL of the image to download.
7373
* @param imageView The ImageView to bind the downloaded image to.
74+
* @param listener A listener that will be called back once the image has been loaded.
7475
*/
75-
public void loadImage(Object data, ImageView imageView) {
76+
public void loadImage(Object data, ImageView imageView, OnImageLoadedListener listener) {
7677
if (data == null) {
7778
return;
7879
}
@@ -86,9 +87,12 @@ public void loadImage(Object data, ImageView imageView) {
8687
if (value != null) {
8788
// Bitmap found in memory cache
8889
imageView.setImageDrawable(value);
90+
if (listener != null) {
91+
listener.onImageLoaded(true);
92+
}
8993
} else if (cancelPotentialWork(data, imageView)) {
9094
//BEGIN_INCLUDE(execute_background_task)
91-
final BitmapWorkerTask task = new BitmapWorkerTask(data, imageView);
95+
final BitmapWorkerTask task = new BitmapWorkerTask(data, imageView, listener);
9296
final AsyncDrawable asyncDrawable =
9397
new AsyncDrawable(mResources, mLoadingBitmap, task);
9498
imageView.setImageDrawable(asyncDrawable);
@@ -101,6 +105,21 @@ public void loadImage(Object data, ImageView imageView) {
101105
}
102106
}
103107

108+
/**
109+
* Load an image specified by the data parameter into an ImageView (override
110+
* {@link ImageWorker#processBitmap(Object)} to define the processing logic). A memory and
111+
* disk cache will be used if an {@link ImageCache} has been added using
112+
* {@link ImageWorker#addImageCache(android.support.v4.app.FragmentManager, ImageCache.ImageCacheParams)}. If the
113+
* image is found in the memory cache, it is set immediately, otherwise an {@link AsyncTask}
114+
* will be created to asynchronously load the bitmap.
115+
*
116+
* @param data The URL of the image to download.
117+
* @param imageView The ImageView to bind the downloaded image to.
118+
*/
119+
public void loadImage(Object data, ImageView imageView) {
120+
loadImage(data, imageView, null);
121+
}
122+
104123
/**
105124
* Set placeholder bitmap that shows when the the background thread is running.
106125
*
@@ -238,10 +257,18 @@ private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
238257
private class BitmapWorkerTask extends AsyncTask<Void, Void, BitmapDrawable> {
239258
private Object mData;
240259
private final WeakReference<ImageView> imageViewReference;
260+
private final OnImageLoadedListener mOnImageLoadedListener;
241261

242262
public BitmapWorkerTask(Object data, ImageView imageView) {
243263
mData = data;
244264
imageViewReference = new WeakReference<ImageView>(imageView);
265+
mOnImageLoadedListener = null;
266+
}
267+
268+
public BitmapWorkerTask(Object data, ImageView imageView, OnImageLoadedListener listener) {
269+
mData = data;
270+
imageViewReference = new WeakReference<ImageView>(imageView);
271+
mOnImageLoadedListener = listener;
245272
}
246273

247274
/**
@@ -318,6 +345,7 @@ protected BitmapDrawable doInBackground(Void... params) {
318345
@Override
319346
protected void onPostExecute(BitmapDrawable value) {
320347
//BEGIN_INCLUDE(complete_background_work)
348+
boolean success = false;
321349
// if cancel was called on this task or the "exit early" flag is set then we're done
322350
if (isCancelled() || mExitTasksEarly) {
323351
value = null;
@@ -328,8 +356,12 @@ protected void onPostExecute(BitmapDrawable value) {
328356
if (BuildConfig.DEBUG) {
329357
Log.d(TAG, "onPostExecute - setting bitmap");
330358
}
359+
success = true;
331360
setImageDrawable(imageView, value);
332361
}
362+
if (mOnImageLoadedListener != null) {
363+
mOnImageLoadedListener.onImageLoaded(success);
364+
}
333365
//END_INCLUDE(complete_background_work)
334366
}
335367

@@ -357,6 +389,19 @@ private ImageView getAttachedImageView() {
357389
}
358390
}
359391

392+
/**
393+
* Interface definition for callback on image loaded successfully.
394+
*/
395+
public interface OnImageLoadedListener {
396+
397+
/**
398+
* Called once the image has been loaded.
399+
* @param success True if the image was loaded successfully, false if
400+
* there was an error.
401+
*/
402+
void onImageLoaded(boolean success);
403+
}
404+
360405
/**
361406
* A custom Drawable that will be attached to the imageView while the work is in progress.
362407
* Contains a reference to the actual worker task, so that it can be stopped if a new binding is

samples/browseable/FindMyPhone/Wearable/src/com.example.android.wearable.findphone/FindPhoneService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ protected void onHandleIntent(Intent intent) {
100100
// when it receives the change.
101101
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(PATH_SOUND_ALARM);
102102
putDataMapRequest.getDataMap().putBoolean(FIELD_ALARM_ON, alarmOn);
103+
putDataMapRequest.setUrgent();
103104
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataMapRequest.asPutDataRequest())
104105
.await();
105106
} else {

samples/browseable/Geofencing/Application/src/com.example.android.wearable.geofencing/GeofenceTransitionsIntentService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ protected void onHandleIntent(Intent intent) {
8989
final PutDataMapRequest putDataMapRequest =
9090
PutDataMapRequest.create(GEOFENCE_DATA_ITEM_PATH);
9191
putDataMapRequest.getDataMap().putString(KEY_GEOFENCE_ID, triggeredGeoFenceId);
92+
putDataMapRequest.setUrgent();
9293
if (mGoogleApiClient.isConnected()) {
9394
Wearable.DataApi.putDataItem(
9495
mGoogleApiClient, putDataMapRequest.asPutDataRequest()).await();

samples/browseable/Quiz/Application/src/com.example.android.wearable.quiz/MainActivity.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ public PutDataRequest toPutDataRequest() {
248248
dataMap.putInt(QUESTION_INDEX, questionIndex);
249249
dataMap.putStringArray(ANSWERS, answers);
250250
dataMap.putInt(CORRECT_ANSWER_INDEX, correctAnswerIndex);
251-
return request.asPutDataRequest();
251+
PutDataRequest putDataRequest = request.asPutDataRequest();
252+
putDataRequest.setUrgent();
253+
return putDataRequest;
252254
}
253255
}
254256

@@ -496,7 +498,10 @@ public void onResult(DataApi.DataItemResult dataItemResult) {
496498
dataMap.putBoolean(QUESTION_WAS_DELETED, false);
497499
if (!mHasQuestionBeenAsked && dataMap.getInt(QUESTION_INDEX) == 0) {
498500
// Ask the first question now.
499-
Wearable.DataApi.putDataItem(mGoogleApiClient, request.asPutDataRequest());
501+
PutDataRequest putDataRequest = request.asPutDataRequest();
502+
// Set to high priority in case it isn't already.
503+
putDataRequest.setUrgent();
504+
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
500505
setHasQuestionBeenAsked(true);
501506
} else {
502507
// Enqueue future questions.

samples/browseable/Quiz/Wearable/src/com.example.android.wearable.quiz/DeleteQuestionService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public void onHandleIntent(Intent intent) {
7676
DataMap dataMap = putDataMapRequest.getDataMap();
7777
dataMap.putBoolean(QUESTION_WAS_DELETED, true);
7878
PutDataRequest request = putDataMapRequest.asPutDataRequest();
79+
request.setUrgent();
7980
Wearable.DataApi.putDataItem(mGoogleApiClient, request).await();
8081
mGoogleApiClient.disconnect();
8182
}

0 commit comments

Comments
 (0)