release-request-a0f7506a-1cd6-4a03-927b-8d29e0c545e6-for-git_oc-mr1-release-4277764 snap-temp-L23600000093606242

Change-Id: I691ff8b6cc9f8f732ff1eaea5878731d55ba8625
diff --git a/PMC/AndroidManifest.xml b/PMC/AndroidManifest.xml
index 127e11d..d8485b9 100644
--- a/PMC/AndroidManifest.xml
+++ b/PMC/AndroidManifest.xml
@@ -1,4 +1,20 @@
 <?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2017 The Android Open Source Project
+
+  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 xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.android.pmc"
     android:versionCode="3"
@@ -19,6 +35,12 @@
     <uses-permission android:name="android.permission.BLUETOOTH" />
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
+    <uses-permission android:name="android.permission.CONNECTIVITY_INTERNAL" />
+    <uses-permission android:name="android.permission.MANAGE_USB" />
+    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
+    <uses-permission android:name="android.permission.TETHER_PRIVILEGED" />
 
     <application
         android:allowBackup="true"
diff --git a/PMC/res/layout/activity_linear.xml b/PMC/res/layout/activity_linear.xml
index 6461b4d..0df847b 100644
--- a/PMC/res/layout/activity_linear.xml
+++ b/PMC/res/layout/activity_linear.xml
@@ -1,4 +1,19 @@
 <?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2017 The Android Open Source Project
+
+  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.
+-->
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
@@ -66,6 +81,12 @@
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/iperf_client" />
+
+        <RadioButton
+            android:id="@+id/rb_usb_tethering"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/usb_tethering" />
     </RadioGroup>
 
     <Button
diff --git a/PMC/res/values/strings.xml b/PMC/res/values/strings.xml
index 2d37c06..1114856 100644
--- a/PMC/res/values/strings.xml
+++ b/PMC/res/values/strings.xml
@@ -1,4 +1,19 @@
 <?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2017 The Android Open Source Project
+
+  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">PMC</string>
@@ -23,5 +38,6 @@
     <string name="interval">Download/Scan Interval (seconds)</string>
     <string name="iperf_bandwidth">Iperf Bandwidth</string>
     <string name="iperf_logfile">Iperf Logfile</string>
+    <string name="usb_tethering">USB Tethering</string>
 
 </resources>
diff --git a/PMC/src/com/android/pmc/PMCMainActivity.java b/PMC/src/com/android/pmc/PMCMainActivity.java
index d134bdc..8f59ef2 100644
--- a/PMC/src/com/android/pmc/PMCMainActivity.java
+++ b/PMC/src/com/android/pmc/PMCMainActivity.java
@@ -23,6 +23,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.net.ConnectivityManager;
 import android.net.wifi.WifiScanner;
 import android.net.wifi.WifiScanner.ChannelSpec;
 import android.net.wifi.WifiScanner.ScanSettings;
@@ -68,6 +69,7 @@
     private WifiGScanReceiver mGScanR = null;
     private WifiDownloadReceiver mDR = null;
     private IperfClient mIperfClient = null;
+    private boolean mTethered = false;
     private RadioGroup mRadioGroup;
     private Button mBtnStart;
     private Button mBtnStop;
@@ -77,13 +79,23 @@
     private A2dpReceiver mA2dpReceiver;
     private AlarmManager mAlarmManager;
     private PowerManager.WakeLock mWakeLock;
+    private ConnectivityManager mConnManager;
+    private int mProvisionCheckSleep = 1250;
 
+    class OnStartTetheringCallback extends ConnectivityManager.OnStartTetheringCallback {
+        @Override
+        public void onTetheringStarted() {
+            mTethered = true;
+        }
+    }
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         //Initiate wifi service manger
         mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
+        mConnManager = (ConnectivityManager)
+                this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
         mPIGScan = PendingIntent.getBroadcast(this, 0, new Intent(sGScanAction), 0);
         mPIDownload = PendingIntent.getBroadcast(this, 0, new Intent(sDownloadAction), 0);
         mPIConnScan = PendingIntent.getBroadcast(this, 0, new Intent(sConnScanAction), 0);
@@ -145,6 +157,9 @@
                     case R.id.rb_iperf_client:
                         startIperfClient();
                         break;
+                    case R.id.rb_usb_tethering:
+                        startUSBTethering();
+                        break;
                     default:
                         return;
                 }
@@ -158,6 +173,7 @@
                 stopDownloadFile();
                 stopGScan();
                 stopIperfClient();
+                stopUSBTethering();
                 mBtnStart.setEnabled(true);
             }
         });
@@ -286,6 +302,32 @@
         }
     }
 
+    private void startUSBTethering() {
+        OnStartTetheringCallback tetherCallback = new OnStartTetheringCallback();
+        mConnManager.startTethering(ConnectivityManager.TETHERING_USB, true, tetherCallback);
+        // sleep until provisioning check for tethering is done
+        try {
+            Thread.sleep(mProvisionCheckSleep);
+        } catch (InterruptedException e) {
+            Log.d(TAG, "Sleep exception after enabling USB tethering");
+        }
+        if (mTethered) {
+            mBtnStart.setEnabled(false);
+            mRadioGroup.setFocusable(false);
+            mTextView.setText("Started usb tethering");
+        }
+    }
+
+    private void stopUSBTethering() {
+        if (mTethered) {
+            mConnManager.stopTethering(ConnectivityManager.TETHERING_USB);
+            mTethered = false;
+            mBtnStart.setEnabled(true);
+            mRadioGroup.setFocusable(true);
+            mTextView.setText("Stopped usb tethering");
+        }
+    }
+
     private void turnScreenOn(Context context) {
         if (mWakeLock == null) {
             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
@@ -395,6 +437,10 @@
                             startIperfClient();
                         } else if (actionstring.equalsIgnoreCase("StopIperfClient")) {
                             stopIperfClient();
+                        } else if (actionstring.equalsIgnoreCase("StartUSBTethering")) {
+                            startUSBTethering();
+                        } else if (actionstring.equalsIgnoreCase("StopUSBTethering")) {
+                            stopUSBTethering();
                         } else if (actionstring.equalsIgnoreCase("TurnScreenOn")) {
                             turnScreenOn(context);
                         } else if (actionstring.equalsIgnoreCase("TurnScreenOff")) {
diff --git a/UID/Android.mk b/UID/Android.mk
new file mode 100644
index 0000000..19851f6
--- /dev/null
+++ b/UID/Android.mk
@@ -0,0 +1,30 @@
+#
+##  Copyright (C) 2017 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_PACKAGE_NAME := ConnectivityUIDTest
+LOCAL_CERTIFICATE := platform
+LOCAL_DEX_PREOPT := false
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
+
+LOCAL_PRIVILEGED_MODULE := true
+LOCAL_PROGUARD_ENABLED := disabled
+
+include $(BUILD_PACKAGE)
diff --git a/UID/AndroidManifest.xml b/UID/AndroidManifest.xml
new file mode 100644
index 0000000..0baa509
--- /dev/null
+++ b/UID/AndroidManifest.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2017 The Android Open Source Project
+
+  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 xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.tests.connectivity.uid"
+    android:versionCode="3"
+    android:versionName="3.0" >
+
+    <uses-sdk
+        android:minSdkVersion="19"
+        android:targetSdkVersion="26" />
+
+    <uses-permission android:name="android.permission.INTERNET" />
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+
+    <application
+        android:allowBackup="true"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme" >
+        <!-- Dummy activity -->
+        <activity
+            android:name="com.android.tests.connectivity.uid.ConnectivityTestActivity"
+            android:configChanges="keyboardHidden|orientation|screenSize"
+            android:label="@string/app_name" >
+        </activity>
+    </application>
+
+</manifest>
diff --git a/UID/res/values/strings.xml b/UID/res/values/strings.xml
new file mode 100644
index 0000000..9f0cf32
--- /dev/null
+++ b/UID/res/values/strings.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2017 The Android Open Source Project
+
+  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">ConnectivityUIDTest</string>
+    <string name="settings">Settings</string>
+
+</resources>
diff --git a/UID/res/values/styles.xml b/UID/res/values/styles.xml
new file mode 100644
index 0000000..2847191
--- /dev/null
+++ b/UID/res/values/styles.xml
@@ -0,0 +1,33 @@
+<!--
+  Copyright 2017 The Android Open Source Project
+
+  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>
+
+    <!--
+        Base application theme for API 14+. This theme completely replaces
+        AppBaseTheme from BOTH res/values/styles.xml and
+        res/values-v11/styles.xml on API 14+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
+        <!-- API 14 theme customizations can go here. -->
+    </style>
+
+     <!-- Application theme. -->
+    <style name="AppTheme" parent="AppBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+    </style>
+
+
+</resources>
diff --git a/UID/src/com/android/tests/connectivity/uid/ConnectivityTestActivity.java b/UID/src/com/android/tests/connectivity/uid/ConnectivityTestActivity.java
new file mode 100644
index 0000000..49c6c14
--- /dev/null
+++ b/UID/src/com/android/tests/connectivity/uid/ConnectivityTestActivity.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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.android.tests.connectivity.uid;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.os.Bundle;
+import android.os.StrictMode;
+import android.util.Log;
+import java.io.*;
+import java.lang.Thread;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+public class ConnectivityTestActivity extends Activity {
+
+    ConnectivityManager connectivityManager;
+    NetworkInfo netInfo;
+    public static final String TAG = "ConnectivityUIDTest";
+    private static final String RESULT = "result";
+
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        StrictMode.ThreadPolicy policy =
+                new StrictMode.ThreadPolicy.Builder().permitAll().build();
+        StrictMode.setThreadPolicy(policy);
+    }
+
+    public void onResume() {
+        super.onResume();
+        boolean conn = checkNow(this.getApplicationContext());
+        Intent returnIntent = new Intent();
+        returnIntent.putExtra(RESULT, conn);
+        setResult(RESULT_OK, returnIntent);
+        finish();
+    }
+
+    public boolean checkNow(Context con) {
+        try{
+            connectivityManager = (ConnectivityManager)
+                    con.getSystemService(Context.CONNECTIVITY_SERVICE);
+            netInfo = connectivityManager.getActiveNetworkInfo();
+            return netInfo.isConnected() && httpRequest();
+        } catch(Exception e) {
+            Log.e(TAG, "CheckConnectivity exception: ", e);
+        }
+
+        return false;
+    }
+
+    private boolean httpRequest() throws IOException {
+        URL targetURL = new URL("http://www.google.com/generate_204");
+        HttpURLConnection urlConnection = null;
+        try {
+            urlConnection = (HttpURLConnection) targetURL.openConnection();
+            urlConnection.connect();
+            int respCode = urlConnection.getResponseCode();
+            return (respCode == 204);
+        } catch (IOException e) {
+            Log.e(TAG, "Checkconnectivity exception: ", e);
+        }
+        return false;
+    }
+}