-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpermissions_screen.dart
357 lines (331 loc) · 12.1 KB
/
permissions_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:async';
import 'package:provider/provider.dart';
import 'package:square_mobile_payments_sdk/square_mobile_payments_sdk.dart';
import 'package:square_mobile_payments_sdk_example/auth_state.dart';
class PermissionsScreen extends StatefulWidget {
const PermissionsScreen({super.key});
@override
State<PermissionsScreen> createState() => _PermissionsScreenState();
}
enum SignInState {
idle,
loading,
error,
}
class _PermissionsScreenState extends State<PermissionsScreen> {
bool isBluetoothGranted = false;
bool isLocationGranted = false;
bool isMicrophoneGranted = false;
bool isReadStateGranted = false;
final _squareMobilePaymentsSdkPlugin = SquareMobilePaymentsSdk();
SignInState _signInState = SignInState.idle;
Future<void> _checkInitialPermissions() async {
final bluetoothConnectStatus = await Permission.bluetoothConnect.status;
final bluetoothScanStatus = await Permission.bluetoothScan.status;
final locationStatus = await Permission.location.status;
final microphoneStatus = await Permission.microphone.status;
if (Platform.isAndroid) {
final readStateStatus = await Permission.phone.status;
setState(() {
isReadStateGranted = readStateStatus.isGranted;
});
}
setState(() {
isBluetoothGranted =
bluetoothConnectStatus.isGranted && bluetoothScanStatus.isGranted;
isLocationGranted = locationStatus.isGranted;
isMicrophoneGranted = microphoneStatus.isGranted;
});
}
// Method to request Bluetooth permission
Future<void> _requestBluetoothPermissions() async {
final statuses = await [
Permission.bluetoothConnect,
Permission.bluetoothScan,
].request(); // Request both permissions at once
setState(() {
// Consider Bluetooth granted only if BOTH permissions are granted
isBluetoothGranted = statuses[Permission.bluetoothConnect]!.isGranted &&
statuses[Permission.bluetoothScan]!.isGranted;
});
}
// Method to request Location permission
Future<void> _requestLocationPermission() async {
final status = await Permission.location.request();
setState(() {
isLocationGranted = status.isGranted;
});
}
// Method to request Microphone permission
Future<void> _requestMicrophonePermission() async {
final status = await Permission.microphone.request();
setState(() {
isMicrophoneGranted = status.isGranted;
});
}
// Method to request Read State permission
Future<void> _requestReadStatePermission() async {
final status = await Permission.phone.request();
setState(() {
isReadStateGranted = status.isGranted;
});
}
Future<void> authorizeSDK() async {
String accessToken = "YOUR_ACCESS_TOKEN";
String locationId = "YOUR_LOCATION_ID";
String response;
setState(() {
_signInState = SignInState.loading;
});
await Future.delayed(const Duration(seconds: 1));
try {
await _squareMobilePaymentsSdkPlugin.authManager
.authorize(accessToken, locationId);
if (!mounted) return;
Provider.of<AuthState>(context, listen: false).authorize();
} on AuthorizeError catch (e) {
setState(() {
_signInState = SignInState.error;
});
if (!mounted) return;
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text("${e.code}, ->>> ${e.message}"),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text("OK"),
),
],
);
},
);
}
}
Future<void> hideReader() async {
try {
await _squareMobilePaymentsSdkPlugin.readerManager.hideMockReaderUI();
} on Exception {
print("Exception in hide reader");
}
}
Future<void> deauthorizeSDK() async {
String response;
try {
response =
await _squareMobilePaymentsSdkPlugin.authManager.deauthorize() ??
'Unknown response';
} on Exception {
response = 'Failed';
}
if (!mounted) return;
if (response == 'Deauthorized') {
Provider.of<AuthState>(context, listen: false).signOut();
hideReader();
setState(() {
_signInState = SignInState.idle;
});
}
}
_onSignIn() {
authorizeSDK();
}
_signOut() {
deauthorizeSDK();
}
@override
void initState() {
super.initState();
_checkInitialPermissions();
}
@override
Widget build(BuildContext context) {
bool areAllPermissionsGranted = (isBluetoothGranted || Platform.isIOS) &&
isLocationGranted &&
isMicrophoneGranted &&
(isReadStateGranted || Platform.isIOS);
final isAuthorized = Provider.of<AuthState>(context).isAuthorized;
Widget _buildPermissionItem({
required String title,
required String description,
required bool isGranted,
required VoidCallback onRequestPermission,
}) {
return Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style:
const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 5),
Text(
description,
style: const TextStyle(fontSize: 14, color: Colors.grey),
),
],
)),
Checkbox(
value: isGranted,
onChanged: (_) => onRequestPermission(),
),
],
);
}
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
leading: IconButton(
icon: const Icon(Icons.close, color: Colors.black),
style: TextButton.styleFrom(
backgroundColor: Colors.grey.shade200,
padding: const EdgeInsets.all(8.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
),
onPressed: () {
Navigator.pop(context);
},
),
title: const Text(
'Permissions',
style: TextStyle(
color: Colors.black, fontSize: 20, fontWeight: FontWeight.bold),
),
centerTitle: true,
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 40),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildPermissionItem(
title: 'Bluetooth',
description:
'Square uses Bluetooth to connect and communicate with Square devices. You should ask for this permission if you are using readers that connect via Bluetooth.',
isGranted: isBluetoothGranted,
onRequestPermission: _requestBluetoothPermissions,
),
_buildPermissionItem(
title: 'Location',
description:
'Square uses location to know where transactions take place. This reduces risk and minimizes payment disputes.',
isGranted: isLocationGranted,
onRequestPermission: _requestLocationPermission,
),
_buildPermissionItem(
title: 'Microphone',
description:
'Square\'s R4 reader uses the microphone jack to communicate payment card data to your device. You should ask for this permission if you are using an R4 reader.',
isGranted: isMicrophoneGranted,
onRequestPermission: _requestMicrophonePermission,
),
if (!Platform.isIOS) const SizedBox(height: 20),
if (!Platform.isIOS)
_buildPermissionItem(
title: 'Read Phone State',
description:
'Square needs phone access in order to uniquely identify the devices associated with your account and ensure that unauthorized devices are not able to act on your behalf.',
isGranted: isReadStateGranted,
onRequestPermission: _requestReadStatePermission,
),
const SizedBox(height: 40),
SizedBox(
width: double.infinity,
height: 60,
child: isAuthorized
? ElevatedButton(
onPressed: _signOut,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey.shade200,
padding: const EdgeInsets.symmetric(vertical: 15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
),
child: const Text(
'Sign Out',
style: TextStyle(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.bold),
),
)
: ElevatedButton(
onPressed: areAllPermissionsGranted ? _onSignIn : null,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple.shade200,
padding: const EdgeInsets.symmetric(vertical: 15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
),
child: _signInState == SignInState.loading
? const SizedBox.square(
dimension: 20,
child: CircularProgressIndicator(
color: Colors.black87,
strokeWidth: 2,
))
: const Text(
'Sign In',
style: TextStyle(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.bold),
),
),
),
const SizedBox(height: 10),
isAuthorized
? const Text(
'This device is authorized.',
style: TextStyle(
color: Colors.green,
fontSize: 16,
fontWeight: FontWeight.bold),
)
: _signInState == SignInState.loading
? const Text(
'Authorizing',
style: TextStyle(
color: Colors.grey,
fontSize: 16,
),
)
: _signInState == SignInState.error
? const Text(
'Authorization failed.',
style: TextStyle(
color: Colors.red,
fontSize: 16,
fontWeight: FontWeight.bold),
)
: const Text(
'Device not authorized.',
style: TextStyle(
color: Color.fromARGB(255, 187, 122, 24),
fontSize: 16,
fontWeight: FontWeight.bold),
),
],
),
),
),
);
}
}