FileDocCategorySizeDatePackage
MobileDataControllerImpl.javaAPI DocAndroid 5.1 API9817Thu Mar 12 22:22:42 GMT 2015com.android.systemui.statusbar.policy

MobileDataControllerImpl

public class MobileDataControllerImpl extends Object implements NetworkController.MobileDataController

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final long
DEFAULT_WARNING_LEVEL
private static final int
FIELDS
private static final StringBuilder
PERIOD_BUILDER
private static final Formatter
PERIOD_FORMATTER
private final android.content.Context
mContext
private final android.telephony.TelephonyManager
mTelephonyManager
private final android.net.ConnectivityManager
mConnectivityManager
private final android.net.INetworkStatsService
mStatsService
private final android.net.NetworkPolicyManager
mPolicyManager
private android.net.INetworkStatsSession
mSession
private Callback
mCallback
private NetworkControllerImpl
mNetworkController
Constructors Summary
public MobileDataControllerImpl(android.content.Context context)


       
        mContext = context;
        mTelephonyManager = TelephonyManager.from(context);
        mConnectivityManager = ConnectivityManager.from(context);
        mStatsService = INetworkStatsService.Stub.asInterface(
                ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
        mPolicyManager = NetworkPolicyManager.from(mContext);
    
Methods Summary
private static android.text.format.TimeaddMonth(android.text.format.Time t, int months)

        final Time rt = new Time(t);
        rt.set(t.monthDay, t.month + months, t.year);
        rt.normalize(false);
        return rt;
    
private android.net.NetworkPolicyfindNetworkPolicy(android.net.NetworkTemplate template)

        if (mPolicyManager == null || template == null) return null;
        final NetworkPolicy[] policies = mPolicyManager.getNetworkPolicies();
        if (policies == null) return null;
        final int N = policies.length;
        for (int i = 0; i < N; i++) {
            final NetworkPolicy policy = policies[i];
            if (policy != null && template.equals(policy.template)) {
                return policy;
            }
        }
        return null;
    
private java.lang.StringformatDateRange(long start, long end)

        final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
        synchronized (PERIOD_BUILDER) {
            PERIOD_BUILDER.setLength(0);
            return DateUtils.formatDateRange(mContext, PERIOD_FORMATTER, start, end, flags, null)
                    .toString();
        }
    
private static java.lang.StringgetActiveSubscriberId(android.content.Context context)

        final TelephonyManager tele = TelephonyManager.from(context);
        final String actualSubscriberId = tele.getSubscriberId(
                SubscriptionManager.getDefaultDataSubId());
        return actualSubscriberId;
    
public DataUsageInfogetDataUsageInfo()

        final String subscriberId = getActiveSubscriberId(mContext);
        if (subscriberId == null) {
            return warn("no subscriber id");
        }
        final INetworkStatsSession session = getSession();
        if (session == null) {
            return warn("no stats session");
        }
        NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
        template = NetworkTemplate.normalize(template, mTelephonyManager.getMergedSubscriberIds());

        final NetworkPolicy policy = findNetworkPolicy(template);
        try {
            final NetworkStatsHistory history = mSession.getHistoryForNetwork(template, FIELDS);
            final long now = System.currentTimeMillis();
            final long start, end;
            if (policy != null && policy.cycleDay > 0) {
                // period = determined from cycleDay
                if (DEBUG) Log.d(TAG, "Cycle day=" + policy.cycleDay + " tz="
                        + policy.cycleTimezone);
                final Time nowTime = new Time(policy.cycleTimezone);
                nowTime.setToNow();
                final Time policyTime = new Time(nowTime);
                policyTime.set(policy.cycleDay, policyTime.month, policyTime.year);
                policyTime.normalize(false);
                if (nowTime.after(policyTime)) {
                    start = policyTime.toMillis(false);
                    end = addMonth(policyTime, 1).toMillis(false);
                } else {
                    start = addMonth(policyTime, -1).toMillis(false);
                    end = policyTime.toMillis(false);
                }
            } else {
                // period = last 4 wks
                end = now;
                start = now - DateUtils.WEEK_IN_MILLIS * 4;
            }
            final long callStart = System.currentTimeMillis();
            final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null);
            final long callEnd = System.currentTimeMillis();
            if (DEBUG) Log.d(TAG, String.format("history call from %s to %s now=%s took %sms: %s",
                    new Date(start), new Date(end), new Date(now), callEnd - callStart,
                    historyEntryToString(entry)));
            if (entry == null) {
                return warn("no entry data");
            }
            final long totalBytes = entry.rxBytes + entry.txBytes;
            final DataUsageInfo usage = new DataUsageInfo();
            usage.usageLevel = totalBytes;
            usage.period = formatDateRange(start, end);
            if (policy != null) {
                usage.limitLevel = policy.limitBytes > 0 ? policy.limitBytes : 0;
                usage.warningLevel = policy.warningBytes > 0 ? policy.warningBytes : 0;
            } else {
                usage.warningLevel = DEFAULT_WARNING_LEVEL;
            }
            if (usage != null) {
                usage.carrier = mNetworkController.getMobileNetworkName();
            }
            return usage;
        } catch (RemoteException e) {
            return warn("remote call failed");
        }
    
private android.net.INetworkStatsSessiongetSession()

        if (mSession == null) {
            try {
                mSession = mStatsService.openSession();
            } catch (RemoteException e) {
                Log.w(TAG, "Failed to open stats session", e);
            } catch (RuntimeException e) {
                Log.w(TAG, "Failed to open stats session", e);
            }
        }
        return mSession;
    
private static java.lang.StringhistoryEntryToString(NetworkStatsHistory.Entry entry)

        return entry == null ? null : new StringBuilder("Entry[")
                .append("bucketDuration=").append(entry.bucketDuration)
                .append(",bucketStart=").append(entry.bucketStart)
                .append(",activeTime=").append(entry.activeTime)
                .append(",rxBytes=").append(entry.rxBytes)
                .append(",rxPackets=").append(entry.rxPackets)
                .append(",txBytes=").append(entry.txBytes)
                .append(",txPackets=").append(entry.txPackets)
                .append(",operations=").append(entry.operations)
                .append(']").toString();
    
public booleanisMobileDataEnabled()

        return mTelephonyManager.getDataEnabled();
    
public booleanisMobileDataSupported()

        // require both supported network and ready SIM
        return mConnectivityManager.isNetworkSupported(TYPE_MOBILE)
                && mTelephonyManager.getSimState() == SIM_STATE_READY;
    
public voidsetCallback(com.android.systemui.statusbar.policy.MobileDataControllerImpl$Callback callback)

        mCallback = callback;
    
public voidsetMobileDataEnabled(boolean enabled)

        Log.d(TAG, "setMobileDataEnabled: enabled=" + enabled);
        mTelephonyManager.setDataEnabled(enabled);
        if (mCallback != null) {
            mCallback.onMobileDataEnabled(enabled);
        }
    
public voidsetNetworkController(NetworkControllerImpl networkController)

        mNetworkController = networkController;
    
private DataUsageInfowarn(java.lang.String msg)

        Log.w(TAG, "Failed to get data usage, " + msg);
        return null;