1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.vcn; 18 19 import android.annotation.NonNull; 20 import android.content.Context; 21 import android.os.Looper; 22 23 import java.util.Objects; 24 25 /** 26 * A simple class to pass around context information. 27 * 28 * @hide 29 */ 30 public class VcnContext { 31 @NonNull private final Context mContext; 32 @NonNull private final Looper mLooper; 33 @NonNull private final VcnNetworkProvider mVcnNetworkProvider; 34 private final boolean mIsInTestMode; 35 VcnContext( @onNull Context context, @NonNull Looper looper, @NonNull VcnNetworkProvider vcnNetworkProvider, boolean isInTestMode)36 public VcnContext( 37 @NonNull Context context, 38 @NonNull Looper looper, 39 @NonNull VcnNetworkProvider vcnNetworkProvider, 40 boolean isInTestMode) { 41 mContext = Objects.requireNonNull(context, "Missing context"); 42 mLooper = Objects.requireNonNull(looper, "Missing looper"); 43 mVcnNetworkProvider = Objects.requireNonNull(vcnNetworkProvider, "Missing networkProvider"); 44 mIsInTestMode = isInTestMode; 45 } 46 47 @NonNull getContext()48 public Context getContext() { 49 return mContext; 50 } 51 52 @NonNull getLooper()53 public Looper getLooper() { 54 return mLooper; 55 } 56 57 @NonNull getVcnNetworkProvider()58 public VcnNetworkProvider getVcnNetworkProvider() { 59 return mVcnNetworkProvider; 60 } 61 isInTestMode()62 public boolean isInTestMode() { 63 return mIsInTestMode; 64 } 65 66 /** 67 * Verifies that the caller is running on the VcnContext Thread. 68 * 69 * @throwsIllegalStateException if the caller is not running on the VcnContext Thread. 70 */ ensureRunningOnLooperThread()71 public void ensureRunningOnLooperThread() { 72 if (getLooper().getThread() != Thread.currentThread()) { 73 throw new IllegalStateException("Not running on VcnMgmtSvc thread"); 74 } 75 } 76 } 77