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
17package com.android.settings.anomaly.tester.utils;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.le.BluetoothLeScanner;
21import android.bluetooth.le.ScanCallback;
22import android.bluetooth.le.ScanResult;
23import android.bluetooth.le.ScanSettings;
24import android.content.Context;
25import android.os.PowerManager;
26import android.util.Log;
27
28import java.util.List;
29
30/**
31 * Actions to generate anomaly.
32 */
33public class AnomalyActions {
34    private static final String TAG = AnomalyActions.class.getSimpleName();
35
36    public static final String KEY_ACTION = "action";
37    public static final String KEY_DURATION_MS = "duration_ms";
38    public static final String KEY_RESULT_RECEIVER = "result_receiver";
39
40    public static final String ACTION_BLE_SCAN_UNOPTIMIZED = "action.ble_scan_unoptimized";
41    public static final String ACTION_WAKE_LOCK = "action.wake_lock";
42
43    public static void doAction(Context ctx, String actionCode, long durationMs) {
44        if (actionCode == null) {
45            Log.e(TAG, "Intent was missing action.");
46            return;
47        }
48        switch (actionCode) {
49            case ACTION_BLE_SCAN_UNOPTIMIZED:
50                doUnoptimizedBleScan(ctx, durationMs);
51                break;
52            case ACTION_WAKE_LOCK:
53                doHoldWakelock(ctx, durationMs);
54            default:
55                Log.e(TAG, "Intent had invalid action");
56        }
57    }
58
59    private static void doUnoptimizedBleScan(Context ctx, long durationMs) {
60        ScanSettings scanSettings = new ScanSettings.Builder()
61                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
62
63        // perform ble scanning
64        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
65        if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled() ) {
66            Log.e(TAG, "Device does not support Bluetooth or Bluetooth not enabled");
67            return;
68        }
69        BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
70        if (bleScanner == null) {
71            Log.e(TAG, "Cannot access BLE scanner");
72            return;
73        }
74
75        ScanCallback scanCallback = new ScanCallback() {
76            @Override
77            public void onScanResult(int callbackType, ScanResult result) {
78                Log.v(TAG, "called onScanResult");
79            }
80
81            @Override
82            public void onScanFailed(int errorCode) {
83                Log.v(TAG, "called onScanFailed");
84            }
85
86            @Override
87            public void onBatchScanResults(List<ScanResult> results) {
88                Log.v(TAG, "called onBatchScanResults");
89            }
90        };
91
92        bleScanner.startScan(null, scanSettings, scanCallback);
93        try {
94            Thread.sleep(durationMs);
95        } catch (InterruptedException e) {
96            Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
97        }
98        bleScanner.stopScan(scanCallback);
99    }
100
101    private static void doHoldWakelock(Context ctx, long durationMs) {
102        PowerManager powerManager = ctx.getSystemService(PowerManager.class);
103        PowerManager.WakeLock wl = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
104                "AnomalyWakeLock");
105        wl.acquire();
106        try {
107            Thread.sleep(durationMs);
108        } catch (InterruptedException e) {
109            Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
110        }
111        wl.release();
112    }
113}
114