Example usage for android.util Patterns WEB_URL

List of usage examples for android.util Patterns WEB_URL

Introduction

In this page you can find the example usage for android.util Patterns WEB_URL.

Prototype

Pattern WEB_URL

To view the source code for android.util Patterns WEB_URL.

Click Source Link

Document

Regular expression pattern to match most part of RFC 3987 Internationalized URLs, aka IRIs.

Usage

From source file:com.lee.sdk.utils.Utils.java

/**
 * ??url/*from w w w. j  av  a2s . c  o m*/
 * 
 * @param query String
 * @return true: ?url
 */
public static boolean isUrl(String query) {
    Matcher matcher = Patterns.WEB_URL.matcher(query);
    if (matcher.matches()) {
        return true;
    }

    return false;
}

From source file:com.nbplus.vbroadlauncher.BroadcastWebViewActivity.java

/**
 * Handle onNewIntent() to inform the fragment manager that the
 * state is not saved.  If you are handling new intents and may be
 * making changes to the fragment state, you want to be sure to call
 * through to the super-class here first.  Otherwise, if your state
 * is saved but the activity is not stopped, you could get an
 * onNewIntent() call which happens before onResume() and trying to
 * perform fragment operations at that point will throw IllegalStateException
 * because the fragment manager thinks the state is still saved.
 *
 * @param intent/*from  w  w  w.j a  v a2 s. co m*/
 */
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.d(TAG, "BroadcastWebViewActivity onNewIntent()");
    if (mWebViewClient == null) {
        Log.e(TAG, "mWebViewClient is null..");
        return;
    }

    if (mWebViewClient != null) {
        Log.d(TAG, "Prev url is = " + mWebViewClient.getWebView().getUrl());
    }
    String url = null;
    if (intent != null && Constants.ACTION_SHOW_NOTIFICATION_CONTENTS.equals(getIntent().getAction())) {
        url = intent.getStringExtra(Constants.EXTRA_SHOW_NOTIFICATION_CONTENTS);
    } else {
        mShortcutData = intent.getParcelableExtra(Constants.EXTRA_NAME_SHORTCUT_DATA);
        url = mShortcutData.getDomain() + mShortcutData.getPath();
    }

    mWebViewClient.stopPageLoading();
    Log.d(TAG, ">> reset url = " + url);
    if (StringUtils.isEmptyString(url) || !Patterns.WEB_URL.matcher(url).matches()) {
        Log.e(TAG, "Wrong url ....");
        new AlertDialog.Builder(this).setMessage(R.string.alert_wrong_page_url)
                //.setTitle(R.string.alert_network_title)
                .setCancelable(true)
                .setPositiveButton(R.string.alert_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        finishActivity();
                    }
                }).show();
    } else {
        mWebViewClient.loadWebUrl(url);
    }
}

From source file:com.nbplus.vbroadlistener.BroadcastWebViewActivity.java

private void loadWebView(String url) {
    if (StringUtils.isEmptyString(url)) {
        VBroadcastServer serverInfo = LauncherSettings.getInstance(this).getServerInformation();
        if (serverInfo == null || !LauncherSettings.getInstance(this).getRegisterAddress()
                .equals(serverInfo.getInitialServerPage())) {
            url = LauncherSettings.getInstance(this).getRegisterAddress();
            LauncherSettings.getInstance(this).setServerInformation(null);
        } else {/* w ww  .j  av a2 s  .c  o m*/
            if (!StringUtils.isEmptyString(serverInfo.getDocServer())) {
                url = serverInfo.getDocServer() + LauncherSettings.firstPageContext;
            } else {
                url = LauncherSettings.getInstance(this).getRegisterAddress();
                LauncherSettings.getInstance(this).setServerInformation(null);
            }
        }
    }
    if (StringUtils.isEmptyString(url) || !Patterns.WEB_URL.matcher(url).matches()) {
        Log.e(TAG, "Wrong url ....");
        new AlertDialog.Builder(this).setMessage(R.string.alert_wrong_page_url)
                //.setTitle(R.string.alert_network_title)
                .setCancelable(true)
                .setPositiveButton(R.string.alert_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        finish();
                    }
                }).show();
    } else {
        mWebViewClient.loadWebUrl(url);
    }
    Log.d(TAG, ">> Start url = " + url);

    setContentViewByOrientation();
}

From source file:cw.kop.autobackground.files.DownloadThread.java

private Bitmap getImage(String url) {

    if (Patterns.WEB_URL.matcher(url).matches()) {
        try {//from  w w w. java 2 s  . c o  m
            int minWidth = AppSettings.getImageWidth();
            int minHeight = AppSettings.getImageHeight();
            System.gc();
            URL imageUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection();
            connection.connect();
            InputStream input = connection.getInputStream();

            if (!connection.getHeaderField("Content-Type").startsWith("image/")
                    && !AppSettings.forceDownload()) {
                Log.i(TAG, "Not an image: " + url);
                return null;
            }

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            options.inJustDecodeBounds = true;
            if (!AppSettings.useHighQuality()) {
                options.inPreferredConfig = Bitmap.Config.RGB_565;
            }

            BitmapFactory.decodeStream(input, null, options);

            input.close();

            int bitWidth = options.outWidth;
            int bitHeight = options.outHeight;
            options.inJustDecodeBounds = false;

            Log.i(TAG, "bitWidth: " + bitWidth + " bitHeight: " + bitHeight);

            if (bitWidth + 10 < minWidth || bitHeight + 10 < minHeight) {
                return null;
            }

            int sampleSize = 1;

            if (!AppSettings.useFullResolution()) {

                if (bitHeight > minHeight || bitWidth > minWidth) {

                    final int halfHeight = bitHeight / 2;
                    final int halfWidth = bitWidth / 2;
                    while ((halfHeight / sampleSize) > minHeight && (halfWidth / sampleSize) > minWidth) {
                        sampleSize *= 2;
                    }
                }
            }

            options.inSampleSize = sampleSize;

            connection = (HttpURLConnection) imageUrl.openConnection();
            connection.setConnectTimeout(5000);
            connection.setConnectTimeout(30000);
            connection.connect();
            input = connection.getInputStream();

            Bitmap bitmap = BitmapFactory.decodeStream(input, null, options);

            if (bitmap == null) {
                Log.i(TAG, "Null bitmap");
                return null;
            }
            return bitmap;

        } catch (InterruptedIOException e) {
            this.interrupt();
            Log.i(TAG, "Interrupted");
        } catch (OutOfMemoryError | IOException e) {
            interrupt();
            e.printStackTrace();
        }
    }
    Log.i(TAG, "Possible malformed URL");
    return null;
}

From source file:com.btmura.android.reddit.app.ComposeFormFragment.java

private void validateText(CharSequence s) {
    if (linkMatcher == null) {
        linkMatcher = Patterns.WEB_URL.matcher(s);
    }//from ww  w .  java 2s.  co m
    linkSwitch.setChecked(linkMatcher.reset(s).matches());
}

From source file:uk.ac.ucl.excites.sapelli.collector.activities.ProjectManagerActivity.java

public void loadProject(String path) {
    // Check path:
    if (path == null || path.trim().isEmpty())
        return;//from  w ww .  j  ava2 s .c om

    final String location = path.trim();
    if (Patterns.WEB_URL.matcher(location).matches()) { // Location is a (remote) URL: download Sapelli file:
        runFileStorageTask(new FileStorageTask() {
            @Override
            public void run(FileStorageProvider fsp) {
                try { // loading & storing of the project will happen upon successful download (via callback)
                    AsyncDownloader.Download(ProjectManagerActivity.this,
                            fsp.getSapelliDownloadsFolder() /*throws FileStorageException*/, location,
                            ProjectManagerActivity.this);
                } catch (FileStorageException fse) {
                    showErrorDialog(getString(R.string.storageError, fse.getMessage()));
                }
            }
        });
    } else
        // Load project from local file:
        loadProject(new File(location), null, true);
}

From source file:com.linkbubble.ui.ContentView.java

private void WorkWithURL(String strUrl, SearchURLSuggestions.SearchEngine selectedSearchEngine,
        boolean fromGoAction) {

    metUrl.dismissDropDown();/* w  w w  .j a  va2 s.  c  om*/

    strUrl = strUrl.trim();
    String strUrlWithPrefix = strUrl;
    if (!strUrl.startsWith(getContext().getString(R.string.http_prefix))
            && !strUrl.startsWith(getContext().getString(R.string.https_prefix)))
        strUrlWithPrefix = getContext().getString(R.string.http_prefix) + strUrl;

    if (SearchURLSuggestions.SearchEngine.NONE == selectedSearchEngine
            && Patterns.WEB_URL.matcher(strUrlWithPrefix).matches()) {
        LoadWebPage(strUrlWithPrefix);
    } else if (SearchURLSuggestions.SearchEngine.NONE == selectedSearchEngine && fromGoAction) {
        if (null != mFirstSuggestedItem) {
            WorkWithURL(strUrl, mFirstSuggestedItem.EngineToUse, false);
        }
    } else if (SearchURLSuggestions.SearchEngine.DUCKDUCKGO == selectedSearchEngine) {
        // Make the search using duck duck go
        try {
            String strQuery = String.format(getContext().getString(R.string.duckduckgo_search_engine),
                    URLEncoder.encode(strUrl, "UTF-8"));
            LoadWebPage(strQuery);
        } catch (IOException ioe) {
            Log.e(TAG, ioe.getMessage(), ioe);
        }
    } else if (SearchURLSuggestions.SearchEngine.GOOGLE == selectedSearchEngine) {
        // Make the search using google
        try {
            String strQuery = getContext().getString(R.string.google_search_engine)
                    + URLEncoder.encode(strUrl, "UTF-8");
            LoadWebPage(strQuery);
        } catch (IOException ioe) {
            Log.e(TAG, ioe.getMessage(), ioe);
        }
    } else if (SearchURLSuggestions.SearchEngine.YAHOO == selectedSearchEngine) {
        // Make the search using yahoo
        try {
            String strQuery = getContext().getString(R.string.yahoo_search_engine)
                    + URLEncoder.encode(strUrl, "UTF-8");
            LoadWebPage(strQuery);
        } catch (IOException ioe) {
            Log.e(TAG, ioe.getMessage(), ioe);
        }
    } else if (SearchURLSuggestions.SearchEngine.AMAZON == selectedSearchEngine) {
        // Make the search using amazon
        try {
            String strQuery = getContext().getString(R.string.amazon_search_engine)
                    + URLEncoder.encode(strUrl, "UTF-8");
            LoadWebPage(strQuery);
        } catch (IOException ioe) {
            Log.e(TAG, ioe.getMessage(), ioe);
        }
    }

    mToolbarLayout.bringToFront();
}

From source file:org.proninyaroslav.libretorrent.fragments.MainFragment.java

private boolean checkEditTextField(String s, TextInputLayout layout) {
    if (s == null || layout == null) {
        return false;
    }//  ww  w  .ja  v  a 2  s .  c om

    if (TextUtils.isEmpty(s)) {
        layout.setErrorEnabled(true);
        layout.setError(getString(R.string.error_empty_link));
        layout.requestFocus();

        return false;
    }

    if (s.startsWith(Utils.MAGNET_PREFIX)) {
        layout.setErrorEnabled(false);
        layout.setError(null);

        return true;
    }

    if (!Patterns.WEB_URL.matcher(s).matches()) {
        layout.setErrorEnabled(true);
        layout.setError(getString(R.string.error_invalid_link));
        layout.requestFocus();

        return false;
    }

    layout.setErrorEnabled(false);
    layout.setError(null);

    return true;
}

From source file:org.wso2.iot.system.service.SystemService.java

/**
 * Upgrading device firmware over the air (OTA).
 *///from   www.jav a 2  s  . com
public void upgradeFirmware(final boolean isStatusCheck) {
    Log.i(TAG, "An upgrade has been requested");

    Preference.putBoolean(context, context.getResources().getString(R.string.firmware_status_check_in_progress),
            isStatusCheck);
    Preference.putString(context, context.getResources().getString(R.string.firmware_download_progress),
            String.valueOf(DEFAULT_STATE_INFO_CODE));
    Preference.putInt(context, context.getResources().getString(R.string.operation_id), operationId);

    String schedule = null;
    String server;
    if (command != null && !command.trim().isEmpty()) {
        try {
            JSONObject upgradeData = new JSONObject(command);
            if (!upgradeData.isNull(context.getResources().getString(R.string.alarm_schedule))) {
                schedule = (String) upgradeData.get(context.getResources().getString(R.string.alarm_schedule));
            }

            boolean isAutomaticRetry = !Preference.hasPreferenceKey(context,
                    context.getResources().getString(R.string.firmware_upgrade_automatic_retry))
                    || Preference.getBoolean(context,
                            context.getResources().getString(R.string.firmware_upgrade_automatic_retry));
            if (!upgradeData
                    .isNull(context.getResources().getString(R.string.firmware_upgrade_automatic_retry))) {
                isAutomaticRetry = upgradeData.getBoolean(
                        context.getResources().getString(R.string.firmware_upgrade_automatic_retry));
                if (!isAutomaticRetry) {
                    Log.i(TAG, "Automatic retry on firmware upgrade failure is disabled.");
                }
            }

            Preference.putBoolean(context,
                    context.getResources().getString(R.string.firmware_upgrade_automatic_retry),
                    isAutomaticRetry);

            if (!upgradeData.isNull(context.getResources().getString(R.string.firmware_server))) {
                server = (String) upgradeData.get(context.getResources().getString(R.string.firmware_server));
                // When the server is empty, that means it is indicating to download from default server
                if (!server.isEmpty() && !Patterns.WEB_URL.matcher(server).matches()) {
                    String message = "Firmware upgrade URL provided is not valid.";
                    CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                            Constants.Code.FAILURE, Constants.Status.MALFORMED_OTA_URL, message);
                    CommonUtils.callAgentApp(context, Constants.Operation.FIRMWARE_UPGRADE_FAILURE,
                            Preference.getInt(context, context.getResources().getString(R.string.operation_id)),
                            message);
                    Log.e(TAG, message);
                    return;
                } else {
                    Preference.putString(context, context.getResources().getString(R.string.firmware_server),
                            server);
                }
            }
        } catch (JSONException e) {
            Log.e(TAG, "Firmware upgrade payload parsing failed." + e);
            return;
        }
    }
    if (schedule != null && !schedule.trim().isEmpty()) {
        Log.i(TAG, "Upgrade scheduled received: " + schedule);
        Preference.putString(context, context.getResources().getString(R.string.alarm_schedule), schedule);
        try {
            AlarmUtils.setOneTimeAlarm(context, schedule, Constants.Operation.UPGRADE_FIRMWARE, null);
        } catch (ParseException e) {
            CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE, Constants.Code.FAILURE,
                    Constants.Status.MALFORMED_REQUEST, e.getMessage());
        }
    } else {
        if (isStatusCheck) {
            Log.i(TAG, "Firmware status check is initiated by admin.");
        } else {
            Log.i(TAG, "Upgrade request initiated by admin.");

            String status = Preference.getString(context,
                    context.getResources().getString(R.string.upgrade_download_status));
            boolean isAutomaticUpgrade = Preference.getBoolean(context,
                    context.getResources().getString(R.string.firmware_upgrade_automatic_retry));

            if (Constants.Status.WIFI_OFF.equals(status) && isAutomaticUpgrade && !checkNetworkOnline()) {
                String msg = "Ignoring request as service waiting for WiFi to start upgrade.";
                Log.d(TAG, msg);
                CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE, Constants.Code.PENDING,
                        Constants.Status.OTA_UPGRADE_PENDING, msg);
                return;
            } else if (Constants.Status.OTA_UPGRADE_ONGOING.equals(status)) {
                String msg = "Checking for existing download. Will proceed this request if current download is no longer ongoing.";
                Log.d(TAG, msg);
                CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE, Constants.Code.PENDING,
                        Constants.Status.OTA_UPGRADE_ONGOING, msg);
                Preference.putString(context,
                        context.getResources().getString(R.string.upgrade_download_status),
                        Constants.Status.REQUEST_PLACED);
                Timer timeoutTimer = new Timer();
                timeoutTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if (Constants.Status.REQUEST_PLACED.equals(Preference.getString(context,
                                context.getResources().getString(R.string.upgrade_download_status)))) {
                            Log.d(TAG,
                                    "Download is no longer ongoing. Proceeding download request from the agent.");
                            OTADownload otaDownload = new OTADownload(context);
                            otaDownload.startOTA();
                        } else {
                            String msg = "Request ignored because another download is ongoing.";
                            Log.d(TAG, msg);
                            CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                                    Constants.Code.FAILURE, Constants.Status.OTA_UPGRADE_ONGOING, msg);
                        }
                    }
                }, Constants.FIRMWARE_UPGRADE_READ_TIMEOUT);
                return;
            }
        }

        //Prepare for upgrade
        OTADownload otaDownload = new OTADownload(context);
        otaDownload.startOTA();
    }
}

From source file:org.wso2.emm.system.service.EMMSystemService.java

/**
 * Upgrading device firmware over the air (OTA).
 *//*  w w w  .  j  ava 2  s .  c  o m*/
public void upgradeFirmware(final boolean isStatusCheck) {
    Log.i(TAG, "An upgrade has been requested");

    Preference.putBoolean(context, context.getResources().getString(R.string.firmware_status_check_in_progress),
            isStatusCheck);
    Preference.putString(context, context.getResources().getString(R.string.firmware_download_progress),
            String.valueOf(DEFAULT_STATE_INFO_CODE));
    Preference.putInt(context, context.getResources().getString(R.string.operation_id), operationId);

    String schedule = null;
    String server;
    if (command != null && !command.trim().isEmpty()) {
        try {
            JSONObject upgradeData = new JSONObject(command);
            if (!upgradeData.isNull(context.getResources().getString(R.string.alarm_schedule))) {
                schedule = (String) upgradeData.get(context.getResources().getString(R.string.alarm_schedule));
            }

            boolean isAutomaticRetry = (Preference.hasPreferenceKey(context,
                    context.getResources().getString(R.string.firmware_upgrade_automatic_retry))
                    && Preference.getBoolean(context,
                            context.getResources().getString(R.string.firmware_upgrade_automatic_retry)))
                    || !Preference.hasPreferenceKey(context,
                            context.getResources().getString(R.string.firmware_upgrade_automatic_retry));
            if (!upgradeData
                    .isNull(context.getResources().getString(R.string.firmware_upgrade_automatic_retry))) {
                isAutomaticRetry = upgradeData.getBoolean(
                        context.getResources().getString(R.string.firmware_upgrade_automatic_retry));
                if (!isAutomaticRetry) {
                    Log.i(TAG, "Automatic retry on firmware upgrade failure is disabled.");
                }
            }

            Preference.putBoolean(context,
                    context.getResources().getString(R.string.firmware_upgrade_automatic_retry),
                    isAutomaticRetry);

            if (!upgradeData.isNull(context.getResources().getString(R.string.firmware_server))) {
                server = (String) upgradeData.get(context.getResources().getString(R.string.firmware_server));
                // When the server is empty, that means it is indicating to download from default server
                if (!server.isEmpty() && !Patterns.WEB_URL.matcher(server).matches()) {
                    String message = "Firmware upgrade URL provided is not valid.";
                    CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                            Constants.Code.FAILURE, Constants.Status.MALFORMED_OTA_URL, message);
                    CommonUtils.callAgentApp(context, Constants.Operation.FIRMWARE_UPGRADE_FAILURE,
                            Preference.getInt(context, context.getResources().getString(R.string.operation_id)),
                            message);
                    Log.e(TAG, message);
                    return;
                } else {
                    Preference.putString(context, context.getResources().getString(R.string.firmware_server),
                            server);
                }
            }
        } catch (JSONException e) {
            Log.e(TAG, "Firmware upgrade payload parsing failed." + e);
            return;
        }
    }
    if (schedule != null && !schedule.trim().isEmpty()) {
        Log.i(TAG, "Upgrade scheduled received: " + schedule);
        Preference.putString(context, context.getResources().getString(R.string.alarm_schedule), schedule);
        try {
            AlarmUtils.setOneTimeAlarm(context, schedule, Constants.Operation.UPGRADE_FIRMWARE, null);
        } catch (ParseException e) {
            CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE, Constants.Code.FAILURE,
                    Constants.Status.MALFORMED_REQUEST, e.getMessage());
        }
    } else {
        if (isStatusCheck) {
            Log.i(TAG, "Firmware status check is initiated by admin.");
        } else {
            Log.i(TAG, "Upgrade request initiated by admin.");

            String status = Preference.getString(context,
                    context.getResources().getString(R.string.upgrade_download_status));
            boolean isAutomaticUpgrade = Preference.getBoolean(context,
                    context.getResources().getString(R.string.firmware_upgrade_automatic_retry));

            if (Constants.Status.WIFI_OFF.equals(status) && isAutomaticUpgrade && !checkNetworkOnline()) {
                String msg = "Ignoring request as service waiting for WiFi to start upgrade.";
                Log.d(TAG, msg);
                CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE, Constants.Code.PENDING,
                        Constants.Status.OTA_UPGRADE_PENDING, msg);
                return;
            } else if (Constants.Status.OTA_UPGRADE_ONGOING.equals(status)) {
                String msg = "Checking for existing download. Will proceed this request if current download is no longer ongoing.";
                Log.d(TAG, msg);
                CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE, Constants.Code.PENDING,
                        Constants.Status.OTA_UPGRADE_ONGOING, msg);
                Preference.putString(context,
                        context.getResources().getString(R.string.upgrade_download_status),
                        Constants.Status.REQUEST_PLACED);
                Timer timeoutTimer = new Timer();
                timeoutTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if (Constants.Status.REQUEST_PLACED.equals(Preference.getString(context,
                                context.getResources().getString(R.string.upgrade_download_status)))) {
                            Log.d(TAG,
                                    "Download is no longer ongoing. Proceeding download request from the agent.");
                            OTADownload otaDownload = new OTADownload(context);
                            otaDownload.startOTA();
                        } else {
                            String msg = "Request ignored because another download is ongoing.";
                            Log.d(TAG, msg);
                            CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                                    Constants.Code.FAILURE, Constants.Status.OTA_UPGRADE_ONGOING, msg);
                        }
                    }
                }, Constants.FIRMWARE_UPGRADE_READ_TIMEOUT);
                return;
            }
        }

        //Prepare for upgrade
        OTADownload otaDownload = new OTADownload(context);
        otaDownload.startOTA();
    }
}