diff --git a/Week_09_Services_and_wearables/MyWearables2/.idea/misc.xml b/Week_09_Services_and_wearables/MyWearables2/.idea/misc.xml index 5d19981..fbb6828 100644 --- a/Week_09_Services_and_wearables/MyWearables2/.idea/misc.xml +++ b/Week_09_Services_and_wearables/MyWearables2/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/Week_09_Services_and_wearables/MyWearables2/.idea/modules.xml b/Week_09_Services_and_wearables/MyWearables2/.idea/modules.xml index c69a079..9493730 100644 --- a/Week_09_Services_and_wearables/MyWearables2/.idea/modules.xml +++ b/Week_09_Services_and_wearables/MyWearables2/.idea/modules.xml @@ -2,7 +2,6 @@ - diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.google/packaging.yaml b/Week_09_Services_and_wearables/android-DataLayer-master/.google/packaging.yaml new file mode 100644 index 0000000..34a922f --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.google/packaging.yaml @@ -0,0 +1,19 @@ + +# GOOGLE SAMPLE PACKAGING DATA +# +# This file is used by Google as part of our samples packaging process. +# End users may safely ignore this file. It has no relevance to other systems. +--- +status: PUBLISHED +technologies: [Android] +categories: [UI, Wearable] +languages: [Java] +solutions: [Mobile] +github: android-DataLayer +level: ADVANCED +icon: Application/src/main/res/drawable-xxhdpi/ic_launcher.png +apiRefs: + - gms:com.google.android.gms.wearable.DataApi + - gms:com.google.android.gms.wearable.DataEvent + - gms:com.google.android.gms.wearable.WearableListenerService +license: apache2 diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/compiler.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/copyright/profiles_settings.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/gradle.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/gradle.xml new file mode 100644 index 0000000..23fc949 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/1_wearable_2_0_5.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/1_wearable_2_0_5.xml new file mode 100644 index 0000000..648b33d --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/1_wearable_2_0_5.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/animated_vector_drawable_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/animated_vector_drawable_26_0_0.xml new file mode 100644 index 0000000..8c4f369 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/animated_vector_drawable_26_0_0.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/appcompat_v7_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/appcompat_v7_26_0_0.xml new file mode 100644 index 0000000..0266d2f --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/appcompat_v7_26_0_0.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/cardview_v7_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/cardview_v7_26_0_0.xml new file mode 100644 index 0000000..ce9cf5c --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/cardview_v7_26_0_0.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/percent_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/percent_26_0_0.xml new file mode 100644 index 0000000..d47aacb --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/percent_26_0_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_base_11_4_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_base_11_4_0.xml new file mode 100644 index 0000000..c5a7b81 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_base_11_4_0.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_basement_11_4_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_basement_11_4_0.xml new file mode 100644 index 0000000..7de74f9 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_basement_11_4_0.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_tasks_11_4_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_tasks_11_4_0.xml new file mode 100644 index 0000000..28f60a4 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_tasks_11_4_0.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_wearable_11_4_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_wearable_11_4_0.xml new file mode 100644 index 0000000..a1fb6e8 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/play_services_wearable_11_4_0.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/recyclerview_v7_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/recyclerview_v7_26_0_0.xml new file mode 100644 index 0000000..cbbb454 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/recyclerview_v7_26_0_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_annotations_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_annotations_26_0_0.xml new file mode 100644 index 0000000..3ed0e05 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_annotations_26_0_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_compat_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_compat_26_0_0.xml new file mode 100644 index 0000000..35be97f --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_compat_26_0_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_core_ui_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_core_ui_26_0_0.xml new file mode 100644 index 0000000..eea8b78 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_core_ui_26_0_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_core_utils_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_core_utils_26_0_0.xml new file mode 100644 index 0000000..b24c4ac --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_core_utils_26_0_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_fragment_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_fragment_26_0_0.xml new file mode 100644 index 0000000..d74d20c --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_fragment_26_0_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_media_compat_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_media_compat_26_0_0.xml new file mode 100644 index 0000000..c247a23 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_media_compat_26_0_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_v13_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_v13_26_0_0.xml new file mode 100644 index 0000000..073d75e --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_v13_26_0_0.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_v4_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_v4_26_0_0.xml new file mode 100644 index 0000000..e919710 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_v4_26_0_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_vector_drawable_26_0_0.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_vector_drawable_26_0_0.xml new file mode 100644 index 0000000..1280a7e --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/support_vector_drawable_26_0_0.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/wearable_2_0_5.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/wearable_2_0_5.xml new file mode 100644 index 0000000..7a91a6b --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/libraries/wearable_2_0_5.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/misc.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/misc.xml new file mode 100644 index 0000000..5d19981 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/misc.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/modules.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/modules.xml new file mode 100644 index 0000000..9f84d4e --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/runConfigurations.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/.idea/workspace.xml b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/workspace.xml new file mode 100644 index 0000000..3445996 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/.idea/workspace.xml @@ -0,0 +1,3229 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1512047094889 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/build.gradle b/Week_09_Services_and_wearables/android-DataLayer-master/Application/build.gradle new file mode 100644 index 0000000..82bcf89 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/Application/build.gradle @@ -0,0 +1,67 @@ + +buildscript { + repositories { + jcenter() + } + + dependencies { + classpath 'com.android.tools.build:gradle:2.3.3' + } +} + +apply plugin: 'com.android.application' + +repositories { + jcenter() + maven { + url 'https://maven.google.com' + } +} + +dependencies { + compile "com.android.support:support-v4:26.0.0" + compile "com.android.support:support-v13:26.0.0" + compile "com.android.support:cardview-v7:26.0.0" + compile "com.android.support:appcompat-v7:26.0.0" + compile 'com.google.android.gms:play-services-wearable:11.4.0' + compile 'com.android.support:support-v13:26.0.0' + wearApp project(':Wearable') +} + +// The sample build uses multiple directories to +// keep boilerplate and common code separate from +// the main sample code. +List dirs = [ + 'main', // main sample code; look here for the interesting stuff. + 'common', // components that are reused by multiple samples + 'template'] // boilerplate code that is generated by the sample template process + +android { + + compileSdkVersion 26 + + buildToolsVersion "26.0.1" + + defaultConfig { + minSdkVersion 18 + targetSdkVersion 25 + } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_7 + targetCompatibility JavaVersion.VERSION_1_7 + } + + sourceSets { + main { + dirs.each { dir -> + java.srcDirs "src/${dir}/java" + res.srcDirs "src/${dir}/res" + } + } + androidTest.setRoot('tests') + androidTest.java.srcDirs = ['tests/src'] + + } + +} diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/AndroidManifest.xml b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/AndroidManifest.xml new file mode 100644 index 0000000..ed1cec3 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/AndroidManifest.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/java/com/example/android/wearable/datalayer/MainActivity.java b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/java/com/example/android/wearable/datalayer/MainActivity.java new file mode 100644 index 0000000..ca86d7a --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/java/com/example/android/wearable/datalayer/MainActivity.java @@ -0,0 +1,459 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * 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.example.android.wearable.datalayer; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.IntentSender; +import android.content.pm.PackageManager; +import android.graphics.Bitmap; +import android.net.Uri; +import android.os.AsyncTask; +import android.os.Bundle; +import android.provider.MediaStore; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; +import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; +import com.google.android.gms.common.api.ResultCallback; +import com.google.android.gms.wearable.Asset; +import com.google.android.gms.wearable.CapabilityApi; +import com.google.android.gms.wearable.CapabilityInfo; +import com.google.android.gms.wearable.DataApi; +import com.google.android.gms.wearable.DataApi.DataItemResult; +import com.google.android.gms.wearable.DataEvent; +import com.google.android.gms.wearable.DataEventBuffer; +import com.google.android.gms.wearable.MessageApi; +import com.google.android.gms.wearable.MessageApi.SendMessageResult; +import com.google.android.gms.wearable.MessageEvent; +import com.google.android.gms.wearable.Node; +import com.google.android.gms.wearable.NodeApi; +import com.google.android.gms.wearable.PutDataMapRequest; +import com.google.android.gms.wearable.PutDataRequest; +import com.google.android.gms.wearable.Wearable; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Collection; +import java.util.Date; +import java.util.HashSet; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * Receives its own events using a listener API designed for foreground activities. Updates a data + * item every second while it is open. Also allows user to take a photo and send that as an asset + * to the paired wearable. + */ +public class MainActivity extends Activity implements + CapabilityApi.CapabilityListener, + MessageApi.MessageListener, + DataApi.DataListener, + ConnectionCallbacks, + OnConnectionFailedListener { + + private static final String TAG = "MainActivity"; + + //Request code for launching the Intent to resolve Google Play services errors. + private static final int REQUEST_RESOLVE_ERROR = 1000; + + private static final int REQUEST_IMAGE_CAPTURE = 1; + + private static final String START_ACTIVITY_PATH = "/start-activity"; + private static final String COUNT_PATH = "/count"; + private static final String IMAGE_PATH = "/image"; + private static final String IMAGE_KEY = "photo"; + private static final String COUNT_KEY = "count"; + + private GoogleApiClient mGoogleApiClient; + private boolean mResolvingError = false; + private boolean mCameraSupported = false; + + private ListView mDataItemList; + private Button mSendPhotoBtn; + private ImageView mThumbView; + private Bitmap mImageBitmap; + private View mStartActivityBtn; + + private DataItemAdapter mDataItemListAdapter; + + // Send DataItems. + private ScheduledExecutorService mGeneratorExecutor; + private ScheduledFuture mDataItemGeneratorFuture; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + LOGD(TAG, "onCreate"); + mCameraSupported = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA); + setContentView(R.layout.main_activity); + setupViews(); + + // Stores DataItems received by the local broadcaster or from the paired watch. + mDataItemListAdapter = new DataItemAdapter(this, android.R.layout.simple_list_item_1); + mDataItemList.setAdapter(mDataItemListAdapter); + + mGeneratorExecutor = new ScheduledThreadPoolExecutor(1); + + mGoogleApiClient = new GoogleApiClient.Builder(this) + .addApi(Wearable.API) + .addConnectionCallbacks(this) + .addOnConnectionFailedListener(this) + .build(); + } + + @Override + protected void onStart() { + super.onStart(); + if (!mResolvingError) { + mGoogleApiClient.connect(); + } + } + + @Override + public void onResume() { + super.onResume(); + mDataItemGeneratorFuture = mGeneratorExecutor.scheduleWithFixedDelay( + new DataItemGenerator(), 1, 5, TimeUnit.SECONDS); + } + + @Override + public void onPause() { + super.onPause(); + mDataItemGeneratorFuture.cancel(true /* mayInterruptIfRunning */); + } + + @Override + protected void onStop() { + if (!mResolvingError && (mGoogleApiClient != null) && (mGoogleApiClient.isConnected())) { + Wearable.DataApi.removeListener(mGoogleApiClient, this); + Wearable.MessageApi.removeListener(mGoogleApiClient, this); + Wearable.CapabilityApi.removeListener(mGoogleApiClient, this); + mGoogleApiClient.disconnect(); + } + super.onStop(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { + Bundle extras = data.getExtras(); + mImageBitmap = (Bitmap) extras.get("data"); + mThumbView.setImageBitmap(mImageBitmap); + } + } + + @Override + public void onConnected(Bundle connectionHint) { + LOGD(TAG, "Google API Client was connected"); + mResolvingError = false; + mStartActivityBtn.setEnabled(true); + mSendPhotoBtn.setEnabled(mCameraSupported); + Wearable.DataApi.addListener(mGoogleApiClient, this); + Wearable.MessageApi.addListener(mGoogleApiClient, this); + Wearable.CapabilityApi.addListener( + mGoogleApiClient, this, Uri.parse("wear://"), CapabilityApi.FILTER_REACHABLE); + } + + @Override + public void onConnectionSuspended(int cause) { + LOGD(TAG, "Connection to Google API client was suspended"); + mStartActivityBtn.setEnabled(false); + mSendPhotoBtn.setEnabled(false); + } + + @Override + public void onConnectionFailed(ConnectionResult result) { + if (!mResolvingError) { + + if (result.hasResolution()) { + try { + mResolvingError = true; + result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); + } catch (IntentSender.SendIntentException e) { + // There was an error with the resolution intent. Try again. + mGoogleApiClient.connect(); + } + } else { + Log.e(TAG, "Connection to Google API client has failed"); + mResolvingError = false; + mStartActivityBtn.setEnabled(false); + mSendPhotoBtn.setEnabled(false); + Wearable.DataApi.removeListener(mGoogleApiClient, this); + Wearable.MessageApi.removeListener(mGoogleApiClient, this); + Wearable.CapabilityApi.removeListener(mGoogleApiClient, this); + } + } + } + + @Override + public void onDataChanged(DataEventBuffer dataEvents) { + LOGD(TAG, "onDataChanged: " + dataEvents); + + for (DataEvent event : dataEvents) { + if (event.getType() == DataEvent.TYPE_CHANGED) { + mDataItemListAdapter.add( + new Event("DataItem Changed", event.getDataItem().toString())); + } else if (event.getType() == DataEvent.TYPE_DELETED) { + mDataItemListAdapter.add( + new Event("DataItem Deleted", event.getDataItem().toString())); + } + } + } + + @Override + public void onMessageReceived(final MessageEvent messageEvent) { + LOGD(TAG, "onMessageReceived() A message from watch was received:" + + messageEvent.getRequestId() + " " + messageEvent.getPath()); + + mDataItemListAdapter.add(new Event("Message from watch", messageEvent.toString())); + } + + @Override + public void onCapabilityChanged(final CapabilityInfo capabilityInfo) { + LOGD(TAG, "onCapabilityChanged: " + capabilityInfo); + + mDataItemListAdapter.add(new Event("onCapabilityChanged", capabilityInfo.toString())); + } + + /** + * Sets up UI components and their callback handlers. + */ + private void setupViews() { + mSendPhotoBtn = (Button) findViewById(R.id.sendPhoto); + mThumbView = (ImageView) findViewById(R.id.imageView); + mDataItemList = (ListView) findViewById(R.id.data_item_list); + mStartActivityBtn = findViewById(R.id.start_wearable_activity); + } + + public void onTakePhotoClick(View view) { + dispatchTakePictureIntent(); + } + + public void onSendPhotoClick(View view) { + if (null != mImageBitmap && mGoogleApiClient.isConnected()) { + sendPhoto(toAsset(mImageBitmap)); + } + } + + /** + * Sends an RPC to start a fullscreen Activity on the wearable. + */ + public void onStartWearableActivityClick(View view) { + LOGD(TAG, "Generating RPC"); + + // Trigger an AsyncTask that will query for a list of connected nodes and send a + // "start-activity" message to each connected node. + new StartWearableActivityTask().execute(); + } + + private void sendStartActivityMessage(String node) { + Wearable.MessageApi.sendMessage( + mGoogleApiClient, node, START_ACTIVITY_PATH, new byte[0]).setResultCallback( + new ResultCallback() { + @Override + public void onResult(SendMessageResult sendMessageResult) { + if (!sendMessageResult.getStatus().isSuccess()) { + Log.e(TAG, "Failed to send message with status code: " + + sendMessageResult.getStatus().getStatusCode()); + } + } + } + ); + } + + /** + * Dispatches an {@link android.content.Intent} to take a photo. Result will be returned back + * in onActivityResult(). + */ + private void dispatchTakePictureIntent() { + Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + if (takePictureIntent.resolveActivity(getPackageManager()) != null) { + startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); + } + } + + /** + * Builds an {@link com.google.android.gms.wearable.Asset} from a bitmap. The image that we get + * back from the camera in "data" is a thumbnail size. Typically, your image should not exceed + * 320x320 and if you want to have zoom and parallax effect in your app, limit the size of your + * image to 640x400. Resize your image before transferring to your wearable device. + */ + private static Asset toAsset(Bitmap bitmap) { + ByteArrayOutputStream byteStream = null; + try { + byteStream = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream); + return Asset.createFromBytes(byteStream.toByteArray()); + } finally { + if (null != byteStream) { + try { + byteStream.close(); + } catch (IOException e) { + // ignore + } + } + } + } + + /** + * Sends the asset that was created from the photo we took by adding it to the Data Item store. + */ + private void sendPhoto(Asset asset) { + PutDataMapRequest dataMap = PutDataMapRequest.create(IMAGE_PATH); + dataMap.getDataMap().putAsset(IMAGE_KEY, asset); + dataMap.getDataMap().putLong("time", new Date().getTime()); + PutDataRequest request = dataMap.asPutDataRequest(); + request.setUrgent(); + + Wearable.DataApi.putDataItem(mGoogleApiClient, request) + .setResultCallback(new ResultCallback() { + @Override + public void onResult(DataItemResult dataItemResult) { + LOGD(TAG, "Sending image was successful: " + dataItemResult.getStatus() + .isSuccess()); + } + }); + } + + private Collection getNodes() { + HashSet results = new HashSet<>(); + NodeApi.GetConnectedNodesResult nodes = + Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); + + for (Node node : nodes.getNodes()) { + results.add(node.getId()); + } + + return results; + } + + /** + * As simple wrapper around Log.d + */ + private static void LOGD(final String tag, String message) { + if (Log.isLoggable(tag, Log.DEBUG)) { + Log.d(tag, message); + } + } + + /** + * A View Adapter for presenting the Event objects in a list + */ + private static class DataItemAdapter extends ArrayAdapter { + + private final Context mContext; + + public DataItemAdapter(Context context, int unusedResource) { + super(context, unusedResource); + mContext = context; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewHolder holder; + if (convertView == null) { + holder = new ViewHolder(); + LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( + Context.LAYOUT_INFLATER_SERVICE); + convertView = inflater.inflate(android.R.layout.two_line_list_item, null); + convertView.setTag(holder); + holder.text1 = (TextView) convertView.findViewById(android.R.id.text1); + holder.text2 = (TextView) convertView.findViewById(android.R.id.text2); + } else { + holder = (ViewHolder) convertView.getTag(); + } + Event event = getItem(position); + holder.text1.setText(event.title); + holder.text2.setText(event.text); + return convertView; + } + + private class ViewHolder { + TextView text1; + TextView text2; + } + } + + private class Event { + + String title; + String text; + + public Event(String title, String text) { + this.title = title; + this.text = text; + } + } + + private class StartWearableActivityTask extends AsyncTask { + + @Override + protected Void doInBackground(Void... args) { + Collection nodes = getNodes(); + for (String node : nodes) { + sendStartActivityMessage(node); + } + return null; + } + } + + /** + * Generates a DataItem based on an incrementing count. + */ + private class DataItemGenerator implements Runnable { + + private int count = 0; + + @Override + public void run() { + PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(COUNT_PATH); + putDataMapRequest.getDataMap().putInt(COUNT_KEY, count++); + + PutDataRequest request = putDataMapRequest.asPutDataRequest(); + request.setUrgent(); + + LOGD(TAG, "Generating DataItem: " + request); + if (!mGoogleApiClient.isConnected()) { + return; + } + Wearable.DataApi.putDataItem(mGoogleApiClient, request) + .setResultCallback(new ResultCallback() { + @Override + public void onResult(DataItemResult dataItemResult) { + if (!dataItemResult.getStatus().isSuccess()) { + Log.e(TAG, "ERROR: failed to putDataItem, status code: " + + dataItemResult.getStatus().getStatusCode()); + } + } + }); + } + } +} \ No newline at end of file diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-hdpi/ic_content_picture.png b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-hdpi/ic_content_picture.png new file mode 100644 index 0000000..597997c Binary files /dev/null and b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-hdpi/ic_content_picture.png differ diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-hdpi/ic_launcher.png b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000..589f229 Binary files /dev/null and b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-hdpi/ic_launcher.png differ diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-hdpi/tile.9.png b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-hdpi/tile.9.png new file mode 100644 index 0000000..1358628 Binary files /dev/null and b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-hdpi/tile.9.png differ diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-mdpi/ic_content_picture.png b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-mdpi/ic_content_picture.png new file mode 100644 index 0000000..c23f4c0 Binary files /dev/null and b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-mdpi/ic_content_picture.png differ diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-mdpi/ic_launcher.png b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 0000000..77dd571 Binary files /dev/null and b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-mdpi/ic_launcher.png differ diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xhdpi/ic_content_picture.png b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xhdpi/ic_content_picture.png new file mode 100644 index 0000000..289adf1 Binary files /dev/null and b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xhdpi/ic_content_picture.png differ diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xhdpi/ic_launcher.png b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000..fe34ebe Binary files /dev/null and b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xhdpi/ic_launcher.png differ diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xxhdpi/ic_content_picture.png b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xxhdpi/ic_content_picture.png new file mode 100644 index 0000000..073ad55 Binary files /dev/null and b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xxhdpi/ic_content_picture.png differ diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xxhdpi/ic_launcher.png b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..ab80bcd Binary files /dev/null and b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable/divider.xml b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable/divider.xml new file mode 100644 index 0000000..95c4d33 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/drawable/divider.xml @@ -0,0 +1,20 @@ + + + + + + + diff --git a/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/layout/main_activity.xml b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/layout/main_activity.xml new file mode 100644 index 0000000..0412023 --- /dev/null +++ b/Week_09_Services_and_wearables/android-DataLayer-master/Application/src/main/res/layout/main_activity.xml @@ -0,0 +1,86 @@ + + + + + + + +