SmsUsageMonitor.java revision 81ddb888952763b5eb7a4a38fb293fc86b897968
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.telephony;
18
19import android.app.AppGlobals;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.content.pm.ApplicationInfo;
23import android.content.res.XmlResourceParser;
24import android.database.ContentObserver;
25import android.os.Binder;
26import android.os.Handler;
27import android.os.Message;
28import android.os.Process;
29import android.os.RemoteException;
30import android.os.UserHandle;
31import android.provider.Settings;
32import android.telephony.PhoneNumberUtils;
33import android.util.AtomicFile;
34import android.util.Log;
35import android.util.Xml;
36
37import com.android.internal.util.FastXmlSerializer;
38import com.android.internal.util.XmlUtils;
39
40import org.xmlpull.v1.XmlPullParser;
41import org.xmlpull.v1.XmlPullParserException;
42import org.xmlpull.v1.XmlPullParserFactory;
43import org.xmlpull.v1.XmlSerializer;
44
45import java.io.File;
46import java.io.FileInputStream;
47import java.io.FileNotFoundException;
48import java.io.FileOutputStream;
49import java.io.FileReader;
50import java.io.IOException;
51import java.io.StringReader;
52import java.util.ArrayList;
53import java.util.concurrent.atomic.AtomicBoolean;
54import java.util.HashMap;
55import java.util.Iterator;
56import java.util.Map;
57import java.util.regex.Pattern;
58
59/**
60 * Implement the per-application based SMS control, which limits the number of
61 * SMS/MMS messages an app can send in the checking period.
62 *
63 * This code was formerly part of {@link SMSDispatcher}, and has been moved
64 * into a separate class to support instantiation of multiple SMSDispatchers on
65 * dual-mode devices that require support for both 3GPP and 3GPP2 format messages.
66 */
67public class SmsUsageMonitor {
68    private static final String TAG = "SmsUsageMonitor";
69    private static final boolean DBG = false;
70    private static final boolean VDBG = false;
71
72    private static final String SHORT_CODE_PATH = "/data/misc/sms/codes";
73
74    /** Default checking period for SMS sent without user permission. */
75    private static final int DEFAULT_SMS_CHECK_PERIOD = 1800000;    // 30 minutes
76
77    /** Default number of SMS sent in checking period without user permission. */
78    private static final int DEFAULT_SMS_MAX_COUNT = 30;
79
80    /** Return value from {@link #checkDestination} for regular phone numbers. */
81    static final int CATEGORY_NOT_SHORT_CODE = 0;
82
83    /** Return value from {@link #checkDestination} for free (no cost) short codes. */
84    static final int CATEGORY_FREE_SHORT_CODE = 1;
85
86    /** Return value from {@link #checkDestination} for standard rate (non-premium) short codes. */
87    static final int CATEGORY_STANDARD_SHORT_CODE = 2;
88
89    /** Return value from {@link #checkDestination} for possible premium short codes. */
90    static final int CATEGORY_POSSIBLE_PREMIUM_SHORT_CODE = 3;
91
92    /** Return value from {@link #checkDestination} for premium short codes. */
93    static final int CATEGORY_PREMIUM_SHORT_CODE = 4;
94
95    /** @hide */
96    public static int mergeShortCodeCategories(int type1, int type2) {
97        if (type1 > type2) return type1;
98        return type2;
99    }
100
101    /** Premium SMS permission for a new package (ask user when first premium SMS sent). */
102    public static final int PREMIUM_SMS_PERMISSION_UNKNOWN = 0;
103
104    /** Default premium SMS permission (ask user for each premium SMS sent). */
105    public static final int PREMIUM_SMS_PERMISSION_ASK_USER = 1;
106
107    /** Premium SMS permission when the owner has denied the app from sending premium SMS. */
108    public static final int PREMIUM_SMS_PERMISSION_NEVER_ALLOW = 2;
109
110    /** Premium SMS permission when the owner has allowed the app to send premium SMS. */
111    public static final int PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW = 3;
112
113    private final int mCheckPeriod;
114    private final int mMaxAllowed;
115
116    private final HashMap<String, ArrayList<Long>> mSmsStamp =
117            new HashMap<String, ArrayList<Long>>();
118
119    /** Context for retrieving regexes from XML resource. */
120    private final Context mContext;
121
122    /** Country code for the cached short code pattern matcher. */
123    private String mCurrentCountry;
124
125    /** Cached short code pattern matcher for {@link #mCurrentCountry}. */
126    private ShortCodePatternMatcher mCurrentPatternMatcher;
127
128    /** Notice when the enabled setting changes - can be changed through gservices */
129    private final AtomicBoolean mCheckEnabled = new AtomicBoolean(true);
130
131    /** Cached short code regex patterns from secure settings for {@link #mCurrentCountry}. */
132    private String mSettingsShortCodePatterns;
133
134    /** Handler for responding to content observer updates. */
135    private final SettingsObserverHandler mSettingsObserverHandler;
136
137    /** File holding the patterns */
138    private final File mPatternFile = new File(SHORT_CODE_PATH);
139
140    /** Last modified time for pattern file */
141    private long mPatternFileLastModified = 0;
142
143    /** Directory for per-app SMS permission XML file. */
144    private static final String SMS_POLICY_FILE_DIRECTORY = "/data/radio";
145
146    /** Per-app SMS permission XML filename. */
147    private static final String SMS_POLICY_FILE_NAME = "premium_sms_policy.xml";
148
149    /** XML tag for root element. */
150    private static final String TAG_SHORTCODES = "shortcodes";
151
152    /** XML tag for short code patterns for a specific country. */
153    private static final String TAG_SHORTCODE = "shortcode";
154
155    /** XML attribute for the country code. */
156    private static final String ATTR_COUNTRY = "country";
157
158    /** XML attribute for the short code regex pattern. */
159    private static final String ATTR_PATTERN = "pattern";
160
161    /** XML attribute for the premium short code regex pattern. */
162    private static final String ATTR_PREMIUM = "premium";
163
164    /** XML attribute for the free short code regex pattern. */
165    private static final String ATTR_FREE = "free";
166
167    /** XML attribute for the standard rate short code regex pattern. */
168    private static final String ATTR_STANDARD = "standard";
169
170    /** Stored copy of premium SMS package permissions. */
171    private AtomicFile mPolicyFile;
172
173    /** Loaded copy of premium SMS package permissions. */
174    private final HashMap<String, Integer> mPremiumSmsPolicy = new HashMap<String, Integer>();
175
176    /** XML tag for root element of premium SMS permissions. */
177    private static final String TAG_SMS_POLICY_BODY = "premium-sms-policy";
178
179    /** XML tag for a package. */
180    private static final String TAG_PACKAGE = "package";
181
182    /** XML attribute for the package name. */
183    private static final String ATTR_PACKAGE_NAME = "name";
184
185    /** XML attribute for the package's premium SMS permission (integer type). */
186    private static final String ATTR_PACKAGE_SMS_POLICY = "sms-policy";
187
188    /**
189     * SMS short code regex pattern matcher for a specific country.
190     */
191    private static final class ShortCodePatternMatcher {
192        private final Pattern mShortCodePattern;
193        private final Pattern mPremiumShortCodePattern;
194        private final Pattern mFreeShortCodePattern;
195        private final Pattern mStandardShortCodePattern;
196
197        ShortCodePatternMatcher(String shortCodeRegex, String premiumShortCodeRegex,
198                String freeShortCodeRegex, String standardShortCodeRegex) {
199            mShortCodePattern = (shortCodeRegex != null ? Pattern.compile(shortCodeRegex) : null);
200            mPremiumShortCodePattern = (premiumShortCodeRegex != null ?
201                    Pattern.compile(premiumShortCodeRegex) : null);
202            mFreeShortCodePattern = (freeShortCodeRegex != null ?
203                    Pattern.compile(freeShortCodeRegex) : null);
204            mStandardShortCodePattern = (standardShortCodeRegex != null ?
205                    Pattern.compile(standardShortCodeRegex) : null);
206        }
207
208        int getNumberCategory(String phoneNumber) {
209            if (mFreeShortCodePattern != null && mFreeShortCodePattern.matcher(phoneNumber)
210                    .matches()) {
211                return CATEGORY_FREE_SHORT_CODE;
212            }
213            if (mStandardShortCodePattern != null && mStandardShortCodePattern.matcher(phoneNumber)
214                    .matches()) {
215                return CATEGORY_STANDARD_SHORT_CODE;
216            }
217            if (mPremiumShortCodePattern != null && mPremiumShortCodePattern.matcher(phoneNumber)
218                    .matches()) {
219                return CATEGORY_PREMIUM_SHORT_CODE;
220            }
221            if (mShortCodePattern != null && mShortCodePattern.matcher(phoneNumber).matches()) {
222                return CATEGORY_POSSIBLE_PREMIUM_SHORT_CODE;
223            }
224            return CATEGORY_NOT_SHORT_CODE;
225        }
226    }
227
228    /**
229     * Observe the secure setting for enable flag
230     */
231    private static class SettingsObserver extends ContentObserver {
232        private final Context mContext;
233        private final AtomicBoolean mEnabled;
234
235        SettingsObserver(Handler handler, Context context, AtomicBoolean enabled) {
236            super(handler);
237            mContext = context;
238            mEnabled = enabled;
239            onChange(false);
240        }
241
242        @Override
243        public void onChange(boolean selfChange) {
244            mEnabled.set(Settings.Global.getInt(mContext.getContentResolver(),
245                    Settings.Global.SMS_SHORT_CODE_CONFIRMATION, 1) != 0);
246        }
247    }
248
249    private static class SettingsObserverHandler extends Handler {
250        SettingsObserverHandler(Context context, AtomicBoolean enabled) {
251            ContentResolver resolver = context.getContentResolver();
252            ContentObserver globalObserver = new SettingsObserver(this, context, enabled);
253            resolver.registerContentObserver(Settings.Global.getUriFor(
254                    Settings.Global.SMS_SHORT_CODE_CONFIRMATION), false, globalObserver);
255        }
256    }
257
258    /**
259     * Create SMS usage monitor.
260     * @param context the context to use to load resources and get TelephonyManager service
261     */
262    public SmsUsageMonitor(Context context) {
263        mContext = context;
264        ContentResolver resolver = context.getContentResolver();
265
266        mMaxAllowed = Settings.Global.getInt(resolver,
267                Settings.Global.SMS_OUTGOING_CHECK_MAX_COUNT,
268                DEFAULT_SMS_MAX_COUNT);
269
270        mCheckPeriod = Settings.Global.getInt(resolver,
271                Settings.Global.SMS_OUTGOING_CHECK_INTERVAL_MS,
272                DEFAULT_SMS_CHECK_PERIOD);
273
274        mSettingsObserverHandler = new SettingsObserverHandler(mContext, mCheckEnabled);
275
276        loadPremiumSmsPolicyDb();
277    }
278
279    /**
280     * Return a pattern matcher object for the specified country.
281     * @param country the country to search for
282     * @return a {@link ShortCodePatternMatcher} for the specified country, or null if not found
283     */
284    private ShortCodePatternMatcher getPatternMatcherFromFile(String country) {
285        FileReader patternReader = null;
286        XmlPullParser parser = null;
287        try {
288            patternReader = new FileReader(mPatternFile);
289            parser = Xml.newPullParser();
290            parser.setInput(patternReader);
291            return getPatternMatcherFromXmlParser(parser, country);
292        } catch (FileNotFoundException e) {
293            Log.e(TAG, "Short Code Pattern File not found");
294        } catch (XmlPullParserException e) {
295            Log.e(TAG, "XML parser exception reading short code pattern file", e);
296        } finally {
297            mPatternFileLastModified = mPatternFile.lastModified();
298            if (patternReader != null) {
299                try {
300                    patternReader.close();
301                } catch (IOException e) {}
302            }
303        }
304        return null;
305    }
306
307    private ShortCodePatternMatcher getPatternMatcherFromResource(String country) {
308        int id = com.android.internal.R.xml.sms_short_codes;
309        XmlResourceParser parser = null;
310        try {
311            parser = mContext.getResources().getXml(id);
312            return getPatternMatcherFromXmlParser(parser, country);
313        } finally {
314            if (parser != null) parser.close();
315        }
316    }
317
318    private ShortCodePatternMatcher getPatternMatcherFromXmlParser(XmlPullParser parser,
319            String country) {
320        try {
321            XmlUtils.beginDocument(parser, TAG_SHORTCODES);
322
323            while (true) {
324                XmlUtils.nextElement(parser);
325                String element = parser.getName();
326                if (element == null) break;
327
328                if (element.equals(TAG_SHORTCODE)) {
329                    String currentCountry = parser.getAttributeValue(null, ATTR_COUNTRY);
330                    if (country.equals(currentCountry)) {
331                        String pattern = parser.getAttributeValue(null, ATTR_PATTERN);
332                        String premium = parser.getAttributeValue(null, ATTR_PREMIUM);
333                        String free = parser.getAttributeValue(null, ATTR_FREE);
334                        String standard = parser.getAttributeValue(null, ATTR_STANDARD);
335                        return new ShortCodePatternMatcher(pattern, premium, free, standard);
336                    }
337                } else {
338                    Log.e(TAG, "Error: skipping unknown XML tag " + element);
339                }
340            }
341        } catch (XmlPullParserException e) {
342            Log.e(TAG, "XML parser exception reading short code patterns", e);
343        } catch (IOException e) {
344            Log.e(TAG, "I/O exception reading short code patterns", e);
345        }
346        return null;    // country not found
347    }
348
349    /** Clear the SMS application list for disposal. */
350    void dispose() {
351        mSmsStamp.clear();
352    }
353
354    /**
355     * Check to see if an application is allowed to send new SMS messages, and confirm with
356     * user if the send limit was reached or if a non-system app is potentially sending to a
357     * premium SMS short code or number.
358     *
359     * @param appName the package name of the app requesting to send an SMS
360     * @param smsWaiting the number of new messages desired to send
361     * @return true if application is allowed to send the requested number
362     *  of new sms messages
363     */
364    public boolean check(String appName, int smsWaiting) {
365        synchronized (mSmsStamp) {
366            removeExpiredTimestamps();
367
368            ArrayList<Long> sentList = mSmsStamp.get(appName);
369            if (sentList == null) {
370                sentList = new ArrayList<Long>();
371                mSmsStamp.put(appName, sentList);
372            }
373
374            return isUnderLimit(sentList, smsWaiting);
375        }
376    }
377
378    /**
379     * Check if the destination is a possible premium short code.
380     * NOTE: the caller is expected to strip non-digits from the destination number with
381     * {@link PhoneNumberUtils#extractNetworkPortion} before calling this method.
382     * This happens in {@link SMSDispatcher#sendRawPdu} so that we use the same phone number
383     * for testing and in the user confirmation dialog if the user needs to confirm the number.
384     * This makes it difficult for malware to fool the user or the short code pattern matcher
385     * by using non-ASCII characters to make the number appear to be different from the real
386     * destination phone number.
387     *
388     * @param destAddress the destination address to test for possible short code
389     * @return {@link #CATEGORY_NOT_SHORT_CODE}, {@link #CATEGORY_FREE_SHORT_CODE},
390     *  {@link #CATEGORY_POSSIBLE_PREMIUM_SHORT_CODE}, or {@link #CATEGORY_PREMIUM_SHORT_CODE}.
391     */
392    public int checkDestination(String destAddress, String countryIso) {
393        synchronized (mSettingsObserverHandler) {
394            // always allow emergency numbers
395            if (PhoneNumberUtils.isEmergencyNumber(destAddress, countryIso)) {
396                return CATEGORY_NOT_SHORT_CODE;
397            }
398            // always allow if the feature is disabled
399            if (!mCheckEnabled.get()) {
400                return CATEGORY_NOT_SHORT_CODE;
401            }
402
403            ShortCodePatternMatcher patternMatcher = null;
404
405            if (countryIso != null) {
406                if (mCurrentCountry == null || !countryIso.equals(mCurrentCountry) ||
407                        mPatternFile.lastModified() != mPatternFileLastModified) {
408                    if (mPatternFile.exists()) {
409                        if (DBG) Log.d(TAG, "Loading SMS Short Code patterns from file");
410                        patternMatcher = getPatternMatcherFromFile(countryIso);
411                    } else {
412                        if (DBG) Log.d(TAG, "Loading SMS Short Code patterns from resource");
413                        patternMatcher = getPatternMatcherFromResource(countryIso);
414                    }
415                    mCurrentCountry = countryIso;
416                    mCurrentPatternMatcher = patternMatcher;    // may be null if not found
417                }
418            }
419
420            if (patternMatcher != null) {
421                return patternMatcher.getNumberCategory(destAddress);
422            } else {
423                // Generic rule: numbers of 5 digits or less are considered potential short codes
424                Log.e(TAG, "No patterns for \"" + countryIso + "\": using generic short code rule");
425                if (destAddress.length() <= 5) {
426                    return CATEGORY_POSSIBLE_PREMIUM_SHORT_CODE;
427                } else {
428                    return CATEGORY_NOT_SHORT_CODE;
429                }
430            }
431        }
432    }
433
434    /**
435     * Load the premium SMS policy from an XML file.
436     * Based on code from NotificationManagerService.
437     */
438    private void loadPremiumSmsPolicyDb() {
439        synchronized (mPremiumSmsPolicy) {
440            if (mPolicyFile == null) {
441                File dir = new File(SMS_POLICY_FILE_DIRECTORY);
442                mPolicyFile = new AtomicFile(new File(dir, SMS_POLICY_FILE_NAME));
443
444                mPremiumSmsPolicy.clear();
445
446                FileInputStream infile = null;
447                try {
448                    infile = mPolicyFile.openRead();
449                    final XmlPullParser parser = Xml.newPullParser();
450                    parser.setInput(infile, null);
451
452                    XmlUtils.beginDocument(parser, TAG_SMS_POLICY_BODY);
453
454                    while (true) {
455                        XmlUtils.nextElement(parser);
456
457                        String element = parser.getName();
458                        if (element == null) break;
459
460                        if (element.equals(TAG_PACKAGE)) {
461                            String packageName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
462                            String policy = parser.getAttributeValue(null, ATTR_PACKAGE_SMS_POLICY);
463                            if (packageName == null) {
464                                Log.e(TAG, "Error: missing package name attribute");
465                            } else if (policy == null) {
466                                Log.e(TAG, "Error: missing package policy attribute");
467                            } else try {
468                                mPremiumSmsPolicy.put(packageName, Integer.parseInt(policy));
469                            } catch (NumberFormatException e) {
470                                Log.e(TAG, "Error: non-numeric policy type " + policy);
471                            }
472                        } else {
473                            Log.e(TAG, "Error: skipping unknown XML tag " + element);
474                        }
475                    }
476                } catch (FileNotFoundException e) {
477                    // No data yet
478                } catch (IOException e) {
479                    Log.e(TAG, "Unable to read premium SMS policy database", e);
480                } catch (NumberFormatException e) {
481                    Log.e(TAG, "Unable to parse premium SMS policy database", e);
482                } catch (XmlPullParserException e) {
483                    Log.e(TAG, "Unable to parse premium SMS policy database", e);
484                } finally {
485                    if (infile != null) {
486                        try {
487                            infile.close();
488                        } catch (IOException ignored) {
489                        }
490                    }
491                }
492            }
493        }
494    }
495
496    /**
497     * Persist the premium SMS policy to an XML file.
498     * Based on code from NotificationManagerService.
499     */
500    private void writePremiumSmsPolicyDb() {
501        synchronized (mPremiumSmsPolicy) {
502            FileOutputStream outfile = null;
503            try {
504                outfile = mPolicyFile.startWrite();
505
506                XmlSerializer out = new FastXmlSerializer();
507                out.setOutput(outfile, "utf-8");
508
509                out.startDocument(null, true);
510
511                out.startTag(null, TAG_SMS_POLICY_BODY);
512
513                for (Map.Entry<String, Integer> policy : mPremiumSmsPolicy.entrySet()) {
514                    out.startTag(null, TAG_PACKAGE);
515                    out.attribute(null, ATTR_PACKAGE_NAME, policy.getKey());
516                    out.attribute(null, ATTR_PACKAGE_SMS_POLICY, policy.getValue().toString());
517                    out.endTag(null, TAG_PACKAGE);
518                }
519
520                out.endTag(null, TAG_SMS_POLICY_BODY);
521                out.endDocument();
522
523                mPolicyFile.finishWrite(outfile);
524            } catch (IOException e) {
525                Log.e(TAG, "Unable to write premium SMS policy database", e);
526                if (outfile != null) {
527                    mPolicyFile.failWrite(outfile);
528                }
529            }
530        }
531    }
532
533    /**
534     * Returns the premium SMS permission for the specified package. If the package has never
535     * been seen before, the default {@link #PREMIUM_SMS_PERMISSION_ASK_USER}
536     * will be returned.
537     * @param packageName the name of the package to query permission
538     * @return one of {@link #PREMIUM_SMS_PERMISSION_UNKNOWN},
539     *  {@link #PREMIUM_SMS_PERMISSION_ASK_USER},
540     *  {@link #PREMIUM_SMS_PERMISSION_NEVER_ALLOW}, or
541     *  {@link #PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW}
542     * @throws SecurityException if the caller is not a system process
543     */
544    public int getPremiumSmsPermission(String packageName) {
545        checkCallerIsSystemOrSameApp(packageName);
546        synchronized (mPremiumSmsPolicy) {
547            Integer policy = mPremiumSmsPolicy.get(packageName);
548            if (policy == null) {
549                return PREMIUM_SMS_PERMISSION_UNKNOWN;
550            } else {
551                return policy;
552            }
553        }
554    }
555
556    /**
557     * Sets the premium SMS permission for the specified package and save the value asynchronously
558     * to persistent storage.
559     * @param packageName the name of the package to set permission
560     * @param permission one of {@link #PREMIUM_SMS_PERMISSION_ASK_USER},
561     *  {@link #PREMIUM_SMS_PERMISSION_NEVER_ALLOW}, or
562     *  {@link #PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW}
563     * @throws SecurityException if the caller is not a system process
564     */
565    public void setPremiumSmsPermission(String packageName, int permission) {
566        checkCallerIsSystemOrPhoneApp();
567        if (permission < PREMIUM_SMS_PERMISSION_ASK_USER
568                || permission > PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW) {
569            throw new IllegalArgumentException("invalid SMS permission type " + permission);
570        }
571        synchronized (mPremiumSmsPolicy) {
572            mPremiumSmsPolicy.put(packageName, permission);
573        }
574        // write policy file in the background
575        new Thread(new Runnable() {
576            @Override
577            public void run() {
578                writePremiumSmsPolicyDb();
579            }
580        }).start();
581    }
582
583    private static void checkCallerIsSystemOrSameApp(String pkg) {
584        int uid = Binder.getCallingUid();
585        if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
586            return;
587        }
588        try {
589            ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
590                    pkg, 0, UserHandle.getCallingUserId());
591            if (!UserHandle.isSameApp(ai.uid, uid)) {
592                throw new SecurityException("Calling uid " + uid + " gave package"
593                        + pkg + " which is owned by uid " + ai.uid);
594            }
595        } catch (RemoteException re) {
596            throw new SecurityException("Unknown package " + pkg + "\n" + re);
597        }
598    }
599
600    private static void checkCallerIsSystemOrPhoneApp() {
601        int uid = Binder.getCallingUid();
602        int appId = UserHandle.getAppId(uid);
603        if (appId == Process.SYSTEM_UID || appId == Process.PHONE_UID || uid == 0) {
604            return;
605        }
606        throw new SecurityException("Disallowed call for uid " + uid);
607    }
608
609    /**
610     * Remove keys containing only old timestamps. This can happen if an SMS app is used
611     * to send messages and then uninstalled.
612     */
613    private void removeExpiredTimestamps() {
614        long beginCheckPeriod = System.currentTimeMillis() - mCheckPeriod;
615
616        synchronized (mSmsStamp) {
617            Iterator<Map.Entry<String, ArrayList<Long>>> iter = mSmsStamp.entrySet().iterator();
618            while (iter.hasNext()) {
619                Map.Entry<String, ArrayList<Long>> entry = iter.next();
620                ArrayList<Long> oldList = entry.getValue();
621                if (oldList.isEmpty() || oldList.get(oldList.size() - 1) < beginCheckPeriod) {
622                    iter.remove();
623                }
624            }
625        }
626    }
627
628    private boolean isUnderLimit(ArrayList<Long> sent, int smsWaiting) {
629        Long ct = System.currentTimeMillis();
630        long beginCheckPeriod = ct - mCheckPeriod;
631
632        if (VDBG) log("SMS send size=" + sent.size() + " time=" + ct);
633
634        while (!sent.isEmpty() && sent.get(0) < beginCheckPeriod) {
635            sent.remove(0);
636        }
637
638        if ((sent.size() + smsWaiting) <= mMaxAllowed) {
639            for (int i = 0; i < smsWaiting; i++ ) {
640                sent.add(ct);
641            }
642            return true;
643        }
644        return false;
645    }
646
647    private static void log(String msg) {
648        Log.d(TAG, msg);
649    }
650}
651