summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/blocking/FilteredNumbersUtil.java
blob: cdcf1f78d580deb620e056e48a8a289709dfdc56 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Copyright (C) 2015 The Android Open Source 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.dialer.blocking;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v4.os.UserManagerCompat;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.widget.Toast;
import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnHasBlockedNumbersListener;
import com.android.dialer.common.LogUtil;
import com.android.dialer.logging.InteractionEvent;
import com.android.dialer.logging.Logger;
import com.android.dialer.notification.NotificationChannelManager;
import com.android.dialer.notification.NotificationChannelManager.Channel;
import com.android.dialer.util.DialerUtils;
import com.android.dialer.util.PermissionsUtil;
import java.util.concurrent.TimeUnit;

/** Utility to help with tasks related to filtered numbers. */
public class FilteredNumbersUtil {

  public static final String CALL_BLOCKING_NOTIFICATION_TAG = "call_blocking";
  public static final int CALL_BLOCKING_DISABLED_BY_EMERGENCY_CALL_NOTIFICATION_ID =
      R.id.notification_call_blocking_disabled_by_emergency_call;
  // Pref key for storing the time of end of the last emergency call in milliseconds after epoch.\
  @VisibleForTesting
  public static final String LAST_EMERGENCY_CALL_MS_PREF_KEY = "last_emergency_call_ms";
  // Pref key for storing whether a notification has been dispatched to notify the user that call
  // blocking has been disabled because of a recent emergency call.
  protected static final String NOTIFIED_CALL_BLOCKING_DISABLED_BY_EMERGENCY_CALL_PREF_KEY =
      "notified_call_blocking_disabled_by_emergency_call";
  // Disable incoming call blocking if there was a call within the past 2 days.
  static final long RECENT_EMERGENCY_CALL_THRESHOLD_MS = TimeUnit.DAYS.toMillis(2);

  /**
   * Used for testing to specify the custom threshold value, in milliseconds for whether an
   * emergency call is "recent". The default value will be used if this custom threshold is less
   * than zero. For example, to set this threshold to 60 seconds:
   *
   * <p>adb shell settings put system dialer_emergency_call_threshold_ms 60000
   */
  private static final String RECENT_EMERGENCY_CALL_THRESHOLD_SETTINGS_KEY =
      "dialer_emergency_call_threshold_ms";

  /** Checks if there exists a contact with {@code Contacts.SEND_TO_VOICEMAIL} set to true. */
  public static void checkForSendToVoicemailContact(
      final Context context, final CheckForSendToVoicemailContactListener listener) {
    final AsyncTask task =
        new AsyncTask<Object, Void, Boolean>() {
          @Override
          public Boolean doInBackground(Object... params) {
            if (context == null || !PermissionsUtil.hasContactsReadPermissions(context)) {
              return false;
            }

            final Cursor cursor =
                context
                    .getContentResolver()
                    .query(
                        Contacts.CONTENT_URI,
                        ContactsQuery.PROJECTION,
                        ContactsQuery.SELECT_SEND_TO_VOICEMAIL_TRUE,
                        null,
                        null);

            boolean hasSendToVoicemailContacts = false;
            if (cursor != null) {
              try {
                hasSendToVoicemailContacts = cursor.getCount() > 0;
              } finally {
                cursor.close();
              }
            }

            return hasSendToVoicemailContacts;
          }

          @Override
          public void onPostExecute(Boolean hasSendToVoicemailContact) {
            if (listener != null) {
              listener.onComplete(hasSendToVoicemailContact);
            }
          }
        };
    task.execute();
  }

  /**
   * Blocks all the phone numbers of any contacts marked as SEND_TO_VOICEMAIL, then clears the
   * SEND_TO_VOICEMAIL flag on those contacts.
   */
  public static void importSendToVoicemailContacts(
      final Context context, final ImportSendToVoicemailContactsListener listener) {
    Logger.get(context).logInteraction(InteractionEvent.Type.IMPORT_SEND_TO_VOICEMAIL);
    final FilteredNumberAsyncQueryHandler mFilteredNumberAsyncQueryHandler =
        new FilteredNumberAsyncQueryHandler(context);

    final AsyncTask<Object, Void, Boolean> task =
        new AsyncTask<Object, Void, Boolean>() {
          @Override
          public Boolean doInBackground(Object... params) {
            if (context == null) {
              return false;
            }

            // Get the phone number of contacts marked as SEND_TO_VOICEMAIL.
            final Cursor phoneCursor =
                context
                    .getContentResolver()
                    .query(
                        Phone.CONTENT_URI,
                        PhoneQuery.PROJECTION,
                        PhoneQuery.SELECT_SEND_TO_VOICEMAIL_TRUE,
                        null,
                        null);

            if (phoneCursor == null) {
              return false;
            }

            try {
              while (phoneCursor.moveToNext()) {
                final String normalizedNumber =
                    phoneCursor.getString(PhoneQuery.NORMALIZED_NUMBER_COLUMN_INDEX);
                final String number = phoneCursor.getString(PhoneQuery.NUMBER_COLUMN_INDEX);
                if (normalizedNumber != null) {
                  // Block the phone number of the contact.
                  mFilteredNumberAsyncQueryHandler.blockNumber(
                      null, normalizedNumber, number, null);
                }
              }
            } finally {
              phoneCursor.close();
            }

            // Clear SEND_TO_VOICEMAIL on all contacts. The setting has been imported to Dialer.
            ContentValues newValues = new ContentValues();
            newValues.put(Contacts.SEND_TO_VOICEMAIL, 0);
            context
                .getContentResolver()
                .update(
                    Contacts.CONTENT_URI,
                    newValues,
                    ContactsQuery.SELECT_SEND_TO_VOICEMAIL_TRUE,
                    null);

            return true;
          }

          @Override
          public void onPostExecute(Boolean success) {
            if (success) {
              if (listener != null) {
                listener.onImportComplete();
              }
            } else if (context != null) {
              String toastStr = context.getString(R.string.send_to_voicemail_import_failed);
              Toast.makeText(context, toastStr, Toast.LENGTH_SHORT).show();
            }
          }
        };
    task.execute();
  }

  public static long getLastEmergencyCallTimeMillis(Context context) {
    return DialerUtils.getDefaultSharedPreferenceForDeviceProtectedStorageContext(context)
        .getLong(LAST_EMERGENCY_CALL_MS_PREF_KEY, 0);
  }

  public static boolean hasRecentEmergencyCall(Context context) {
    if (context == null) {
      return false;
    }

    Long lastEmergencyCallTime = getLastEmergencyCallTimeMillis(context);
    if (lastEmergencyCallTime == 0) {
      return false;
    }

    return (System.currentTimeMillis() - lastEmergencyCallTime)
        < getRecentEmergencyCallThresholdMs(context);
  }

  public static void recordLastEmergencyCallTime(Context context) {
    if (context == null) {
      return;
    }

    DialerUtils.getDefaultSharedPreferenceForDeviceProtectedStorageContext(context)
        .edit()
        .putLong(LAST_EMERGENCY_CALL_MS_PREF_KEY, System.currentTimeMillis())
        .putBoolean(NOTIFIED_CALL_BLOCKING_DISABLED_BY_EMERGENCY_CALL_PREF_KEY, false)
        .apply();

    if (UserManagerCompat.isUserUnlocked(context)) {
      maybeNotifyCallBlockingDisabled(context);
    }
  }

  public static void maybeNotifyCallBlockingDisabled(final Context context) {
    // The Dialer is not responsible for this notification after migrating
    if (FilteredNumberCompat.useNewFiltering(context)) {
      return;
    }
    // Skip if the user has already received a notification for the most recent emergency call.
    if (DialerUtils.getDefaultSharedPreferenceForDeviceProtectedStorageContext(context)
        .getBoolean(NOTIFIED_CALL_BLOCKING_DISABLED_BY_EMERGENCY_CALL_PREF_KEY, false)) {
      return;
    }

    // If the user has blocked numbers, notify that call blocking is temporarily disabled.
    FilteredNumberAsyncQueryHandler queryHandler = new FilteredNumberAsyncQueryHandler(context);
    queryHandler.hasBlockedNumbers(
        new OnHasBlockedNumbersListener() {
          @Override
          public void onHasBlockedNumbers(boolean hasBlockedNumbers) {
            if (context == null || !hasBlockedNumbers) {
              return;
            }

            NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification.Builder builder =
                new Notification.Builder(context)
                    .setSmallIcon(R.drawable.quantum_ic_block_white_24)
                    .setContentTitle(
                        context.getString(R.string.call_blocking_disabled_notification_title))
                    .setContentText(
                        context.getString(R.string.call_blocking_disabled_notification_text))
                    .setAutoCancel(true);

            NotificationChannelManager.applyChannel(builder, context, Channel.DEFAULT, null);
            builder.setContentIntent(
                PendingIntent.getActivity(
                    context,
                    0,
                    FilteredNumberCompat.createManageBlockedNumbersIntent(context),
                    PendingIntent.FLAG_UPDATE_CURRENT));

            notificationManager.notify(
                CALL_BLOCKING_NOTIFICATION_TAG,
                CALL_BLOCKING_DISABLED_BY_EMERGENCY_CALL_NOTIFICATION_ID,
                builder.build());

            // Record that the user has been notified for this emergency call.
            DialerUtils.getDefaultSharedPreferenceForDeviceProtectedStorageContext(context)
                .edit()
                .putBoolean(NOTIFIED_CALL_BLOCKING_DISABLED_BY_EMERGENCY_CALL_PREF_KEY, true)
                .apply();
          }
        });
  }

  /**
   * @param e164Number The e164 formatted version of the number, or {@code null} if such a format
   *     doesn't exist.
   * @param number The number to attempt blocking.
   * @return {@code true} if the number can be blocked, {@code false} otherwise.
   */
  public static boolean canBlockNumber(Context context, String e164Number, String number) {
    String blockableNumber = getBlockableNumber(context, e164Number, number);
    return !TextUtils.isEmpty(blockableNumber)
        && !PhoneNumberUtils.isEmergencyNumber(blockableNumber);
  }

  /**
   * @param e164Number The e164 formatted version of the number, or {@code null} if such a format
   *     doesn't exist..
   * @param number The number to attempt blocking.
   * @return The version of the given number that can be blocked with the current blocking solution.
   */
  @Nullable
  public static String getBlockableNumber(
      Context context, @Nullable String e164Number, String number) {
    if (!FilteredNumberCompat.useNewFiltering(context)) {
      return e164Number;
    }
    return TextUtils.isEmpty(e164Number) ? number : e164Number;
  }

  private static long getRecentEmergencyCallThresholdMs(Context context) {
    if (LogUtil.isVerboseEnabled()) {
      long thresholdMs =
          Settings.System.getLong(
              context.getContentResolver(), RECENT_EMERGENCY_CALL_THRESHOLD_SETTINGS_KEY, 0);
      return thresholdMs > 0 ? thresholdMs : RECENT_EMERGENCY_CALL_THRESHOLD_MS;
    } else {
      return RECENT_EMERGENCY_CALL_THRESHOLD_MS;
    }
  }

  public interface CheckForSendToVoicemailContactListener {

    void onComplete(boolean hasSendToVoicemailContact);
  }

  public interface ImportSendToVoicemailContactsListener {

    void onImportComplete();
  }

  private static class ContactsQuery {

    static final String[] PROJECTION = {Contacts._ID};

    static final String SELECT_SEND_TO_VOICEMAIL_TRUE = Contacts.SEND_TO_VOICEMAIL + "=1";

    static final int ID_COLUMN_INDEX = 0;
  }

  public static class PhoneQuery {

    public static final String[] PROJECTION = {Contacts._ID, Phone.NORMALIZED_NUMBER, Phone.NUMBER};

    public static final int ID_COLUMN_INDEX = 0;
    public static final int NORMALIZED_NUMBER_COLUMN_INDEX = 1;
    public static final int NUMBER_COLUMN_INDEX = 2;

    public static final String SELECT_SEND_TO_VOICEMAIL_TRUE = Contacts.SEND_TO_VOICEMAIL + "=1";
  }
}