1 | /* |
2 | * Tomdroid |
3 | * Tomboy on Android |
4 | * http://www.launchpad.net/tomdroid |
5 | * |
6 | * Copyright 2010 Olivier Bilodeau <olivier@bottomlesspit.org> |
7 | * |
8 | * This file is part of Tomdroid. |
9 | * |
10 | * Tomdroid is free software: you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by |
12 | * the Free Software Foundation, either version 3 of the License, or |
13 | * (at your option) any later version. |
14 | * |
15 | * Tomdroid is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License |
21 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
22 | */ |
23 | // Portions of this file are covered under APL 2 and taken from |
24 | // http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/text/util/Linkify.java |
25 | // http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/text/util/Regex.java |
26 | /* |
27 | * Copyright (C) 2007 The Android Open Source Project |
28 | * |
29 | * Licensed under the Apache License, Version 2.0 (the "License"); |
30 | * you may not use this file except in compliance with the License. |
31 | * You may obtain a copy of the License at |
32 | * |
33 | * http://www.apache.org/licenses/LICENSE-2.0 |
34 | * |
35 | * Unless required by applicable law or agreed to in writing, software |
36 | * distributed under the License is distributed on an "AS IS" BASIS, |
37 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
38 | * See the License for the specific language governing permissions and |
39 | * limitations under the License. |
40 | */ |
41 | |
42 | package org.tomdroid.util; |
43 | |
44 | import java.util.regex.Pattern; |
45 | |
46 | import android.text.util.Linkify.MatchFilter; |
47 | |
48 | /** |
49 | * Statics useful for Linkify to create a better phone handler than android's default one |
50 | * Fixes bugs like lp:512204 |
51 | */ |
52 | public class LinkifyPhone { |
53 | /** |
54 | * Don't treat anything with fewer than this many digits as a |
55 | * phone number. |
56 | */ |
57 | private static final int PHONE_NUMBER_MINIMUM_DIGITS = 5; |
58 | |
59 | public static final Pattern PHONE_PATTERN = Pattern.compile( // sdd = space, dot, or dash |
60 | "(\\+[0-9]+[\\- \\.]*)?" // +<digits><sdd>* |
61 | + "(\\([0-9]+\\)[\\- \\.]*)?" // (<digits>)<sdd>* |
62 | + "([0-9][0-9\\- \\.][0-9\\- \\.]+[0-9])"); // <digit><digit|sdd>+<digit> |
63 | |
64 | /** |
65 | * Filters out URL matches that: |
66 | * - the character before the match is not a whitespace |
67 | * - don't have enough digits to be a phone number |
68 | */ |
69 | public static final MatchFilter sPhoneNumberMatchFilter = new MatchFilter() { |
70 | |
71 | public final boolean acceptMatch(CharSequence s, int start, int end) { |
72 | |
73 | // make sure there was a whitespace before pattern |
74 | try { |
75 | if (!Character.isWhitespace(s.charAt(start - 1))) { |
76 | return false; |
77 | } |
78 | } catch (IndexOutOfBoundsException e) { |
79 | //Do nothing |
80 | } |
81 | |
82 | // minimum length |
83 | int digitCount = 0; |
84 | for (int i = start; i < end; i++) { |
85 | if (Character.isDigit(s.charAt(i))) { |
86 | digitCount++; |
87 | if (digitCount >= PHONE_NUMBER_MINIMUM_DIGITS) { |
88 | return true; |
89 | } |
90 | } |
91 | } |
92 | return false; |
93 | } |
94 | }; |
95 | |
96 | } |