1/*
2 * Copyright (C) 2015 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 */
16package com.android.dialer.filterednumber;
17
18import android.app.FragmentManager;
19import android.content.Context;
20import android.database.Cursor;
21import android.telephony.PhoneNumberUtils;
22import android.view.View;
23
24import com.android.contacts.common.ContactPhotoManager;
25import com.android.contacts.common.GeoUtil;
26import com.android.dialer.R;
27import com.android.dialer.calllog.ContactInfoHelper;
28import com.android.dialer.database.FilteredNumberContract.FilteredNumberColumns;
29import com.android.dialer.logging.InteractionEvent;
30import com.android.dialer.logging.Logger;
31
32public class BlockedNumbersAdapter extends NumbersAdapter {
33
34    private BlockedNumbersAdapter(
35            Context context,
36            FragmentManager fragmentManager,
37            ContactInfoHelper contactInfoHelper,
38            ContactPhotoManager contactPhotoManager) {
39        super(context, fragmentManager, contactInfoHelper, contactPhotoManager);
40    }
41
42    public static BlockedNumbersAdapter newBlockedNumbersAdapter(
43            Context context, FragmentManager fragmentManager) {
44        return new BlockedNumbersAdapter(
45                context,
46                fragmentManager,
47                new ContactInfoHelper(context, GeoUtil.getCurrentCountryIso(context)),
48                ContactPhotoManager.getInstance(context));
49    }
50
51    @Override
52    public void bindView(View view, final Context context, Cursor cursor) {
53        super.bindView(view, context, cursor);
54        final Integer id = cursor.getInt(cursor.getColumnIndex(FilteredNumberColumns._ID));
55        final String countryIso = cursor.getString(cursor.getColumnIndex(
56                FilteredNumberColumns.COUNTRY_ISO));
57        final String number = cursor.getString(cursor.getColumnIndex(FilteredNumberColumns.NUMBER));
58        final String normalizedNumber = cursor.getString(cursor.getColumnIndex(
59                FilteredNumberColumns.NORMALIZED_NUMBER));
60
61        final View deleteButton = view.findViewById(R.id.delete_button);
62        deleteButton.setOnClickListener(new View.OnClickListener() {
63            @Override
64            public void onClick(View view) {
65                BlockNumberDialogFragment.show(
66                        id,
67                        number,
68                        countryIso,
69                        PhoneNumberUtils.formatNumber(number, countryIso),
70                        R.id.blocked_numbers_activity_container,
71                        getFragmentManager(),
72                        new BlockNumberDialogFragment.Callback() {
73                            @Override
74                            public void onFilterNumberSuccess() {}
75
76                            @Override
77                            public void onUnfilterNumberSuccess() {
78                                Logger.logInteraction(
79                                        InteractionEvent.UNBLOCK_NUMBER_MANAGEMENT_SCREEN);
80                            }
81
82                            @Override
83                            public void onChangeFilteredNumberUndo() {}
84                        });
85            }
86        });
87
88        updateView(view, number, countryIso);
89    }
90
91    @Override
92    public boolean isEmpty() {
93        // Always return false, so that the header with blocking-related options always shows.
94        return false;
95    }
96}
97