Add browseable samples for Clockwork Beryl

Bug: 17473824
Change-Id: Id4c637733c491ac71bb6e269f55939c082fb0994
This commit is contained in:
Trevor Johns
2014-10-22 20:10:29 -07:00
parent 4a5ce9aedf
commit f420ff0d80
797 changed files with 34843 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
/*
* 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.datalayer;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.data.FreezableUtils;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.MessageEvent;
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.concurrent.TimeUnit;
/**
* Listens to DataItems and Messages from the local node.
*/
public class DataLayerListenerService extends WearableListenerService {
private static final String TAG = "DataLayerListenerServic";
private static final String START_ACTIVITY_PATH = "/start-activity";
private static final String DATA_ITEM_RECEIVED_PATH = "/data-item-received";
public static final String COUNT_PATH = "/count";
public static final String IMAGE_PATH = "/image";
public static final String IMAGE_KEY = "photo";
private static final String COUNT_KEY = "count";
private static final int MAX_LOG_TAG_LENGTH = 23;
GoogleApiClient mGoogleApiClient;
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
LOGD(TAG, "onDataChanged: " + dataEvents);
final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
dataEvents.close();
if(!mGoogleApiClient.isConnected()) {
ConnectionResult connectionResult = mGoogleApiClient
.blockingConnect(30, TimeUnit.SECONDS);
if (!connectionResult.isSuccess()) {
Log.e(TAG, "DataLayerListenerService failed to connect to GoogleApiClient.");
return;
}
}
// Loop through the events and send a message back to the node that created the data item.
for (DataEvent event : events) {
Uri uri = event.getDataItem().getUri();
String path = uri.getPath();
if (COUNT_PATH.equals(path)) {
// Get the node id of the node that created the data item from the host portion of
// the uri.
String nodeId = uri.getHost();
// Set the data of the message to be the bytes of the Uri.
byte[] payload = uri.toString().getBytes();
// Send the rpc
Wearable.MessageApi.sendMessage(mGoogleApiClient, nodeId, DATA_ITEM_RECEIVED_PATH,
payload);
}
}
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
LOGD(TAG, "onMessageReceived: " + messageEvent);
// Check to see if the message is to start an activity
if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
Intent startIntent = new Intent(this, MainActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startIntent);
}
}
@Override
public void onPeerConnected(Node peer) {
LOGD(TAG, "onPeerConnected: " + peer);
}
@Override
public void onPeerDisconnected(Node peer) {
LOGD(TAG, "onPeerDisconnected: " + peer);
}
public static void LOGD(final String tag, String message) {
if (Log.isLoggable(tag, Log.DEBUG)) {
Log.d(tag, message);
}
}
}

View File

@@ -0,0 +1,255 @@
/*
* 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.datalayer;
import static com.example.android.datalayer.DataLayerListenerService.LOGD;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
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.data.FreezableUtils;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageApi;
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.Wearable;
import java.io.InputStream;
import java.util.List;
/**
* Shows events and photo from the Wearable APIs.
*/
public class MainActivity extends Activity implements ConnectionCallbacks,
OnConnectionFailedListener, DataApi.DataListener, MessageApi.MessageListener,
NodeApi.NodeListener {
private static final String TAG = "MainActivity";
private GoogleApiClient mGoogleApiClient;
private ListView mDataItemList;
private TextView mIntroText;
private DataItemAdapter mDataItemListAdapter;
private View mLayout;
private Handler mHandler;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
mHandler = new Handler();
LOGD(TAG, "onCreate");
setContentView(R.layout.main_activity);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mDataItemList = (ListView) findViewById(R.id.dataItem_list);
mIntroText = (TextView) findViewById(R.id.intro);
mLayout = findViewById(R.id.layout);
// Stores data events received by the local broadcaster.
mDataItemListAdapter = new DataItemAdapter(this, android.R.layout.simple_list_item_1);
mDataItemList.setAdapter(mDataItemListAdapter);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
protected void onPause() {
super.onPause();
Wearable.DataApi.removeListener(mGoogleApiClient, this);
Wearable.MessageApi.removeListener(mGoogleApiClient, this);
Wearable.NodeApi.removeListener(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
@Override
public void onConnected(Bundle connectionHint) {
LOGD(TAG, "onConnected(): Successfully connected to Google API client");
Wearable.DataApi.addListener(mGoogleApiClient, this);
Wearable.MessageApi.addListener(mGoogleApiClient, this);
Wearable.NodeApi.addListener(mGoogleApiClient, this);
}
@Override
public void onConnectionSuspended(int cause) {
LOGD(TAG, "onConnectionSuspended(): Connection to Google API client was suspended");
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.e(TAG, "onConnectionFailed(): Failed to connect, with result: " + result);
}
private void generateEvent(final String title, final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mIntroText.setVisibility(View.INVISIBLE);
mDataItemListAdapter.add(new Event(title, text));
}
});
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
LOGD(TAG, "onDataChanged(): " + dataEvents);
final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
dataEvents.close();
for (DataEvent event : events) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
String path = event.getDataItem().getUri().getPath();
if (DataLayerListenerService.IMAGE_PATH.equals(path)) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
Asset photo = dataMapItem.getDataMap()
.getAsset(DataLayerListenerService.IMAGE_KEY);
final Bitmap bitmap = loadBitmapFromAsset(mGoogleApiClient, photo);
mHandler.post(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Setting background image..");
mLayout.setBackground(new BitmapDrawable(getResources(), bitmap));
}
});
} else if (DataLayerListenerService.COUNT_PATH.equals(path)) {
LOGD(TAG, "Data Changed for COUNT_PATH");
generateEvent("DataItem Changed", event.getDataItem().toString());
} else {
LOGD(TAG, "Unrecognized path: " + path);
}
} else if (event.getType() == DataEvent.TYPE_DELETED) {
generateEvent("DataItem Deleted", event.getDataItem().toString());
} else {
generateEvent("Unknown data event type", "Type = " + event.getType());
}
}
}
/**
* Extracts {@link android.graphics.Bitmap} data from the
* {@link com.google.android.gms.wearable.Asset}
*/
private Bitmap loadBitmapFromAsset(GoogleApiClient apiClient, Asset asset) {
if (asset == null) {
throw new IllegalArgumentException("Asset must be non-null");
}
InputStream assetInputStream = Wearable.DataApi.getFdForAsset(
apiClient, asset).await().getInputStream();
if (assetInputStream == null) {
Log.w(TAG, "Requested an unknown Asset.");
return null;
}
return BitmapFactory.decodeStream(assetInputStream);
}
@Override
public void onMessageReceived(MessageEvent event) {
LOGD(TAG, "onMessageReceived: " + event);
generateEvent("Message", event.toString());
}
@Override
public void onPeerConnected(Node node) {
generateEvent("Node Connected", node.getId());
}
@Override
public void onPeerDisconnected(Node node) {
generateEvent("Node Disconnected", node.getId());
}
private static class DataItemAdapter extends ArrayAdapter<Event> {
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;
}
}
}