Skip to content

Commit bd422f6

Browse files
committed
Request location permission for WiFi network listing
Request the ACCESS_FINE_LOCATION permission prior to calling WifiManager.getConfiguredNetworks. TestDPC can no longer grant itself this permission. Also, fix a crash in DevicePolicyManagerGatewayImpl when TestDPC is not a DPC. Bug: 185463607 Bug: 182894941 Test: Manual, create a work profile and start "Modify Wi-Fi configuration" fragment. Change-Id: Iaf40e344e55a667c75b4d52ffc19e8664df7b572
1 parent f537981 commit bd422f6

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

app/src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGatewayImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.os.UserManager;
3232
import android.util.Log;
3333
import androidx.annotation.NonNull;
34+
import androidx.annotation.Nullable;
3435

3536
import com.afwsamples.testdpc.common.ReflectionUtil;
3637
import com.afwsamples.testdpc.common.ReflectionUtil.ReflectionIsTemporaryException;
@@ -92,11 +93,18 @@ public DevicePolicyManager getDevicePolicyManager() {
9293

9394
@Override
9495
public boolean isProfileOwnerApp() {
96+
if (mAdminComponentName == null) {
97+
return false;
98+
}
9599
return mDevicePolicyManager.isProfileOwnerApp(mAdminComponentName.getPackageName());
96100
}
97101

98102
@Override
99103
public boolean isDeviceOwnerApp() {
104+
if (mAdminComponentName == null) {
105+
return false;
106+
}
107+
100108
return mDevicePolicyManager.isDeviceOwnerApp(mAdminComponentName.getPackageName());
101109
}
102110

app/src/main/java/com/afwsamples/testdpc/policy/wifimanagement/WifiModificationFragment.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
package com.afwsamples.testdpc.policy.wifimanagement;
1818

19-
import static android.net.wifi.WifiEnterpriseConfig.Eap;
20-
19+
import android.Manifest;
2120
import android.Manifest.permission;
21+
import android.app.Activity;
2222
import android.app.AlertDialog;
2323
import android.app.DialogFragment;
2424
import android.app.Fragment;
2525
import android.content.Context;
26+
import android.content.pm.PackageManager;
2627
import android.net.wifi.WifiConfiguration;
2728
import android.net.wifi.WifiManager;
2829
import android.os.Build.VERSION_CODES;
@@ -35,14 +36,20 @@
3536
import android.widget.CheckedTextView;
3637
import android.widget.ListView;
3738
import android.widget.TextView;
39+
import android.widget.Toast;
40+
41+
import androidx.annotation.NonNull;
3842
import androidx.annotation.RequiresApi;
39-
import com.afwsamples.testdpc.DeviceAdminReceiver;
43+
import androidx.core.content.ContextCompat;
44+
4045
import com.afwsamples.testdpc.R;
41-
import com.afwsamples.testdpc.common.PermissionsHelper;
4246
import com.afwsamples.testdpc.common.Util;
47+
4348
import java.util.ArrayList;
4449
import java.util.List;
4550

51+
import static android.net.wifi.WifiEnterpriseConfig.Eap;
52+
4653
/**
4754
* Fragment for WiFi configuration editing.
4855
*/
@@ -115,6 +122,17 @@ public void onResume() {
115122
updateConfigsList();
116123
}
117124

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+
118136
@RequiresApi(api = VERSION_CODES.M)
119137
@Override
120138
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
@@ -132,14 +150,13 @@ public View onCreateView(LayoutInflater inflater, final ViewGroup container,
132150
mConfigsList.setAdapter(mConfigsAdapter);
133151
mConfigsList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
134152

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();
139158
}
140159

141-
updateConfigsList();
142-
143160
Button updateConfigButton = (Button) view.findViewById(R.id.updateSelectedConfig);
144161
if (updateConfigButton != null) {
145162
updateConfigButton.setOnClickListener(new View.OnClickListener() {
@@ -200,10 +217,26 @@ public void onDismiss() {
200217
public void onCancel() {
201218
}
202219

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+
203229
private void showError(final String message) {
204230
new AlertDialog.Builder(getActivity()).setTitle(R.string.wifi_modification_error_title)
205231
.setIconAttribute(android.R.attr.alertDialogIcon)
206232
.setMessage(message).setPositiveButton(android.R.string.ok, null).show();
207233
}
208234

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+
}
209242
}

0 commit comments

Comments
 (0)