16
16
17
17
package com .afwsamples .testdpc .policy .wifimanagement ;
18
18
19
- import static android .net .wifi .WifiEnterpriseConfig .Eap ;
20
-
19
+ import android .Manifest ;
21
20
import android .Manifest .permission ;
21
+ import android .app .Activity ;
22
22
import android .app .AlertDialog ;
23
23
import android .app .DialogFragment ;
24
24
import android .app .Fragment ;
25
25
import android .content .Context ;
26
+ import android .content .pm .PackageManager ;
26
27
import android .net .wifi .WifiConfiguration ;
27
28
import android .net .wifi .WifiManager ;
28
29
import android .os .Build .VERSION_CODES ;
35
36
import android .widget .CheckedTextView ;
36
37
import android .widget .ListView ;
37
38
import android .widget .TextView ;
39
+ import android .widget .Toast ;
40
+
41
+ import androidx .annotation .NonNull ;
38
42
import androidx .annotation .RequiresApi ;
39
- import com .afwsamples .testdpc .DeviceAdminReceiver ;
43
+ import androidx .core .content .ContextCompat ;
44
+
40
45
import com .afwsamples .testdpc .R ;
41
- import com .afwsamples .testdpc .common .PermissionsHelper ;
42
46
import com .afwsamples .testdpc .common .Util ;
47
+
43
48
import java .util .ArrayList ;
44
49
import java .util .List ;
45
50
51
+ import static android .net .wifi .WifiEnterpriseConfig .Eap ;
52
+
46
53
/**
47
54
* Fragment for WiFi configuration editing.
48
55
*/
@@ -115,6 +122,17 @@ public void onResume() {
115
122
updateConfigsList ();
116
123
}
117
124
125
+ @ Override
126
+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions ,
127
+ @ NonNull int [] grantResults ) {
128
+ if (grantResults [0 ] != PackageManager .PERMISSION_GRANTED ) {
129
+ showToast ("Location permission denied, network list unavailable." );
130
+ } else {
131
+ updateConfigsList ();
132
+ }
133
+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
134
+ }
135
+
118
136
@ RequiresApi (api = VERSION_CODES .M )
119
137
@ Override
120
138
public View onCreateView (LayoutInflater inflater , final ViewGroup container ,
@@ -132,14 +150,13 @@ public View onCreateView(LayoutInflater inflater, final ViewGroup container,
132
150
mConfigsList .setAdapter (mConfigsAdapter );
133
151
mConfigsList .setChoiceMode (ListView .CHOICE_MODE_SINGLE );
134
152
135
- if (Util .SDK_INT >= VERSION_CODES .M ) {
136
- PermissionsHelper
137
- .ensureRequiredPermissions (new String []{permission .ACCESS_FINE_LOCATION },
138
- DeviceAdminReceiver .getComponentName (this .getActivity ()), this .getContext ());
153
+ if (shouldRequestLocationPermission (getContext ())) {
154
+ requestPermissions (new String []{Manifest .permission .ACCESS_FINE_LOCATION },
155
+ /* requestCode= */ 0 );
156
+ } else {
157
+ updateConfigsList ();
139
158
}
140
159
141
- updateConfigsList ();
142
-
143
160
Button updateConfigButton = (Button ) view .findViewById (R .id .updateSelectedConfig );
144
161
if (updateConfigButton != null ) {
145
162
updateConfigButton .setOnClickListener (new View .OnClickListener () {
@@ -200,10 +217,26 @@ public void onDismiss() {
200
217
public void onCancel () {
201
218
}
202
219
220
+ public static boolean shouldRequestLocationPermission (Context context ) {
221
+ if (Util .SDK_INT < VERSION_CODES .M ) {
222
+ return false ;
223
+ }
224
+
225
+ return ContextCompat .checkSelfPermission (context , permission .ACCESS_FINE_LOCATION )
226
+ != PackageManager .PERMISSION_GRANTED ;
227
+ }
228
+
203
229
private void showError (final String message ) {
204
230
new AlertDialog .Builder (getActivity ()).setTitle (R .string .wifi_modification_error_title )
205
231
.setIconAttribute (android .R .attr .alertDialogIcon )
206
232
.setMessage (message ).setPositiveButton (android .R .string .ok , null ).show ();
207
233
}
208
234
235
+ private void showToast (String msg ) {
236
+ Activity activity = getActivity ();
237
+ if (activity == null || activity .isFinishing ()) {
238
+ return ;
239
+ }
240
+ Toast .makeText (activity , msg , Toast .LENGTH_SHORT ).show ();
241
+ }
209
242
}
0 commit comments