1 /*
2  * Copyright (C) 2017 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.android.tests.connectivity.uid;
18 
19 import android.app.Activity;
20 import android.app.DownloadManager;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.net.ConnectivityManager;
25 import android.net.NetworkInfo;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.Environment;
29 import android.os.StrictMode;
30 import android.util.Log;
31 import android.webkit.MimeTypeMap;
32 import android.webkit.URLUtil;
33 import java.io.*;
34 import java.net.HttpURLConnection;
35 import java.net.URL;
36 
37 
38 public class ConnectivityTestActivity extends Activity {
39 
40     Context mContext;
41     ConnectivityManager mConnectivityManager;
42     DownloadManager mDownloadManager;
43 
44     public static final String TAG = "ConnectivityUIDTest";
45     private static final String RESULT = "result";
46     private static final String URL = "url";
47 
onCreate(Bundle savedInstanceState)48     public void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         StrictMode.ThreadPolicy policy =
51                 new StrictMode.ThreadPolicy.Builder().permitAll().build();
52         StrictMode.setThreadPolicy(policy);
53 
54         mContext = this.getApplicationContext();
55         mConnectivityManager = (ConnectivityManager)
56                 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
57         mDownloadManager = (DownloadManager)
58                 mContext.getSystemService(Context.DOWNLOAD_SERVICE);
59     }
60 
onResume()61     public void onResume() {
62         super.onResume();
63         boolean conn = checkNow();
64         Intent returnIntent = new Intent();
65         returnIntent.putExtra(RESULT, conn);
66         setResult(RESULT_OK, returnIntent);
67         Bundle extras = getIntent().getExtras();
68         downloadData(extras);
69         finish();
70     }
71 
checkNow()72     private boolean checkNow() {
73         try{
74             NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
75             return netInfo.isConnected() && httpRequest();
76         } catch(Exception e) {
77             Log.e(TAG, "CheckConnectivity exception: ", e);
78         }
79 
80         return false;
81     }
82 
httpRequest()83     private boolean httpRequest() {
84         HttpURLConnection urlConnection = null;
85         try {
86             URL targetURL = new URL("http://www.google.com/generate_204");
87             urlConnection = (HttpURLConnection) targetURL.openConnection();
88             urlConnection.connect();
89             int respCode = urlConnection.getResponseCode();
90             return (respCode == 204);
91         } catch (Exception e) {
92             Log.e(TAG, "Checkconnectivity exception: ", e);
93         }
94         return false;
95     }
96 
downloadData(Bundle extras)97     private void downloadData(Bundle extras) {
98         if(extras == null)
99             return;
100         String url = extras.getString(URL);
101         Log.d(TAG, "URL IS: " + url);
102         DownloadManager.Request request =
103                 new DownloadManager.Request(Uri.parse(url));
104         request.setNotificationVisibility(
105                 DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
106         String name = URLUtil.guessFileName(
107                 url, null, MimeTypeMap.getFileExtensionFromUrl(url));
108         request.setDestinationInExternalPublicDir(
109                 Environment.DIRECTORY_DOWNLOADS, name);
110         mDownloadManager.enqueue(request);
111     }
112 }
113