Example usage for android.webkit URLUtil isNetworkUrl

List of usage examples for android.webkit URLUtil isNetworkUrl

Introduction

In this page you can find the example usage for android.webkit URLUtil isNetworkUrl.

Prototype

public static boolean isNetworkUrl(String url) 

Source Link

Usage

From source file:Main.java

@SuppressLint("NewApi")
public static CompressFormat guessImageFormatC(String urlOrPath) {
    CompressFormat format = null;// w w w. j a v  a  2s. c om
    String fileName;
    if (URLUtil.isNetworkUrl(urlOrPath)) {
        fileName = URLUtil.guessFileName(urlOrPath, null, null);
    } else if (urlOrPath.lastIndexOf('.') <= 0) {
        return null;
    } else {
        fileName = urlOrPath;
    }
    fileName = fileName.toLowerCase(Locale.US);
    if (fileName.endsWith(".png")) {
        format = CompressFormat.PNG;
    } else if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) {
        format = CompressFormat.JPEG;
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && fileName.endsWith(".webp")) {
        format = CompressFormat.WEBP;
    }
    return format;
}

From source file:io.github.droidkaigi.confsched2017.view.helper.Navigator.java

public void navigateToWebPage(@NonNull String url) {
    if (TextUtils.isEmpty(url) || !URLUtil.isNetworkUrl(url)) {
        return;//www . ja  va2 s  .c  o m
    }

    CustomTabsIntent intent = new CustomTabsIntent.Builder().setShowTitle(true)
            .setToolbarColor(ContextCompat.getColor(activity, R.color.theme)).build();

    intent.launchUrl(activity, Uri.parse(url));
}

From source file:Main.java

/**
 * Creates the Uri string with embedded expansion codes.
 *
 * @param uri to be encoded/*from   w  ww  . j ava2 s.c  o m*/
 * @return the Uri string with expansion codes.
 */
public static byte[] encodeUri(String uri) {
    if (uri == null || uri.length() == 0) {
        Log.i(TAG, "null or empty uri");
        return new byte[0];
    }
    ByteBuffer bb = ByteBuffer.allocate(uri.length());
    // UUIDs are ordered as byte array, which means most significant first
    bb.order(ByteOrder.BIG_ENDIAN);
    int position = 0;

    // Add the byte code for the scheme or return null if none
    Byte schemeCode = encodeUriScheme(uri);
    if (schemeCode == null) {
        Log.i(TAG, "null scheme code");
        return null;
    }
    String scheme = URI_SCHEMES.get(schemeCode);
    bb.put(schemeCode);
    position += scheme.length();

    if (URLUtil.isNetworkUrl(scheme)) {
        Log.i(TAG, "is network URL");
        return encodeUrl(uri, position, bb);
    } else if ("urn:uuid:".equals(scheme)) {
        Log.i(TAG, "is UUID");
        return encodeUrnUuid(uri, position, bb);
    }
    return null;
}

From source file:net.luna.common.util.ParseUtils.java

public static byte[] htmlToBytes(String url) {
    if (!URLUtil.isNetworkUrl(url))
        return null;

    URL htmlUrl;/*  w  ww.j a v  a2s .c  o  m*/
    InputStream inputStream;
    try {
        htmlUrl = new URL(url);
        HttpURLConnection urlConnection = (HttpURLConnection) htmlUrl.openConnection();
        int responseCode = urlConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inputStream = urlConnection.getInputStream();
            return inputStreamToBytes(inputStream);
        } else {
            LunaLog.d("===Resonpse Code===" + responseCode);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.nxt.zyl.data.volley.toolbox.BasicNetwork.java

@SuppressLint("DefaultLocale")
@Override//from   w w  w. j a  v a  2  s  . c  om
public NetworkResponse performRequest(ResponseDelivery delivery, Request<?> request) throws VolleyError {
    long requestStart = SystemClock.elapsedRealtime();
    while (true) {
        HttpResponse httpResponse = null;
        byte[] responseContents = null;
        Map<String, String> responseHeaders = Collections.emptyMap();
        try {
            if (!URLUtil.isNetworkUrl(request.getUrl())) {
                return new NetworkResponse(responseContents);
            }
            // Gather headers.
            Map<String, String> headers = new HashMap<String, String>();
            addCacheHeaders(headers, request.getCacheEntry());
            httpResponse = mHttpStack.performRequest(request, headers);
            StatusLine statusLine = httpResponse.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            responseHeaders = convertHeaders(httpResponse.getAllHeaders());
            // Handle cache validation.
            if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
                Cache.Entry entry = request.getCacheEntry();
                if (entry == null) {
                    return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, null, responseHeaders, true);
                }
                // A HTTP 304 response does not have all header fields. We
                // have to use the header fields from the cache entry plus
                // the new ones from the response.
                // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
                entry.responseHeaders.putAll(responseHeaders);
                return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, entry.data, entry.responseHeaders, true);
            }

            // Handle moved resources
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                    || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                String newUrl = responseHeaders.get("Location");
                request.setRedirectUrl(newUrl);
            }

            // Some responses such as 204s do not have content.  We must check.
            if (httpResponse.getEntity() != null) {
                if (request instanceof DownloadRequest) {
                    DownloadRequest downloadRequest = (DownloadRequest) request;
                    // ???range???
                    if (downloadRequest.isResume() && !isSupportRange(httpResponse)) {
                        downloadRequest.setResume(false);
                    }
                    if (statusCode == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
                        return new NetworkResponse(HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE,
                                downloadRequest.getTarget().getBytes(), responseHeaders, false);
                    } else if (statusCode >= 300) {
                        responseContents = entityToBytes(delivery, request, httpResponse.getEntity());
                    } else {
                        responseContents = handleEntity(delivery, (DownloadRequest) request,
                                httpResponse.getEntity());
                    }
                } else {
                    responseContents = entityToBytes(delivery, request, httpResponse.getEntity());
                }
            } else {
                // Add 0 byte response as a way of honestly representing a
                // no-content request.
                responseContents = new byte[0];
            }

            // if the request is slow, log it.
            long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
            logSlowRequests(requestLifetime, request, responseContents, statusLine);

            if (statusCode < 200 || statusCode > 299) {
                throw new IOException();
            }
            return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
        } catch (SocketTimeoutException e) {
            attemptRetryOnException("socket", request, new TimeoutError());
        } catch (ConnectTimeoutException e) {
            attemptRetryOnException("connection", request, new TimeoutError());
        } catch (MalformedURLException e) {
            throw new RuntimeException("Bad URL " + request.getUrl(), e);
        } catch (IOException e) {
            int statusCode = 0;
            NetworkResponse networkResponse = null;
            if (httpResponse != null) {
                statusCode = httpResponse.getStatusLine().getStatusCode();
            } else {
                throw new NoConnectionError(e);
            }
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                    || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                VolleyLog.e("Request at %s has been redirected to %s", request.getOriginUrl(),
                        request.getUrl());
            } else {
                VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
            }
            if (responseContents != null) {
                networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false);
                if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
                    attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
                } else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                        || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                    attemptRetryOnException("redirect", request, new AuthFailureError(networkResponse));
                } else {
                    // TODO: Only throw ServerError for 5xx status codes.
                    throw new ServerError(networkResponse);
                }
            } else {
                throw new NetworkError(networkResponse);
            }
        } finally {
            try {
                if (httpResponse != null && httpResponse.getEntity() != null) {
                    httpResponse.getEntity().getContent().close();
                }
            } catch (IllegalStateException | IOException ignored) {
            }
        }
    }
}

From source file:org.dvbviewer.controller.ui.fragments.VideoPlayer.java

/**
 * Je nachdem ob ein lokales File abgespielt wird, oder ein Stream gespielt
 * wird<br>//from  w  w w .  j  a  v a2 s.  co m
 * wird hier die videoSource fr den VideoView gesetzt.
 *
 * @param source the new video source
 * @author RayBa
 * @date 22.02.2011
 */
private void setVideoSource(String source) {
    if (!URLUtil.isNetworkUrl(source)) {
        mVideoView.setVideoPath(source);
    } else {
        mVideoView.setVideoURI(Uri.parse(source));
    }
}

From source file:com.techgrains.service.TGRequestQueue.java

/**
 * Adds provided TGRequest into the appropriate queue.
 * @param request TGRequest//from   ww  w . j a v a  2 s .co m
 */
public void addRequest(Request<?> request) {
    if (URLUtil.isNetworkUrl(request.getUrl()))
        httpRequestQueue.add(request);
    else
        fileRequestQueue.add(request);
}

From source file:com.github.snowdream.android.app.downloader.DownloadTask.java

/**
 * Check whether the Task is valid.
 *
 * @return true is valid,otherwise not.
 */
public boolean isValid() {
    return URLUtil.isNetworkUrl(url);
}

From source file:com.jungle.base.utils.MiscUtils.java

public static boolean openUrlByBrowser(Context context, String url) {
    if (TextUtils.isEmpty(url)) {
        return false;
    }/*from   w ww  . jav  a  2  s.c  o  m*/

    final String HTTP_TAG = "http://";
    url = url.trim();
    if (!URLUtil.isNetworkUrl(url)) {
        url = HTTP_TAG + url;
    }

    try {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(uri);
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:org.physical_web.physicalweb.PwoMetadata.java

public String getNavigableUrl(Context context) {
    String urlToNavigateTo = url;
    if (hasUrlMetadata()) {
        String siteUrl = urlMetadata.siteUrl;
        if (siteUrl != null) {
            urlToNavigateTo = siteUrl;//from w w  w  .jav  a  2  s  .  com
        }
    }
    if (!URLUtil.isNetworkUrl(urlToNavigateTo)) {
        urlToNavigateTo = "http://" + urlToNavigateTo;
    }
    return urlToNavigateTo;
}