blob: 58d6800adbba13feb59ff7c6ed792e1d1809d49b [file] [log] [blame]
Chiao Cheng1f4214d2012-10-30 09:43:39 -07001/*
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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.util;
Chiao Cheng1f4214d2012-10-30 09:43:39 -070018
19import android.content.Context;
20import android.os.AsyncTask;
21import android.telephony.PhoneNumberFormattingTextWatcher;
22import android.widget.TextView;
23
Gary Mai0a49afa2016-12-05 15:53:58 -080024import com.android.contacts.GeoUtil;
Gary Mai69c182a2016-12-05 13:07:03 -080025import com.android.contacts.compat.PhoneNumberFormattingTextWatcherCompat;
Chiao Cheng1f4214d2012-10-30 09:43:39 -070026
27public final class PhoneNumberFormatter {
28 private PhoneNumberFormatter() {}
29
30 /**
31 * Load {@link TextWatcherLoadAsyncTask} in a worker thread and set it to a {@link TextView}.
32 */
33 private static class TextWatcherLoadAsyncTask extends
34 AsyncTask<Void, Void, PhoneNumberFormattingTextWatcher> {
35 private final String mCountryCode;
36 private final TextView mTextView;
guanxiongliu7cb94832016-02-01 16:28:59 -080037 private final boolean mFormatAfterWatcherSet;
Chiao Cheng1f4214d2012-10-30 09:43:39 -070038
guanxiongliu7cb94832016-02-01 16:28:59 -080039 public TextWatcherLoadAsyncTask(
40 String countryCode, TextView textView, boolean formatAfterWatcherSet) {
Chiao Cheng1f4214d2012-10-30 09:43:39 -070041 mCountryCode = countryCode;
42 mTextView = textView;
guanxiongliu7cb94832016-02-01 16:28:59 -080043 mFormatAfterWatcherSet = formatAfterWatcherSet;
Chiao Cheng1f4214d2012-10-30 09:43:39 -070044 }
45
46 @Override
47 protected PhoneNumberFormattingTextWatcher doInBackground(Void... params) {
Wenyi Wangfe4048c2015-12-21 15:53:43 -080048 return PhoneNumberFormattingTextWatcherCompat.newInstance(mCountryCode);
Chiao Cheng1f4214d2012-10-30 09:43:39 -070049 }
50
51 @Override
52 protected void onPostExecute(PhoneNumberFormattingTextWatcher watcher) {
53 if (watcher == null || isCancelled()) {
54 return; // May happen if we cancel the task.
55 }
guanxiongliu7cb94832016-02-01 16:28:59 -080056
Chiao Cheng1f4214d2012-10-30 09:43:39 -070057 // Setting a text changed listener is safe even after the view is detached.
58 mTextView.addTextChangedListener(watcher);
59
guanxiongliu7cb94832016-02-01 16:28:59 -080060 // Forcing formatting the existing phone number
61 if (mFormatAfterWatcherSet) {
62 watcher.afterTextChanged(mTextView.getEditableText());
63 }
Chiao Cheng1f4214d2012-10-30 09:43:39 -070064 }
65 }
66
67 /**
68 * Delay-set {@link PhoneNumberFormattingTextWatcher} to a {@link TextView}.
69 */
70 public static final void setPhoneNumberFormattingTextWatcher(Context context,
71 TextView textView) {
guanxiongliu7cb94832016-02-01 16:28:59 -080072 setPhoneNumberFormattingTextWatcher(context, textView,
73 /* formatAfterWatcherSet =*/ false);
74 }
75
76 /**
77 * Delay-sets {@link PhoneNumberFormattingTextWatcher} to a {@link TextView}
78 * and formats the number immediately if formatAfterWaterSet is true.
79 * In some cases, formatting before user editing might cause unwanted results
80 * (e.g. the editor thinks the user changed the content, and would save
81 * when closed even when the user didn't make other changes.)
82 */
83 public static final void setPhoneNumberFormattingTextWatcher(
84 Context context, TextView textView, boolean formatAfterWatcherSet) {
85 new TextWatcherLoadAsyncTask(GeoUtil.getCurrentCountryIso(context),
86 textView, formatAfterWatcherSet)
Chiao Cheng1f4214d2012-10-30 09:43:39 -070087 .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
88 }
89}