Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;
import android.content.Intent;

import android.content.pm.PackageManager;

import android.net.Uri;

import android.text.ClipboardManager;

public class Main {
    private static final String GMM_PACKAGE_NAME = "com.google.android.apps.maps";
    private static final String GMM_CLASS_NAME = "com.google.android.maps.MapsActivity";
    private static final String YT_PACKAGE_NAME = "com.google.android.youtube";

    public static Intent getLaunchIntent(Context context, String title, String url, String sel) {
        Intent intent = null;
        String number = parseTelephoneNumber(sel);
        if (number != null) {
            intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else {
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (isMapsURL(url)) {
                intent.setClassName(GMM_PACKAGE_NAME, GMM_CLASS_NAME);
            } else if (isYouTubeURL(url)) {
                intent.setPackage(YT_PACKAGE_NAME);
            }

            // Fall back if we can't resolve intent (i.e. app missing)
            PackageManager pm = context.getPackageManager();
            if (null == intent.resolveActivity(pm)) {
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
        }

        if (sel != null && sel.length() > 0) {
            ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            cm.setText(sel);
        }

        return intent;
    }

    public static String parseTelephoneNumber(String sel) {
        if (sel == null || sel.length() == 0)
            return null;

        // Hack: Remove trailing left-to-right mark (Google Maps adds this)
        if (sel.codePointAt(sel.length() - 1) == 8206) {
            sel = sel.substring(0, sel.length() - 1);
        }

        String number = null;
        if (sel.matches("([Tt]el[:]?)?\\s?[+]?(\\(?[0-9|\\s|\\-|\\.]\\)?)+")) {
            String elements[] = sel.split("([Tt]el[:]?)");
            number = elements.length > 1 ? elements[1] : elements[0];
            number = number.replace(" ", "");

            // Remove option (0) in international numbers, e.g. +44 (0)20 ...
            if (number.matches("\\+[0-9]{2,3}\\(0\\).*")) {
                int openBracket = number.indexOf('(');
                int closeBracket = number.indexOf(')');
                number = number.substring(0, openBracket) + number.substring(closeBracket + 1);
            }
        }
        return number;
    }

    public static boolean isMapsURL(String url) {
        return url.matches("http[s]://maps\\.google\\.[a-z]{2,3}(\\.[a-z]{2})?[/?].*")
                || url.matches("http[s]://www\\.google\\.[a-z]{2,3}(\\.[a-z]{2})?/maps.*");
    }

    public static boolean isYouTubeURL(String url) {
        return url.matches("http[s]://www\\.youtube\\.[a-z]{2,3}(\\.[a-z]{2})?/.*");
    }
}