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 
32 import com.android.server.telecom.R;
33 
34 public class BlockedNumbersAdapter extends SimpleCursorAdapter {
BlockedNumbersAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)35     public BlockedNumbersAdapter(Context context, int layout, Cursor c, String[] from, int[] to,
36             int flags) {
37         super(context, layout, c, from, to, flags);
38     }
39 
40     @Override
bindView(View view, final Context context, final Cursor cursor)41     public void bindView(View view, final Context context, final Cursor cursor) {
42         super.bindView(view, context, cursor);
43         final String rawNumber = cursor.getString(cursor.getColumnIndex(
44                 BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER));
45         final String formattedNumber = BlockedNumbersUtil.formatNumber(rawNumber);
46         TextView textView = (TextView) view.findViewById(R.id.blocked_number);
47 
48         if (formattedNumber != null
49                 && formattedNumber.contains("@") || formattedNumber.contains("%40")) {
50             // An email address
51             textView.setText(formattedNumber);
52         } else {
53             // A phone number
54             Spannable numberSpannable = new SpannableString(formattedNumber);
55             PhoneNumberUtils.addTtsSpan(numberSpannable, 0, numberSpannable.length());
56             textView.setText(numberSpannable);
57         }
58 
59         View deleteButton = view.findViewById(R.id.delete_blocked_number);
60         deleteButton.setOnClickListener(new View.OnClickListener() {
61             @Override
62             public void onClick(View view) {
63                 showDeleteBlockedNumberDialog(context, rawNumber, formattedNumber);
64             }
65         });
66     }
67 
showDeleteBlockedNumberDialog(final Context context, final String rawNumber, final String formattedNumber)68     private void showDeleteBlockedNumberDialog(final Context context, final String rawNumber,
69             final String formattedNumber) {
70         String message = context.getString(R.string.unblock_dialog_body, formattedNumber);
71         int startingPosition = message.indexOf(formattedNumber);
72         Spannable messageSpannable = new SpannableString(message);
73         PhoneNumberUtils.addTtsSpan(messageSpannable, startingPosition,
74                 startingPosition + formattedNumber.length());
75         AlertDialog dialog = new AlertDialog.Builder(context)
76                 .setMessage(messageSpannable)
77                 .setPositiveButton(R.string.unblock_button,
78                         new DialogInterface.OnClickListener() {
79                             public void onClick(DialogInterface dialog, int id) {
80                                 deleteBlockedNumber(context, rawNumber);
81                             }
82                         }
83                 )
84                 .setNegativeButton(android.R.string.cancel,
85                         new DialogInterface.OnClickListener() {
86                             public void onClick(DialogInterface dialog, int id) {
87                                 dialog.dismiss();
88                             }
89                         }
90                 )
91                 .create();
92         dialog.setOnShowListener(new AlertDialog.OnShowListener() {
93             @Override
94             public void onShow(DialogInterface dialog) {
95                 ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setAllCaps(false);
96                 ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE).setAllCaps(false);
97             }
98         });
99         dialog.show();
100     }
101 
deleteBlockedNumber(Context context, String number)102     private void deleteBlockedNumber(Context context, String number) {
103         ContentResolver contentResolver = context.getContentResolver();
104         contentResolver.delete(BlockedNumberContract.BlockedNumbers.CONTENT_URI,
105                 BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER + "=?",
106                 new String[] {number});
107         BlockedNumbersUtil.showToastWithFormattedNumber(mContext,
108                 R.string.blocked_numbers_number_unblocked_message, number);
109     }
110 }
111