query
parameter.
150 | * @param method Can be {@link com.google.devrel.wcl.connectivity.WearHttpHelper#METHOD_GET}
151 | * or {@link com.google.devrel.wcl.connectivity.WearHttpHelper#METHOD_POST}
152 | * @param query Used for POST requests. The format should be
153 | * param1=value1¶m2=value2&..
154 | * and it value1, value2, ...
should all be URLEncoded by the caller.
155 | *
156 | * @throws IOException
157 | */
158 | private void makeHttpCall(String url, String method, String query, String charset,
159 | String nodeId, String requestId) throws IOException {
160 | URLConnection urlConnection = new URL(url).openConnection();
161 | urlConnection.setRequestProperty("Accept-Charset", charset);
162 | // Note: the following if-clause will not be executed for this particular example
163 | if (WearHttpHelper.METHOD_POST.equals(method)) {
164 | urlConnection.setDoOutput(true);
165 | urlConnection.setRequestProperty("Content-Type",
166 | "application/x-www-form-urlencoded;charset=" + charset);
167 | if (!TextUtils.isEmpty(query)) {
168 | OutputStream output = urlConnection.getOutputStream();
169 | output.write(query.getBytes(charset));
170 | }
171 | }
172 | BufferedReader in = new BufferedReader(
173 | new InputStreamReader(urlConnection.getInputStream()));
174 | String inputLine;
175 | StringBuilder sb = new StringBuilder();
176 | while ((inputLine = in.readLine()) != null) {
177 | sb.append(inputLine);
178 | }
179 | in.close();
180 | int statusCode = ((HttpURLConnection) urlConnection).getResponseCode();
181 | WearManager.getInstance().sendHttpResponse(sb.toString(), statusCode, nodeId, requestId,
182 | mResultCallback);
183 | }
184 |
185 | /**
186 | * Write a message to the display; it can append to the existing message if {@code append} is
187 | * {@code true}.
188 | */
189 | private void writeMessage(final String message, final boolean append) {
190 | mHandler.post(new Runnable() {
191 | @Override
192 | public void run() {
193 | String msg = message;
194 | if (append) {
195 | msg = mMessageView.getText().toString() + "\n" + msg;
196 | }
197 | mMessageView.setText(msg);
198 | }
199 | });
200 | }
201 |
202 | @Override
203 | public void onPause() {
204 | mWearManager.removeWearConsumer(mWearConsumer);
205 | mWearManager.removeCapabilities(Constants.CAPABILITY_HTTP_HANDLER);
206 | super.onPause();
207 | }
208 |
209 | @Override
210 | public void onResume() {
211 | super.onResume();
212 | mWearManager.addWearConsumer(mWearConsumer);
213 | mWearManager.addCapabilities(Constants.CAPABILITY_HTTP_HANDLER);
214 | MobileApplication.setPage(Constants.TARGET_STOCK);
215 | writeMessage("", false);
216 | }
217 | }
218 |
--------------------------------------------------------------------------------
/mobile/src/main/java/com/example/android/wearable/wcldemo/pages/VoiceFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * imitations under the License.
15 | */
16 |
17 | package com.example.android.wearable.wcldemo.pages;
18 |
19 | import android.os.Bundle;
20 | import android.support.annotation.Nullable;
21 | import android.support.v4.app.Fragment;
22 | import android.util.Log;
23 | import android.view.LayoutInflater;
24 | import android.view.View;
25 | import android.view.ViewGroup;
26 | import android.widget.TextView;
27 |
28 | import com.google.android.gms.wearable.Channel;
29 | import com.google.android.gms.wearable.WearableStatusCodes;
30 | import com.google.devrel.wcl.WearManager;
31 | import com.google.devrel.wcl.callbacks.AbstractWearConsumer;
32 | import com.google.devrel.wcl.widgets.recording.WclSoundManager;
33 |
34 | import com.example.android.wearable.wcldemo.MobileApplication;
35 | import com.example.android.wearable.wcldemo.R;
36 | import com.example.android.wearable.wcldemo.common.Constants;
37 |
38 | import java.io.InputStream;
39 |
40 | /**
41 | * The introductory fragment.
42 | */
43 | public class VoiceFragment extends Fragment {
44 |
45 | private static final String TAG = "VoiceFragment";
46 | private WearManager mWearManager;
47 | private AbstractWearConsumer mWearConsumer;
48 | private WclSoundManager mSoundManager;
49 | private TextView mMessageView;
50 |
51 | @Override
52 | public void onCreate(Bundle savedInstanceState) {
53 | super.onCreate(savedInstanceState);
54 | setUpWearListeners();
55 | }
56 |
57 | @Override
58 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
59 | Bundle savedInstanceState) {
60 | View view = inflater.inflate(R.layout.voice_fragment, container, false);
61 | mMessageView = (TextView) view.findViewById(R.id.message);
62 | return view;
63 | }
64 |
65 | /**
66 | * Creates a listener to be called when a channel and an input stream is available to receive
67 | * sound stream.
68 | */
69 | private void setUpWearListeners() {
70 | mWearManager = WearManager.getInstance();
71 | mWearConsumer = new AbstractWearConsumer() {
72 |
73 | @Override
74 | public void onWearableInputStreamForChannelOpened(int statusCode, String requestId,
75 | final Channel channel, final InputStream inputStream) {
76 | if (statusCode != WearableStatusCodes.SUCCESS) {
77 | Log.e(TAG, "onWearableInputStreamForChannelOpened(): "
78 | + "Failed to get input stream");
79 | return;
80 | }
81 | Log.d(TAG, "Channel opened for path: " + channel.getPath());
82 | mMessageView.setText(R.string.voice_stream_started);
83 | mSoundManager = new WclSoundManager(getActivity());
84 | mSoundManager.play(inputStream, new WclSoundManager.OnVoicePlaybackFinishedListener() {
85 | @Override
86 | public void onPlaybackFinished(int reason, @Nullable String reasonMessage) {
87 | Log.d(TAG, "Voice ended with reason: " + reason);
88 | mMessageView.setText(R.string.voice_stream_ended);
89 | }
90 | });
91 | }
92 |
93 | };
94 | }
95 |
96 | @Override
97 | public void onResume() {
98 | super.onResume();
99 | mWearManager.addWearConsumer(mWearConsumer);
100 | mWearManager.addCapabilities(Constants.CAPABILITY_VOICE_PROCESSING);
101 | MobileApplication.setPage(Constants.TARGET_VOICE_STREAM);
102 | }
103 |
104 | @Override
105 | public void onPause() {
106 | mWearManager.removeWearConsumer(mWearConsumer);
107 | mWearManager.removeCapabilities(Constants.CAPABILITY_VOICE_PROCESSING);
108 | if (mSoundManager != null) {
109 | mSoundManager.cleanUp();
110 | }
111 | super.onPause();
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-hdpi/ic_file_download_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-hdpi/ic_file_download_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-hdpi/ic_home_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-hdpi/ic_home_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-hdpi/ic_import_export_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-hdpi/ic_import_export_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-hdpi/ic_mic_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-hdpi/ic_mic_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-hdpi/ic_photo_200dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-hdpi/ic_photo_200dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-hdpi/ic_trending_up_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-hdpi/ic_trending_up_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-hdpi/ic_watch_white_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-hdpi/ic_watch_white_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-mdpi/ic_file_download_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-mdpi/ic_file_download_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-mdpi/ic_home_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-mdpi/ic_home_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-mdpi/ic_import_export_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-mdpi/ic_import_export_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-mdpi/ic_mic_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-mdpi/ic_mic_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-mdpi/ic_photo_200dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-mdpi/ic_photo_200dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-mdpi/ic_trending_up_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-mdpi/ic_trending_up_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-mdpi/ic_watch_white_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-mdpi/ic_watch_white_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xhdpi/ic_file_download_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xhdpi/ic_file_download_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xhdpi/ic_home_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xhdpi/ic_home_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xhdpi/ic_import_export_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xhdpi/ic_import_export_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xhdpi/ic_mic_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xhdpi/ic_mic_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xhdpi/ic_photo_200dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xhdpi/ic_photo_200dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xhdpi/ic_trending_up_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xhdpi/ic_trending_up_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xhdpi/ic_watch_white_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xhdpi/ic_watch_white_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xxhdpi/ic_file_download_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xxhdpi/ic_file_download_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xxhdpi/ic_home_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xxhdpi/ic_home_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xxhdpi/ic_import_export_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xxhdpi/ic_import_export_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xxhdpi/ic_mic_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xxhdpi/ic_mic_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xxhdpi/ic_photo_200dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xxhdpi/ic_photo_200dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xxhdpi/ic_trending_up_grey_200_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xxhdpi/ic_trending_up_grey_200_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xxhdpi/ic_watch_white_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xxhdpi/ic_watch_white_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xxxhdpi/ic_photo_200dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xxxhdpi/ic_photo_200dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable-xxxhdpi/ic_watch_white_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-WclDemoSample/44fe2d183ae066c6a4fc882d37cd09ebd2bba1d2/mobile/src/main/res/drawable-xxxhdpi/ic_watch_white_48dp.png
--------------------------------------------------------------------------------
/mobile/src/main/res/drawable/side_nav_bar.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 | In the first approach (called high-level on the UI), we have a text file that we want to 54 | * transfer across. We use {@link WearFileTransfer#startTransfer()} method to accomplish this. 55 | * 56 | *
The second approach, called low-level, transfers an image from the wear device (as a "raw" 57 | * asset) to the phone and we use {@link WearFileTransfer} apis to open a channel between the two 58 | * devices first and then open an {@link OutputStream} on the wear device and an {@link InputStream} 59 | * on the phone (through the channel). Then we simply read chunks of bytes from the image and write 60 | * to the {@code OutputStream} opened on the wear and read the transferred bytes from the 61 | * {@code InputStream} on the mobile. This approach is more useful for real-time communication to 62 | * transfer bytes as they become available (for example as the microphone on the watch is recording 63 | * a voice message). In this sample, we show a progress bar on the wear as we transfer bytes of the 64 | * image to the phone. 65 | * 66 | *
A lot of the complexity involved in the orchestration of this approach is done in the library
67 | * and is hidden from the developer; developers only request an output stream on one end and will be
68 | * notified when the channel is established and when an output stream is available on the sender
69 | * side, and will receive a similar callback on the other end when an input stream is available.
70 | */
71 | public class FileTransferActivity extends WearableActivity
72 | implements WearFileTransfer.OnChannelTransferProgressListener {
73 |
74 | private static final String TAG = "MainActivity";
75 | private static final int BUFFER_SIZE = 1024;
76 |
77 | // the name of the text file that is in the assets directory and will be transferred across in
78 | // the "high-level" approach
79 | private static final String TEXT_FILE_NAME = "text_file.txt";
80 |
81 | // the resource pointing to the image that we transfer in the "low-level" approach
82 | private static final int IMAGE_RESOURCE_ID = R.raw.android_wear;
83 |
84 | private WearManager mWearManager;
85 | private AbstractWearConsumer mWearConsumer;
86 | private ProgressBar mProgressBar;
87 | private Handler mHandler;
88 |
89 |
90 | @Override
91 | protected void onCreate(Bundle savedInstanceState) {
92 | super.onCreate(savedInstanceState);
93 |
94 | // moving a text file from "assets" directory to the internal app directory so we can get
95 | // a File reference to that for one of the examples below
96 | new Thread(new Runnable() {
97 | @Override
98 | public void run() {
99 | copyFileToPrivateDataIfNeededAndReturn(TEXT_FILE_NAME);
100 | }
101 | }).start();
102 |
103 | mHandler = new Handler();
104 | setContentView(R.layout.file_transfer);
105 | final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
106 | stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
107 | @Override
108 | public void onLayoutInflated(WatchViewStub stub) {
109 | mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
110 | }
111 | });
112 | mWearManager = WearManager.getInstance();
113 |
114 | // we define a listener to inform us of the status of the file transfer
115 | mWearConsumer = new AbstractWearConsumer() {
116 | @Override
117 | public void onWearableSendFileResult(int statusCode, String requestId) {
118 | Log.d(TAG, String.format("Status Code=%d, requestId=%s", statusCode, requestId));
119 | }
120 | };
121 |
122 | setAmbientEnabled();
123 | }
124 |
125 | public void onClick(View view) {
126 | // first we try to find at least one nearby connected node
127 | Set