• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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.libraries.testing.deviceshadower.internal.common;
18 
19 import com.android.libraries.testing.deviceshadower.Enums;
20 
21 import java.io.IOException;
22 import java.util.HashSet;
23 import java.util.Set;
24 
25 /**
26  * Interrupter sets and checks interruptible point, and interrupt operation by throwing
27  * IOException.
28  */
29 public class Interrupter {
30 
31     private final InheritableThreadLocal<Integer> mCurrentIdentifier;
32     private int mInterruptIdentifier;
33 
34     private final Set<Enums.Operation> mInterruptOperations = new HashSet<>();
35 
Interrupter()36     public Interrupter() {
37         mCurrentIdentifier = new InheritableThreadLocal<Integer>() {
38             @Override
39             protected Integer initialValue() {
40                 return -1;
41             }
42         };
43     }
44 
checkInterrupt()45     public void checkInterrupt() throws IOException {
46         if (mCurrentIdentifier.get() == mInterruptIdentifier) {
47             throw new IOException(
48                     "Bluetooth interrupted at identifier: " + mCurrentIdentifier.get());
49         }
50     }
51 
setInterruptible(int identifier)52     public void setInterruptible(int identifier) {
53         mCurrentIdentifier.set(identifier);
54     }
55 
interrupt(int identifier)56     public void interrupt(int identifier) {
57         mInterruptIdentifier = identifier;
58     }
59 
addInterruptOperation(Enums.Operation operation)60     public void addInterruptOperation(Enums.Operation operation) {
61         mInterruptOperations.add(operation);
62     }
63 
shouldInterrupt(Enums.Operation operation)64     public boolean shouldInterrupt(Enums.Operation operation) {
65         return mInterruptOperations.contains(operation);
66     }
67 
68 }
69