Sync sample prebuilts to lmp-mr1-ub-docs
Upstream commit: f76d10f3b6abad250d94b5b9a5c73faae6ce8cc5 Change-Id: I32f2690e36f444cae123134268472fb526c26163
This commit is contained in:
@@ -19,27 +19,33 @@ package com.example.android.wearable.datalayer;
|
||||
import static com.example.android.wearable.datalayer.DataLayerListenerService.LOGD;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.app.Fragment;
|
||||
import android.app.FragmentManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.support.wearable.view.DotsPageIndicator;
|
||||
import android.support.wearable.view.FragmentGridPagerAdapter;
|
||||
import android.support.wearable.view.GridViewPager;
|
||||
import android.text.TextUtils;
|
||||
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 android.widget.Toast;
|
||||
|
||||
import com.example.android.wearable.datalayer.fragments.AssetFragment;
|
||||
import com.example.android.wearable.datalayer.fragments.DataFragment;
|
||||
import com.example.android.wearable.datalayer.fragments.DiscoveryFragment;
|
||||
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.common.data.FreezableUtils;
|
||||
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.DataEvent;
|
||||
import com.google.android.gms.wearable.DataEventBuffer;
|
||||
@@ -51,39 +57,47 @@ import com.google.android.gms.wearable.NodeApi;
|
||||
import com.google.android.gms.wearable.Wearable;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Shows events and photo from the Wearable APIs.
|
||||
* The main activity with a view pager, containing three pages:<p/>
|
||||
* <ul>
|
||||
* <li>
|
||||
* Page 1: shows a list of DataItems received from the phone application
|
||||
* </li>
|
||||
* <li>
|
||||
* Page 2: shows the photo that is sent from the phone application
|
||||
* </li>
|
||||
* <li>
|
||||
* Page 3: includes two buttons to show the connected phone and watch devices
|
||||
* </li>
|
||||
* </ul>
|
||||
*/
|
||||
public class MainActivity extends Activity implements ConnectionCallbacks,
|
||||
OnConnectionFailedListener, DataApi.DataListener, MessageApi.MessageListener,
|
||||
NodeApi.NodeListener {
|
||||
|
||||
private static final String TAG = "MainActivity";
|
||||
private static final String CAPABILITY_1_NAME = "capability_1";
|
||||
private static final String CAPABILITY_2_NAME = "capability_2";
|
||||
|
||||
private GoogleApiClient mGoogleApiClient;
|
||||
private ListView mDataItemList;
|
||||
private TextView mIntroText;
|
||||
private DataItemAdapter mDataItemListAdapter;
|
||||
private View mLayout;
|
||||
private Handler mHandler;
|
||||
private GridViewPager mPager;
|
||||
private DataFragment mDataFragment;
|
||||
private AssetFragment mAssetFragment;
|
||||
|
||||
@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);
|
||||
|
||||
setupViews();
|
||||
mGoogleApiClient = new GoogleApiClient.Builder(this)
|
||||
.addApi(Wearable.API)
|
||||
.addConnectionCallbacks(this)
|
||||
@@ -128,8 +142,7 @@ public class MainActivity extends Activity implements ConnectionCallbacks,
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mIntroText.setVisibility(View.INVISIBLE);
|
||||
mDataItemListAdapter.add(new Event(title, text));
|
||||
mDataFragment.appendItem(title, text);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -151,8 +164,9 @@ public class MainActivity extends Activity implements ConnectionCallbacks,
|
||||
mHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.d(TAG, "Setting background image..");
|
||||
mLayout.setBackground(new BitmapDrawable(getResources(), bitmap));
|
||||
Log.d(TAG, "Setting background image on second page..");
|
||||
moveToPage(1);
|
||||
mAssetFragment.setBackgroundImage(bitmap);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -171,6 +185,70 @@ public class MainActivity extends Activity implements ConnectionCallbacks,
|
||||
}
|
||||
}
|
||||
|
||||
public void onClicked(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.capability_2_btn:
|
||||
showNodes(CAPABILITY_2_NAME);
|
||||
break;
|
||||
case R.id.capabilities_1_and_2_btn:
|
||||
showNodes(CAPABILITY_1_NAME, CAPABILITY_2_NAME);
|
||||
break;
|
||||
default:
|
||||
Log.e(TAG, "Unknown click event registered");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the connected nodes that provide at least one of the given capabilities
|
||||
*/
|
||||
private void showNodes(final String... capabilityNames) {
|
||||
Wearable.CapabilityApi.getAllCapabilities(mGoogleApiClient,
|
||||
CapabilityApi.FILTER_REACHABLE).setResultCallback(
|
||||
|
||||
new ResultCallback<CapabilityApi.GetAllCapabilitiesResult>() {
|
||||
@Override
|
||||
public void onResult(
|
||||
CapabilityApi.GetAllCapabilitiesResult getAllCapabilitiesResult) {
|
||||
if (!getAllCapabilitiesResult.getStatus().isSuccess()) {
|
||||
Log.e(TAG, "Failed to get capabilities");
|
||||
return;
|
||||
}
|
||||
Map<String, CapabilityInfo>
|
||||
capabilitiesMap = getAllCapabilitiesResult.getAllCapabilities();
|
||||
Set<Node> nodes = new HashSet<>();
|
||||
if (capabilitiesMap.isEmpty()) {
|
||||
showDiscoveredNodes(nodes);
|
||||
return;
|
||||
}
|
||||
for (String capabilityName : capabilityNames) {
|
||||
CapabilityInfo capabilityInfo = capabilitiesMap.get(capabilityName);
|
||||
if (capabilityInfo != null) {
|
||||
nodes.addAll(capabilityInfo.getNodes());
|
||||
}
|
||||
}
|
||||
showDiscoveredNodes(nodes);
|
||||
}
|
||||
|
||||
private void showDiscoveredNodes(Set<Node> nodes) {
|
||||
List<String> nodesList = new ArrayList<>();
|
||||
for (Node node : nodes) {
|
||||
nodesList.add(node.getDisplayName());
|
||||
}
|
||||
Log.d(TAG, "Connected Nodes: " + (nodesList.isEmpty()
|
||||
? "No connected device was found for the given capabilities"
|
||||
: TextUtils.join(",", nodesList)));
|
||||
String msg;
|
||||
if (!nodesList.isEmpty()) {
|
||||
msg = getString(R.string.connected_nodes,
|
||||
TextUtils.join(", ", nodesList));
|
||||
} else {
|
||||
msg = getString(R.string.no_device);
|
||||
}
|
||||
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts {@link android.graphics.Bitmap} data from the
|
||||
* {@link com.google.android.gms.wearable.Asset}
|
||||
@@ -206,50 +284,53 @@ public class MainActivity extends Activity implements ConnectionCallbacks,
|
||||
generateEvent("Node Disconnected", node.getId());
|
||||
}
|
||||
|
||||
private static class DataItemAdapter extends ArrayAdapter<Event> {
|
||||
private void setupViews() {
|
||||
mPager = (GridViewPager) findViewById(R.id.pager);
|
||||
mPager.setOffscreenPageCount(2);
|
||||
DotsPageIndicator dotsPageIndicator = (DotsPageIndicator) findViewById(R.id.page_indicator);
|
||||
dotsPageIndicator.setDotSpacing((int) getResources().getDimension(R.dimen.dots_spacing));
|
||||
dotsPageIndicator.setPager(mPager);
|
||||
mDataFragment = new DataFragment();
|
||||
mAssetFragment = new AssetFragment();
|
||||
DiscoveryFragment discoveryFragment = new DiscoveryFragment();
|
||||
List<Fragment> pages = new ArrayList<>();
|
||||
pages.add(mDataFragment);
|
||||
pages.add(mAssetFragment);
|
||||
pages.add(discoveryFragment);
|
||||
final MyPagerAdapter adapter = new MyPagerAdapter(getFragmentManager(), pages);
|
||||
mPager.setAdapter(adapter);
|
||||
}
|
||||
|
||||
private final Context mContext;
|
||||
/**
|
||||
* Switches to the page {@code index}. The first page has index 0.
|
||||
*/
|
||||
private void moveToPage(int index) {
|
||||
mPager.setCurrentItem(0, index, true);
|
||||
}
|
||||
|
||||
public DataItemAdapter(Context context, int unusedResource) {
|
||||
super(context, unusedResource);
|
||||
mContext = context;
|
||||
private class MyPagerAdapter extends FragmentGridPagerAdapter {
|
||||
|
||||
private List<Fragment> mFragments;
|
||||
|
||||
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
|
||||
super(fm);
|
||||
mFragments = fragments;
|
||||
}
|
||||
|
||||
@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;
|
||||
public int getRowCount() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
private class ViewHolder {
|
||||
|
||||
TextView text1;
|
||||
TextView text2;
|
||||
@Override
|
||||
public int getColumnCount(int row) {
|
||||
return mFragments == null ? 0 : mFragments.size();
|
||||
}
|
||||
}
|
||||
|
||||
private class Event {
|
||||
|
||||
String title;
|
||||
String text;
|
||||
|
||||
public Event(String title, String text) {
|
||||
this.title = title;
|
||||
this.text = text;
|
||||
@Override
|
||||
public Fragment getFragment(int row, int column) {
|
||||
return mFragments.get(column);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user