cn.edu.mju.Thriphoto.net.NetStateManager.java Source code

Java tutorial

Introduction

Here is the source code for cn.edu.mju.Thriphoto.net.NetStateManager.java

Source

/*
 * Copyright (C) 2010-2013 The SINA WEIBO 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 cn.edu.mju.Thriphoto.net;

import org.apache.http.HttpHost;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

/**
 * ??
 * TODOTo be design...
 *
 * @author SINA
 * @since 2013-11-05
 */
public class NetStateManager {
    private static Context mContext;

    public static NetState CUR_NETSTATE = NetState.Mobile;

    public enum NetState {
        Mobile, WIFI, NOWAY
    }

    public class NetStateReceive extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            mContext = context;
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo info = wifiManager.getConnectionInfo();
                if (!wifiManager.isWifiEnabled() || -1 == info.getNetworkId()) {
                    CUR_NETSTATE = NetState.Mobile;
                }
            }
        }
    }

    /**
     * ?? APN  {@link org.apache.http.HttpHost} 
     *
     * @return {@link org.apache.http.HttpHost} 
     */
    public static HttpHost getAPN() {
        HttpHost proxy = null;
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = null;
        if (null != mContext) {
            mCursor = mContext.getContentResolver().query(uri, null, null, null, null);
        }
        if (mCursor != null && mCursor.moveToFirst()) {
            // ???
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                proxy = new HttpHost(proxyStr, 80);
            }
            mCursor.close();
        }
        return proxy;
    }
}