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

Commit 25f8df2d authored by Cody Kesting's avatar Cody Kesting
Browse files

Define CarrierPrivilegesTracker for tracking CarrierPrivileged UIDs.

CarrierPrivilegesTracker is defined for tracking Carrier Privileged UIDs
for a specific Phone. CarrierPrivilegesTracker will monitor for
CarrierConfig changes, SIM state changes, and Package
installs/uninstalls - each of which can impact which UIDs should receive
Carrier Privileges.

Telephony currently has no centralized source for tracking Carrier
Privileges. Carrier Privileges can be queried via
TelephonyManager#hasCarrierPrivileges and by checking the Carrier
Configs for included certs. With CarrierPrivilegesTracker, internal
Telephony apps will be able to enumerate all Carrier Privileged UIDs for
a specific phone.

Bug: 147391402
Test: Tested on crosshatch build
Change-Id: I903805d6085dca13cfc05ecc01a8c439061bcc60
parent 52300cde
Loading
Loading
Loading
Loading
+103 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * 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 com.android.internal.telephony;

import android.annotation.NonNull;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Registrant;
import android.os.RegistrantList;
import android.util.Log;

/**
 * CarrierPrivilegesTracker will track the Carrier Privileges for a specific {@link Phone}.
 * Registered Telephony entities will receive notifications when the UIDs with these privileges
 * change.
 */
public class CarrierPrivilegesTracker extends Handler {
    private static final String TAG = CarrierPrivilegesTracker.class.getSimpleName();

    /**
     * Action to register a Registrant with this Tracker.
     * obj: Registrant that will be notified of Carrier Privileged UID changes.
     */
    private static final int ACTION_REGISTER_LISTENER = 1;

    /**
     * Action to unregister a Registrant with this Tracker.
     * obj: Handler used by the Registrant that will be removed.
     */
    private static final int ACTION_UNREGISTER_LISTENER = 2;

    private final RegistrantList mRegistrantList;
    private int[] mPrivilegedUids;

    public CarrierPrivilegesTracker(@NonNull Looper looper) {
        super(looper);
        mRegistrantList = new RegistrantList();
        mPrivilegedUids = new int[0];
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case ACTION_REGISTER_LISTENER: {
                handleRegisterListener((Registrant) msg.obj);
                break;
            }
            case ACTION_UNREGISTER_LISTENER: {
                handleUnregisterListener((Handler) msg.obj);
                break;
            }
            default: {
                Log.e(TAG, "Received unknown msg type: " + msg.what);
                break;
            }
        }
    }

    private void handleRegisterListener(Registrant registrant) {
        mRegistrantList.add(registrant);
        registrant.notifyResult(mPrivilegedUids);
    }

    private void handleUnregisterListener(Handler handler) {
        mRegistrantList.remove(handler);
    }

    /**
     * Registers the given Registrant with this tracker.
     *
     * <p>After being registered, the Registrant will be notified with the current Carrier
     * Privileged UIDs for this Phone.
     *
     * @hide
     */
    public void registerCarrierPrivilegesListener(Handler h, int what, Object obj) {
        sendMessage(obtainMessage(ACTION_REGISTER_LISTENER, new Registrant(h, what, obj)));
    }

    /**
     * Unregisters the given listener with this tracker.
     *
     * @hide
     */
    public void unregisterCarrierPrivilegesListener(Handler handler) {
        sendMessage(obtainMessage(ACTION_UNREGISTER_LISTENER, handler));
    }
}