upload android base code part7

This commit is contained in:
August 2018-08-08 18:09:17 +08:00
parent 4e516ec6ed
commit 841ae54672
25229 changed files with 1709508 additions and 0 deletions

View file

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Google Inc.
~
~ 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.
-->
<manifest package="com.example.android.wearable.wear.wearhighbandwidthnetworking"
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0">
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

View file

@ -0,0 +1,13 @@
page.tags="WearHighBandwidthNetworking"
sample.group=Wearable
@jd:body
<p>
Sample demonstrates how to determine if a high-bandwidth network is available for use cases that
require a minimum network bandwidth, such as streaming media or downloading large files. In
addition, the sample demonstrates best practices for asking a user to add a new Wi-Fi network for
high-bandwidth network operations if the bandwidth of currently available networks is inadequate.
</p>

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" />

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M19.35 10.04C18.67 6.59 15.64 4 12 4c-1.48 0-2.85.43-4.01 1.17l1.46 1.46C10.21 6.23 11.08 6 12 6c3.04 0 5.5 2.46 5.5 5.5v.5H19c1.66 0 3 1.34 3 3 0 1.13-.64 2.11-1.56 2.62l1.45 1.45C23.16 18.16 24 16.68 24 15c0-2.64-2.05-4.78-4.65-4.96zM3 5.27l2.75 2.74C2.56 8.15 0 10.77 0 14c0 3.31 2.69 6 6 6h11.73l2 2L21 20.73 4.27 4 3 5.27zM7.73 10l8 8H6c-2.21 0-4-1.79-4-4s1.79-4 4-4h1.73z" />
</vector>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M19.35,10.04 C18.67,6.59,15.64,4,12,4 C9.11,4,6.6,5.64,5.35,8.04
C2.34,8.36,0,10.91,0,14 C0,17.31,2.69,20,6,20 L19,20 C21.76,20,24,17.76,24,15
C24,12.36,21.95,10.22,19.35,10.04 Z M19,18 L6,18 C3.79,18,2,16.21,2,14
S3.79,10,6,10 L6.71,10 C7.37,7.69,9.48,6,12,6 C15.04,6,17.5,8.46,17.5,11.5
L17.5,12 L19,12 C20.66,12,22,13.34,22,15 S20.66,18,19,18 Z" />
<path
android:strokeColor="#000000"
android:strokeWidth="2"
android:pathData="M6.58994,13.1803 C6.58994,13.1803,8.59173,15.8724,12.011,15.8726
C15.2788,15.8728,17.3696,13.2502,17.3696,13.2502" />
</vector>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M19.35,10.04 C18.67,6.59,15.64,4,12,4 C9.11,4,6.6,5.64,5.35,8.04
C2.34,8.36,0,10.91,0,14 C0,17.31,2.69,20,6,20 L19,20 C21.76,20,24,17.76,24,15
C24,12.36,21.95,10.22,19.35,10.04 Z M19,18 L6,18 C3.79,18,2,16.21,2,14
S3.79,10,6,10 L6.71,10 C7.37,7.69,9.48,6,12,6 C15.04,6,17.5,8.46,17.5,11.5
L17.5,12 L19,12 C20.66,12,22,13.34,22,15 S20.66,18,19,18 Z" />
<path
android:strokeColor="#000000"
android:strokeWidth="2"
android:pathData="M6.58994,15.9661 C6.58994,15.9661,8.56048,12.5553,11.9798,12.5551
C15.2476,12.5549,17.3696,15.8962,17.3696,15.8962" />
</vector>

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#fff"
android:pathData="M15.9 5c-.17 0-.32.09-.41.23l-.07.15-5.18 11.65c-.16.29-.26.61-.26.96 0 1.11.9 2.01 2.01 2.01.96 0 1.77-.68 1.96-1.59l.01-.03L16.4 5.5c0-.28-.22-.5-.5-.5zM1 9l2 2c2.88-2.88 6.79-4.08 10.53-3.62l1.19-2.68C9.89 3.84 4.74 5.27 1 9zm20 2l2-2c-1.64-1.64-3.55-2.82-5.59-3.57l-.53 2.82c1.5.62 2.9 1.53 4.12 2.75zm-4 4l2-2c-.8-.8-1.7-1.42-2.66-1.89l-.55 2.92c.42.27.83.59 1.21.97zM5 13l2 2c1.13-1.13 2.56-1.79 4.03-2l1.28-2.88c-2.63-.08-5.3.87-7.31 2.88z"/>
</vector>

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#fff"
android:pathData="M23.64 7c-.45-.34-4.93-4-11.64-4-1.5 0-2.89.19-4.15.48L18.18 13.8 23.64 7zm-6.6 8.22L3.27 1.44 2 2.72l2.05 2.06C1.91 5.76.59 6.82.36 7l11.63 14.49.01.01.01-.01 3.9-4.86 3.32 3.32 1.27-1.27-3.46-3.46z"/>
</vector>

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#fff"
android:pathData="M3.53 10.95l8.46 10.54.01.01.01-.01 8.46-10.54C20.04 10.62 16.81 8 12 8c-4.81 0-8.04 2.62-8.47 2.95z"/>
</vector>

View file

@ -0,0 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Google Inc.
~
~ 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.example.android.wearable.wear.wearhighbandwidthnetworking.MainActivity">
<RelativeLayout
android:id="@+id/connectivity_indicator"
android:layout_height="56dp"
android:layout_width="match_parent"
android:paddingTop="12dp"
style="@style/connectivity_indicator">
<ImageView
android:id="@+id/connectivity_icon"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_cloud_disconnected"
style="@style/connectivity_indicator_icon" />
<TextView
android:id="@+id/connectivity_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/connectivity_icon"
android:layout_centerHorizontal="true"
android:text="@string/network_disconnected"
style="@style/connectivity_indicator_text" />
</RelativeLayout>
<android.support.wearable.view.BoxInsetLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_box="left|right">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="@string/info_request_network"
android:textAlignment="center" />
<RelativeLayout
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/info_text"
android:focusable="true"
android:onClick="onButtonClick">
<ImageView
android:id="@+id/button_icon"
android:src="@drawable/ic_fast_network"
style="@style/action_button" />
<TextView
android:id="@+id/button_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@id/button_icon"
android:layout_centerVertical="true"
android:text="@string/button_request_network" />
</RelativeLayout>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:indeterminate="true"
android:visibility="gone" />
</RelativeLayout>
</android.support.wearable.view.BoxInsetLayout>
</LinearLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

View file

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Google Inc.
~
~ 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.
-->
<resources>
<!-- Colors for primary UI elements -->
<item name="primary" type="color">@color/indigo</item>
<item name="primary_dark" type="color">@color/indigo_40b</item>
<item name="primary_accent" type="color">@color/indigo_100b</item>
<item name="window_background" type="color">@color/indigo_15b</item>
<item name="text_primary" type="color">#CCBBBBBB</item>
<item name="text_dark" type="color">#CC000000</item>
<item name="white_80a" type="color">#CCFFFFFF</item>
<item name="white_70a" type="color">#B3FFFFFF</item>
<item name="white_60a" type="color">#99FFFFFF</item>
<item name="white_50a" type="color">#80FFFFFF</item>
<!-- Material Design Indigo 500 palette for wearables -->
<item name="indigo" type="color">#FF3F51B5</item>
<item name="indigo_100b" type="color">#FF8093FF</item>
<item name="indigo_65b" type="color">#FF3A4AA6</item>
<item name="indigo_40b" type="color">#FF242E66</item>
<item name="indigo_30b" type="color">#FF000B4D</item>
<item name="indigo_15b" type="color">#FF000626</item>
<!-- Alert overlay colors -->
<item name="connectivity_indicator_text" type="color">#9d9d9d</item>
<item name="connectivity_indicator_bg" type="color">#434343</item>
</resources>

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Google Inc.
~
~ 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.
-->
<resources>
<!-- Generic text dimensions for styles -->
<dimen name="text_large">22sp</dimen>
<dimen name="text_medium">16sp</dimen>
<dimen name="text_small">12sp</dimen>
</resources>

View file

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Google Inc.
~
~ 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.
-->
<resources>
<string name="app_name">Wear High Bandwidth Networking</string>
<string name="network_fast">Network Fast</string>
<string name="network_slow">Network Slow</string>
<string name="network_disconnected">Disconnected</string>
<string name="network_connecting">Connecting</string>
<string name="info_request_network">To use this sample, ensure that you have removed saved Wi-Fi networks and the device is unplugged</string>
<string name="info_release_network">A high-bandwidth network is now available</string>
<string name="info_add_wifi">No high-bandwidth network is available. You must add a Wi-Fi network to continue</string>
<string name="button_request_network">Request High-bandwidth Network</string>
<string name="button_release_network">Release High-bandwidth Network</string>
<string name="button_add_wifi">Add Wi-Fi Network</string>
</resources>

View file

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Google Inc.
~
~ 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.
-->
<resources>
<style name="text_header" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/text_primary</item>
<item name="android:textSize">@dimen/text_large</item>
</style>
<style name="text_normal" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/text_primary</item>
<item name="android:textSize">@dimen/text_medium</item>
</style>
<style name="text_small" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/text_primary</item>
<item name="android:textSize">@dimen/text_small</item>
</style>
<style name="action_button" parent="@android:style/Widget.ActionButton">
<item name="android:background">@drawable/bg_action_button</item>
<item name="android:backgroundTint">@color/primary</item>
<item name="android:tint">@color/white</item>
<item name="android:layout_width">40dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:padding">8dp</item>
<item name="android:paddingStart">8dp</item>
<item name="android:paddingEnd">8dp</item>
<item name="android:scaleType">fitXY</item>
</style>
<style name="connectivity_indicator">
<item name="android:background">@color/connectivity_indicator_bg</item>
</style>
<style name="connectivity_indicator_text">
<item name="android:textColor">@color/connectivity_indicator_text</item>
<item name="android:textSize">14sp</item>
</style>
<style name="connectivity_indicator_icon">
<item name="android:tint">@color/connectivity_indicator_text</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">16dp</item>
</style>
</resources>

View file

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Google Inc.
~
~ 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.
-->
<resources>
<style name="AppTheme" parent="@android:style/Theme.DeviceDefault">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<item name="android:colorAccent">@color/primary_accent</item>
<item name="android:windowBackground">@color/window_background</item>
</style>
</resources>

View file

@ -0,0 +1,340 @@
/*
* Copyright (C) 2016 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
* limitations under the License.
*/
package com.example.android.wearable.wear.wearhighbandwidthnetworking;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
/**
* This sample demonstrates how to determine if a high-bandwidth network is available for use cases
* that require a minimum network bandwidth, such as streaming media or downloading large files.
* In addition, the sample demonstrates best practices for asking a user to add a new Wi-Fi network
* for high-bandwidth network operations, if currently available networks are inadequate.
*/
public class MainActivity extends Activity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
// Intent action for sending the user directly to the add Wi-Fi network activity.
private static final String ACTION_ADD_NETWORK_SETTINGS =
"com.google.android.clockwork.settings.connectivity.wifi.ADD_NETWORK_SETTINGS";
// Message to notify the network request timout handler that too much time has passed.
private static final int MESSAGE_CONNECTIVITY_TIMEOUT = 1;
// How long the app should wait trying to connect to a sufficient high-bandwidth network before
// asking the user to add a new Wi-Fi network.
private static final long NETWORK_CONNECTIVITY_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(10);
// The minimum network bandwidth required by the app for high-bandwidth operations.
private static final int MIN_NETWORK_BANDWIDTH_KBPS = 10000;
private ConnectivityManager mConnectivityManager;
private ConnectivityManager.NetworkCallback mNetworkCallback;
// Handler for dealing with network connection timeouts.
private Handler mHandler;
private ImageView mConnectivityIcon;
private TextView mConnectivityText;
private View mButton;
private ImageView mButtonIcon;
private TextView mButtonText;
private TextView mInfoText;
private View mProgressBar;
// Tags added to the button in the UI to detect what operation the user has requested.
// These are required since the app reuses the button for different states of the app/UI.
// See onButtonClick() for how these tags are used.
static final String TAG_REQUEST_NETWORK = "REQUEST_NETWORK";
static final String TAG_RELEASE_NETWORK = "RELEASE_NETWORK";
static final String TAG_ADD_WIFI = "ADD_WIFI";
// These constants are used by setUiState() to determine what information to display in the UI,
// as this app reuses UI components for the various states of the app, which is dependent on
// the state of the network.
static final int UI_STATE_REQUEST_NETWORK = 1;
static final int UI_STATE_REQUESTING_NETWORK = 2;
static final int UI_STATE_NETWORK_CONNECTED = 3;
static final int UI_STATE_CONNECTION_TIMEOUT = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mConnectivityIcon = (ImageView) findViewById(R.id.connectivity_icon);
mConnectivityText = (TextView) findViewById(R.id.connectivity_text);
mProgressBar = findViewById(R.id.progress_bar);
mButton = findViewById(R.id.button);
mButton.setTag(TAG_REQUEST_NETWORK);
mButtonIcon = (ImageView) findViewById(R.id.button_icon);
mButtonText = (TextView) findViewById(R.id.button_label);
mInfoText = (TextView) findViewById(R.id.info_text);
mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_CONNECTIVITY_TIMEOUT:
Log.d(LOG_TAG, "Network connection timeout");
setUiState(UI_STATE_CONNECTION_TIMEOUT);
unregisterNetworkCallback();
break;
}
}
};
}
@Override
public void onStop() {
releaseHighBandwidthNetwork();
super.onStop();
}
@Override
public void onResume() {
super.onResume();
if (isNetworkHighBandwidth()) {
setUiState(UI_STATE_NETWORK_CONNECTED);
} else {
setUiState(UI_STATE_REQUEST_NETWORK);
}
}
private void unregisterNetworkCallback() {
if (mNetworkCallback != null) {
Log.d(LOG_TAG, "Unregistering network callback");
mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);
mNetworkCallback = null;
}
}
// Determine if there is a high-bandwidth network exists. Checks both the active
// and bound networks. Returns false if no network is available (low or high-bandwidth).
private boolean isNetworkHighBandwidth() {
Network network = mConnectivityManager.getBoundNetworkForProcess();
network = network == null ? mConnectivityManager.getActiveNetwork() : network;
if (network == null) {
return false;
}
// requires android.permission.ACCESS_NETWORK_STATE
int bandwidth = mConnectivityManager
.getNetworkCapabilities(network).getLinkDownstreamBandwidthKbps();
if (bandwidth >= MIN_NETWORK_BANDWIDTH_KBPS) {
return true;
}
return false;
}
private void requestHighBandwidthNetwork() {
// Before requesting a high-bandwidth network, ensure prior requests are invalidated.
unregisterNetworkCallback();
Log.d(LOG_TAG, "Requesting high-bandwidth network");
// Requesting an unmetered network may prevent you from connecting to the cellular
// network on the user's watch or phone; however, unless you explicitly ask for permission
// to a access the user's cellular network, you should request an unmetered network.
NetworkRequest request = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.build();
mNetworkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(final Network network) {
mHandler.removeMessages(MESSAGE_CONNECTIVITY_TIMEOUT);
runOnUiThread(new Runnable() {
@Override
public void run() {
// requires android.permission.INTERNET
if (!mConnectivityManager.bindProcessToNetwork(network)) {
Log.e(LOG_TAG, "ConnectivityManager.bindProcessToNetwork()"
+ " requires android.permission.INTERNET");
setUiState(UI_STATE_REQUEST_NETWORK);
} else {
Log.d(LOG_TAG, "Network available");
setUiState(UI_STATE_NETWORK_CONNECTED);
}
}
});
}
@Override
public void onCapabilitiesChanged(Network network,
NetworkCapabilities networkCapabilities) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(LOG_TAG, "Network capabilities changed");
}
});
}
@Override
public void onLost(Network network) {
Log.d(LOG_TAG, "Network lost");
runOnUiThread(new Runnable() {
@Override
public void run() {
setUiState(UI_STATE_REQUEST_NETWORK);
}
});
}
};
// requires android.permission.CHANGE_NETWORK_STATE
mConnectivityManager.requestNetwork(request, mNetworkCallback);
mHandler.sendMessageDelayed(
mHandler.obtainMessage(MESSAGE_CONNECTIVITY_TIMEOUT),
NETWORK_CONNECTIVITY_TIMEOUT_MS);
}
private void releaseHighBandwidthNetwork() {
mConnectivityManager.bindProcessToNetwork(null);
unregisterNetworkCallback();
}
private void addWifiNetwork() {
// requires android.permission.CHANGE_WIFI_STATE
startActivity(new Intent(ACTION_ADD_NETWORK_SETTINGS));
}
/**
* Click handler for the button in the UI. The view tag is used to determine the specific
* function of the button.
*
* @param view The view that was clicked
*/
public void onButtonClick(View view) {
switch (view.getTag().toString()) {
case TAG_REQUEST_NETWORK:
requestHighBandwidthNetwork();
setUiState(UI_STATE_REQUESTING_NETWORK);
break;
case TAG_RELEASE_NETWORK:
releaseHighBandwidthNetwork();
setUiState(UI_STATE_REQUEST_NETWORK);
break;
case TAG_ADD_WIFI:
addWifiNetwork();
break;
}
}
// Sets the text and icons the connectivity indicator, button, and info text in the app UI,
// which are all reused for the various states of the app and network connectivity. Also,
// will show/hide a progress bar, which is dependent on the state of the network connectivity
// request.
private void setUiState(int uiState) {
switch (uiState) {
case UI_STATE_REQUEST_NETWORK:
if (isNetworkHighBandwidth()) {
mConnectivityIcon.setImageResource(R.drawable.ic_cloud_happy);
mConnectivityText.setText(R.string.network_fast);
} else {
mConnectivityIcon.setImageResource(R.drawable.ic_cloud_sad);
mConnectivityText.setText(R.string.network_slow);
}
mButton.setTag(TAG_REQUEST_NETWORK);
mButtonIcon.setImageResource(R.drawable.ic_fast_network);
mButtonText.setText(R.string.button_request_network);
mInfoText.setText(R.string.info_request_network);
break;
case UI_STATE_REQUESTING_NETWORK:
mConnectivityIcon.setImageResource(R.drawable.ic_cloud_disconnected);
mConnectivityText.setText(R.string.network_connecting);
mProgressBar.setVisibility(View.VISIBLE);
mInfoText.setVisibility(View.GONE);
mButton.setVisibility(View.GONE);
break;
case UI_STATE_NETWORK_CONNECTED:
if (isNetworkHighBandwidth()) {
mConnectivityIcon.setImageResource(R.drawable.ic_cloud_happy);
mConnectivityText.setText(R.string.network_fast);
} else {
mConnectivityIcon.setImageResource(R.drawable.ic_cloud_sad);
mConnectivityText.setText(R.string.network_slow);
}
mProgressBar.setVisibility(View.GONE);
mInfoText.setVisibility(View.VISIBLE);
mButton.setVisibility(View.VISIBLE);
mButton.setTag(TAG_RELEASE_NETWORK);
mButtonIcon.setImageResource(R.drawable.ic_no_network);
mButtonText.setText(R.string.button_release_network);
mInfoText.setText(R.string.info_release_network);
break;
case UI_STATE_CONNECTION_TIMEOUT:
mConnectivityIcon.setImageResource(R.drawable.ic_cloud_disconnected);
mConnectivityText.setText(R.string.network_disconnected);
mProgressBar.setVisibility(View.GONE);
mInfoText.setVisibility(View.VISIBLE);
mButton.setVisibility(View.VISIBLE);
mButton.setTag(TAG_ADD_WIFI);
mButtonIcon.setImageResource(R.drawable.ic_wifi_network);
mButtonText.setText(R.string.button_add_wifi);
mInfoText.setText(R.string.info_add_wifi);
break;
}
}
}