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; 18 19 import com.android.libraries.testing.deviceshadower.Bluelet; 20 import com.android.libraries.testing.deviceshadower.Devicelet; 21 import com.android.libraries.testing.deviceshadower.Enums.Distance; 22 import com.android.libraries.testing.deviceshadower.Nfclet; 23 import com.android.libraries.testing.deviceshadower.Smslet; 24 import com.android.libraries.testing.deviceshadower.internal.bluetooth.BlueletImpl; 25 import com.android.libraries.testing.deviceshadower.internal.common.BroadcastManager; 26 import com.android.libraries.testing.deviceshadower.internal.common.Scheduler; 27 import com.android.libraries.testing.deviceshadower.internal.nfc.NfcletImpl; 28 import com.android.libraries.testing.deviceshadower.internal.sms.SmsletImpl; 29 30 import java.util.HashMap; 31 import java.util.Map; 32 33 /** 34 * DeviceletImpl is the implementation to hold different medium-let in DeviceShadowEnvironment. 35 */ 36 public class DeviceletImpl implements Devicelet { 37 38 private final BlueletImpl mBluelet; 39 private final NfcletImpl mNfclet; 40 private final SmsletImpl mSmslet; 41 private final BroadcastManager mBroadcastManager; 42 private final String mAddress; 43 private final Map<String, Distance> mDistanceMap = new HashMap<>(); 44 private final Scheduler mServiceScheduler; 45 private final Scheduler mUiScheduler; 46 DeviceletImpl(String address)47 public DeviceletImpl(String address) { 48 this.mAddress = address; 49 this.mServiceScheduler = new Scheduler(address + "-service"); 50 this.mUiScheduler = new Scheduler(address + "-main"); 51 this.mBroadcastManager = new BroadcastManager(mUiScheduler); 52 this.mBluelet = new BlueletImpl(address, mBroadcastManager); 53 this.mNfclet = new NfcletImpl(); 54 this.mSmslet = new SmsletImpl(); 55 } 56 57 @Override bluetooth()58 public Bluelet bluetooth() { 59 return mBluelet; 60 } 61 blueletImpl()62 public BlueletImpl blueletImpl() { 63 return mBluelet; 64 } 65 66 @Override nfc()67 public Nfclet nfc() { 68 return mNfclet; 69 } 70 nfcletImpl()71 public NfcletImpl nfcletImpl() { 72 return mNfclet; 73 } 74 75 @Override sms()76 public Smslet sms() { 77 return mSmslet; 78 } 79 smsletImpl()80 public SmsletImpl smsletImpl() { 81 return mSmslet; 82 } 83 getBroadcastManager()84 public BroadcastManager getBroadcastManager() { 85 return mBroadcastManager; 86 } 87 88 @Override getAddress()89 public String getAddress() { 90 return mAddress; 91 } 92 getServiceScheduler()93 Scheduler getServiceScheduler() { 94 return mServiceScheduler; 95 } 96 getUiScheduler()97 Scheduler getUiScheduler() { 98 return mUiScheduler; 99 } 100 101 /** 102 * Update distance to remote device. 103 * 104 * @return true if distance updated. 105 */ updateDistance(String remoteAddress, Distance distance)106 /*package*/ boolean updateDistance(String remoteAddress, Distance distance) { 107 Distance currentDistance = mDistanceMap.get(remoteAddress); 108 if (currentDistance == null || !distance.equals(currentDistance)) { 109 mDistanceMap.put(remoteAddress, distance); 110 return true; 111 } 112 return false; 113 } 114 onDistanceChange(DeviceletImpl remote, Distance distance)115 /*package*/ void onDistanceChange(DeviceletImpl remote, Distance distance) { 116 if (distance == Distance.NEAR) { 117 mNfclet.onNear(remote.mNfclet); 118 } 119 } 120 121 } 122