Skip to content

Commit cb33069

Browse files
committed
Require permissions to avoid lint errors
bug: v/81613 some permissions not checked before use Signed-off-by: Yuheng Li <liyuheng@xiaomi.com>
1 parent 88b0709 commit cb33069

File tree

14 files changed

+102
-2
lines changed

14 files changed

+102
-2
lines changed

tools/test_suite/android/app/src/main/java/com/openvela/bluetoothtest/LocalAdapter/OnOffActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.openvela.bluetoothtest.LocalAdapter;
1818

19+
import android.Manifest;
1920
import android.bluetooth.BluetoothAdapter;
2021
import android.bluetooth.BluetoothManager;
2122
import android.content.Intent;
@@ -26,6 +27,7 @@
2627
import android.widget.Button;
2728
import android.widget.EditText;
2829
import androidx.annotation.Nullable;
30+
import androidx.annotation.RequiresPermission;
2931
import androidx.appcompat.app.AppCompatActivity;
3032
import com.openvela.bluetooth.BluetoothStateObserver;
3133
import com.openvela.bluetooth.callback.BluetoothStateCallback;
@@ -59,6 +61,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5961

6062
Button buttonEnable = findViewById(R.id.button_enable_bluetooth);
6163
buttonEnable.setOnClickListener(new View.OnClickListener() {
64+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
6265
@Override
6366
public void onClick(View v) {
6467
String str = textNumOfCycles.getText().toString();
@@ -84,6 +87,7 @@ public void onClick(View v) {
8487

8588
Log.d(TAG, "onClick: Disable Bluetooth, timesOfCycles = " + timesOfCycles);
8689
new AsyncTask<Void, Void, Void>() {
90+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
8791
@Override
8892
protected Void doInBackground(Void... params) {
8993
// Time consuming operation
@@ -109,6 +113,7 @@ protected void onDestroy() {
109113
private void listenBluetoothState() {
110114
btStateObserver = new BluetoothStateObserver(this);
111115
btStateObserver.registerReceiver(new BluetoothStateCallback() {
116+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
112117
@Override
113118
public void onEnabled() {
114119
String str = textResultDisplay.getText().toString();
@@ -121,6 +126,7 @@ public void onEnabled() {
121126
disableBluetooth();
122127
}
123128

129+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
124130
@Override
125131
public void onDisabled() {
126132
String str = textResultDisplay.getText().toString();
@@ -142,10 +148,12 @@ private boolean isBluetoothEnabled() {
142148
return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
143149
}
144150

151+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
145152
private void enableBluetooth() {
146153
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), REQUEST_ENABLE_BT);
147154
}
148155

156+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
149157
private void disableBluetooth() {
150158
bluetoothAdapter.disable();
151159
}

tools/test_suite/android/app/src/main/java/com/openvela/bluetoothtest/ble/BleCentralActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package com.openvela.bluetoothtest.ble;
1818

19+
import android.Manifest;
1920
import android.os.Bundle;
2021
import android.widget.Button;
2122
import android.widget.TextView;
2223

2324
import android.bluetooth.BluetoothProfile;
2425

26+
import androidx.annotation.RequiresPermission;
2527
import androidx.appcompat.app.AppCompatActivity;
2628
import androidx.recyclerview.widget.DividerItemDecoration;
2729
import androidx.recyclerview.widget.LinearLayoutManager;
@@ -42,6 +44,7 @@ public class BleCentralActivity extends AppCompatActivity {
4244
private @NotNull BtDevice currentDevice;
4345
private GattClientAdapter gattClientAdapter;
4446

47+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
4548
@Override
4649
protected void onCreate(@Nullable Bundle savedInstanceState) {
4750
super.onCreate(savedInstanceState);
@@ -54,6 +57,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5457
gattClientAdapter.connect(currentDevice, connectCallback);
5558
}
5659

60+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
5761
@Override
5862
protected void onDestroy() {
5963
super.onDestroy();
@@ -64,6 +68,7 @@ protected void onDestroy() {
6468
}
6569
}
6670

71+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
6772
private void initView() {
6873
tvConnectState = findViewById(R.id.tv_connect_state);
6974
btnConnect = findViewById(R.id.btn_connect);

tools/test_suite/android/app/src/main/java/com/openvela/bluetoothtest/ble/BleL2capActivity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.openvela.bluetoothtest.ble;
1818

19+
import android.Manifest;
1920
import android.os.Bundle;
2021
import android.os.Handler;
2122
import android.os.Looper;
@@ -26,6 +27,7 @@
2627
import android.widget.EditText;
2728

2829
import androidx.annotation.Nullable;
30+
import androidx.annotation.RequiresPermission;
2931
import androidx.appcompat.app.AppCompatActivity;
3032

3133
import com.openvela.bluetooth.BtSock;
@@ -61,6 +63,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6163
// Register Server #1
6264
Button buttonRegister_1 = findViewById(R.id.button_spp_server_register_1);
6365
buttonRegister_1.setOnClickListener(new View.OnClickListener() {
66+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
6467
@Override
6568
public void onClick(View v) {
6669
String uuid = textServiceUUID_1.getText().toString();
@@ -73,6 +76,7 @@ public void onClick(View v) {
7376
// Unregister Server #1
7477
Button buttonUnregister_1 = findViewById(R.id.button_spp_server_unregister_1);
7578
buttonUnregister_1.setOnClickListener(new View.OnClickListener() {
79+
@RequiresPermission(Manifest.permission.BLUETOOTH_ADVERTISE)
7680
@Override
7781
public void onClick(View v) {
7882
Log.d(TAG, "onClick: Unregister Server#1");
@@ -86,6 +90,7 @@ public void onClick(View v) {
8690
// Register Server #2
8791
Button buttonRegister_2 = findViewById(R.id.button_spp_server_register_2);
8892
buttonRegister_2.setOnClickListener(new View.OnClickListener() {
93+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
8994
@Override
9095
public void onClick(View v) {
9196
String uuid = textServiceUUID_2.getText().toString();
@@ -98,6 +103,7 @@ public void onClick(View v) {
98103
// Unregister Server #2
99104
Button buttonUnregister_2 = findViewById(R.id.button_spp_server_unregister_2);
100105
buttonUnregister_2.setOnClickListener(new View.OnClickListener() {
106+
@RequiresPermission(Manifest.permission.BLUETOOTH_ADVERTISE)
101107
@Override
102108
public void onClick(View v) {
103109
Log.d(TAG, "onClick: Unregister Server#2");

tools/test_suite/android/app/src/main/java/com/openvela/bluetoothtest/ble/BlePeripheralActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.openvela.bluetoothtest.ble;
1818

19+
import android.Manifest;
20+
import android.content.pm.PackageManager;
1921
import android.os.Bundle;
2022
import android.util.Log;
2123
import android.widget.EditText;
@@ -29,6 +31,7 @@
2931
import android.bluetooth.le.BluetoothLeAdvertiser;
3032

3133
import androidx.appcompat.app.AppCompatActivity;
34+
import androidx.core.app.ActivityCompat;
3235

3336
import com.openvela.bluetoothtest.R;
3437

@@ -96,6 +99,16 @@ private void startAdvertising(final byte[] payload) {
9699
public void stopAdvertising() {
97100
Log.d(TAG, "stopAdvertising");
98101

102+
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADVERTISE) != PackageManager.PERMISSION_GRANTED) {
103+
// TODO: Consider calling
104+
// ActivityCompat#requestPermissions
105+
// here to request the missing permissions, and then overriding
106+
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
107+
// int[] grantResults)
108+
// to handle the case where the user grants the permission. See the documentation
109+
// for ActivityCompat#requestPermissions for more details.
110+
return;
111+
}
99112
bluetoothAdvertiser.stopAdvertising(advertiseCallback);
100113
bluetoothAdvertiser = null;
101114
}

tools/test_suite/android/app/src/main/java/com/openvela/bluetoothtest/ble/BleScanActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package com.openvela.bluetoothtest.ble;
1818

19+
import android.Manifest;
1920
import android.os.Bundle;
2021
import android.util.Log;
2122
import android.widget.EditText;
2223
import android.widget.TextView;
2324
import android.widget.Button;
2425

2526
import androidx.annotation.Nullable;
27+
import androidx.annotation.RequiresPermission;
2628
import androidx.appcompat.app.AppCompatActivity;
2729
import androidx.recyclerview.widget.DividerItemDecoration;
2830
import androidx.recyclerview.widget.LinearLayoutManager;
@@ -40,6 +42,7 @@ public class BleScanActivity extends AppCompatActivity {
4042
private EditText etFilter;
4143
private BleScanAdapter bleScanAdapter;
4244

45+
@RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
4346
@Override
4447
protected void onCreate(@Nullable Bundle savedInstanceState) {
4548
super.onCreate(savedInstanceState);
@@ -48,6 +51,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4851
initView();
4952
}
5053

54+
@RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
5155
@Override
5256
protected void onDestroy() {
5357
super.onDestroy();
@@ -56,6 +60,7 @@ protected void onDestroy() {
5660
}
5761
}
5862

63+
@RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
5964
private void initView() {
6065
tvScanState = findViewById(R.id.tv_scan_state);
6166
btnScan = findViewById(R.id.btn_scan);

tools/test_suite/android/app/src/main/java/com/openvela/bluetoothtest/ble/BleScanAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import android.Manifest;
2223
import android.annotation.SuppressLint;
2324
import android.content.Context;
2425
import android.content.Intent;
@@ -35,6 +36,7 @@
3536
import android.bluetooth.le.ScanResult;
3637
import android.bluetooth.le.ScanSettings;
3738

39+
import androidx.annotation.RequiresPermission;
3840
import androidx.core.os.HandlerCompat;
3941

4042
import com.openvela.bluetooth.adapter.RecyclerAdapter;
@@ -59,6 +61,7 @@ public BleScanAdapter(Context context) {
5961
super(context, R.layout.item_scan_result, new ArrayList<>());
6062
}
6163

64+
@RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
6265
@SuppressLint("DefaultLocale")
6366
@Override
6467
public void onBindViewHolderItem(RecyclerViewHolder viewHolder, BtDevice device) {
@@ -139,6 +142,7 @@ public boolean isScanning() {
139142
return (bluetoothScanner != null);
140143
}
141144

145+
@RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
142146
@SuppressLint("NotifyDataSetChanged")
143147
public void startScan(final String[] scanFilters, long scanPeriod, BluetoothDiscoveryCallback<BtDevice> callback) {
144148
bleDiscoveryCallback = callback;
@@ -175,6 +179,7 @@ public void startScan(final String[] scanFilters, long scanPeriod, BluetoothDisc
175179
}
176180
}
177181

182+
@RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
178183
public void stopScan() {
179184
if (!isScanning()) {
180185
return;
@@ -190,6 +195,7 @@ private void cancelScanTimer() {
190195
handler.removeCallbacksAndMessages(SCAN_TIMEOUT_TOKEN);
191196
}
192197

198+
@RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
193199
private void startScanTimer(long scanPeriod) {
194200
cancelScanTimer();
195201

@@ -203,6 +209,7 @@ private void startScanTimer(long scanPeriod) {
203209
}
204210

205211
private final ScanCallback scanCallback = new ScanCallback() {
212+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
206213
@SuppressLint("NotifyDataSetChanged")
207214
@Override
208215
public void onScanResult(final int callbackType, final ScanResult result) {
@@ -252,6 +259,7 @@ public void onScanResult(final int callbackType, final ScanResult result) {
252259
}
253260
}
254261

262+
@RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
255263
@Override
256264
public void onScanFailed(final int errorCode) {
257265
stopScan();

tools/test_suite/android/app/src/main/java/com/openvela/bluetoothtest/ble/GattClientAdapter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.HashMap;
2121
import java.util.Map;
22+
23+
import android.Manifest;
2224
import android.view.View;
2325

2426
import android.annotation.SuppressLint;
@@ -34,6 +36,7 @@
3436
import android.bluetooth.BluetoothGattService;
3537
import android.bluetooth.BluetoothProfile;
3638

39+
import androidx.annotation.RequiresPermission;
3740
import androidx.core.os.HandlerCompat;
3841
import androidx.recyclerview.widget.LinearLayoutManager;
3942
import androidx.recyclerview.widget.RecyclerView;
@@ -102,6 +105,7 @@ public void onBindViewHolderItem(RecyclerViewHolder viewHolder, BluetoothGattSer
102105
});
103106
}
104107

108+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
105109
public void connect(BtDevice device, BleConnectCallback<BtDevice> callback) {
106110
bleConnectCallback = callback;
107111

@@ -121,6 +125,7 @@ public void connect(BtDevice device, BleConnectCallback<BtDevice> callback) {
121125
}
122126
}
123127

128+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
124129
public void disconnect(BtDevice device) {
125130
String address = device.getAddress();
126131
BluetoothGatt bluetoothGatt = gattHashMap.get(address);
@@ -132,6 +137,7 @@ public void disconnect(BtDevice device) {
132137
}
133138
}
134139

140+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
135141
public void cancelConnect(BtDevice device) {
136142
String address = device.getAddress();
137143
BluetoothGatt bluetoothGatt = gattHashMap.get(address);
@@ -147,6 +153,7 @@ private void cancelConnectTimer(String address){
147153
handler.removeCallbacksAndMessages(address);
148154
}
149155

156+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
150157
private void startConnectTimer(String address) {
151158
cancelConnectTimer(address);
152159

@@ -156,6 +163,7 @@ private void startConnectTimer(String address) {
156163
}, address, GATT_CONNECT_TIMEOUT_MS);
157164
}
158165

166+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
159167
private void close(String address) {
160168
BluetoothGatt bluetoothGatt = gattHashMap.get(address);
161169
if (bluetoothGatt != null) {
@@ -165,6 +173,7 @@ private void close(String address) {
165173
}
166174

167175
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
176+
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
168177
@Override
169178
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
170179
BluetoothDevice device = gatt.getDevice();

0 commit comments

Comments
 (0)