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 { BlockedNumbersAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)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 bindView(View view, final Context context, final Cursor cursor)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 final String formattedNumber = BlockedNumbersUtil.formatNumber(rawNumber); 45 TextView numberView = (TextView) view.findViewById(R.id.blocked_number); 46 Spannable numberSpannable = new SpannableString(formattedNumber); 47 PhoneNumberUtils.addTtsSpan(numberSpannable, 0, numberSpannable.length()); 48 numberView.setText(numberSpannable); 49 50 View deleteButton = view.findViewById(R.id.delete_blocked_number); 51 deleteButton.setOnClickListener(new View.OnClickListener() { 52 @Override 53 public void onClick(View view) { 54 showDeleteBlockedNumberDialog(context, rawNumber, formattedNumber); 55 } 56 }); 57 } 58 showDeleteBlockedNumberDialog(final Context context, final String rawNumber, final String formattedNumber)59 private void showDeleteBlockedNumberDialog(final Context context, final String rawNumber, 60 final String formattedNumber) { 61 String message = context.getString(R.string.unblock_dialog_body, formattedNumber); 62 int startingPosition = message.indexOf(formattedNumber); 63 Spannable messageSpannable = new SpannableString(message); 64 PhoneNumberUtils.addTtsSpan(messageSpannable, startingPosition, 65 startingPosition + formattedNumber.length()); 66 new AlertDialog.Builder(context) 67 .setMessage(messageSpannable) 68 .setPositiveButton(R.string.unblock_button, 69 new DialogInterface.OnClickListener() { 70 public void onClick(DialogInterface dialog, int id) { 71 deleteBlockedNumber(context, rawNumber); 72 } 73 } 74 ) 75 .setNegativeButton(android.R.string.cancel, 76 new DialogInterface.OnClickListener() { 77 public void onClick(DialogInterface dialog, int id) { 78 dialog.dismiss(); 79 } 80 } 81 ) 82 .create() 83 .show(); 84 } 85 deleteBlockedNumber(Context context, String number)86 private void deleteBlockedNumber(Context context, String number) { 87 ContentResolver contentResolver = context.getContentResolver(); 88 contentResolver.delete(BlockedNumberContract.BlockedNumbers.CONTENT_URI, 89 BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER + "=?", 90 new String[] {number}); 91 BlockedNumbersUtil.showToastWithFormattedNumber(mContext, 92 R.string.blocked_numbers_number_unblocked_message, number); 93 } 94 } 95