summaryrefslogtreecommitdiffstats
path: root/java/com/android/incallui/spam/SpamNotificationService.java
blob: bf107f7896657690f242e07b184dbbc749d86998 (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
/*
 * Copyright (C) 2016 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.incallui.spam;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.provider.CallLog;
import android.support.annotation.Nullable;
import com.android.contacts.common.GeoUtil;
import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
import com.android.dialer.common.LogUtil;
import com.android.dialer.logging.Logger;
import com.android.dialer.logging.nano.DialerImpression;
import com.android.dialer.logging.nano.ReportingLocation;
import com.android.dialer.spam.Spam;
import com.android.incallui.call.DialerCall;

/**
 * This service determines if the device is locked/unlocked and takes an action based on the state.
 * A service is used to to determine this, as opposed to an activity, because the user must unlock
 * the device before a notification can start an activity. This is not the case for a service, and
 * intents can be sent to this service even from the lock screen. This allows users to quickly
 * report a number as spam or not spam from their lock screen.
 */
public class SpamNotificationService extends Service {

  private static final String TAG = "SpamNotificationSvc";

  private static final String EXTRA_PHONE_NUMBER = "service_phone_number";
  private static final String EXTRA_CALL_ID = "service_call_id";
  private static final String EXTRA_CALL_START_TIME_MILLIS = "service_call_start_time_millis";
  private static final String EXTRA_NOTIFICATION_ID = "service_notification_id";
  private static final String EXTRA_CONTACT_LOOKUP_RESULT_TYPE =
      "service_contact_lookup_result_type";
  /** Creates an intent to start this service. */
  public static Intent createServiceIntent(
      Context context, DialerCall call, String action, int notificationId) {
    Intent intent = new Intent(context, SpamNotificationService.class);
    intent.setAction(action);
    intent.putExtra(EXTRA_PHONE_NUMBER, call.getNumber());
    intent.putExtra(EXTRA_CALL_ID, call.getUniqueCallId());
    intent.putExtra(EXTRA_CALL_START_TIME_MILLIS, call.getTimeAddedMs());
    intent.putExtra(EXTRA_NOTIFICATION_ID, notificationId);
    intent.putExtra(EXTRA_CONTACT_LOOKUP_RESULT_TYPE, call.getLogState().contactLookupResult);
    return intent;
  }

  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    // Return null because clients cannot bind to this service
    return null;
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    LogUtil.d(TAG, "onStartCommand");
    if (intent == null) {
      LogUtil.d(TAG, "Null intent");
      stopSelf();
      // Return {@link #START_NOT_STICKY} so service is not restarted.
      return START_NOT_STICKY;
    }
    String number = intent.getStringExtra(EXTRA_PHONE_NUMBER);
    int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, 1);
    String countryIso = GeoUtil.getCurrentCountryIso(this);
    int contactLookupResultType = intent.getIntExtra(EXTRA_CONTACT_LOOKUP_RESULT_TYPE, 0);

    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
        .cancel(number, notificationId);

    switch (intent.getAction()) {
      case SpamNotificationActivity.ACTION_MARK_NUMBER_AS_SPAM:
        logCallImpression(
            intent, DialerImpression.Type.SPAM_NOTIFICATION_SERVICE_ACTION_MARK_NUMBER_AS_SPAM);
        Spam.get(this)
            .reportSpamFromAfterCallNotification(
                number,
                countryIso,
                CallLog.Calls.INCOMING_TYPE,
                ReportingLocation.Type.FEEDBACK_PROMPT,
                contactLookupResultType);
        new FilteredNumberAsyncQueryHandler(this).blockNumber(null, number, countryIso);
        break;
      case SpamNotificationActivity.ACTION_MARK_NUMBER_AS_NOT_SPAM:
        logCallImpression(
            intent, DialerImpression.Type.SPAM_NOTIFICATION_SERVICE_ACTION_MARK_NUMBER_AS_NOT_SPAM);
        Spam.get(this)
            .reportNotSpamFromAfterCallNotification(
                number,
                countryIso,
                CallLog.Calls.INCOMING_TYPE,
                ReportingLocation.Type.FEEDBACK_PROMPT,
                contactLookupResultType);
        break;
    }
    // TODO: call stopSelf() after async tasks complete (b/28441936)
    stopSelf();
    return START_NOT_STICKY;
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    LogUtil.d(TAG, "onDestroy");
  }

  private void logCallImpression(Intent intent, int impression) {
    Logger.get(this)
        .logCallImpression(
            impression,
            intent.getStringExtra(EXTRA_CALL_ID),
            intent.getLongExtra(EXTRA_CALL_START_TIME_MILLIS, 0));
  }
}