/* * Copyright (C) 2015 Google Inc. All Rights Reserved. * * 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 * imitations under the License. */ package com.example.android.wearable.wcldemo.pages; import android.os.Bundle; import android.os.Handler; import android.support.wearable.activity.WearableActivity; import android.support.wearable.view.WatchViewStub; import android.util.Log; import android.view.View; import android.widget.ProgressBar; import android.widget.Toast; import com.google.android.gms.wearable.Channel; import com.google.android.gms.wearable.Node; import com.google.android.gms.wearable.WearableStatusCodes; import com.google.devrel.wcl.WearManager; import com.google.devrel.wcl.callbacks.AbstractWearConsumer; import com.google.devrel.wcl.connectivity.WearFileTransfer; import com.google.devrel.wcl.filters.NearbyFilter; import com.google.devrel.wcl.filters.SingleNodeFilter; import com.example.android.wearable.wcldemo.R; import com.example.android.wearable.wcldemo.WearApplication; import com.example.android.wearable.wcldemo.common.Constants; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Set; /** * An activity that shows two different approaches to transferring files between wear nodes. * *
In the first approach (called high-level on the UI), we have a text file that we want to * transfer across. We use {@link WearFileTransfer#startTransfer()} method to accomplish this. * *
The second approach, called low-level, transfers an image from the wear device (as a "raw" * asset) to the phone and we use {@link WearFileTransfer} apis to open a channel between the two * devices first and then open an {@link OutputStream} on the wear device and an {@link InputStream} * on the phone (through the channel). Then we simply read chunks of bytes from the image and write * to the {@code OutputStream} opened on the wear and read the transferred bytes from the * {@code InputStream} on the mobile. This approach is more useful for real-time communication to * transfer bytes as they become available (for example as the microphone on the watch is recording * a voice message). In this sample, we show a progress bar on the wear as we transfer bytes of the * image to the phone. * *
A lot of the complexity involved in the orchestration of this approach is done in the library
* and is hidden from the developer; developers only request an output stream on one end and will be
* notified when the channel is established and when an output stream is available on the sender
* side, and will receive a similar callback on the other end when an input stream is available.
*/
public class FileTransferActivity extends WearableActivity
implements WearFileTransfer.OnChannelTransferProgressListener {
private static final String TAG = "MainActivity";
private static final int BUFFER_SIZE = 1024;
// the name of the text file that is in the assets directory and will be transferred across in
// the "high-level" approach
private static final String TEXT_FILE_NAME = "text_file.txt";
// the resource pointing to the image that we transfer in the "low-level" approach
private static final int IMAGE_RESOURCE_ID = R.raw.android_wear;
private WearManager mWearManager;
private AbstractWearConsumer mWearConsumer;
private ProgressBar mProgressBar;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// moving a text file from "assets" directory to the internal app directory so we can get
// a File reference to that for one of the examples below
new Thread(new Runnable() {
@Override
public void run() {
copyFileToPrivateDataIfNeededAndReturn(TEXT_FILE_NAME);
}
}).start();
mHandler = new Handler();
setContentView(R.layout.file_transfer);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
}
});
mWearManager = WearManager.getInstance();
// we define a listener to inform us of the status of the file transfer
mWearConsumer = new AbstractWearConsumer() {
@Override
public void onWearableSendFileResult(int statusCode, String requestId) {
Log.d(TAG, String.format("Status Code=%d, requestId=%s", statusCode, requestId));
}
};
setAmbientEnabled();
}
public void onClick(View view) {
// first we try to find at least one nearby connected node
Set