Example usage for android.telephony TelephonyManager listen

List of usage examples for android.telephony TelephonyManager listen

Introduction

In this page you can find the example usage for android.telephony TelephonyManager listen.

Prototype

public void listen(PhoneStateListener listener, int events) 

Source Link

Document

Registers a listener object to receive notification of changes in specified telephony states.

Usage

From source file:eu.faircode.adblocker.ServiceSinkhole.java

@Override
public void onDestroy() {
    Log.i(TAG, "Destroy");

    commandLooper.quit();//from   w  ww.jav  a  2s .c om
    logLooper.quit();
    statsLooper.quit();

    unregisterReceiver(interactiveStateReceiver);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        unregisterReceiver(powerSaveReceiver);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        unregisterReceiver(userReceiver);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        unregisterReceiver(idleStateReceiver);
    unregisterReceiver(connectivityChangedReceiver);
    unregisterReceiver(packageAddedReceiver);

    if (phone_state) {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
            phone_state = false;
        }
    }

    if (subscriptionsChangedListener != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager sm = SubscriptionManager.from(this);
        sm.removeOnSubscriptionsChangedListener(
                (SubscriptionManager.OnSubscriptionsChangedListener) subscriptionsChangedListener);
        subscriptionsChangedListener = null;
    }

    try {
        if (vpn != null) {
            stopNative(vpn, true, true);
            stopVPN(vpn);
            vpn = null;
            unprepare();
        }
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }

    jni_done();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.unregisterOnSharedPreferenceChangeListener(this);

    super.onDestroy();
}

From source file:eu.faircode.netguard.ServiceSinkhole.java

@Override
public void onDestroy() {
    Log.i(TAG, "Destroy");

    commandLooper.quit();//ww  w.j a  v  a  2 s .  c  om
    logLooper.quit();
    statsLooper.quit();

    if (registeredInteractiveState) {
        unregisterReceiver(interactiveStateReceiver);
        registeredInteractiveState = false;
    }
    if (registeredPowerSave) {
        unregisterReceiver(powerSaveReceiver);
        registeredPowerSave = false;
    }
    if (registeredUser) {
        unregisterReceiver(userReceiver);
        registeredUser = false;
    }
    if (registeredIdleState) {
        unregisterReceiver(idleStateReceiver);
        registeredIdleState = false;
    }
    if (registeredConnectivityChanged) {
        unregisterReceiver(connectivityChangedReceiver);
        registeredConnectivityChanged = false;
    }
    if (registeredPackageChanged) {
        unregisterReceiver(packageChangedReceiver);
        registeredPackageChanged = false;
    }

    if (phone_state) {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
            phone_state = false;
        }
    }

    if (subscriptionsChangedListener != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager sm = SubscriptionManager.from(this);
        sm.removeOnSubscriptionsChangedListener(
                (SubscriptionManager.OnSubscriptionsChangedListener) subscriptionsChangedListener);
        subscriptionsChangedListener = null;
    }

    try {
        if (vpn != null) {
            stopNative(vpn, true);
            stopVPN(vpn);
            vpn = null;
            unprepare();
        }
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }

    jni_done();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.unregisterOnSharedPreferenceChangeListener(this);

    super.onDestroy();
}

From source file:org.wso2.emm.agent.services.DeviceInfoPayload.java

/**
 * Fetch all device runtime information.
 * @throws AndroidAgentException/*from w w w . j  a  va  2  s  . co m*/
 */
private void getInfo() throws AndroidAgentException {

    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    Location deviceLocation = locationService.getLastKnownLocation();
    if (device == null) {
        device = new Device();
    }
    deviceInfo = new DeviceInfo(context);
    Power power = phoneState.getBatteryDetails();
    device.setDeviceIdentifier(deviceInfo.getDeviceId());
    device.setDescription(deviceInfo.getDeviceName());
    device.setName(deviceInfo.getDeviceName());

    List<Device.Property> properties = new ArrayList<>();

    Device.Property property = new Device.Property();
    property.setName(Constants.Device.SERIAL);
    property.setValue(deviceInfo.getDeviceSerialNumber());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.IMEI);
    property.setValue(telephonyManager.getDeviceId());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.IMSI);
    property.setValue(deviceInfo.getIMSINumber());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MAC);
    property.setValue(deviceInfo.getMACAddress());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MODEL);
    property.setValue(deviceInfo.getDeviceModel());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.VENDOR);
    property.setValue(deviceInfo.getDeviceManufacturer());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.OS);
    property.setValue(deviceInfo.getOsVersion());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.OS_BUILD_DATE);
    property.setValue(deviceInfo.getOSBuildDate());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.NAME);
    property.setValue(deviceInfo.getDeviceName());
    properties.add(property);

    if (deviceLocation != null) {
        double latitude = deviceLocation.getLatitude();
        double longitude = deviceLocation.getLongitude();

        if (latitude != 0 && longitude != 0) {
            property = new Device.Property();
            property.setName(Constants.Device.MOBILE_DEVICE_LATITUDE);
            property.setValue(String.valueOf(latitude));
            properties.add(property);

            property = new Device.Property();
            property.setName(Constants.Device.MOBILE_DEVICE_LONGITUDE);
            property.setValue(String.valueOf(longitude));
            properties.add(property);
        }
    }

    if (registrationId != null) {
        property = new Device.Property();
        property.setName(Constants.Device.GCM_TOKEN);
        property.setValue(registrationId);
        properties.add(property);
    }

    List<Device.Property> deviceInfoProperties = new ArrayList<>();

    property = new Device.Property();
    property.setName(Constants.Device.ENCRYPTION_STATUS);
    property.setValue(String.valueOf(deviceInfo.isEncryptionEnabled()));
    deviceInfoProperties.add(property);

    if ((deviceInfo.getSdkVersion() >= Build.VERSION_CODES.LOLLIPOP)) {
        property = new Device.Property();
        property.setName(Constants.Device.PASSCODE_STATUS);
        property.setValue(String.valueOf(deviceInfo.isPasscodeEnabled()));
        deviceInfoProperties.add(property);
    }

    property = new Device.Property();
    property.setName(Constants.Device.BATTERY_LEVEL);
    int batteryLevel = Math.round(power.getLevel());
    property.setValue(String.valueOf(batteryLevel));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MEMORY_INFO_INTERNAL_TOTAL);
    property.setValue(String.valueOf(phoneState.getTotalInternalMemorySize()));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MEMORY_INFO_INTERNAL_AVAILABLE);
    property.setValue(String.valueOf(phoneState.getAvailableInternalMemorySize()));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MEMORY_INFO_EXTERNAL_TOTAL);
    property.setValue(String.valueOf(phoneState.getTotalExternalMemorySize()));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MEMORY_INFO_EXTERNAL_AVAILABLE);
    property.setValue(String.valueOf(phoneState.getAvailableExternalMemorySize()));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.NETWORK_OPERATOR);
    property.setValue(String.valueOf(deviceInfo.getNetworkOperatorName()));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.PHONE_NUMBER);
    String mPhoneNumber = telephonyManager.getLine1Number();
    property.setValue(mPhoneNumber);
    deviceInfoProperties.add(property);

    DeviceNetworkStatus deviceNetworkStatus = DeviceNetworkStatus.getInstance(context);
    if (deviceNetworkStatus.isConnectedMobile()) {
        telephonyManager.listen(deviceNetworkStatus, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }

    String network = deviceNetworkStatus.getNetworkStatus();
    if (network != null) {
        property = new Device.Property();
        property.setName(Constants.Device.NETWORK_INFO);
        property.setValue(network);
        properties.add(property);
    }

    // adding wifi scan results..
    property = new Device.Property();
    property.setName(Constants.Device.WIFI_SCAN_RESULT);
    property.setValue(deviceNetworkStatus.getWifiScanResult());
    properties.add(property);

    RuntimeInfo runtimeInfo = new RuntimeInfo(context);
    String cpuInfoPayload;
    try {
        cpuInfoPayload = mapper.writeValueAsString(runtimeInfo.getCPUInfo());
    } catch (JsonProcessingException e) {
        String errorMsg = "Error occurred while parsing property CPU info object to json.";
        Log.e(TAG, errorMsg, e);
        throw new AndroidAgentException(errorMsg, e);
    }

    property = new Device.Property();
    property.setName(Constants.Device.CPU_INFO);
    property.setValue(cpuInfoPayload);
    properties.add(property);

    String ramInfoPayload;
    try {
        ramInfoPayload = mapper.writeValueAsString(runtimeInfo.getRAMInfo());
    } catch (JsonProcessingException e) {
        String errorMsg = "Error occurred while parsing property RAM info object to json.";
        Log.e(TAG, errorMsg, e);
        throw new AndroidAgentException(errorMsg, e);
    }

    property = new Device.Property();
    property.setName(Constants.Device.RAM_INFO);
    property.setValue(ramInfoPayload);
    properties.add(property);

    List<Device.Property> batteryProperties = new ArrayList<>();
    property = new Device.Property();
    property.setName(Constants.Device.BATTERY_LEVEL);
    property.setValue(String.valueOf(power.getLevel()));
    batteryProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.SCALE);
    property.setValue(String.valueOf(power.getScale()));
    batteryProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.BATTERY_VOLTAGE);
    property.setValue(String.valueOf(power.getVoltage()));
    batteryProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.HEALTH);
    property.setValue(String.valueOf(power.getHealth()));
    batteryProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.STATUS);
    property.setValue(String.valueOf(power.getStatus()));
    batteryProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.PLUGGED);
    property.setValue(String.valueOf(power.getPlugged()));
    batteryProperties.add(property);

    String batteryInfoPayload;
    try {
        batteryInfoPayload = mapper.writeValueAsString(batteryProperties);
    } catch (JsonProcessingException e) {
        String errorMsg = "Error occurred while parsing property battery info object to json.";
        Log.e(TAG, errorMsg, e);
        throw new AndroidAgentException(errorMsg, e);
    }

    property = new Device.Property();
    property.setName(Constants.Device.BATTERY_INFO);
    property.setValue(batteryInfoPayload);
    properties.add(property);

    // building device info json payload
    String deviceInfoPayload;
    try {
        deviceInfoPayload = mapper.writeValueAsString(deviceInfoProperties);
    } catch (JsonProcessingException e) {
        String errorMsg = "Error occurred while parsing property object to json.";
        Log.e(TAG, errorMsg, e);
        throw new AndroidAgentException(errorMsg, e);
    }
    property = new Device.Property();
    property.setName(Constants.Device.INFO);
    property.setValue(deviceInfoPayload);
    properties.add(property);

    device.setProperties(properties);
}