diff options
author | Rohit Yengisetty <rohit@cyngn.com> | 2016-01-12 22:37:33 -0800 |
---|---|---|
committer | Rohit Yengisetty <rohit@cyngn.com> | 2016-01-12 22:37:33 -0800 |
commit | cf866936d5b061ed31b5dab35b6624e1c05b22bd (patch) | |
tree | f50be12d07e55a21619c396ea22598d069663c02 /src | |
parent | bbf7e8276d72c2e524c13cf659c64e473385265e (diff) | |
download | android_packages_apps_ContactsCommon-cf866936d5b061ed31b5dab35b6624e1c05b22bd.tar.gz android_packages_apps_ContactsCommon-cf866936d5b061ed31b5dab35b6624e1c05b22bd.tar.bz2 android_packages_apps_ContactsCommon-cf866936d5b061ed31b5dab35b6624e1c05b22bd.zip |
Add block contact dialog and supporting helper classes
Change-Id: I7f2697a843d4f615fe1ae9e8525baaefcbe9428b
Diffstat (limited to 'src')
3 files changed, 306 insertions, 0 deletions
diff --git a/src/com/android/contacts/common/activity/fragment/BlockContactDialogFragment.java b/src/com/android/contacts/common/activity/fragment/BlockContactDialogFragment.java new file mode 100644 index 00000000..cb4e099b --- /dev/null +++ b/src/com/android/contacts/common/activity/fragment/BlockContactDialogFragment.java @@ -0,0 +1,121 @@ +package com.android.contacts.common.activity.fragment; + +import android.app.Activity; +import android.app.DialogFragment; +import android.app.Dialog; +import android.content.Context; +import android.os.Bundle; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.CheckBox; +import com.android.contacts.common.R; + +/** + * Interacts with the framework implementation of Blacklist and any phonenumber Lookup Providers + * interested in spam collection + * + * NOTE: ensure you have Blacklist permissions before using this class + */ +public class BlockContactDialogFragment extends DialogFragment + implements DialogInterface.OnClickListener { + + public static final int BLOCK_MODE = 0; + public static final int UNBLOCK_MODE = 1; + public static final String KEY_CURRENT_LOOKUP_PROVIDER_NAME = "CURRENT_LOOKUP_PROVIDER_NAME"; + public static final String KEY_LAUNCH_MODE = "LAUNCH_MODE"; + + private int mLaunchMode; + private CheckBox mNotifyProviderCheckBox; + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + String lookupProviderName = null; + if (getArguments() != null) { + Bundle bundle = getArguments(); + mLaunchMode = bundle.getInt(KEY_LAUNCH_MODE); + lookupProviderName = bundle.getString(KEY_CURRENT_LOOKUP_PROVIDER_NAME); + } + + Activity hostActivity = getActivity(); + boolean blockMode = mLaunchMode == BLOCK_MODE; + String dialogTitle; + String positiveButtonText; + + AlertDialog.Builder builder = new AlertDialog.Builder(hostActivity); + LayoutInflater inflater = (LayoutInflater) hostActivity.getSystemService( + Context.LAYOUT_INFLATER_SERVICE); + View v = inflater.inflate(R.layout.block_contact_dialog_view, null); + mNotifyProviderCheckBox = (CheckBox) v.findViewById(R.id.spamCheckbox); + + if (blockMode) { + dialogTitle = hostActivity.getString(R.string.block_dialog_title); + positiveButtonText = hostActivity.getString(R.string.block_dialog_positive); + } else { + // unblock mode + dialogTitle = hostActivity.getString(R.string.unblock_dialog_title); + positiveButtonText = hostActivity.getString(R.string.unblock_dialog_positive); + v.findViewById(R.id.description).setVisibility(View.GONE); + } + + String checkboxDescription = TextUtils.isEmpty(lookupProviderName) ? + null : + hostActivity.getResources().getString(R.string.block_dialog_report_spam, + lookupProviderName); + + if (!blockMode || TextUtils.isEmpty(checkboxDescription)) { + mNotifyProviderCheckBox.setChecked(false); + mNotifyProviderCheckBox.setVisibility(View.GONE); + } else { + mNotifyProviderCheckBox.setText(checkboxDescription); + mNotifyProviderCheckBox.setVisibility(View.VISIBLE); + } + + builder.setTitle(dialogTitle) + .setView(v) + .setPositiveButton(positiveButtonText, this) + .setNegativeButton(R.string.block_dialog_negative, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + return builder.create(); + } + + @Override + public void onCancel(DialogInterface dialog) {} + + @Override + public void onClick(DialogInterface dialog, int which) { + boolean mCheckboxStatus = mNotifyProviderCheckBox.isChecked(); + Activity parentActivity = getActivity(); + if (parentActivity instanceof BlockContactCallbacks) { + if (mLaunchMode == BLOCK_MODE) { + ((BlockContactCallbacks) parentActivity).onBlockContact(mCheckboxStatus); + } else { + ((BlockContactCallbacks) parentActivity).onUnblockContact(mCheckboxStatus); + } + } + } + + public interface BlockContactCallbacks { + /** + * Callback noting that the user opted to block the contact + * + * @param notifyLookupProvider indicates whether the user opted to report the contact + * to the current LookupProvider + */ + void onBlockContact(boolean notifyLookupProvider); + + /** + * Callback noting that the user opted to unblock the contact + * + * @param notifyLookupProvider indicates whether the user opted to notify the current + * LookupProvider of the unblock + */ + void onUnblockContact(boolean notifyLookupProvider); + } +} diff --git a/src/com/android/contacts/common/util/BlockContactHelper.java b/src/com/android/contacts/common/util/BlockContactHelper.java new file mode 100644 index 00000000..c605a978 --- /dev/null +++ b/src/com/android/contacts/common/util/BlockContactHelper.java @@ -0,0 +1,179 @@ +package com.android.contacts.common.util; + +import android.app.DialogFragment; +import android.content.Context; +import android.os.AsyncTask; +import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; +import android.telephony.PhoneNumberUtils; +import android.text.TextUtils; +import com.android.contacts.common.R; +import com.android.contacts.common.activity.fragment.BlockContactDialogFragment; +import com.android.contacts.common.model.Contact; +import com.android.contacts.common.model.RawContact; +import com.android.contacts.common.model.dataitem.DataItem; +import com.android.contacts.common.model.dataitem.PhoneDataItem; +import com.android.internal.telephony.util.BlacklistUtils; +import com.cyanogen.lookup.phonenumber.contract.LookupProvider; + +import java.util.List; + +/** + * Helper class used to interface with the framework implementation of Blacklist and delegating + * the apropos information to the active phonenumber LookupProvider, if any. + */ +public class BlockContactHelper { + private final Context mContext; + private AsyncTask mBackgroundTask; + private Contact mContact; + private LookupProvider mLookupProvider; + private volatile boolean mIsBlacklisted; + private volatile boolean mIsProviderInitialized; + private boolean mBackgroundTaskCompleted; + + public enum BlockMode { + BLOCK, + UNBLOCK + } + + public BlockContactHelper(Context context, LookupProvider lookupProvider) { + mContext = context; + mLookupProvider = lookupProvider; + } + + public void setContactInfo(Contact contact) { + mContact = contact; + } + + public void gatherDataInBackground() { + mBackgroundTask = new AsyncTask<Void, Void, Void>() { + @Override + protected Void doInBackground(Void... params) { + if (mContact == null) return null; + // check blacklist status of all of the contact's numbers + contact: + for (RawContact rawContact : mContact.getRawContacts()) { + List<DataItem> data = rawContact.getDataItems(); + for(DataItem item : data) { + if (item instanceof PhoneDataItem) { + PhoneDataItem phoneDataItem = (PhoneDataItem) item; + if (isBlacklisted(phoneDataItem.getNumber())) { + mIsBlacklisted = true; + break contact; + } + } + } + } + + if (mLookupProvider.initialize()) { + mIsProviderInitialized = true; + } + + mBackgroundTaskCompleted = true; + return null; + } + }.execute(); + } + + public boolean isContactBlacklisted() { + return mIsBlacklisted; + } + + /** + * Note: run on non-ui thread as this call executes a content provider query + */ + private void toggleBlacklistStatus(String number, boolean shouldBlacklist) { + String nn = PhoneNumberUtils.normalizeNumber(number); + BlacklistUtils.addOrUpdate(mContext, nn, + shouldBlacklist ? BlacklistUtils.BLOCK_CALLS | BlacklistUtils.BLOCK_MESSAGES + : 0, BlacklistUtils.BLOCK_CALLS | BlacklistUtils.BLOCK_MESSAGES); + } + + /** + * Note: run on non-ui thread as this call executes a content provider query + */ + private boolean isBlacklisted(String number) { + String nn = PhoneNumberUtils.normalizeNumber(number); + return BlacklistUtils.isListed(mContext, nn, BlacklistUtils.BLOCK_CALLS) + != BlacklistUtils.MATCH_NONE; + } + + public DialogFragment getBlockContactDialog(BlockMode blockMode) { + BlockContactDialogFragment f = new BlockContactDialogFragment(); + Bundle bundle = new Bundle(); + int launchMode = blockMode == BlockMode.BLOCK ? BlockContactDialogFragment.BLOCK_MODE : + BlockContactDialogFragment.UNBLOCK_MODE; + bundle.putInt(BlockContactDialogFragment.KEY_LAUNCH_MODE, launchMode); + String providerName = mLookupProvider.getDisplayName(); + bundle.putString(BlockContactDialogFragment.KEY_CURRENT_LOOKUP_PROVIDER_NAME, providerName); + + f.setArguments(bundle); + return f; + } + + public void blockContact(boolean notifyLookupProvider) { + for (RawContact rawContact : mContact.getRawContacts()) { + List<DataItem> data = rawContact.getDataItems(); + for(DataItem item : data) { + if (item instanceof PhoneDataItem) { + PhoneDataItem phoneDataItem = (PhoneDataItem) item; + String number = phoneDataItem.getNumber(); + toggleBlacklistStatus(number, true /*block contact*/); + + if (notifyLookupProvider && mIsProviderInitialized && + mLookupProvider.supportsSpamReporting()) { + String formattedNumber = PhoneNumberHelper.formatPhoneNumber(mContext, + number); + mLookupProvider.markAsSpam(formattedNumber); + } + } + } + } + + gatherDataInBackground(); + } + + public void unblockContact(boolean notifyLookupProvider) { + for (RawContact rawContact : mContact.getRawContacts()) { + List<DataItem> data = rawContact.getDataItems(); + for(DataItem item : data) { + if (item instanceof PhoneDataItem) { + PhoneDataItem phoneDataItem = (PhoneDataItem) item; + String number = phoneDataItem.getNumber(); + toggleBlacklistStatus(number, false /*unblock contact*/); + + if (notifyLookupProvider && mIsProviderInitialized && + mLookupProvider.supportsSpamReporting()) { + String formattedNumber = PhoneNumberHelper.formatPhoneNumber(mContext, + number); + mLookupProvider.unmarkAsSpam(formattedNumber); + } + } + } + } + + gatherDataInBackground(); + } + + public void blockContactAsync(final boolean notifyLookupProvider) { + new AsyncTask<Void, Void, Void>() { + @Override + protected Void doInBackground(Void... params) { + blockContact(notifyLookupProvider); + return null; + } + }.execute(); + } + + public void unblockContactAsync(final boolean notifyLookupProvider) { + new AsyncTask<Void, Void, Void>() { + @Override + protected Void doInBackground(Void... params) { + unblockContact(notifyLookupProvider); + return null; + } + }.execute(); + } + +} diff --git a/src/com/android/contacts/common/util/PhoneNumberHelper.java b/src/com/android/contacts/common/util/PhoneNumberHelper.java index 34514024..2b6efc66 100644 --- a/src/com/android/contacts/common/util/PhoneNumberHelper.java +++ b/src/com/android/contacts/common/util/PhoneNumberHelper.java @@ -16,10 +16,12 @@ package com.android.contacts.common.util; import android.content.Context; +import android.location.Geocoder; import android.telephony.PhoneNumberUtils; import android.text.TextUtils; import android.util.Log; +import com.android.contacts.common.GeoUtil; import com.google.i18n.phonenumbers.NumberParseException; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; @@ -144,6 +146,10 @@ public class PhoneNumberHelper { return result; } + public static String formatPhoneNumber(Context context, String phoneNumber) { + return formatNumber(phoneNumber, GeoUtil.getCurrentCountryIso(context)); + } + /** * Normalize a phone number by removing the characters other than digits. If * the given number has keypad letters, the letters will be converted to |