1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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  * limitations under the License.
15  */
16 
17 package com.example.android.lce;
18 
19 import android.annotation.TargetApi;
20 import android.app.Activity;
21 import android.net.ConnectivityManager;
22 import android.net.Network;
23 import android.net.NetworkCapabilities;
24 import android.net.NetworkRequest;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.os.Message;
28 import android.text.method.ScrollingMovementMethod;
29 import android.view.View;
30 import android.view.Menu;
31 import android.view.MenuItem;
32 import android.widget.Button;
33 import android.widget.TextView;
34 
35 public class LceDemoActivity extends Activity {
36     private ConnectivityManager connectityManager;
37 
38     @Override
39     @TargetApi(23)
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42 
43         // Default toolbar action handler.
44         setContentView(R.layout.activity_lce_demo);
45         final TextView text = (TextView) findViewById(R.id.lceText);
46         text.setMovementMethod(new ScrollingMovementMethod());
47 
48         // Get an instance of ConnectivityManager.
49         connectityManager = (ConnectivityManager)getSystemService(
50             getApplicationContext().CONNECTIVITY_SERVICE);
51 
52         // Create a handler to update text on the screen.
53         final Handler handler = new Handler() {
54             @Override
55             public void handleMessage(Message msg) {
56                 text.append((String)msg.obj);
57                 super.handleMessage(msg);
58             }
59         };
60 
61         // Now register the network call back function.
62         NetworkRequest.Builder builder = new NetworkRequest.Builder();
63         connectityManager.registerNetworkCallback(builder.build(),
64             new ConnectivityManager.NetworkCallback() {
65             @Override
66             public void onCapabilitiesChanged(Network network, NetworkCapabilities cap) {
67                 Message message = handler.obtainMessage();
68                 message.obj = "New downlink capacity received: " +
69                     cap.getLinkDownstreamBandwidthKbps() + " kbps on network: "
70                     + network.toString() + "\n";
71                 handler.sendMessage(message);
72             }
73         });
74 
75         // Regsiter button action callback function, which pulls the LCE service
76         // for bandwidth update.
77         final Button button = (Button) findViewById(R.id.lceButton);
78         button.setOnClickListener(new View.OnClickListener() {
79             public void onClick(View v) {
80                 for(Network network : connectityManager.getAllNetworks()) {
81                     // No-op for network interface not supporting LCE.
82                     connectityManager.requestBandwidthUpdate(network);
83                 }
84             }
85         });
86     }
87 }
88