• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.support.v4.net;
18 
19 import static android.net.ConnectivityManager.TYPE_MOBILE;
20 import static android.net.ConnectivityManager.TYPE_WIFI;
21 
22 import android.content.Intent;
23 import android.net.ConnectivityManager;
24 import android.net.NetworkInfo;
25 import android.os.Build;
26 
27 /**
28  * Helper for accessing features in {@link ConnectivityManager} introduced after
29  * API level 16 in a backwards compatible fashion.
30  */
31 public class ConnectivityManagerCompat {
32 
33     interface ConnectivityManagerCompatImpl {
isActiveNetworkMetered(ConnectivityManager cm)34         boolean isActiveNetworkMetered(ConnectivityManager cm);
35     }
36 
37     static class BaseConnectivityManagerCompatImpl implements ConnectivityManagerCompatImpl {
38         @Override
isActiveNetworkMetered(ConnectivityManager cm)39         public boolean isActiveNetworkMetered(ConnectivityManager cm) {
40             final NetworkInfo info = cm.getActiveNetworkInfo();
41             if (info == null) {
42                 // err on side of caution
43                 return true;
44             }
45 
46             final int type = info.getType();
47             switch (type) {
48                 case TYPE_MOBILE:
49                     return true;
50                 case TYPE_WIFI:
51                     return false;
52                 default:
53                     // err on side of caution
54                     return true;
55             }
56         }
57     }
58 
59     static class GingerbreadConnectivityManagerCompatImpl implements ConnectivityManagerCompatImpl {
60         @Override
isActiveNetworkMetered(ConnectivityManager cm)61         public boolean isActiveNetworkMetered(ConnectivityManager cm) {
62             return ConnectivityManagerCompatGingerbread.isActiveNetworkMetered(cm);
63         }
64     }
65 
66     static class HoneycombMR2ConnectivityManagerCompatImpl
67             implements ConnectivityManagerCompatImpl {
68         @Override
isActiveNetworkMetered(ConnectivityManager cm)69         public boolean isActiveNetworkMetered(ConnectivityManager cm) {
70             return ConnectivityManagerCompatHoneycombMR2.isActiveNetworkMetered(cm);
71         }
72     }
73 
74     static class JellyBeanConnectivityManagerCompatImpl implements ConnectivityManagerCompatImpl {
75         @Override
isActiveNetworkMetered(ConnectivityManager cm)76         public boolean isActiveNetworkMetered(ConnectivityManager cm) {
77             return ConnectivityManagerCompatJellyBean.isActiveNetworkMetered(cm);
78         }
79     }
80 
81     private static final ConnectivityManagerCompatImpl IMPL;
82 
83     static {
84         if (Build.VERSION.SDK_INT >= 16) {
85             IMPL = new JellyBeanConnectivityManagerCompatImpl();
86         } else if (Build.VERSION.SDK_INT >= 13) {
87             IMPL = new HoneycombMR2ConnectivityManagerCompatImpl();
88         } else if (Build.VERSION.SDK_INT >= 8) {
89             IMPL = new GingerbreadConnectivityManagerCompatImpl();
90         } else {
91             IMPL = new BaseConnectivityManagerCompatImpl();
92         }
93     }
94 
95     /**
96      * Returns if the currently active data network is metered. A network is
97      * classified as metered when the user is sensitive to heavy data usage on
98      * that connection. You should check this before doing large data transfers,
99      * and warn the user or delay the operation until another network is
100      * available.
101      */
isActiveNetworkMetered(ConnectivityManager cm)102     public static boolean isActiveNetworkMetered(ConnectivityManager cm) {
103         return IMPL.isActiveNetworkMetered(cm);
104     }
105 
106     /**
107      * Return the {@link NetworkInfo} that caused the given
108      * {@link ConnectivityManager#CONNECTIVITY_ACTION} broadcast. This obtains
109      * the current state from {@link ConnectivityManager} instead of using the
110      * potentially-stale value from
111      * {@link ConnectivityManager#EXTRA_NETWORK_INFO}.
112      */
getNetworkInfoFromBroadcast(ConnectivityManager cm, Intent intent)113     public static NetworkInfo getNetworkInfoFromBroadcast(ConnectivityManager cm, Intent intent) {
114         final NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
115         return cm.getNetworkInfo(info.getType());
116     }
117 }
118