summaryrefslogtreecommitdiffstats
path: root/src/com/android/dialer/discovery/DiscoverySignalReceiver.java
blob: 680426543cecb334249c6ac4beb3541c64fd4a6e (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
package com.android.dialer.discovery;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import android.text.TextUtils;
import android.util.Log;
import com.android.contacts.common.GeoUtil;
import com.android.dialer.DialtactsActivity;
import com.android.dialer.incall.InCallMetricsHelper;
import com.android.phone.common.incall.utils.CallMethodUtils;
import com.cyanogen.ambient.discovery.util.NudgeKey;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonemetadata;
import com.google.i18n.phonenumbers.Phonenumber;

public class DiscoverySignalReceiver extends BroadcastReceiver {

    private static final String TAG = "DiscoverySignalReceiver";
    private static final boolean DEBUG_CONNECTIVITY = false;

    public static final String DISCOVERY_NUDGE_SHOWN =
            "com.android.dialer.discovery.DiscoverySignalReceiver.ON_SHOW";

    public static final String DISCOVERY_NUDGE_DISMISS =
            "com.android.dialer.discovery.DiscoverySignalReceiver.ON_DISMISS";

    public static final String NUDGE_SHARED_PREF = "nudge_shared_pref";

    public static final String NUDGE_ID = "nudge_id";
    public static final String NUDGE_KEY = "nudge_key";
    public static final String NUDGE_COMPONENT = "nudge_component";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        String nudgeId = intent.getStringExtra(NUDGE_ID);
        String nudgeKey = intent.getStringExtra(NUDGE_KEY);

        ComponentName nudgeComponent = null;
        if (intent.hasExtra(NUDGE_COMPONENT)
                && !TextUtils.isEmpty(intent.getStringExtra(NUDGE_COMPONENT))) {

            nudgeComponent
                    = ComponentName.unflattenFromString(intent.getStringExtra(NUDGE_COMPONENT));

        }

        switch (action) {
            case Intent.ACTION_NEW_OUTGOING_CALL:
                String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                SharedPreferences preferences = context
                        .getSharedPreferences(DialtactsActivity.SHARED_PREFS_NAME,
                                Context.MODE_PRIVATE);
                int currentCount = preferences.getInt(CallMethodUtils.PREF_INTERNATIONAL_CALLS, 0);
                if (isMaybeInternationalNumber(context, phoneNumber)) {
                    preferences.edit().putInt(CallMethodUtils.PREF_INTERNATIONAL_CALLS,
                            ++currentCount).apply();
                    startServiceForInternationalCallMade(context);
                }
                break;
            case DISCOVERY_NUDGE_SHOWN:
                if (nudgeComponent == null) {
                    Log.e(TAG, "The nudge component is null, not counting as a nudge event");
                    return;
                }

                SharedPreferences sp = context.getSharedPreferences(NUDGE_SHARED_PREF,
                        Context.MODE_PRIVATE);

                SharedPreferences.Editor editor = sp.edit();

                NudgeItem ni = new NudgeItem(nudgeComponent, nudgeKey, nudgeId,
                        System.currentTimeMillis());

                String countKey = ni.getCountKey();
                String timeKey = ni.getTimeKey();

                long count = sp.getLong(countKey, 0) + 1;

                editor.putLong(countKey, count);
                editor.putLong(timeKey, System.currentTimeMillis());
                editor.apply();

                recordDiscoveryCount(nudgeComponent, nudgeKey,
                        InCallMetricsHelper.Parameters.COUNT);

                break;
        }

    }

    private void recordDiscoveryCount(ComponentName componentName, String nudgeKey,
                                      InCallMetricsHelper.Parameters param) {
        InCallMetricsHelper.Events event = null;
        switch(nudgeKey) {
            case NudgeKey.NOTIFICATION_INTERNATIONAL_CALL:
                event = InCallMetricsHelper.Events.NUDGE_EVENT_INTL;
                break;
            case NudgeKey.NOTIFICATION_WIFI_CALL:
                event = InCallMetricsHelper.Events.NUDGE_EVENT_WIFI;
                break;
            case NudgeKey.NOTIFICATION_ROAMING:
                event = InCallMetricsHelper.Events.NUDGE_EVENT_ROAMING;
                break;
        }

        if (event != null) {
            InCallMetricsHelper.increaseCountOfMetric(componentName, event,
                    InCallMetricsHelper.Categories.DISCOVERY_NUDGES,
                    param);
        }
    }


    private boolean isMaybeInternationalNumber(Context context, String number) {
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        String currentCountryIso = GeoUtil.getCurrentCountryIso(context);

        try {
            Phonenumber.PhoneNumber phoneNumberCalling = phoneUtil.parse(number, currentCountryIso);

            int callingToCode = phoneNumberCalling.getCountryCode();
            int callingFromCode = phoneUtil.getCountryCodeForRegion(currentCountryIso);

            if (callingToCode == 1) {
                if (phoneUtil.isNANPACountry(currentCountryIso)) {
                    // This is NANP country
                    // There is no way to reliably know what country we are calling to
                    // this even catches local numbers, we must return false to avoid the
                    // repercussions of false positives. The only way to know for sure is to build
                    // an area code list and keep that up to date.
                    return false;
                }
            }

            // if true, we are making an international call
            // if false, we are making a local call or a call between countries that share calling
            // codes
            return callingToCode != callingFromCode;

        } catch (NumberParseException e) {
            Log.e(TAG, "Phone number is invalid", e);
        }
        return false;
    }

    private static void startServiceForInternationalCallMade(Context context) {
        Intent serviceIntent = getIntentForDiscoveryService(context);
        serviceIntent.setAction(Intent.ACTION_NEW_OUTGOING_CALL);
        context.startService(serviceIntent);
    }

    public static void startServiceForConnectivityChanged(Context context) {
        Intent serviceIntent = getIntentForDiscoveryService(context);
        serviceIntent.setAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.startService(serviceIntent);
    }

    private static Intent getIntentForDiscoveryService(Context context) {
        return new Intent(context, DiscoveryService.class);
    }

}