Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b058e6e6 authored by Stephen Bird's avatar Stephen Bird
Browse files

Increment nudge only after shown intent returns

Previously there was a chance that a nudge would be sent to
discovery but then not shown.

Ticket: CD-681
Change-Id: I8671a83b23ed77b2ae1fabcfcdf4a01ec5eae108
(cherry picked from commit 5287347e)
parent 7d2a815e
Loading
Loading
Loading
Loading
+37 −11
Original line number Diff line number Diff line
@@ -91,8 +91,12 @@ public class DiscoveryEventHandler {

                            Bundle b = theEntry.getValue();

                            // If we are ready to show the nudge, then we will not increment the
                            // count here. Instead the count will be incremented in
                            // DiscoverySignalReceiver when the nudge is properly shown.
                            if (!validateShouldShowNudge(key, b) && !isTesting) {
                                // Nudge not yet ready for this item.
                                // Nudge not yet ready for this item. increment event count
                                incrementCount(key, mContext);
                                continue;
                            }

@@ -238,16 +242,9 @@ public class DiscoveryEventHandler {
        SharedPreferences preferences = mContext.getSharedPreferences(DialtactsActivity
                .SHARED_PREFS_NAME, Context.MODE_PRIVATE);

        int count = 0;

        // The count starts at 1 here because this is the first time we've seen this item.
        if (key.equals(NudgeKey.NOTIFICATION_INTERNATIONAL_CALL)) {
            count = preferences.getInt(CallMethodUtils.PREF_INTERNATIONAL_CALLS, 1);
        } else if (key.equals(NudgeKey.NOTIFICATION_WIFI_CALL)) {
            count = preferences.getInt(CallMethodUtils.PREF_WIFI_CALL, 1);
        } else if (key.equals(NudgeKey.NOTIFICATION_ROAMING)) {
            count = preferences.getInt(CallMethodUtils.PREF_ROAMING_CALLS, 1);
        }
        String preferenceKey = getPreferenceKeyForNudgeKey(key);
        // If the preference does not exist then this is the first event so default to 1;
        int count = preferences.getInt(preferenceKey, 1);

        checkCount = (count == b.getInt(NudgeKey.NOTIFICATION_PARAM_EVENTS_FIRST_NUDGE, 0)) ||
                (count == b.getInt(NudgeKey.NOTIFICATION_PARAM_EVENTS_SECOND_NUDGE, 0));
@@ -256,4 +253,33 @@ public class DiscoveryEventHandler {
        return checkCount;
    }

    private static String getPreferenceKeyForNudgeKey(String nudgeKey) {
        String prefKey = null;
        switch(nudgeKey) {
            case NudgeKey.NOTIFICATION_INTERNATIONAL_CALL:
                prefKey = CallMethodUtils.PREF_INTERNATIONAL_CALLS;
                break;
            case NudgeKey.NOTIFICATION_WIFI_CALL:
                prefKey = CallMethodUtils.PREF_WIFI_CALL;
                break;
            case NudgeKey.NOTIFICATION_ROAMING:
                prefKey = CallMethodUtils.PREF_ROAMING_CALLS;
                break;
        }
        return prefKey;
    }

    public static void incrementCount(String nudgeKey, Context context) {
        String prefKey = getPreferenceKeyForNudgeKey(nudgeKey);

        if (prefKey != null) {
            SharedPreferences nudgePrefs = context.getSharedPreferences(
                    DialtactsActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
            // If the preference does not exist then set default to zero so when we increment it
            // right after then we will have the correct value (1).
            int currentCount = nudgePrefs.getInt(prefKey, 0);
            nudgePrefs.edit().putInt(prefKey, ++currentCount).apply();
        }
    }

}
+4 −6
Original line number Diff line number Diff line
@@ -57,13 +57,7 @@ public class DiscoverySignalReceiver extends BroadcastReceiver {
        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;
@@ -90,6 +84,10 @@ public class DiscoverySignalReceiver extends BroadcastReceiver {
                editor.putLong(timeKey, System.currentTimeMillis());
                editor.apply();

                // The nudge was shown, so we want to increment the count because this is a real
                // event.
                DiscoveryEventHandler.incrementCount(nudgeKey, context);

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

+0 −9
Original line number Diff line number Diff line
@@ -140,20 +140,11 @@ public class WifiCallStatusNudgeListener {
    private static void callOnWifiSuccess() {
        if (DEBUG) Log.v(TAG, "call was made with wifi connected the whole time");

        SharedPreferences preferences = mContext
                .getSharedPreferences(DialtactsActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE);

        int currentCount;
        // If the network is roaming and we are on wifi,
        // then we want to show a potential roaming nudge instead of a wifi nudge.
        if (mTelephonyManager.isNetworkRoaming()) {
            currentCount = preferences.getInt(CallMethodUtils.PREF_ROAMING_CALLS, 0);
            preferences.edit().putInt(CallMethodUtils.PREF_ROAMING_CALLS, ++currentCount).apply();
            DiscoverySignalReceiver.startServiceForConnectivityChanged(mContext);
        } else {
            currentCount = preferences.getInt(CallMethodUtils.PREF_WIFI_CALL, 0);
            preferences.edit().putInt(CallMethodUtils.PREF_WIFI_CALL, ++currentCount).apply();

            new DiscoveryEventHandler(mContext).getNudgeProvidersWithKey(
                    NudgeKey.NOTIFICATION_WIFI_CALL);
        }