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

Commit 40517c2f authored by Chiachang Wang's avatar Chiachang Wang Committed by android-build-merger
Browse files

Merge "Add NetworkShim"

am: c95af409

Change-Id: I927586dd6f951d07b8b02df098cbe4331aba5921
parents b7885851 c95af409
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.networkstack.apishim.api29;

import android.net.Network;

import androidx.annotation.NonNull;

import com.android.networkstack.apishim.NetworkShim;
import com.android.networkstack.apishim.UnsupportedApiLevelException;
/**
 * Implementation of NetworkShim for API 29.
 */
public class NetworkShimImpl implements NetworkShim {
    @NonNull
    protected final Network mNetwork;

    protected NetworkShimImpl(@NonNull Network network) {
        mNetwork = network;
    }

    /**
     * Get a new instance of {@link NetworkShim}.
     *
     * Use com.android.networkstack.apishim.NetworkShimImpl#newInstance()
     * (non-API29 version) instead, to use the correct shims depending on build SDK.
     */
    public static NetworkShim newInstance(@NonNull Network network) {
        return new NetworkShimImpl(network);
    }

    /**
     * Get the netId of the network.
     * Throw UnsupportedApiLevelException if API is not available in this API level.
     */
    @Override
    public int getNetId() throws UnsupportedApiLevelException {
        // Not supported for API 29.
        throw new UnsupportedApiLevelException("Not supported in API 29.");
    }
}
+49 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.networkstack.apishim;

import android.net.Network;
import android.os.Build;

import androidx.annotation.NonNull;

/**
 * Implementation of {@link NetworkShim} for API 30.
 */
public class NetworkShimImpl extends com.android.networkstack.apishim.api29.NetworkShimImpl {
    protected NetworkShimImpl(@NonNull Network network) {
        super(network);
    }

    /**
     * Get a new instance of {@link NetworkShim}.
     */
    public static NetworkShim newInstance(@NonNull Network network) {
        if (!ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)) {
            return com.android.networkstack.apishim.api29.NetworkShimImpl.newInstance(network);
        }
        return new NetworkShimImpl(network);
    }

    /**
     * Get the netId of the network.
     */
    @Override
    public int getNetId() {
        return mNetwork.netId;
    }
}
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.networkstack.apishim;

/**
 * Interface used to access API methods in {@link android.net.Network}, with appropriate fallbacks
 * if the methods are not yet part of the released API.
 *
 * <p>This interface makes it easier for callers to use NetworkShimImpl, as it's more obvious what
 * methods must be implemented on each API level, and it abstracts from callers the need to
 * reference classes that have different implementations (which also does not work well with IDEs).
 */
public interface NetworkShim {
    /**
     * @see android.net.Network.netId.
     *
     * Throw UnsupportedApiLevelException if API is not available in the API level.
     */
    int getNetId() throws UnsupportedApiLevelException;
}
+69 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.networkstack.apishim;

import androidx.annotation.Nullable;

/**
 * Signals that the calling API is not supported with device release or development API level
 */
public class UnsupportedApiLevelException extends Exception {
    /**
     * Constructs an {@code UnsupportedApiLevelException} with {@code null} as its error detail
     * message.
     */
    public UnsupportedApiLevelException() {
        super();
    }

    /**
     * Constructs an {@code UnsupportedApiLevelException} with the specified detail message.
     *
     * @param message The detail message (which is saved for later retrieval by the
     *                {@link #getMessage()} method)
     */
    public UnsupportedApiLevelException(String message) {
        super(message);
    }

    /**
     * Constructs an {@code UnsupportedApiLevelException} with the specified detail message
     * and cause.
     *
     * @param message The detail message (which is saved for later retrieval by the
     *                {@link #getMessage()} method)
     *
     * @param cause The cause (which is saved for later retrieval by the {@link #getCause()}
     *              method).  (A null value is permitted, and indicates that the cause is
     *              nonexistent or unknown.)
     */
    public UnsupportedApiLevelException(String message, @Nullable Throwable cause) {
        super(message, cause);
    }

    /**
     * Constructs an {@code UnsupportedApiLevelException} with the specified cause and a
     * detail message of {@code (cause==null ? null : cause.toString())} (which typically contains
     * the class and detail message of {@code cause}).
     *
     * @param cause The cause (which is saved for later retrieval by the {@link #getCause()}
     *              method).  (A null value is permitted, and indicates that the cause is
     *              nonexistent or unknown.)
     */
    public UnsupportedApiLevelException(@Nullable Throwable cause) {
        super(cause);
    }
}