OSDN Git Service

Add bt anomaly action in testing app
[android-x86/packages-apps-Settings.git] / tests / anomaly-tester / src / com / android / settings / anomaly / tester / utils / AnomalyActions.java
1 /*
2  * Copyright (C) 2017 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.settings.anomaly.tester.utils;
18
19 import android.bluetooth.BluetoothAdapter;
20 import android.bluetooth.le.BluetoothLeScanner;
21 import android.bluetooth.le.ScanCallback;
22 import android.bluetooth.le.ScanResult;
23 import android.bluetooth.le.ScanSettings;
24 import android.content.Context;
25 import android.util.Log;
26
27 import java.util.List;
28
29 /**
30  * Actions to generate anomaly.
31  */
32 public class AnomalyActions {
33     private static final String TAG = AnomalyActions.class.getSimpleName();
34
35     public static final String KEY_ACTION = "action";
36     public static final String KEY_DURATION_MS = "duration_ms";
37     public static final String KEY_RESULT_RECEIVER = "result_receiver";
38
39     public static final String ACTION_BLE_SCAN_UNOPTIMIZED = "action.ble_scan_unoptimized";
40
41     public static void doAction(Context ctx, String actionCode, long durationMs) {
42         if (actionCode == null) {
43             Log.e(TAG, "Intent was missing action.");
44             return;
45         }
46         switch (actionCode) {
47             case ACTION_BLE_SCAN_UNOPTIMIZED:
48                 doUnoptimizedBleScan(ctx, durationMs);
49                 break;
50             default:
51                 Log.e(TAG, "Intent had invalid action");
52         }
53     }
54
55     private static void doUnoptimizedBleScan(Context ctx, long durationMs) {
56         ScanSettings scanSettings = new ScanSettings.Builder()
57                 .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
58
59         // perform ble scanning
60         BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
61         if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled() ) {
62             Log.e(TAG, "Device does not support Bluetooth or Bluetooth not enabled");
63             return;
64         }
65         BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
66         if (bleScanner == null) {
67             Log.e(TAG, "Cannot access BLE scanner");
68             return;
69         }
70
71         ScanCallback scanCallback = new ScanCallback() {
72             @Override
73             public void onScanResult(int callbackType, ScanResult result) {
74                 Log.v(TAG, "called onScanResult");
75             }
76
77             @Override
78             public void onScanFailed(int errorCode) {
79                 Log.v(TAG, "called onScanFailed");
80             }
81
82             @Override
83             public void onBatchScanResults(List<ScanResult> results) {
84                 Log.v(TAG, "called onBatchScanResults");
85             }
86         };
87
88         bleScanner.startScan(null, scanSettings, scanCallback);
89         try {
90             Thread.sleep(durationMs);
91         } catch (InterruptedException e) {
92             Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
93         }
94         bleScanner.stopScan(scanCallback);
95     }
96 }