Sync sample prebuilts to lmp-mr1-ub-docs

Upstream commit: f76d10f3b6abad250d94b5b9a5c73faae6ce8cc5

Change-Id: I32f2690e36f444cae123134268472fb526c26163
This commit is contained in:
Trevor Johns
2015-04-28 10:39:50 -07:00
parent 9a2076aef4
commit 03236391f1
43 changed files with 908 additions and 179 deletions

View File

@@ -18,7 +18,7 @@
package="com.example.android.wearable.findphone">
<uses-sdk android:minSdkVersion="18"
android:targetSdkVersion="21" />
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.VIBRATE" />
<application

View File

@@ -0,0 +1,7 @@
<resources>
<string-array name="android_wear_capabilities">
<!-- declaring that phone provides find_me capability -->
<item>find_me</item>
</string-array>
</resources>

View File

@@ -18,7 +18,7 @@
package="com.example.android.wearable.findphone" >
<uses-sdk android:minSdkVersion="20"
android:targetSdkVersion="21" />
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.type.watch" />

View File

@@ -18,25 +18,105 @@ package com.example.android.wearable.findphone;
import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.CapabilityApi;
import com.google.android.gms.wearable.CapabilityInfo;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;
import java.util.List;
import java.util.Set;
/**
* Listens for disconnection from home device.
* Listens for changes in connectivity between this wear device and the phone. More precisely, we
* need to distinguish the case that the wear device and the phone are connected directly from all
* other possible cases. To this end, the phone app has registered itself to provide the "find_me"
* capability and we need to look for connected nodes that provide this capability AND are nearby,
* to exclude a connection through the cloud. The proper way would have been to use the
* {@code onCapabilitiesChanged()} callback but currently that callback cannot discover the case
* where a connection switches from wifi to direct; this shortcoming will be addressed in future
* updates but for now we will use the {@code onConnectedNodes()} callback.
*/
public class DisconnectListenerService extends WearableListenerService {
public class DisconnectListenerService extends WearableListenerService
implements GoogleApiClient.ConnectionCallbacks {
private static final String TAG = "ExampleFindPhoneApp";
private static final int FORGOT_PHONE_NOTIFICATION_ID = 1;
/* the capability that the phone app would provide */
private static final String FIND_ME_CAPABILITY_NAME = "find_me";
private GoogleApiClient mGoogleApiClient;
@Override
public void onPeerDisconnected(com.google.android.gms.wearable.Node peer) {
// Create a "forgot phone" notification when phone connection is broken.
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.build();
}
@Override
public void onConnectedNodes(List<Node> connectedNodes) {
// After we are notified by this callback, we need to query for the nodes that provide the
// "find_me" capability and are directly connected.
if (mGoogleApiClient.isConnected()) {
setOrUpdateNotification();
} else if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
private void setOrUpdateNotification() {
Wearable.CapabilityApi.getCapability(
mGoogleApiClient, FIND_ME_CAPABILITY_NAME,
CapabilityApi.FILTER_REACHABLE).setResultCallback(
new ResultCallback<CapabilityApi.GetCapabilityResult>() {
@Override
public void onResult(CapabilityApi.GetCapabilityResult result) {
if (result.getStatus().isSuccess()) {
updateFindMeCapability(result.getCapability());
} else {
Log.e(TAG,
"setOrUpdateNotification() Failed to get capabilities, "
+ "status: "
+ result.getStatus().getStatusMessage());
}
}
});
}
private void updateFindMeCapability(CapabilityInfo capabilityInfo) {
Set<Node> connectedNodes = capabilityInfo.getNodes();
if (connectedNodes.isEmpty()) {
setupLostConnectivityNotification();
} else {
for (Node node : connectedNodes) {
// we are only considering those nodes that are directly connected
if (node.isNearby()) {
((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
.cancel(FORGOT_PHONE_NOTIFICATION_ID);
}
}
}
}
/**
* Creates a notification to inform user that the connectivity to phone has been lost (possibly
* left the phone behind).
*/
private void setupLostConnectivityNotification() {
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setContentTitle(getString(R.string.left_phone_title))
.setContentText(getString(R.string.left_phone_content))
.setVibrate(new long[] {0, 200}) // Vibrate for 200 milliseconds.
.setVibrate(new long[]{0, 200}) // Vibrate for 200 milliseconds.
.setSmallIcon(R.drawable.ic_launcher)
.setLocalOnly(true)
.setPriority(Notification.PRIORITY_MAX);
@@ -46,10 +126,20 @@ public class DisconnectListenerService extends WearableListenerService {
}
@Override
public void onPeerConnected(com.google.android.gms.wearable.Node peer) {
// Remove the "forgot phone" notification when connection is restored.
((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
.cancel(FORGOT_PHONE_NOTIFICATION_ID);
public void onConnected(Bundle bundle) {
setOrUpdateNotification();
}
@Override
public void onConnectionSuspended(int cause) {
}
@Override
public void onDestroy() {
if (mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting()) {
mGoogleApiClient.disconnect();
}
super.onDestroy();
}
}

View File

@@ -28,7 +28,6 @@ import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
public class FindPhoneActivity extends Activity {
private static final int FIND_PHONE_NOTIFICATION_ID = 2;

View File

@@ -79,8 +79,8 @@ public class FindPhoneService extends IntentService implements GoogleApiClient.C
.getBoolean(FIELD_ALARM_ON, false);
} else {
Log.e(TAG, "Unexpected number of DataItems found.\n"
+ "\tExpected: 1\n"
+ "\tActual: " + result.getCount());
+ "\tExpected: 1\n"
+ "\tActual: " + result.getCount());
}
} else if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "onHandleIntent: failed to get current alarm state");
@@ -90,7 +90,7 @@ public class FindPhoneService extends IntentService implements GoogleApiClient.C
alarmOn = !alarmOn;
// Change notification text based on new value of alarmOn.
String notificationText = alarmOn ? getString(R.string.turn_alarm_off)
: getString(R.string.turn_alarm_on);
: getString(R.string.turn_alarm_on);
FindPhoneActivity.updateNotification(this, notificationText);
}
// Use alarmOn boolean to update the DataItem - phone will respond accordingly
@@ -101,7 +101,7 @@ public class FindPhoneService extends IntentService implements GoogleApiClient.C
.await();
} else {
Log.e(TAG, "Failed to toggle alarm on phone - Client disconnected from Google Play "
+ "Services");
+ "Services");
}
mGoogleApiClient.disconnect();
}