<commit_before>package com.wonderpush.sdk;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.core.app.NotificationManagerCompat;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;

/**
 * Implementation of {@link IWonderPush} that performs the required tasks.
 *
 * This implementation is used when user consent is either provided or not required.
 */
class WonderPushImpl implements IWonderPush {

    @Override
    public void _activate() {
        WonderPush.resumeInAppMessaging();
    }

    @Override
    public void _deactivate() {
        WonderPush.pauseInAppMessaging();
    }

    @Override
    public String getAccessToken() {
        String accessToken = null;
        try {
            accessToken = WonderPushConfiguration.getAccessToken();
        } catch (Exception e) {
            Log.e(WonderPush.TAG, "Unexpected error while getting accessToken", e);
        }
        return accessToken;
    }

    @Override
    public String getDeviceId() {
        String deviceId = null;
        try {
            deviceId = WonderPushConfiguration.getDeviceId();
            if (deviceId == null) {
                // Read from OpenUDID storage to keep a smooth transition off using OpenUDID
                SharedPreferences sharedPrefs =  WonderPush.getApplicationContext().getSharedPreferences("openudid_prefs", Context.MODE_PRIVATE);
                deviceId = sharedPrefs.getString("openudid", null);
                if (deviceId == null) {
                    // Generate an UUIDv4
                    deviceId = UUID.randomUUID().toString();
                }
                // and store it for us
                WonderPushConfiguration.setDeviceId(deviceId);
            }
        } catch (Exception e) {
            Log.e(WonderPush.TAG, "Unexpected error while getting deviceId", e);
        }
        return deviceId;
    }

    @Override
    public String getInstallationId() {
        String installationId = null;
        try {
            installationId = WonderPushConfiguration.getInstallationId();
        } catch (Exception e) {
            Log.e(WonderPush.TAG, "Unexpected error while getting installationId", e);
        }
        return installationId;
    }

    @Override
    public String getPushToken() {
        String pushToken = null;
        try {
            pushToken = WonderPushConfiguration.getGCMRegistrationId();
        } catch (Exception e) {
            Log.e(WonderPush.TAG, "Unexpected error while getting pushToken", e);
        }
        return pushToken;
    }

    @Override
    public void subscribeToNotifications(boolean fallbackToSettings) {
        setNotificationEnabled(true, fallbackToSettings);
    }

    @Override
    public void subscribeToNotifications() {
        subscribeToNotifications(false);
    }

    @Override
    public void unsubscribeFromNotifications() {
        setNotificationEnabled(false);
    }

    @Override
    public boolean isSubscribedToNotifications() {
        return getNotificationEnabled();
    }

    @Override
    @Deprecated
    public boolean getNotificationEnabled() {
        return WonderPushConfiguration.getNotificationEnabled();
    }

    @Override
    @Deprecated
    public void setNotificationEnabled(boolean status) {
        setNotificationEnabled(status, false);
    }

    private void setNotificationEnabled(boolean status, boolean fallbackToSetting) {
        WonderPush.logDebug("Set notification enabled: " + status);
        WonderPushConfiguration.setNotificationEnabled(status);
        refreshSubscriptionStatus();
        NotificationPermissionController.getInstance().prompt(fallbackToSetting, null);
    }

    public void refreshSubscriptionStatus() {
        try {
            boolean subscribedToNotifications = WonderPushConfiguration.getNotificationEnabled();
            WonderPush.SubscriptionStatus previousSubscriptionStatus = WonderPush.getSubscriptionStatus();
            boolean osAreNotificationsEnabled = NotificationManagerCompat.from(WonderPush.getApplicationContext()).areNotificationsEnabled();
            Set<String> disabledChannels = WonderPushUserPreferences.getDisabledChannelIds();
            String value = subscribedToNotifications && osAreNotificationsEnabled
                    ? WonderPush.INSTALLATION_PREFERENCES_SUBSCRIPTION_STATUS_OPTIN
                    : WonderPush.INSTALLATION_PREFERENCES_SUBSCRIPTION_STATUS_OPTOUT;

            JSONObject diff = new JSONObject();
            JSONObject preferences = new JSONObject();
            diff.put("preferences", preferences);
            preferences.put("subscriptionStatus", value);
            preferences.put("subscribedToNotifications", subscribedToNotifications);
            preferences.put("osNotificationsVisible", osAreNotificationsEnabled);
            preferences.put("disabledAndroidChannels", new JSONArray(disabledChannels));
            JSONSyncInstallation.forCurrentUser().put(diff);

            WonderPushConfiguration.setNotificationEnabled(subscribedToNotifications);
            WonderPushConfiguration.setCachedOsAreNotificationsEnabled(osAreNotificationsEnabled);
            WonderPushConfiguration.setCachedOsAreNotificationsEnabledDate(TimeSync.getTime());
            WonderPushConfiguration.setCachedDisabledNotificationChannelIds(disabledChannels);
            WonderPushConfiguration.setCachedDisabledNotificationChannelIdsDate(TimeSync.getTime());

            if (WonderPush.getSubscriptionStatus() != previousSubscriptionStatus) {
                WonderPush.subscriptionStatusChanged();
            }

        } catch (Exception e) {
            Log.e(WonderPush.TAG, "Unexpected error while refreshing subscription status", e);
        }
    }

    @Override
    public JSONObject getProperties() {
        return getInstallationCustomProperties();
    }

    @Override
    public void putProperties(JSONObject properties) {<commit_msg>Extraneous useless line<commit_after>package com.wonderpush.sdk;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.core.app.NotificationManagerCompat;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;

/**
 * Implementation of {@link IWonderPush} that performs the required tasks.
 *
 * This implementation is used when user consent is either provided or not required.
 */
class WonderPushImpl implements IWonderPush {

    @Override
    public void _activate() {
        WonderPush.resumeInAppMessaging();
    }

    @Override
    public void _deactivate() {
        WonderPush.pauseInAppMessaging();
    }

    @Override
    public String getAccessToken() {
        String accessToken = null;
        try {
            accessToken = WonderPushConfiguration.getAccessToken();
        } catch (Exception e) {
            Log.e(WonderPush.TAG, "Unexpected error while getting accessToken", e);
        }
        return accessToken;
    }

    @Override
    public String getDeviceId() {
        String deviceId = null;
        try {
            deviceId = WonderPushConfiguration.getDeviceId();
            if (deviceId == null) {
                // Read from OpenUDID storage to keep a smooth transition off using OpenUDID
                SharedPreferences sharedPrefs =  WonderPush.getApplicationContext().getSharedPreferences("openudid_prefs", Context.MODE_PRIVATE);
                deviceId = sharedPrefs.getString("openudid", null);
                if (deviceId == null) {
                    // Generate an UUIDv4
                    deviceId = UUID.randomUUID().toString();
                }
                // and store it for us
                WonderPushConfiguration.setDeviceId(deviceId);
            }
        } catch (Exception e) {
            Log.e(WonderPush.TAG, "Unexpected error while getting deviceId", e);
        }
        return deviceId;
    }

    @Override
    public String getInstallationId() {
        String installationId = null;
        try {
            installationId = WonderPushConfiguration.getInstallationId();
        } catch (Exception e) {
            Log.e(WonderPush.TAG, "Unexpected error while getting installationId", e);
        }
        return installationId;
    }

    @Override
    public String getPushToken() {
        String pushToken = null;
        try {
            pushToken = WonderPushConfiguration.getGCMRegistrationId();
        } catch (Exception e) {
            Log.e(WonderPush.TAG, "Unexpected error while getting pushToken", e);
        }
        return pushToken;
    }

    @Override
    public void subscribeToNotifications(boolean fallbackToSettings) {
        setNotificationEnabled(true, fallbackToSettings);
    }

    @Override
    public void subscribeToNotifications() {
        subscribeToNotifications(false);
    }

    @Override
    public void unsubscribeFromNotifications() {
        setNotificationEnabled(false);
    }

    @Override
    public boolean isSubscribedToNotifications() {
        return getNotificationEnabled();
    }

    @Override
    @Deprecated
    public boolean getNotificationEnabled() {
        return WonderPushConfiguration.getNotificationEnabled();
    }

    @Override
    @Deprecated
    public void setNotificationEnabled(boolean status) {
        setNotificationEnabled(status, false);
    }

    private void setNotificationEnabled(boolean status, boolean fallbackToSetting) {
        WonderPush.logDebug("Set notification enabled: " + status);
        WonderPushConfiguration.setNotificationEnabled(status);
        refreshSubscriptionStatus();
        NotificationPermissionController.getInstance().prompt(fallbackToSetting, null);
    }

    public void refreshSubscriptionStatus() {
        try {
            boolean subscribedToNotifications = WonderPushConfiguration.getNotificationEnabled();
            WonderPush.SubscriptionStatus previousSubscriptionStatus = WonderPush.getSubscriptionStatus();
            boolean osAreNotificationsEnabled = NotificationManagerCompat.from(WonderPush.getApplicationContext()).areNotificationsEnabled();
            Set<String> disabledChannels = WonderPushUserPreferences.getDisabledChannelIds();
            String value = subscribedToNotifications && osAreNotificationsEnabled
                    ? WonderPush.INSTALLATION_PREFERENCES_SUBSCRIPTION_STATUS_OPTIN
                    : WonderPush.INSTALLATION_PREFERENCES_SUBSCRIPTION_STATUS_OPTOUT;

            JSONObject diff = new JSONObject();
            JSONObject preferences = new JSONObject();
            diff.put("preferences", preferences);
            preferences.put("subscriptionStatus", value);
            preferences.put("subscribedToNotifications", subscribedToNotifications);
            preferences.put("osNotificationsVisible", osAreNotificationsEnabled);
            preferences.put("disabledAndroidChannels", new JSONArray(disabledChannels));
            JSONSyncInstallation.forCurrentUser().put(diff);

            WonderPushConfiguration.setCachedOsAreNotificationsEnabled(osAreNotificationsEnabled);
            WonderPushConfiguration.setCachedOsAreNotificationsEnabledDate(TimeSync.getTime());
            WonderPushConfiguration.setCachedDisabledNotificationChannelIds(disabledChannels);
            WonderPushConfiguration.setCachedDisabledNotificationChannelIdsDate(TimeSync.getTime());

            if (WonderPush.getSubscriptionStatus() != previousSubscriptionStatus) {
                WonderPush.subscriptionStatusChanged();
            }

        } catch (Exception e) {
            Log.e(WonderPush.TAG, "Unexpected error while refreshing subscription status", e);
        }
    }

    @Override
    public JSONObject getProperties() {
        return getInstallationCustomProperties();
    }

    @Override
    public void putProperties(JSONObject properties) {
{'fraction_non_alphanumeric': 0.0783285538713642, 'fraction_numerical': 0.0001638672675133142, 'mean_word_length': 2.873690891780387, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2492805', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2907, 'n_words': 627}