Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2012 madvertise Mobile Advertising GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.os.Build;

import java.util.Locale;

public class Main {
    private static String sUA;

    /**
     * Generate a User-Agent used in HTTP request to pick an ad. Source used
     * from Android source code
     * "frameworks/base/core/java/android/webkit/WebSettings.java"
     * 
     * @return
     */
    public static String getUA() {
        if (sUA != null)
            return sUA;

        StringBuffer arg = new StringBuffer();

        final String version = Build.VERSION.RELEASE;
        if (version.length() > 0) {
            arg.append(version);
        } else {
            arg.append("1.0");
        }
        arg.append("; ");

        final Locale l = Locale.getDefault();
        final String language = l.getLanguage();
        if (language != null) {
            arg.append(language.toLowerCase());
            final String country = l.getCountry();
            if (country != null) {
                arg.append("-");
                arg.append(country.toLowerCase());
            }
        } else {
            arg.append("de");
        }
        final String model = Build.MODEL;
        if (model.length() > 0) {
            arg.append("; ");
            arg.append(model);
        }
        final String id = Build.ID;
        if (id.length() > 0) {
            arg.append(" Build/");
            arg.append(id);
        }

        // TODO: add version detection for AppleWebKit, Version and Safari
        final String rawUA = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2";
        sUA = String.format(rawUA, arg);

        return sUA;
    }
}