ConnectivityManagerCompat.java revision 5d79c2b6211936177817f2492816c1dba76dac3e
1/* 2 * Copyright (C) 2012 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.support.v4.net; 18 19import static android.net.ConnectivityManager.TYPE_MOBILE; 20import static android.net.ConnectivityManager.TYPE_WIFI; 21 22import android.net.ConnectivityManager; 23import android.net.NetworkInfo; 24import android.os.Build; 25 26/** 27 * Helper for accessing features in {@link ConnectivityManager} introduced after 28 * API level 16 in a backwards compatible fashion. 29 */ 30public class ConnectivityManagerCompat { 31 32 interface ConnectivityManagerCompatImpl { 33 boolean isActiveNetworkMetered(ConnectivityManager cm); 34 } 35 36 static class BaseConnectivityManagerCompatImpl implements ConnectivityManagerCompatImpl { 37 @Override 38 public boolean isActiveNetworkMetered(ConnectivityManager cm) { 39 final NetworkInfo info = cm.getActiveNetworkInfo(); 40 if (info == null) { 41 // err on side of caution 42 return true; 43 } 44 45 final int type = info.getType(); 46 switch (type) { 47 case TYPE_MOBILE: 48 return true; 49 case TYPE_WIFI: 50 return false; 51 default: 52 // err on side of caution 53 return true; 54 } 55 } 56 } 57 58 static class GingerbreadConnectivityManagerCompatImpl implements ConnectivityManagerCompatImpl { 59 @Override 60 public boolean isActiveNetworkMetered(ConnectivityManager cm) { 61 return ConnectivityManagerCompatGingerbread.isActiveNetworkMetered(cm); 62 } 63 } 64 65 static class HoneycombMR2ConnectivityManagerCompatImpl 66 implements ConnectivityManagerCompatImpl { 67 @Override 68 public boolean isActiveNetworkMetered(ConnectivityManager cm) { 69 return ConnectivityManagerCompatHoneycombMR2.isActiveNetworkMetered(cm); 70 } 71 } 72 73 static class JellyBeanConnectivityManagerCompatImpl implements ConnectivityManagerCompatImpl { 74 @Override 75 public boolean isActiveNetworkMetered(ConnectivityManager cm) { 76 return ConnectivityManagerCompatJellyBean.isActiveNetworkMetered(cm); 77 } 78 } 79 80 private static final ConnectivityManagerCompatImpl IMPL; 81 82 static { 83 if (Build.VERSION.SDK_INT >= 16) { 84 IMPL = new JellyBeanConnectivityManagerCompatImpl(); 85 } else if (Build.VERSION.SDK_INT >= 13) { 86 IMPL = new HoneycombMR2ConnectivityManagerCompatImpl(); 87 } else if (Build.VERSION.SDK_INT >= 8) { 88 IMPL = new GingerbreadConnectivityManagerCompatImpl(); 89 } else { 90 IMPL = new BaseConnectivityManagerCompatImpl(); 91 } 92 } 93 94 /** 95 * Returns if the currently active data network is metered. A network is 96 * classified as metered when the user is sensitive to heavy data usage on 97 * that connection. You should check this before doing large data transfers, 98 * and warn the user or delay the operation until another network is 99 * available. 100 */ 101 public boolean isActiveNetworkMetered(ConnectivityManager cm) { 102 return IMPL.isActiveNetworkMetered(cm); 103 } 104} 105