package cn.nec.nlc.example.jamesli.activitytest38network;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class MainActivity extends ActionBarActivity {
    private EditText editTextWebsite;
    private WebView webViewShow;    // API Level 1, substituted by other web application

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextWebsite = (EditText) findViewById(R.id.editTextWebUrl);
        webViewShow = (WebView) findViewById(R.id.webViewShow);

        // To allow network access run in main thread, normally in other thread
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    public void onClickStartSurf(View view) {
        String inputWebUrl = editTextWebsite.getText().toString();
        if ((inputWebUrl == null) || (inputWebUrl.equals(""))) {
            // set a reminder
            Toast.makeText(this, "Please input a web address", Toast.LENGTH_LONG).show();
        } else {
            // further check whether the network is available
            if (isNetworkAvailable()) {
                try {
                    URL url = new URL("http://" + inputWebUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    readStream(connection.getInputStream());
//                    // show in webview as well
//                    webViewShow.loadUrl(String.valueOf(url));
                    // or use other browser
                    Uri uri = Uri.parse(String.valueOf(url));
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(this, "Network is not available", Toast.LENGTH_LONG).show();
            }
        }
    }

    private void readStream(InputStream inputStream) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        // if no network is available networkInfo will be null
        // otherwise check if we are connected
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -762,16 +762,29 @@
 ebsite;%0A
+%3C%3C%3C%3C%3C%3C%3C HEAD%0A
     priv
@@ -863,16 +863,106 @@
 lication
+%0A=======%0A    private WebView webViewShow;%0A%3E%3E%3E%3E%3E%3E%3E 5150dc07e66fafe527dea2c5fa3f81c18fe09170
 %0A%0A    @O

Add minor comment to ActivityTest38/MainActivity.java