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

Commit 9467b38d authored by Tyler Gunn's avatar Tyler Gunn
Browse files

Enforce message length using encoding and limit from phone acc extras.

- Enforce length limit and character encoding using configured values from
the phone account extras.

Bug: 22779583
Change-Id: Id007855b4c10e945bdb534d7b877011e9c980c57
parent eec094a2
Loading
Loading
Loading
Loading
+63 −1
Original line number Diff line number Diff line
@@ -27,10 +27,13 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PersistableBundle;
import android.os.ResultReceiver;
import android.preference.PreferenceManager;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.CarrierConfigManager;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
@@ -54,6 +57,7 @@ import com.android.contacts.common.R;
import com.android.contacts.common.util.UriUtils;
import com.android.phone.common.animation.AnimUtils;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@@ -85,6 +89,7 @@ public class CallSubjectDialog extends Activity {
    public static final String ARG_PHONE_ACCOUNT_HANDLE = "PHONE_ACCOUNT_HANDLE";

    private int mAnimationDuration;
    private Charset mMessageEncoding;
    private View mBackgroundView;
    private View mDialogView;
    private QuickContactBadge mContactPhoto;
@@ -277,6 +282,7 @@ public class CallSubjectDialog extends Activity {
        mPhotoSize = getResources().getDimensionPixelSize(
                R.dimen.call_subject_dialog_contact_photo_size);
        readArguments();
        loadConfiguration();
        mSubjectHistory = loadSubjectHistory(mPrefs);

        setContentView(R.layout.dialog_call_subject);
@@ -353,7 +359,17 @@ public class CallSubjectDialog extends Activity {
     * exceeded.
     */
    private void updateCharacterLimit() {
        int length = mCallSubjectView.length();
        String subjectText = mCallSubjectView.getText().toString();
        final int length;

        // If a message encoding is specified, use that to count bytes in the message.
        if (mMessageEncoding != null) {
            length = subjectText.getBytes(mMessageEncoding).length;
        } else {
            // No message encoding specified, so just count characters entered.
            length = subjectText.length();
        }

        mCharacterLimitView.setText(
                getString(R.string.call_subject_limit, length, mLimit));
        if (length >= mLimit) {
@@ -558,4 +574,50 @@ public class CallSubjectDialog extends Activity {
                }
        );
    }

    /**
     * Loads the message encoding and maximum message length from the phone account extras for the
     * current phone account.
     */
    private void loadConfiguration() {
        // Only attempt to load configuration from the phone account extras if the SDK is N or
        // later.  If we've got a prior SDK the default encoding and message length will suffice.
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk <= android.os.Build.VERSION_CODES.M) {
            return;
        }

        if (mPhoneAccountHandle == null) {
            return;
        }

        TelecomManager telecomManager =
                (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
        final PhoneAccount account = telecomManager.getPhoneAccount(mPhoneAccountHandle);

        Bundle phoneAccountExtras = account.getExtras();
        if (phoneAccountExtras == null) {
            return;
        }

        // Get limit, if provided; otherwise default to existing value.
        mLimit = phoneAccountExtras.getInt(PhoneAccount.EXTRA_CALL_SUBJECT_MAX_LENGTH, mLimit);

        // Get charset; default to none (e.g. count characters 1:1).
        String charsetName = phoneAccountExtras.getString(
                PhoneAccount.EXTRA_CALL_SUBJECT_CHARACTER_ENCODING);

        if (!TextUtils.isEmpty(charsetName)) {
            try {
                mMessageEncoding = Charset.forName(charsetName);
            } catch (java.nio.charset.UnsupportedCharsetException uce) {
                // Character set was invalid; log warning and fallback to none.
                Log.w(TAG, "Invalid charset: " + charsetName);
                mMessageEncoding = null;
            }
        } else {
            // No character set specified, so count characters 1:1.
            mMessageEncoding = null;
        }
    }
}