<commit_before>/*
 * Copyright 2016 Middlesex University
 *
 * 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 uk.ac.mdx.cs.ie.acontextlib.hardware;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;

import java.util.HashMap;
import java.util.Map;

import uk.ac.mdx.cs.ie.acontextlib.BroadcastContext;

/**
 * Handles telephone related contexts including:
 * The state of the connection (disconnected, connecting, connected etc)
 * Whether the device is currently roaming
 *
 * @author <NAME> <<EMAIL>>
 */
public class TelephonyContext extends BroadcastContext {

    private boolean mRoaming = false;
    private int mConnectionState = 0;
    public TelephonyManager mPhoneManager;
    public static final String RECEIVER_TELEPHONY_ROAMING = "sensor.telephony_roaming";
    public static final String RECEIVER_TELEPHONY_CONSTATE = "sensor.telephone_connectionstate";

    public TelephonyContext(Context c) {
        super(c, ConnectivityManager.CONNECTIVITY_ACTION, "TelephonyContext");
        mPhoneManager = (TelephonyManager) c
                .getSystemService(Context.TELEPHONY_SERVICE);
    }

    @Override
    public boolean start() {
        checkContext(null);
        return super.start();
    }

    @Override
    protected void checkContext(Bundle data) {
        Map<String, String> contextValues = new HashMap<String, String>();
        checkRoaming(contextValues);
        checkConnectionState(contextValues);
        sendToContextReceivers(contextValues);
    }

    private void checkRoaming(Map<String, String> values) {
        if (mRoaming != mPhoneManager.isNetworkRoaming()) {
            mRoaming = !mRoaming;
            values.put(RECEIVER_TELEPHONY_ROAMING, String.valueOf(mRoaming));
        }
    }

    private void checkConnectionState(Map<String, String> values) {
        int v = mPhoneManager.getDataState();
        if (v != mConnectionState) {
            mConnectionState = v;
            values.put(RECEIVER_TELEPHONY_CONSTATE, getConnectionValue(mConnectionState));
        }
    }

    private static String getConnectionValue(int connectionState) {
<commit_msg>Get initial roaming and telephony connection status<commit_after>/*
 * Copyright 2016 Middlesex University
 *
 * 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 uk.ac.mdx.cs.ie.acontextlib.hardware;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;

import java.util.HashMap;
import java.util.Map;

import uk.ac.mdx.cs.ie.acontextlib.BroadcastContext;

/**
 * Handles telephone related contexts including:
 * The state of the connection (disconnected, connecting, connected etc)
 * Whether the device is currently roaming
 *
 * @author <NAME> <<EMAIL>>
 */
public class TelephonyContext extends BroadcastContext {

    private boolean mRoaming = false;
    private int mConnectionState = -1;
    public TelephonyManager mPhoneManager;
    public static final String RECEIVER_TELEPHONY_ROAMING = "sensor.telephony_roaming";
    public static final String RECEIVER_TELEPHONY_CONSTATE = "sensor.telephone_connectionstate";

    public TelephonyContext(Context c) {
        super(c, ConnectivityManager.CONNECTIVITY_ACTION, "TelephonyContext");
        mPhoneManager = (TelephonyManager) c
                .getSystemService(Context.TELEPHONY_SERVICE);
    }

    @Override
    public boolean start() {
        Map<String, String> contextValues = new HashMap<String, String>();
        contextValues.put(RECEIVER_TELEPHONY_ROAMING, String.valueOf(mPhoneManager.isNetworkRoaming()));
        checkConnectionState(contextValues);
        sendToContextReceivers(contextValues);
        return super.start();
    }

    @Override
    protected void checkContext(Bundle data) {
        Map<String, String> contextValues = new HashMap<String, String>();
        checkRoaming(contextValues);
        checkConnectionState(contextValues);
        sendToContextReceivers(contextValues);
    }

    private void checkRoaming(Map<String, String> values) {
        if (mRoaming != mPhoneManager.isNetworkRoaming()) {
            mRoaming = !mRoaming;
            values.put(RECEIVER_TELEPHONY_ROAMING, String.valueOf(mRoaming));
        }
    }

    private void checkConnectionState(Map<String, String> values) {
        int v = mPhoneManager.getDataState();
        if (v != mConnectionState) {
            mConnectionState = v;
            values.put(RECEIVER_TELEPHONY_CONSTATE, getConnectionValue(mConnectionState));
        }
    }

    private static String getConnectionValue(int connectionState) {

{'fraction_non_alphanumeric': 0.08387322710558572, 'fraction_numerical': 0.003151812292067939, 'mean_word_length': 3.6858080393765382, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7063282', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1547, 'n_words': 441}