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

Commit 81a480f4 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add Ability to Encode PlmnActRecords"

parents 5bde544a 66fbd74b
Loading
Loading
Loading
Loading
+52 −17
Original line number Diff line number Diff line
@@ -16,11 +16,15 @@

package com.android.internal.telephony.uicc;

import android.annotation.IntDef;
import android.os.Parcel;
import android.os.Parcelable;
import android.telephony.Rlog;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.Objects;

/**
 * {@hide}
@@ -28,6 +32,13 @@ import java.util.Arrays;
public class PlmnActRecord implements Parcelable {
    private static final String LOG_TAG = "PlmnActRecord";

    private static final boolean VDBG = false;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef(flag = true, value = {ACCESS_TECH_UTRAN, ACCESS_TECH_EUTRAN, ACCESS_TECH_GSM,
            ACCESS_TECH_GSM_COMPACT, ACCESS_TECH_CDMA2000_HRPD, ACCESS_TECH_CDMA2000_1XRTT,
            ACCESS_TECH_RESERVED})
    public @interface AccessTech {}
    // Values specified in 3GPP 31.102 sec. 4.2.5
    public static final int ACCESS_TECH_UTRAN = 0x8000;
    public static final int ACCESS_TECH_EUTRAN = 0x4000;
@@ -42,20 +53,13 @@ public class PlmnActRecord implements Parcelable {
    public final String plmn;
    public final int accessTechs;

    private static final boolean VDBG = false;

    public static final Parcelable.Creator<PlmnActRecord> CREATOR =
            new Parcelable.Creator<PlmnActRecord>() {
        @Override
        public PlmnActRecord createFromParcel(Parcel source) {
            return new PlmnActRecord(source.readString(), source.readInt());
        }

        @Override
        public PlmnActRecord[] newArray(int size) {
            return new PlmnActRecord[size];
    /**
     * Instantiate a PLMN w/ ACT Record
     */
    public PlmnActRecord(String plmn, int accessTechs) {
        this.plmn = plmn;
        this.accessTechs = accessTechs;
    }
    };

    /* From 3gpp 31.102 section 4.2.5
     * Bytes 0-2 bcd-encoded PLMN-ID
@@ -68,9 +72,15 @@ public class PlmnActRecord implements Parcelable {
                        | Byte.toUnsignedInt(bytes[offset + 4]);
    }

    private PlmnActRecord(String plmn, int accessTechs) {
        this.plmn = plmn;
        this.accessTechs = accessTechs;
    /**
     * Get a record encoded as per 31.102 section 4.2.5
     */
    public byte[] getBytes() {
        byte[] ret = new byte[ENCODED_LENGTH];
        IccUtils.stringToBcdPlmn(this.plmn, ret, 0);
        ret[3] = (byte) (this.accessTechs >> 8);
        ret[4] = (byte) this.accessTechs;
        return ret;
    }

    private String accessTechString() {
@@ -142,4 +152,29 @@ public class PlmnActRecord implements Parcelable {
        dest.writeInt(accessTechs);
    }

    public static final Parcelable.Creator<PlmnActRecord> CREATOR =
            new Parcelable.Creator<PlmnActRecord>() {
        @Override
        public PlmnActRecord createFromParcel(Parcel source) {
            return new PlmnActRecord(source.readString(), source.readInt());
        }

        @Override
        public PlmnActRecord[] newArray(int size) {
            return new PlmnActRecord[size];
        }
    };

    @Override
    public int hashCode() {
        return Objects.hash(plmn, accessTechs);
    }

    @Override
    public boolean equals(Object rhs) {
        if (!(rhs instanceof PlmnActRecord)) return false;

        PlmnActRecord r = (PlmnActRecord) rhs;
        return plmn.equals(r.plmn) && accessTechs == r.accessTechs;
    }
}
+84 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.uicc;

import static org.junit.Assert.assertEquals;

import android.os.Parcel;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.Arrays;

/** Unit tests for PlmnActRecord */

public class PlmnActRecordTest extends AndroidTestCase {

    private static final String TEST_PLMN_5DIGIT = "12345";
    private static final byte[] TEST_RECORD_5DIGIT = new byte[] {
            (byte) 0x21, (byte) 0xF3, (byte) 0x54, (byte) 0xC0, (byte) 0x80};
    private static final String TEST_PLMN_6DIGIT = "123456";
    private static final byte[] TEST_RECORD_6DIGIT = new byte[] {
            (byte) 0x21, (byte) 0x63, (byte) 0x54, (byte) 0xC0, (byte) 0x80};

    private static final int ACCESS_TECHS_3GPP = PlmnActRecord.ACCESS_TECH_EUTRAN
            | PlmnActRecord.ACCESS_TECH_UTRAN | PlmnActRecord.ACCESS_TECH_GSM;

    @SmallTest
    public void testConstructors() {
        PlmnActRecord rec = new PlmnActRecord(TEST_PLMN_5DIGIT, ACCESS_TECHS_3GPP);
        assertEquals(TEST_PLMN_5DIGIT, rec.plmn);
        assertEquals(ACCESS_TECHS_3GPP, rec.accessTechs);

        PlmnActRecord rec2 = new PlmnActRecord(TEST_RECORD_5DIGIT, 0);
        assertEquals(TEST_PLMN_5DIGIT, rec.plmn);
        assertEquals(ACCESS_TECHS_3GPP, rec.accessTechs);

        assertEquals(rec, rec2);

        rec = new PlmnActRecord(TEST_PLMN_6DIGIT, ACCESS_TECHS_3GPP);
        assertEquals(TEST_PLMN_6DIGIT, rec.plmn);
        assertEquals(ACCESS_TECHS_3GPP, rec.accessTechs);

        rec2 = new PlmnActRecord(TEST_RECORD_6DIGIT, 0);
        assertEquals(TEST_PLMN_6DIGIT, rec.plmn);
        assertEquals(ACCESS_TECHS_3GPP, rec.accessTechs);

        assertEquals(rec, rec2);
    }

    @SmallTest
    public void testParcel() {
        PlmnActRecord par = new PlmnActRecord(TEST_PLMN_5DIGIT, ACCESS_TECHS_3GPP);

        Parcel p = Parcel.obtain();
        par.writeToParcel(p, 0);
        p.setDataPosition(0);

        PlmnActRecord par2 = PlmnActRecord.CREATOR.createFromParcel(p);
        assertEquals(par, par2);
    }

    @SmallTest
    public void testEncoding() {
        PlmnActRecord rec = new PlmnActRecord(TEST_RECORD_5DIGIT, 0);
        assertTrue(Arrays.equals(rec.getBytes(), TEST_RECORD_5DIGIT));

        rec = new PlmnActRecord(TEST_RECORD_6DIGIT, 0);
        assertTrue(Arrays.equals(rec.getBytes(), TEST_RECORD_6DIGIT));
    }
}