14a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor/*
24a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor * Copyright (C) 2012 The Android Open Source Project
34a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor *
44a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor * Licensed under the Apache License, Version 2.0 (the "License");
54a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor * you may not use this file except in compliance with the License.
64a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor * You may obtain a copy of the License at
74a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor *
84a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor *      http://www.apache.org/licenses/LICENSE-2.0
94a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor *
104a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor * Unless required by applicable law or agreed to in writing, software
114a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor * distributed under the License is distributed on an "AS IS" BASIS,
124a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor * See the License for the specific language governing permissions and
144a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor * limitations under the License.
154a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor */
164a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor
174a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylorpackage com.android.mms.util;
184a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor
194a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylorimport android.content.Context;
204a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylorimport android.os.AsyncTask;
214a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylorimport android.telephony.PhoneNumberFormattingTextWatcher;
224a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylorimport android.widget.TextView;
234a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor
24d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chenimport com.android.mms.MmsApp;
25d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chen
264a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylorpublic final class PhoneNumberFormatter {
274a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor    private PhoneNumberFormatter() {}
284a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor
294a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor    /**
304a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor     * Load {@link TextWatcherLoadAsyncTask} in a worker thread and set it to a {@link TextView}.
314a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor     */
324a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor    private static class TextWatcherLoadAsyncTask extends
334a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            AsyncTask<Void, Void, PhoneNumberFormattingTextWatcher> {
344a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        private final String mCountryCode;
354a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        private final TextView mTextView;
364a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor
374a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        public TextWatcherLoadAsyncTask(String countryCode, TextView textView) {
384a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            mCountryCode = countryCode;
394a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            mTextView = textView;
404a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        }
414a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor
424a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        @Override
434a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        protected PhoneNumberFormattingTextWatcher doInBackground(Void... params) {
444a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            return new PhoneNumberFormattingTextWatcher(mCountryCode);
454a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        }
464a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor
474a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        @Override
484a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        protected void onPostExecute(PhoneNumberFormattingTextWatcher watcher) {
494a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            if (watcher == null || isCancelled()) {
504a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor                return; // May happen if we cancel the task.
514a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            }
524a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            // Setting a text changed listener is safe even after the view is detached.
534a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            mTextView.addTextChangedListener(watcher);
544a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor
554a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            // Note changes the user made before onPostExecute() will not be formatted, but
564a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            // once they type the next letter we format the entire text, so it's not a big deal.
574a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            // (And loading PhoneNumberFormattingTextWatcher is usually fast enough.)
584a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            // We could use watcher.afterTextChanged(mTextView.getEditableText()) to force format
594a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            // the existing content here, but that could cause unwanted results.
604a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            // (e.g. the contact editor thinks the user changed the content, and would save
614a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            // when closed even when the user didn't make other changes.)
624a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        }
634a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor    }
644a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor
654a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor    /**
664a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor     * Delay-set {@link PhoneNumberFormattingTextWatcher} to a {@link TextView}.
674a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor     */
684a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor    public static final void setPhoneNumberFormattingTextWatcher(Context context,
694a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor            TextView textView) {
704a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor        new TextWatcherLoadAsyncTask(MmsApp.getApplication().getCurrentCountryIso(), textView)
714a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor                .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
724a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor    }
734a44fb2e6796db816197842f48514fcf08fb0c07Tom Taylor}
74