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 / AnomalyActivity.java
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
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
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
13  */
14
15 package com.android.settings.anomaly.tester;
16
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;
28
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;
32
33 /**
34  * Main activity to control and start anomaly
35  */
36 public class AnomalyActivity extends Activity {
37     private static final String TAG = AnomalyActivity.class.getSimpleName();
38
39     public static final String KEY_TARGET_BUTTON = "target_button";
40
41     private AnomalyResultReceiver mResultReceiver;
42
43     @Override
44     protected void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         setContentView(R.layout.activity_main);
47         mResultReceiver = new AnomalyResultReceiver(new Handler());
48     }
49
50     public void startBluetoothAnomaly(View view) {
51         try {
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))
58                     .build();
59             Settings.Global.putString(getContentResolver(),
60                     Settings.Global.ANOMALY_DETECTION_CONSTANTS, config);
61
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());
69             startService(intent);
70
71             view.setEnabled(false);
72         } catch (NumberFormatException e) {
73             Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
74         }
75     }
76
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());
81             if (value > 0) {
82                 return value;
83             }
84         }
85
86         throw new NumberFormatException("Number should be positive");
87     }
88
89     private class AnomalyResultReceiver extends ResultReceiver {
90
91         public AnomalyResultReceiver(Handler handler) {
92             super(handler);
93         }
94
95         @Override
96         protected void onReceiveResult(int resultCode, Bundle resultData) {
97             super.onReceiveResult(resultCode, resultData);
98
99             final Button button = findViewById(resultData.getInt(KEY_TARGET_BUTTON));
100             if (button != null) {
101                 button.setEnabled(true);
102             }
103
104         }
105     }
106 }