Search in sources :

Example 6 with ProtocolException

use of java.net.ProtocolException in project android_frameworks_base by ResurrectionRemix.

the class UsageStatsXmlV1 method loadEvent.

private static void loadEvent(XmlPullParser parser, IntervalStats statsOut) throws XmlPullParserException, IOException {
    final String packageName = XmlUtils.readStringAttribute(parser, PACKAGE_ATTR);
    if (packageName == null) {
        throw new ProtocolException("no " + PACKAGE_ATTR + " attribute present");
    }
    final String className = XmlUtils.readStringAttribute(parser, CLASS_ATTR);
    final UsageEvents.Event event = statsOut.buildEvent(packageName, className);
    // Apply the offset to the beginTime to find the absolute time of this event.
    event.mTimeStamp = statsOut.beginTime + XmlUtils.readLongAttribute(parser, TIME_ATTR);
    event.mEventType = XmlUtils.readIntAttribute(parser, TYPE_ATTR);
    switch(event.mEventType) {
        case UsageEvents.Event.CONFIGURATION_CHANGE:
            event.mConfiguration = new Configuration();
            Configuration.readXmlAttrs(parser, event.mConfiguration);
            break;
        case UsageEvents.Event.SHORTCUT_INVOCATION:
            final String id = XmlUtils.readStringAttribute(parser, SHORTCUT_ID_ATTR);
            event.mShortcutId = (id != null) ? id.intern() : null;
            break;
    }
    if (statsOut.events == null) {
        statsOut.events = new TimeSparseArray<>();
    }
    statsOut.events.put(event.mTimeStamp, event);
}
Also used : ProtocolException(java.net.ProtocolException) Configuration(android.content.res.Configuration) UsageEvents(android.app.usage.UsageEvents)

Example 7 with ProtocolException

use of java.net.ProtocolException in project android_frameworks_base by ResurrectionRemix.

the class NetworkStatsFactory method readNetworkStatsSummaryDev.

/**
     * Parse and return interface-level summary {@link NetworkStats} measured
     * using {@code /proc/net/dev} style hooks, which may include non IP layer
     * traffic. Values monotonically increase since device boot, and may include
     * details about inactive interfaces.
     *
     * @throws IllegalStateException when problem parsing stats.
     */
public NetworkStats readNetworkStatsSummaryDev() throws IOException {
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
    final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    ProcFileReader reader = null;
    try {
        reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceAll));
        while (reader.hasMoreData()) {
            entry.iface = reader.nextString();
            entry.uid = UID_ALL;
            entry.set = SET_ALL;
            entry.tag = TAG_NONE;
            final boolean active = reader.nextInt() != 0;
            // always include snapshot values
            entry.rxBytes = reader.nextLong();
            entry.rxPackets = reader.nextLong();
            entry.txBytes = reader.nextLong();
            entry.txPackets = reader.nextLong();
            // fold in active numbers, but only when active
            if (active) {
                entry.rxBytes += reader.nextLong();
                entry.rxPackets += reader.nextLong();
                entry.txBytes += reader.nextLong();
                entry.txPackets += reader.nextLong();
            }
            stats.addValues(entry);
            reader.finishLine();
        }
    } catch (NullPointerException e) {
        throw new ProtocolException("problem parsing stats", e);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing stats", e);
    } finally {
        IoUtils.closeQuietly(reader);
        StrictMode.setThreadPolicy(savedPolicy);
    }
    return stats;
}
Also used : StrictMode(android.os.StrictMode) ProcFileReader(com.android.internal.util.ProcFileReader) ProtocolException(java.net.ProtocolException) NetworkStats(android.net.NetworkStats) FileInputStream(java.io.FileInputStream)

Example 8 with ProtocolException

use of java.net.ProtocolException in project android_frameworks_base by ResurrectionRemix.

the class NetworkStatsFactory method readNetworkStatsSummaryXt.

/**
     * Parse and return interface-level summary {@link NetworkStats}. Designed
     * to return only IP layer traffic. Values monotonically increase since
     * device boot, and may include details about inactive interfaces.
     *
     * @throws IllegalStateException when problem parsing stats.
     */
public NetworkStats readNetworkStatsSummaryXt() throws IOException {
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
    // return null when kernel doesn't support
    if (!mStatsXtIfaceFmt.exists())
        return null;
    final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    ProcFileReader reader = null;
    try {
        // open and consume header line
        reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceFmt));
        reader.finishLine();
        while (reader.hasMoreData()) {
            entry.iface = reader.nextString();
            entry.uid = UID_ALL;
            entry.set = SET_ALL;
            entry.tag = TAG_NONE;
            entry.rxBytes = reader.nextLong();
            entry.rxPackets = reader.nextLong();
            entry.txBytes = reader.nextLong();
            entry.txPackets = reader.nextLong();
            stats.addValues(entry);
            reader.finishLine();
        }
    } catch (NullPointerException e) {
        throw new ProtocolException("problem parsing stats", e);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing stats", e);
    } finally {
        IoUtils.closeQuietly(reader);
        StrictMode.setThreadPolicy(savedPolicy);
    }
    return stats;
}
Also used : StrictMode(android.os.StrictMode) ProcFileReader(com.android.internal.util.ProcFileReader) ProtocolException(java.net.ProtocolException) NetworkStats(android.net.NetworkStats) FileInputStream(java.io.FileInputStream)

Example 9 with ProtocolException

use of java.net.ProtocolException in project android_frameworks_base by ResurrectionRemix.

the class NetworkStatsFactory method javaReadNetworkStatsDetail.

/**
     * Parse and return {@link NetworkStats} with UID-level details. Values are
     * expected to monotonically increase since device boot.
     */
@VisibleForTesting
public static NetworkStats javaReadNetworkStatsDetail(File detailPath, int limitUid, String[] limitIfaces, int limitTag) throws IOException {
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
    final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 24);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    int idx = 1;
    int lastIdx = 1;
    ProcFileReader reader = null;
    try {
        // open and consume header line
        reader = new ProcFileReader(new FileInputStream(detailPath));
        reader.finishLine();
        while (reader.hasMoreData()) {
            idx = reader.nextInt();
            if (idx != lastIdx + 1) {
                throw new ProtocolException("inconsistent idx=" + idx + " after lastIdx=" + lastIdx);
            }
            lastIdx = idx;
            entry.iface = reader.nextString();
            entry.tag = kernelToTag(reader.nextString());
            entry.uid = reader.nextInt();
            entry.set = reader.nextInt();
            entry.rxBytes = reader.nextLong();
            entry.rxPackets = reader.nextLong();
            entry.txBytes = reader.nextLong();
            entry.txPackets = reader.nextLong();
            if ((limitIfaces == null || ArrayUtils.contains(limitIfaces, entry.iface)) && (limitUid == UID_ALL || limitUid == entry.uid) && (limitTag == TAG_ALL || limitTag == entry.tag)) {
                stats.addValues(entry);
            }
            reader.finishLine();
        }
    } catch (NullPointerException e) {
        throw new ProtocolException("problem parsing idx " + idx, e);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing idx " + idx, e);
    } finally {
        IoUtils.closeQuietly(reader);
        StrictMode.setThreadPolicy(savedPolicy);
    }
    return stats;
}
Also used : StrictMode(android.os.StrictMode) ProcFileReader(com.android.internal.util.ProcFileReader) ProtocolException(java.net.ProtocolException) NetworkStats(android.net.NetworkStats) FileInputStream(java.io.FileInputStream) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 10 with ProtocolException

use of java.net.ProtocolException in project AndroidChromium by JackyAndroid.

the class ConnectivityChecker method checkConnectivitySystemNetworkStack.

static void checkConnectivitySystemNetworkStack(String urlStr, final int timeoutMs, final ConnectivityCheckerCallback callback) {
    if (!nativeIsUrlValid(urlStr)) {
        Log.w(TAG, "Predefined URL invalid.");
        postResult(callback, ConnectivityCheckResult.ERROR);
        return;
    }
    final URL url;
    try {
        url = new URL(urlStr);
    } catch (MalformedURLException e) {
        Log.w(TAG, "Failed to parse predefined URL: " + e);
        postResult(callback, ConnectivityCheckResult.ERROR);
        return;
    }
    new AsyncTask<String, Void, Integer>() {

        @Override
        protected Integer doInBackground(String... strings) {
            try {
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setInstanceFollowRedirects(false);
                conn.setRequestMethod("GET");
                conn.setDoInput(false);
                conn.setDoOutput(false);
                conn.setConnectTimeout(timeoutMs);
                conn.setReadTimeout(timeoutMs);
                conn.connect();
                int responseCode = conn.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
                    return ConnectivityCheckResult.CONNECTED;
                } else {
                    return ConnectivityCheckResult.NOT_CONNECTED;
                }
            } catch (SocketTimeoutException e) {
                return ConnectivityCheckResult.TIMEOUT;
            } catch (ProtocolException e) {
                return ConnectivityCheckResult.ERROR;
            } catch (IOException e) {
                return ConnectivityCheckResult.NOT_CONNECTED;
            }
        }

        @Override
        protected void onPostExecute(Integer result) {
            callback.onResult(result);
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : ProtocolException(java.net.ProtocolException) MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

ProtocolException (java.net.ProtocolException)214 IOException (java.io.IOException)78 URL (java.net.URL)62 HttpURLConnection (java.net.HttpURLConnection)60 MalformedURLException (java.net.MalformedURLException)44 InputStreamReader (java.io.InputStreamReader)32 BufferedReader (java.io.BufferedReader)31 InputStream (java.io.InputStream)21 BufferedInputStream (java.io.BufferedInputStream)20 FileInputStream (java.io.FileInputStream)20 NetworkStats (android.net.NetworkStats)18 NetworkStatsHistory (android.net.NetworkStatsHistory)18 StrictMode (android.os.StrictMode)18 ProcFileReader (com.android.internal.util.ProcFileReader)18 FileNotFoundException (java.io.FileNotFoundException)18 OutputStream (java.io.OutputStream)18 Test (org.junit.Test)16 DataInputStream (java.io.DataInputStream)14 Map (java.util.Map)13 AtomicFile (android.util.AtomicFile)12