2 * Copyright (C) 2017 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
15 package com.android.settings.anomaly.tester;
17 import android.app.Activity;
18 import android.content.Intent;
19 import android.os.Bundle;
20 import android.os.Handler;
21 import android.os.ResultReceiver;
22 import android.provider.Settings;
23 import android.util.Log;
24 import android.view.View;
25 import android.widget.Button;
26 import android.widget.EditText;
27 import android.widget.Toast;
29 import com.android.settings.anomaly.tester.service.AnomalyService;
30 import com.android.settings.anomaly.tester.utils.AnomalyActions;
31 import com.android.settings.anomaly.tester.utils.AnomalyPolicyBuilder;
34 * Main activity to control and start anomaly
36 public class AnomalyActivity extends Activity {
37 private static final String TAG = AnomalyActivity.class.getSimpleName();
39 public static final String KEY_TARGET_BUTTON = "target_button";
41 private AnomalyResultReceiver mResultReceiver;
44 protected void onCreate(Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
46 setContentView(R.layout.activity_main);
47 mResultReceiver = new AnomalyResultReceiver(new Handler());
50 public void startBluetoothAnomaly(View view) {
52 // Enable anomaly detection and change the threshold
53 final String config = new AnomalyPolicyBuilder()
54 .addPolicy(AnomalyPolicyBuilder.KEY_ANOMALY_DETECTION_ENABLED, true)
55 .addPolicy(AnomalyPolicyBuilder.KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, true)
56 .addPolicy(AnomalyPolicyBuilder.KEY_BLUETOOTH_SCAN_THRESHOLD,
57 getValueFromEditText(R.id.bluetooth_threshold))
59 Settings.Global.putString(getContentResolver(),
60 Settings.Global.ANOMALY_DETECTION_CONSTANTS, config);
62 // Start the anomaly service
63 Intent intent = new Intent(this, AnomalyService.class);
64 intent.putExtra(AnomalyActions.KEY_ACTION, AnomalyActions.ACTION_BLE_SCAN_UNOPTIMIZED);
65 intent.putExtra(AnomalyActions.KEY_DURATION_MS,
66 getValueFromEditText(R.id.bluetooth_run_time));
67 intent.putExtra(AnomalyActions.KEY_RESULT_RECEIVER, mResultReceiver);
68 intent.putExtra(KEY_TARGET_BUTTON, view.getId());
71 view.setEnabled(false);
72 } catch (NumberFormatException e) {
73 Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
77 private long getValueFromEditText(final int id) throws NumberFormatException {
78 final EditText editText = findViewById(id);
79 if (editText != null) {
80 final long value = Long.parseLong(editText.getText().toString());
86 throw new NumberFormatException("Number should be positive");
89 private class AnomalyResultReceiver extends ResultReceiver {
91 public AnomalyResultReceiver(Handler handler) {
96 protected void onReceiveResult(int resultCode, Bundle resultData) {
97 super.onReceiveResult(resultCode, resultData);
99 final Button button = findViewById(resultData.getInt(KEY_TARGET_BUTTON));
100 if (button != null) {
101 button.setEnabled(true);