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

Commit 18ec6884 authored by Zoey Chen's avatar Zoey Chen Committed by Gerrit Code Review
Browse files

Merge "[Telephony mainline] Move the functionality into the calling locations."

parents f32ebb8b 175ce0a0
Loading
Loading
Loading
Loading
+19 −3
Original line number Diff line number Diff line
@@ -23,8 +23,7 @@ import android.telephony.Rlog;

/**
 * An {@link AsyncTask} that notifies the Blocked number provider that emergency services were
 * contacted. See {@link BlockedNumberContract.SystemContract#notifyEmergencyContact(Context)}
 * for details.
 * contacted.
 * {@hide}
 */
public class AsyncEmergencyContactNotifier extends AsyncTask<Void, Void, Void> {
@@ -39,10 +38,27 @@ public class AsyncEmergencyContactNotifier extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        try {
            BlockedNumberContract.SystemContract.notifyEmergencyContact(mContext);
            notifyEmergencyContact(mContext);
        } catch (Exception e) {
            Rlog.e(TAG, "Exception notifying emergency contact: " + e);
        }
        return null;
    }

    /**
     * Notifies the provider that emergency services were contacted by the user.
     */
    private void notifyEmergencyContact(Context context) {
        try {
            Rlog.i("notifyEmergencyContact; caller=%s", context.getOpPackageName());
            context.getContentResolver().call(
                    BlockedNumberContract.AUTHORITY_URI,
                    BlockedNumberContract.METHOD_NOTIFY_EMERGENCY_CONTACT,
                    null, null);
        } catch (NullPointerException | IllegalArgumentException ex) {
            // The content resolver can throw an NPE or IAE; we don't want to crash Telecom if
            // either of these happen.
            Rlog.w(null, "notifyEmergencyContact: provider not ready.");
        }
    }
}
+60 −3
Original line number Diff line number Diff line
@@ -67,19 +67,76 @@ public class BlockChecker {
        long startTimeNano = System.nanoTime();

        try {
            blockStatus = BlockedNumberContract.SystemContract.shouldSystemBlockNumber(
                    context, phoneNumber, extras);
            blockStatus = shouldSystemBlockNumber(context, phoneNumber, extras);
            if (blockStatus != BlockedNumberContract.STATUS_NOT_BLOCKED) {
                Rlog.d(TAG, phoneNumber + " is blocked.");
            }
        } catch (Exception e) {
            Rlog.e(TAG, "Exception checking for blocked number: " + e);
        }

        int durationMillis = (int) ((System.nanoTime() - startTimeNano) / 1000000);
        if (durationMillis > 500 || VDBG) {
            Rlog.d(TAG, "Blocked number lookup took: " + durationMillis + " ms.");
        }
        return blockStatus;
    }

    /**
     * Returns {@code true} if {@code phoneNumber} is blocked taking
     * {@link #notifyEmergencyContact(Context)} into consideration. If emergency services
     * have not been contacted recently and enhanced call blocking not been enabled, this
     * method is equivalent to {@link #isBlocked(Context, String)}.
     *
     * @param context the context of the caller.
     * @param phoneNumber the number to check.
     * @param extras the extra attribute of the number.
     * @return result code indicating if the number should be blocked, and if so why.
     *         Valid values are: {@link #STATUS_NOT_BLOCKED}, {@link #STATUS_BLOCKED_IN_LIST},
     *         {@link #STATUS_BLOCKED_NOT_IN_CONTACTS}, {@link #STATUS_BLOCKED_PAYPHONE},
     *         {@link #STATUS_BLOCKED_RESTRICTED}, {@link #STATUS_BLOCKED_UNKNOWN_NUMBER}.
     */
    private static int shouldSystemBlockNumber(Context context, String phoneNumber,
                                              Bundle extras) {
        try {
            String caller = context.getOpPackageName();
            final Bundle res = context.getContentResolver().call(
                    BlockedNumberContract.AUTHORITY_URI,
                    BlockedNumberContract.METHOD_SHOULD_SYSTEM_BLOCK_NUMBER,
                    phoneNumber, extras);
            int blockResult = res != null ? res.getInt(BlockedNumberContract.RES_BLOCK_STATUS,
                    BlockedNumberContract.STATUS_NOT_BLOCKED) :
                    BlockedNumberContract.STATUS_NOT_BLOCKED;
            Rlog.d(TAG, "shouldSystemBlockNumber: number=" + Rlog.pii(TAG, phoneNumber)
                    + "caller=" + caller + "result=" + blockStatusToString(blockResult));
            return blockResult;
        } catch (NullPointerException | IllegalArgumentException ex) {
            // The content resolver can throw an NPE or IAE; we don't want to crash Telecom if
            // either of these happen.
            Rlog.w(null, "shouldSystemBlockNumber: provider not ready.");
            return BlockedNumberContract.STATUS_NOT_BLOCKED;
        }
    }

    /**
     * Converts a block status constant to a string equivalent for logging.
     * @hide
     */
    private static String blockStatusToString(int blockStatus) {
        switch (blockStatus) {
            case BlockedNumberContract.STATUS_NOT_BLOCKED:
                return "not blocked";
            case BlockedNumberContract.STATUS_BLOCKED_IN_LIST:
                return "blocked - in list";
            case BlockedNumberContract.STATUS_BLOCKED_RESTRICTED:
                return "blocked - restricted";
            case BlockedNumberContract.STATUS_BLOCKED_UNKNOWN_NUMBER:
                return "blocked - unknown";
            case BlockedNumberContract.STATUS_BLOCKED_PAYPHONE:
                return "blocked - payphone";
            case BlockedNumberContract.STATUS_BLOCKED_NOT_IN_CONTACTS:
                return "blocked - not in contacts";
        }
        return "unknown";
    }

}
+2 −2
Original line number Diff line number Diff line
@@ -661,14 +661,14 @@ public abstract class TelephonyTest {
        @Override
        public Bundle call(String method, String arg, Bundle extras) {
            switch (method) {
                case BlockedNumberContract.SystemContract.METHOD_SHOULD_SYSTEM_BLOCK_NUMBER:
                case BlockedNumberContract.METHOD_SHOULD_SYSTEM_BLOCK_NUMBER:
                    Bundle bundle = new Bundle();
                    int blockStatus = mBlockedNumbers.contains(arg)
                            ? BlockedNumberContract.STATUS_BLOCKED_IN_LIST
                            : BlockedNumberContract.STATUS_NOT_BLOCKED;
                    bundle.putInt(BlockedNumberContract.RES_BLOCK_STATUS, blockStatus);
                    return bundle;
                case BlockedNumberContract.SystemContract.METHOD_NOTIFY_EMERGENCY_CONTACT:
                case BlockedNumberContract.METHOD_NOTIFY_EMERGENCY_CONTACT:
                    mNumEmergencyContactNotifications++;
                    return new Bundle();
                default: