Search in sources :

Example 1 with ApnException

use of com.android.mms.service_alt.exception.ApnException in project qksms by moezbhatti.

the class ApnSettings method load.

/**
 * Load APN settings from system
 *
 * @param context
 * @param apnName the optional APN name to match
 */
public static ApnSettings load(Context context, String apnName, int subId) throws ApnException {
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    String mmsc = sharedPrefs.getString("mmsc_url", "");
    if (!TextUtils.isEmpty(mmsc)) {
        String mmsProxy = sharedPrefs.getString("mms_proxy", "");
        String mmsPort = sharedPrefs.getString("mms_port", "");
        return new ApnSettings(mmsc, mmsProxy, parsePort(mmsPort), "Default from settings");
    }
    Timber.v("ApnSettings: apnName " + apnName);
    // TODO: CURRENT semantics is currently broken in telephony. Revive this when it is fixed.
    // String selection = Telephony.Carriers.CURRENT + " IS NOT NULL";
    String selection = null;
    String[] selectionArgs = null;
    apnName = apnName != null ? apnName.trim() : null;
    if (!TextUtils.isEmpty(apnName)) {
        // selection += " AND " + Telephony.Carriers.APN + "=?";
        selection = Telephony.Carriers.APN + "=?";
        selectionArgs = new String[] { apnName };
    }
    Cursor cursor = null;
    try {
        cursor = SqliteWrapper.query(context, context.getContentResolver(), Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "/subId/" + subId), APN_PROJECTION, selection, selectionArgs, null);
        if (cursor != null) {
            String mmscUrl = null;
            String proxyAddress = null;
            int proxyPort = -1;
            while (cursor.moveToNext()) {
                // Read values from APN settings
                if (isValidApnType(cursor.getString(COLUMN_TYPE), "mms")) {
                    mmscUrl = trimWithNullCheck(cursor.getString(COLUMN_MMSC));
                    if (TextUtils.isEmpty(mmscUrl)) {
                        continue;
                    }
                    mmscUrl = NetworkUtilsHelper.trimV4AddrZeros(mmscUrl);
                    try {
                        new URI(mmscUrl);
                    } catch (URISyntaxException e) {
                        throw new ApnException("Invalid MMSC url " + mmscUrl);
                    }
                    proxyAddress = trimWithNullCheck(cursor.getString(COLUMN_MMSPROXY));
                    if (!TextUtils.isEmpty(proxyAddress)) {
                        proxyAddress = NetworkUtilsHelper.trimV4AddrZeros(proxyAddress);
                        final String portString = trimWithNullCheck(cursor.getString(COLUMN_MMSPORT));
                        if (portString != null) {
                            try {
                                proxyPort = Integer.parseInt(portString);
                            } catch (NumberFormatException e) {
                                Timber.e("Invalid port " + portString);
                                throw new ApnException("Invalid port " + portString);
                            }
                        }
                    }
                    return new ApnSettings(mmscUrl, proxyAddress, proxyPort, getDebugText(cursor));
                }
            }
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return new ApnSettings("", "", 80, "Failed to find APNs :(");
}
Also used : SharedPreferences(android.content.SharedPreferences) ApnException(com.android.mms.service_alt.exception.ApnException) URISyntaxException(java.net.URISyntaxException) Cursor(android.database.Cursor) URI(java.net.URI)

Example 2 with ApnException

use of com.android.mms.service_alt.exception.ApnException in project qksms by moezbhatti.

the class MmsRequest method execute.

/**
 * Execute the request
 *
 * @param context The context
 * @param networkManager The network manager to use
 */
public void execute(Context context, MmsNetworkManager networkManager) {
    int result = SmsManager.MMS_ERROR_UNSPECIFIED;
    int httpStatusCode = 0;
    byte[] response = null;
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    boolean isWifiEnabled = wifi.isWifiEnabled();
    if (!useWifi(context)) {
        wifi.setWifiEnabled(false);
    }
    mobileDataEnabled = Utils.isMobileDataEnabled(context);
    Timber.v("mobile data enabled: " + mobileDataEnabled);
    if (!mobileDataEnabled && !useWifi(context)) {
        Timber.v("mobile data not enabled, so forcing it to enable");
        Utils.setMobileDataEnabled(context, true);
    }
    if (!ensureMmsConfigLoaded()) {
        // Check mms config
        Timber.e("MmsRequest: mms config is not loaded yet");
        result = SmsManager.MMS_ERROR_CONFIGURATION_ERROR;
    } else if (!prepareForHttpRequest()) {
        // Prepare request, like reading pdu data from user
        Timber.e("MmsRequest: failed to prepare for request");
        result = SmsManager.MMS_ERROR_IO_ERROR;
    } else if (!isDataNetworkAvailable(context, mSubId)) {
        Timber.e("MmsRequest: in airplane mode or mobile data disabled");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            result = SmsManager.MMS_ERROR_NO_DATA_NETWORK;
        } else {
            result = 8;
        }
    } else {
        // Execute
        long retryDelaySecs = 2;
        // Try multiple times of MMS HTTP request
        for (int i = 0; i < RETRY_TIMES; i++) {
            try {
                try {
                    networkManager.acquireNetwork();
                } catch (Exception e) {
                    Timber.e(e, "error acquiring network");
                }
                final String apnName = networkManager.getApnName();
                try {
                    ApnSettings apn = null;
                    try {
                        apn = ApnSettings.load(context, apnName, mSubId);
                    } catch (ApnException e) {
                        // If no APN could be found, fall back to trying without the APN name
                        if (apnName == null) {
                            // If the APN name was already null then don't need to retry
                            throw (e);
                        }
                        Timber.i("MmsRequest: No match with APN name:" + apnName + ", try with no name");
                        apn = ApnSettings.load(context, null, mSubId);
                    }
                    Timber.i("MmsRequest: using " + apn.toString());
                    response = doHttp(context, networkManager, apn);
                    result = Activity.RESULT_OK;
                    // Success
                    break;
                } finally {
                    networkManager.releaseNetwork();
                }
            } catch (ApnException e) {
                Timber.e(e, "MmsRequest: APN failure");
                result = SmsManager.MMS_ERROR_INVALID_APN;
                break;
            // } catch (MmsNetworkException e) {
            // Timber.e(e, "MmsRequest: MMS network acquiring failure");
            // result = SmsManager.MMS_ERROR_UNABLE_CONNECT_MMS;
            // // Retry
            } catch (MmsHttpException e) {
                Timber.e(e, "MmsRequest: HTTP or network I/O failure");
                result = SmsManager.MMS_ERROR_HTTP_FAILURE;
                httpStatusCode = e.getStatusCode();
            // Retry
            } catch (Exception e) {
                Timber.e(e, "MmsRequest: unexpected failure");
                result = SmsManager.MMS_ERROR_UNSPECIFIED;
                break;
            }
            try {
                Thread.sleep(retryDelaySecs * 1000, 0);
            } catch (InterruptedException e) {
            }
            retryDelaySecs <<= 1;
        }
    }
    if (!mobileDataEnabled) {
        Timber.v("setting mobile data back to disabled");
        Utils.setMobileDataEnabled(context, false);
    }
    if (!useWifi(context)) {
        wifi.setWifiEnabled(isWifiEnabled);
    }
    processResult(context, result, response, httpStatusCode);
}
Also used : WifiManager(android.net.wifi.WifiManager) ApnException(com.android.mms.service_alt.exception.ApnException) ApnException(com.android.mms.service_alt.exception.ApnException) MmsHttpException(com.android.mms.service_alt.exception.MmsHttpException) MmsHttpException(com.android.mms.service_alt.exception.MmsHttpException)

Aggregations

ApnException (com.android.mms.service_alt.exception.ApnException)2 SharedPreferences (android.content.SharedPreferences)1 Cursor (android.database.Cursor)1 WifiManager (android.net.wifi.WifiManager)1 MmsHttpException (com.android.mms.service_alt.exception.MmsHttpException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1