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
17package android.media.cts;
18
19import android.content.Context;
20import android.net.ConnectivityManager;
21import android.net.NetworkInfo;
22import android.net.Uri;
23import android.util.Log;
24
25import java.io.BufferedReader;
26import java.io.InputStreamReader;
27import java.io.IOException;
28import java.net.UnknownHostException;
29import java.util.ArrayList;
30
31/**
32 * A class that implements IConnectionStatus interface
33 * to report and test connection status.
34 */
35public class ConnectionStatus implements IConnectionStatus {
36
37    private static final String TAG = "ConnectionStatus";
38
39    private ConnectivityManager mConnectivityManager;
40
41    public ConnectionStatus(Context context) {
42        mConnectivityManager =
43                (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
44    }
45
46    public String getNotConnectedReason() {
47        NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
48        if (networkInfo != null) {
49            return networkInfo.getReason();
50        } else {
51            return "Network info is not available.";
52        }
53    }
54
55    public boolean isAvailable() {
56        NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
57        return (networkInfo != null) && networkInfo.isAvailable();
58    }
59
60    public boolean isConnected() {
61        NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
62        return (networkInfo != null) && networkInfo.isConnected();
63    }
64
65    /**
66     * Print lines.
67     *
68     * Print lines in ArrayList<String>
69     *
70     * @param lines ArrayList<String>
71     */
72    private void printLines(final ArrayList<String> lines) {
73        for (String line : lines) {
74            Log.d(TAG, line);
75        }
76    }
77
78    /**
79     * Perform ping test.
80     *
81     * @param server Server to ping
82     * @return true for success, false for failure
83     */
84    private boolean pingTest(String server) {
85        final long PING_RETRIES = 10;
86        if (server == null || server.isEmpty()) {
87            Log.e(TAG, "Null or empty server name to ping.");
88            return false;
89        }
90
91        int retries = 0;
92        while (retries++ <= PING_RETRIES) {
93            try {
94                Log.d(TAG, "Try pinging " + server);
95                // -c: ping 5 times, -w: limit to 20 seconds
96                Process p = Runtime.getRuntime().exec("ping -c 5 -w 20 " + server);
97                ArrayList<String> lines = new ArrayList<String>();
98                String line;
99                BufferedReader reader = new BufferedReader(
100                        new InputStreamReader(p.getInputStream()));
101                while ((line = reader.readLine()) != null) {
102                    lines.add(line);
103                }
104                printLines(lines);
105                lines.clear();
106
107                // print error if any
108                reader = new BufferedReader(
109                        new InputStreamReader(p.getErrorStream()));
110                while ((line = reader.readLine()) != null) {
111                    lines.add(line);
112                }
113                printLines(lines);
114                if (p.waitFor() == 0) {
115                    return true;
116                }
117            } catch (UnknownHostException e) {
118                Log.e(TAG, "ping not run: Unknown Host");
119                break;
120            } catch (IOException e) {
121                // e.g. if ping is not on the device
122                Log.e(TAG, "ping not found: IOException");
123                break;
124            } catch (InterruptedException e) {
125                Log.e(TAG, "ping failed: InterruptedException");
126                break;
127            }
128        }
129
130        // ping test timeout
131        return false;
132    }
133
134    public void testConnection(Uri uri) {
135        if (pingTest(uri.getHost())) {
136            Log.d(TAG, "Successfully pinged " + uri.getHost());
137        } else {
138            Log.e(TAG, "Failed to ping " + uri.getHost());
139        }
140    }
141}
142
143