1 /* 2 * Copyright 2013 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.basicnetworking; 18 19 import android.content.Context; 20 import android.net.ConnectivityManager; 21 import android.net.NetworkInfo; 22 import android.os.Bundle; 23 import android.support.v4.app.FragmentActivity; 24 import android.util.TypedValue; 25 import android.view.Menu; 26 import android.view.MenuItem; 27 28 import com.example.android.common.logger.Log; 29 import com.example.android.common.logger.LogFragment; 30 import com.example.android.common.logger.LogWrapper; 31 import com.example.android.common.logger.MessageOnlyLogFilter; 32 33 /** 34 * Sample application demonstrating how to test whether a device is connected, 35 * and if so, whether the connection happens to be wifi or mobile (it could be 36 * something else). 37 * 38 * This sample uses the logging framework to display log output in the log 39 * fragment (LogFragment). 40 */ 41 public class MainActivity extends FragmentActivity { 42 43 public static final String TAG = "Basic Network Demo"; 44 // Whether there is a Wi-Fi connection. 45 private static boolean wifiConnected = false; 46 // Whether there is a mobile connection. 47 private static boolean mobileConnected = false; 48 49 // Reference to the fragment showing events, so we can clear it with a button 50 // as necessary. 51 private LogFragment mLogFragment; 52 53 @Override onCreate(Bundle savedInstanceState)54 protected void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 setContentView(R.layout.sample_main); 57 58 // Initialize text fragment that displays intro text. 59 SimpleTextFragment introFragment = (SimpleTextFragment) 60 getSupportFragmentManager().findFragmentById(R.id.intro_fragment); 61 introFragment.setText(R.string.intro_message); 62 introFragment.getTextView().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16.0f); 63 64 // Initialize the logging framework. 65 initializeLogging(); 66 } 67 68 @Override onCreateOptionsMenu(Menu menu)69 public boolean onCreateOptionsMenu(Menu menu) { 70 getMenuInflater().inflate(R.menu.main, menu); 71 return true; 72 } 73 74 @Override onOptionsItemSelected(MenuItem item)75 public boolean onOptionsItemSelected(MenuItem item) { 76 switch (item.getItemId()) { 77 // When the user clicks TEST, display the connection status. 78 case R.id.test_action: 79 checkNetworkConnection(); 80 return true; 81 // Clear the log view fragment. 82 case R.id.clear_action: 83 mLogFragment.getLogView().setText(""); 84 return true; 85 } 86 return false; 87 } 88 89 /** 90 * Check whether the device is connected, and if so, whether the connection 91 * is wifi or mobile (it could be something else). 92 */ checkNetworkConnection()93 private void checkNetworkConnection() { 94 // BEGIN_INCLUDE(connect) 95 ConnectivityManager connMgr = 96 (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 97 NetworkInfo activeInfo = connMgr.getActiveNetworkInfo(); 98 if (activeInfo != null && activeInfo.isConnected()) { 99 wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI; 100 mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE; 101 if(wifiConnected) { 102 Log.i(TAG, getString(R.string.wifi_connection)); 103 } else if (mobileConnected){ 104 Log.i(TAG, getString(R.string.mobile_connection)); 105 } 106 } else { 107 Log.i(TAG, getString(R.string.no_wifi_or_mobile)); 108 } 109 // END_INCLUDE(connect) 110 } 111 112 /** Create a chain of targets that will receive log data */ initializeLogging()113 public void initializeLogging() { 114 115 // Using Log, front-end to the logging chain, emulates 116 // android.util.log method signatures. 117 118 // Wraps Android's native log framework 119 LogWrapper logWrapper = new LogWrapper(); 120 Log.setLogNode(logWrapper); 121 122 // A filter that strips out everything except the message text. 123 MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter(); 124 logWrapper.setNext(msgFilter); 125 126 // On screen logging via a fragment with a TextView. 127 mLogFragment = 128 (LogFragment) getSupportFragmentManager().findFragmentById(R.id.log_fragment); 129 msgFilter.setNext(mLogFragment.getLogView()); 130 } 131 } 132