Search in sources :

Example 6 with NetworkTemplate

use of android.net.NetworkTemplate in project platform_frameworks_base by android.

the class NetworkStatsManager method querySummary.

/**
     * Query network usage statistics summaries. Result filtered to include only uids belonging to
     * calling user. Result is aggregated over time, hence all buckets will have the same start and
     * end timestamps. State is going to be {@link NetworkStats.Bucket#STATE_ALL},
     * uid {@link NetworkStats.Bucket#UID_ALL}, tag {@link NetworkStats.Bucket#TAG_NONE},
     * metered {@link NetworkStats.Bucket#METERED_ALL}, and roaming
     * {@link NetworkStats.Bucket#ROAMING_ALL}.
     *
     * @param networkType As defined in {@link ConnectivityManager}, e.g.
     *            {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
     *            etc.
     * @param subscriberId If applicable, the subscriber id of the network interface.
     * @param startTime Start of period. Defined in terms of "Unix time", see
     *            {@link java.lang.System#currentTimeMillis}.
     * @param endTime End of period. Defined in terms of "Unix time", see
     *            {@link java.lang.System#currentTimeMillis}.
     * @return Statistics object or null if permissions are insufficient or error happened during
     *         statistics collection.
     */
public NetworkStats querySummary(int networkType, String subscriberId, long startTime, long endTime) throws SecurityException, RemoteException {
    NetworkTemplate template;
    try {
        template = createTemplate(networkType, subscriberId);
    } catch (IllegalArgumentException e) {
        if (DBG)
            Log.e(TAG, "Cannot create template", e);
        return null;
    }
    NetworkStats result;
    result = new NetworkStats(mContext, template, startTime, endTime);
    result.startSummaryEnumeration();
    return result;
}
Also used : NetworkTemplate(android.net.NetworkTemplate)

Example 7 with NetworkTemplate

use of android.net.NetworkTemplate in project platform_frameworks_base by android.

the class NetworkStatsManager method queryDetailsForUidTag.

/**
     * Query network usage statistics details for a given uid and tag. Only usable for uids
     * belonging to calling user. Result is aggregated over state but not aggregated over time.
     * This means buckets' start and end timestamps are going to be between 'startTime' and
     * 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid the
     * same as the 'uid' parameter and tag the same as 'tag' parameter.
     * <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't
     * interpolate across partial buckets. Since bucket length is in the order of hours, this
     * method cannot be used to measure data usage on a fine grained time scale.
     *
     * @param networkType As defined in {@link ConnectivityManager}, e.g.
     *            {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
     *            etc.
     * @param subscriberId If applicable, the subscriber id of the network interface.
     * @param startTime Start of period. Defined in terms of "Unix time", see
     *            {@link java.lang.System#currentTimeMillis}.
     * @param endTime End of period. Defined in terms of "Unix time", see
     *            {@link java.lang.System#currentTimeMillis}.
     * @param uid UID of app
     * @param tag TAG of interest. Use {@link NetworkStats.Bucket#TAG_NONE} for no tags.
     * @return Statistics object or null if an error happened during statistics collection.
     * @throws SecurityException if permissions are insufficient to read network statistics.
     */
public NetworkStats queryDetailsForUidTag(int networkType, String subscriberId, long startTime, long endTime, int uid, int tag) throws SecurityException {
    NetworkTemplate template;
    template = createTemplate(networkType, subscriberId);
    NetworkStats result;
    try {
        result = new NetworkStats(mContext, template, startTime, endTime);
        result.startHistoryEnumeration(uid, tag);
    } catch (RemoteException e) {
        Log.e(TAG, "Error while querying stats for uid=" + uid + " tag=" + tag, e);
        return null;
    }
    return result;
}
Also used : NetworkTemplate(android.net.NetworkTemplate) RemoteException(android.os.RemoteException)

Example 8 with NetworkTemplate

use of android.net.NetworkTemplate in project platform_frameworks_base by android.

the class NetworkStatsManager method querySummaryForDevice.

/**
     * Query network usage statistics summaries. Result is summarised data usage for the whole
     * device. Result is a single Bucket aggregated over time, state, uid, tag, metered, and
     * roaming. This means the bucket's start and end timestamp are going to be the same as the
     * 'startTime' and 'endTime' parameters. State is going to be
     * {@link NetworkStats.Bucket#STATE_ALL}, uid {@link NetworkStats.Bucket#UID_ALL},
     * tag {@link NetworkStats.Bucket#TAG_NONE}, metered {@link NetworkStats.Bucket#METERED_ALL},
     * and roaming {@link NetworkStats.Bucket#ROAMING_ALL}.
     *
     * @param networkType As defined in {@link ConnectivityManager}, e.g.
     *            {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
     *            etc.
     * @param subscriberId If applicable, the subscriber id of the network interface.
     * @param startTime Start of period. Defined in terms of "Unix time", see
     *            {@link java.lang.System#currentTimeMillis}.
     * @param endTime End of period. Defined in terms of "Unix time", see
     *            {@link java.lang.System#currentTimeMillis}.
     * @return Bucket object or null if permissions are insufficient or error happened during
     *         statistics collection.
     */
public Bucket querySummaryForDevice(int networkType, String subscriberId, long startTime, long endTime) throws SecurityException, RemoteException {
    NetworkTemplate template;
    try {
        template = createTemplate(networkType, subscriberId);
    } catch (IllegalArgumentException e) {
        if (DBG)
            Log.e(TAG, "Cannot create template", e);
        return null;
    }
    Bucket bucket = null;
    NetworkStats stats = new NetworkStats(mContext, template, startTime, endTime);
    bucket = stats.getDeviceSummaryForNetwork();
    stats.close();
    return bucket;
}
Also used : NetworkTemplate(android.net.NetworkTemplate) Bucket(android.app.usage.NetworkStats.Bucket)

Example 9 with NetworkTemplate

use of android.net.NetworkTemplate in project platform_frameworks_base by android.

the class NetworkStatsManager method registerUsageCallback.

/**
     * Registers to receive notifications about data usage on specified networks.
     *
     * <p>The callbacks will continue to be called as long as the process is live or
     * {@link #unregisterUsageCallback} is called.
     *
     * @param networkType Type of network to monitor. Either
                  {@link ConnectivityManager#TYPE_MOBILE} or {@link ConnectivityManager#TYPE_WIFI}.
     * @param subscriberId If applicable, the subscriber id of the network interface.
     * @param thresholdBytes Threshold in bytes to be notified on.
     * @param callback The {@link UsageCallback} that the system will call when data usage
     *            has exceeded the specified threshold.
     * @param handler to dispatch callback events through, otherwise if {@code null} it uses
     *            the calling thread.
     */
public void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes, UsageCallback callback, @Nullable Handler handler) {
    checkNotNull(callback, "UsageCallback cannot be null");
    final Looper looper;
    if (handler == null) {
        looper = Looper.myLooper();
    } else {
        looper = handler.getLooper();
    }
    if (DBG) {
        Log.d(TAG, "registerUsageCallback called with: {" + " networkType=" + networkType + " subscriberId=" + subscriberId + " thresholdBytes=" + thresholdBytes + " }");
    }
    NetworkTemplate template = createTemplate(networkType, subscriberId);
    DataUsageRequest request = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, template, thresholdBytes);
    try {
        CallbackHandler callbackHandler = new CallbackHandler(looper, networkType, subscriberId, callback);
        callback.request = mService.registerUsageCallback(mContext.getOpPackageName(), request, new Messenger(callbackHandler), new Binder());
        if (DBG)
            Log.d(TAG, "registerUsageCallback returned " + callback.request);
        if (callback.request == null) {
            Log.e(TAG, "Request from callback is null; should not happen");
        }
    } catch (RemoteException e) {
        if (DBG)
            Log.d(TAG, "Remote exception when registering callback");
        throw e.rethrowFromSystemServer();
    }
}
Also used : Looper(android.os.Looper) DataUsageRequest(android.net.DataUsageRequest) Binder(android.os.Binder) NetworkTemplate(android.net.NetworkTemplate) Messenger(android.os.Messenger) RemoteException(android.os.RemoteException)

Example 10 with NetworkTemplate

use of android.net.NetworkTemplate in project platform_frameworks_base by android.

the class DataUsageController method getDataUsageInfo.

public DataUsageInfo getDataUsageInfo() {
    final String subscriberId = getActiveSubscriberId(mContext);
    if (subscriberId == null) {
        return warn("no subscriber id");
    }
    NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
    template = NetworkTemplate.normalize(template, mTelephonyManager.getMergedSubscriberIds());
    return getDataUsageInfo(template);
}
Also used : NetworkTemplate(android.net.NetworkTemplate)

Aggregations

NetworkTemplate (android.net.NetworkTemplate)111 NetworkPolicy (android.net.NetworkPolicy)33 RemoteException (android.os.RemoteException)21 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)15 NetworkStats (android.net.NetworkStats)12 IOException (java.io.IOException)12 AlertDialog (android.app.AlertDialog)6 Dialog (android.app.Dialog)6 DialogInterface (android.content.DialogInterface)6 NetworkIdentity (android.net.NetworkIdentity)6 Time (android.text.format.Time)6 NtpTrustedTime (android.util.NtpTrustedTime)6 TrustedTime (android.util.TrustedTime)6 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)6 AppItem (com.android.settingslib.AppItem)6 FileInputStream (java.io.FileInputStream)6 FileNotFoundException (java.io.FileNotFoundException)6 FileOutputStream (java.io.FileOutputStream)6 XmlSerializer (org.xmlpull.v1.XmlSerializer)6 Bucket (android.app.usage.NetworkStats.Bucket)5