Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.net.Uri;

public class Main {
    /**
     * Checks to see if URL is DuckDuckGo SERP
     * Returns the query if it's a SERP, otherwise null
     * 
     * @param url
     * @return
     */
    static public String getQueryIfSerp(String url) {
        if (!isSerpUrl(url)) {
            return null;
        }

        Uri uri = Uri.parse(url);
        String query = uri.getQueryParameter("q");
        if (query != null)
            return query;

        String lastPath = uri.getLastPathSegment();
        if (lastPath == null)
            return null;

        if (!lastPath.contains(".html")) {
            return lastPath.replace("_", " ");
        }

        return null;
    }

    public static boolean isSerpUrl(String url) {
        return url.contains("duckduckgo.com");
    }
}