summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/common/util/BlockContactHelper.java
blob: f04a12cbe982abef5a786d134c117494511c7530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (C) 2016 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts.common.util;

import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Context;
import android.os.AsyncTask;
import android.telephony.PhoneNumberUtils;

import com.android.contacts.common.activity.fragment.BlockContactDialogFragment;
import com.android.contacts.common.model.BlockRequest;
import com.android.contacts.common.model.Contact;
import com.android.internal.telephony.util.BlacklistUtils;
import com.cyanogen.lookup.phonenumber.contract.LookupProvider;
import com.cyanogen.lookup.phonenumber.provider.LookupProviderImpl;

/**
 * Helper class used to interface with the framework implementation of Blacklist and delegating
 * the apropos information to the active phonenumber LookupProvider, if any.
 *
 * Ensure that {@link #destroy()} is called so that the necessary resource cleanup
 * takes place
 */
public class BlockContactHelper {
    private final Context mContext;
    private AsyncTask mBackgroundTask;
    private BlockRequest mBlockRequest;
    private LookupProvider mLookupProvider;
    private volatile boolean mIsBlacklisted;
    private boolean mBackgroundTaskCompleted;
    private StatusCallbacks mListener;

    public enum BlockOperation {
        BLOCK,
        UNBLOCK
    }

    public BlockContactHelper(Context context) {
        mContext = context;
        mLookupProvider = LookupProviderImpl.INSTANCE.get(context);
    }

    public void setContactInfo(Contact contact) {
        mBlockRequest = BlockRequest.createFrom(mContext, contact);
        gatherDataInBackgroundAsync();
    }

    public void setContactInfo(String phoneNumber) {
        mBlockRequest = BlockRequest.createFrom(mContext, phoneNumber);
        gatherDataInBackgroundAsync();
    }

    public void setStatusListener(StatusCallbacks listener) {
        mListener = listener;
    }

    public void gatherDataInBackgroundAsync() {
        mBackgroundTask = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                gatherDataInBackground();
                mBackgroundTaskCompleted = true;
                return null;
            }

            @Override
            protected void onPostExecute(Void Void) {
                if (mListener != null) {
                    mListener.onInfoAvailable();
                }
            }
        }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
    }

    public void gatherDataInBackground() {
        if (mBlockRequest == null || mBlockRequest == BlockRequest.EMPTY) {
            return;
        }

        mIsBlacklisted = false;

        // check blacklist status of all of the contact's numbers
        for (String phoneNumber : mBlockRequest.phoneNumbers) {
            if (isBlacklisted(phoneNumber)) {
                mIsBlacklisted = true;
                break;
            }
        }
    }

    public boolean isContactBlacklisted() {
        return mIsBlacklisted;
    }

    public boolean canBlockContact(Context context) {
        boolean isBlacklistEnabled = BlacklistUtils.isBlacklistEnabled(context);
        return isBlacklistEnabled && mBlockRequest != null && mBlockRequest != BlockRequest.EMPTY;
    }

    public String getLookupProviderName() {
        if (mLookupProvider.isEnabled()) {
            return mLookupProvider.getDisplayName();
        } else {
            return null;
        }
    }

    /**
     * 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(BlockOperation blockMode) {
        return getBlockContactDialog(blockMode, null);
    }

    public DialogFragment getBlockContactDialog(BlockOperation blockMode, Fragment targetFragment) {
        int launchMode = blockMode == BlockOperation.BLOCK ? BlockContactDialogFragment.BLOCK_MODE :
                BlockContactDialogFragment.UNBLOCK_MODE;
        return BlockContactDialogFragment.create(launchMode, mLookupProvider.getDisplayName(),
                targetFragment);
    }

    public void blockContact(boolean notifyLookupProvider) {
        blockContact(notifyLookupProvider, true /*notify status-listener*/);
    }

    private void blockContact(boolean notifyLookupProvider, boolean notifyListener) {
        for (String phoneNumber : mBlockRequest.phoneNumbers) {
            toggleBlacklistStatus(phoneNumber, true /*block contact*/);

            if (notifyLookupProvider && mLookupProvider.isEnabled() &&
                    mLookupProvider.supportsSpamReporting()) {
                mLookupProvider.markAsSpam(phoneNumber);
            }
        }

        if (notifyListener && mListener != null) {
            mListener.onBlockCompleted();
        }

        gatherDataInBackground();
    }

    public void unblockContact(boolean notifyLookupProvider) {
        unblockContact(notifyLookupProvider, true /*notify status-listener*/);
    }

    private void unblockContact(boolean notifyLookupProvider, boolean notifyListener) {
        for (String phoneNumber : mBlockRequest.phoneNumbers) {
            toggleBlacklistStatus(phoneNumber, false /*unblock contact*/);

            if (notifyLookupProvider && mLookupProvider.isEnabled() &&
                    mLookupProvider.supportsSpamReporting()) {
                mLookupProvider.unmarkAsSpam(phoneNumber);
            }
        }

        if (notifyListener && mListener != null) {
            mListener.onUnblockCompleted();
        }

        gatherDataInBackground();
    }

    public void blockContactAsync(final boolean notifyLookupProvider) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                blockContact(notifyLookupProvider, false);
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                if(mListener != null) {
                    mListener.onBlockCompleted();
                }
            }
        }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
    }

    public void unblockContactAsync(final boolean notifyLookupProvider) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                unblockContact(notifyLookupProvider, false);
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                if(mListener != null) {
                    mListener.onUnblockCompleted();
                }
            }
        }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
    }

    /**
     * Clean-up any external resources that are used
     */
    public void destroy() {
        if (mBackgroundTask != null) {
            mBackgroundTask.cancel(true /*interrupt*/);
        }
        LookupProviderImpl.INSTANCE.release();
    }

    public interface StatusCallbacks {
        /**
         * Callback indicating that Blacklist information about the contact has been fetched
         */
        void onInfoAvailable();

        /**
         * Callback indicating that block action has completed
         */
        void onBlockCompleted();

        /**
         * Callback indicating that unblock action has completed
         */
        void onUnblockCompleted();
    }
}