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.contacts.common.util;
18
19import android.content.Context;
20import android.os.AsyncTask;
21import android.telephony.PhoneNumberFormattingTextWatcher;
22import android.widget.TextView;
23
24import com.android.contacts.common.GeoUtil;
25
26public final class PhoneNumberFormatter {
27    private PhoneNumberFormatter() {}
28
29    /**
30     * Load {@link TextWatcherLoadAsyncTask} in a worker thread and set it to a {@link TextView}.
31     */
32    private static class TextWatcherLoadAsyncTask extends
33            AsyncTask<Void, Void, PhoneNumberFormattingTextWatcher> {
34        private final String mCountryCode;
35        private final TextView mTextView;
36
37        public TextWatcherLoadAsyncTask(String countryCode, TextView textView) {
38            mCountryCode = countryCode;
39            mTextView = textView;
40        }
41
42        @Override
43        protected PhoneNumberFormattingTextWatcher doInBackground(Void... params) {
44            return new PhoneNumberFormattingTextWatcher(mCountryCode);
45        }
46
47        @Override
48        protected void onPostExecute(PhoneNumberFormattingTextWatcher watcher) {
49            if (watcher == null || isCancelled()) {
50                return; // May happen if we cancel the task.
51            }
52            // Setting a text changed listener is safe even after the view is detached.
53            mTextView.addTextChangedListener(watcher);
54
55            // Note changes the user made before onPostExecute() will not be formatted, but
56            // once they type the next letter we format the entire text, so it's not a big deal.
57            // (And loading PhoneNumberFormattingTextWatcher is usually fast enough.)
58            // We could use watcher.afterTextChanged(mTextView.getEditableText()) to force format
59            // the existing content here, but that could cause unwanted results.
60            // (e.g. the contact editor thinks the user changed the content, and would save
61            // when closed even when the user didn't make other changes.)
62        }
63    }
64
65    /**
66     * Delay-set {@link PhoneNumberFormattingTextWatcher} to a {@link TextView}.
67     */
68    public static final void setPhoneNumberFormattingTextWatcher(Context context,
69            TextView textView) {
70        new TextWatcherLoadAsyncTask(GeoUtil.getCurrentCountryIso(context), textView)
71                .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
72    }
73}
74