1 /* 2 * Copyright (C) 2016 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 17 package com.android.server.telecom.settings; 18 19 import android.app.AlertDialog; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.content.DialogInterface; 23 import android.database.Cursor; 24 import android.provider.BlockedNumberContract; 25 import android.telephony.PhoneNumberUtils; 26 import android.text.Spannable; 27 import android.text.SpannableString; 28 import android.view.View; 29 import android.widget.SimpleCursorAdapter; 30 import android.widget.TextView; 31 import com.android.server.telecom.R; 32 33 public class BlockedNumbersAdapter extends SimpleCursorAdapter { 34 public BlockedNumbersAdapter(Context context, int layout, Cursor c, String[] from, int[] to, 35 int flags) { 36 super(context, layout, c, from, to, flags); 37 } 38 39 @Override 40 public void bindView(View view, final Context context, final Cursor cursor) { 41 super.bindView(view, context, cursor); 42 final String rawNumber = cursor.getString(cursor.getColumnIndex( 43 BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER)); 44 String formattedNumber = PhoneNumberUtils.formatNumber(rawNumber, 45 BlockedNumbersUtil.getLocaleDefaultToUS()); 46 final String finalFormattedNumber = formattedNumber == null ? rawNumber : formattedNumber; 47 48 TextView numberView = (TextView) view.findViewById(R.id.blocked_number); 49 Spannable numberSpannable = new SpannableString(finalFormattedNumber); 50 PhoneNumberUtils.addTtsSpan(numberSpannable, 0, numberSpannable.length()); 51 numberView.setText(numberSpannable); 52 53 View deleteButton = view.findViewById(R.id.delete_blocked_number); 54 deleteButton.setOnClickListener(new View.OnClickListener() { 55 @Override 56 public void onClick(View view) { 57 showDeleteBlockedNumberDialog(context, rawNumber, finalFormattedNumber); 58 } 59 }); 60 } 61 62 private void showDeleteBlockedNumberDialog(final Context context, final String rawNumber, 63 final String formattedNumber) { 64 String message = context.getString(R.string.unblock_dialog_body, formattedNumber); 65 int startingPosition = message.indexOf(formattedNumber); 66 Spannable messageSpannable = new SpannableString(message); 67 PhoneNumberUtils.addTtsSpan(messageSpannable, startingPosition, 68 startingPosition + formattedNumber.length()); 69 new AlertDialog.Builder(context) 70 .setMessage(messageSpannable) 71 .setPositiveButton(R.string.unblock_button, 72 new DialogInterface.OnClickListener() { 73 public void onClick(DialogInterface dialog, int id) { 74 deleteBlockedNumber(context, rawNumber); 75 } 76 } 77 ) 78 .setNegativeButton(android.R.string.cancel, 79 new DialogInterface.OnClickListener() { 80 public void onClick(DialogInterface dialog, int id) { 81 dialog.dismiss(); 82 } 83 } 84 ) 85 .create() 86 .show(); 87 } 88 89 private void deleteBlockedNumber(Context context, String number) { 90 ContentResolver contentResolver = context.getContentResolver(); 91 contentResolver.delete(BlockedNumberContract.BlockedNumbers.CONTENT_URI, 92 BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER + "=?", 93 new String[] {number}); 94 BlockedNumbersUtil.showToastWithFormattedNumber(mContext, 95 R.string.blocked_numbers_number_unblocked_message, number); 96 } 97 } 98