Skip to content

Commit 7b88ce8

Browse files
TreeHugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Manage WiFi configurations without location permission" into ub-testdpc-sc
2 parents 386e036 + ed9da7f commit 7b88ce8

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import android.location.LocationManager;
4949
import android.net.ProxyInfo;
5050
import android.net.Uri;
51+
import android.net.wifi.WifiManager;
5152
import android.os.AsyncTask;
5253
import android.os.BatteryManager;
5354
import android.os.Build;
@@ -390,6 +391,8 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
390391
= "create_eap_tls_wifi_configuration";
391392
private static final String WIFI_CONFIG_LOCKDOWN_ENABLE_KEY = "enable_wifi_config_lockdown";
392393
private static final String MODIFY_WIFI_CONFIGURATION_KEY = "modify_wifi_configuration";
394+
private static final String MODIFY_OWNED_WIFI_CONFIGURATION_KEY = "modify_owned_wifi_configuration";
395+
private static final String REMOVE_NOT_OWNED_WIFI_CONFIGURATION_KEY = "remove_not_owned_wifi_configurations";
393396
private static final String TRANSFER_OWNERSHIP_KEY = "transfer_ownership_to_component";
394397
private static final String TAG_WIFI_CONFIG_CREATION = "wifi_config_creation";
395398
private static final String SECURITY_PATCH_FORMAT = "yyyy-MM-dd";
@@ -735,6 +738,8 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
735738
findPreference(WIFI_CONFIG_LOCKDOWN_ENABLE_KEY);
736739
mLockdownAdminConfiguredNetworksPreference.setOnPreferenceChangeListener(this);
737740
findPreference(MODIFY_WIFI_CONFIGURATION_KEY).setOnPreferenceClickListener(this);
741+
findPreference(MODIFY_OWNED_WIFI_CONFIGURATION_KEY).setOnPreferenceClickListener(this);
742+
findPreference(REMOVE_NOT_OWNED_WIFI_CONFIGURATION_KEY).setOnPreferenceClickListener(this);
738743
findPreference(TRANSFER_OWNERSHIP_KEY).setOnPreferenceClickListener(this);
739744
findPreference(SHOW_WIFI_MAC_ADDRESS_KEY).setOnPreferenceClickListener(this);
740745
mInstallNonMarketAppsPreference = (DpcSwitchPreference) findPreference(
@@ -1279,7 +1284,19 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
12791284
showEapTlsWifiConfigCreationDialog();
12801285
return true;
12811286
case MODIFY_WIFI_CONFIGURATION_KEY:
1282-
showFragment(new WifiModificationFragment());
1287+
showFragment(WifiModificationFragment.createFragment(false));
1288+
return true;
1289+
case MODIFY_OWNED_WIFI_CONFIGURATION_KEY:
1290+
showFragment(WifiModificationFragment.createFragment(true));
1291+
return true;
1292+
case REMOVE_NOT_OWNED_WIFI_CONFIGURATION_KEY:
1293+
boolean removed = getContext().getSystemService(WifiManager.class)
1294+
.removeNonCallerConfiguredNetworks();
1295+
if (removed) {
1296+
showToast("One or more networks are removed");
1297+
} else {
1298+
showToast("No network is removed");
1299+
}
12831300
return true;
12841301
case SHOW_WIFI_MAC_ADDRESS_KEY:
12851302
showWifiMacAddress();

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,26 @@
5656
public class WifiModificationFragment extends Fragment
5757
implements WifiConfigCreationDialog.Listener {
5858

59+
// Fragment argument: if set, ony list networks added by TestDPC itself. This causes
60+
// the fragment to switch to different WifiManager APIs that don't rely on location permission.
61+
private static final String ARG_OWNED_NETWORKS_ONLY = "owned_networks_only";
5962
private static final String TAG_WIFI_CONFIG_MODIFICATION = "wifi_config_modification";
6063
private ListView mConfigsList;
6164
private ConfigsAdapter mConfigsAdapter;
6265
private List<WifiConfiguration> mConfiguredNetworks = new ArrayList<>();
6366
private WifiManager mWifiManager;
67+
private boolean mShowOwnedNetworksOnly;
6468

6569
private void updateConfigsList() {
6670
mConfiguredNetworks.clear();
6771
// WifiManager deprecated APIs including #getConfiguredNetworks are restricted to system apps and DPCs
6872
// https://developer.android.com/preview/privacy/camera-connectivity#wifi-network-config-restrictions
69-
List<WifiConfiguration> configuredNetworks = mWifiManager.getConfiguredNetworks();
73+
List<WifiConfiguration> configuredNetworks;
74+
if (mShowOwnedNetworksOnly) {
75+
configuredNetworks = mWifiManager.getCallerConfiguredNetworks();
76+
} else {
77+
configuredNetworks = mWifiManager.getConfiguredNetworks();
78+
}
7079
if (configuredNetworks != null) {
7180
mConfiguredNetworks.addAll(configuredNetworks);
7281
}
@@ -138,6 +147,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
138147
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
139148
Bundle savedInstanceState) {
140149
super.onCreateView(inflater, container, savedInstanceState);
150+
mShowOwnedNetworksOnly = getArguments().getBoolean(ARG_OWNED_NETWORKS_ONLY);
141151
mWifiManager = (WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
142152
View view = inflater.inflate(R.layout.wifi_config_modification, container, false);
143153

@@ -204,7 +214,6 @@ public void onClick(View v) {
204214
}
205215
});
206216
}
207-
208217
return view;
209218
}
210219

@@ -217,11 +226,13 @@ public void onDismiss() {
217226
public void onCancel() {
218227
}
219228

220-
public static boolean shouldRequestLocationPermission(Context context) {
229+
public boolean shouldRequestLocationPermission(Context context) {
221230
if (Util.SDK_INT < VERSION_CODES.M) {
222231
return false;
223232
}
224-
233+
if (mShowOwnedNetworksOnly) {
234+
return false;
235+
}
225236
return ContextCompat.checkSelfPermission(context, permission.ACCESS_FINE_LOCATION)
226237
!= PackageManager.PERMISSION_GRANTED;
227238
}
@@ -239,4 +250,12 @@ private void showToast(String msg) {
239250
}
240251
Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
241252
}
253+
254+
public static WifiModificationFragment createFragment(boolean showOwnedNetworksOnly) {
255+
WifiModificationFragment fragment = new WifiModificationFragment();
256+
Bundle args = new Bundle();
257+
args.putBoolean(ARG_OWNED_NETWORKS_ONLY, showOwnedNetworksOnly);
258+
fragment.setArguments(args);
259+
return fragment;
260+
}
242261
}

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,15 @@
328328
<string name="wifi_config_fail">Failed to save the WI-FI configuration.</string>
329329
<string name="enable_wifi_config_lockdown">DO created WI-FI configs are modifiable only by DO</string>
330330
<string name="modify_wifi_configuration">Modify WI-FI configuration</string>
331+
<string name="modify_owned_wifi_configuration">Modify Owned WI-FI configuration</string>
332+
<string name="remove_not_owned_wifi_configurations">Remove all networks not owned by DO</string>
331333
<string name="show_wifi_mac_address">Show Wi-Fi MAC address</string>
332334
<string name="show_wifi_mac_address_title">Wi-Fi MAC address</string>
333335
<string name="show_wifi_mac_address_not_available_msg">Wi-Fi MAC is not available</string>
334336
<string name="wifi_configs_header">Saved WI-FI configurations</string>
335337
<string name="wifi_update_config_button">Update selected</string>
336338
<string name="wifi_remove_config_button">Remove selected</string>
339+
<string name="wifi_remove_all_config_button">Remove All</string>
337340
<string name="wifi_modification_error_title">Error modifying config</string>
338341
<string name="wifi_remove_config_error">Unable to remove SSID \'<xliff:g id="wifi_ssid">%1$s</xliff:g>\': netId = <xliff:g id="wifi_network_id">%2$d</xliff:g></string>
339342
<string name="create_eap_tls_wifi_configuration">Create EAP-TLS WI-FI configuration</string>

app/src/main/res/xml/device_policy_header.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@
337337
android:title="@string/modify_wifi_configuration"
338338
testdpc:admin="any"
339339
testdpc:minSdkVersion="L" />
340+
<com.afwsamples.testdpc.common.preference.DpcPreference
341+
android:key="modify_owned_wifi_configuration"
342+
android:title="@string/modify_owned_wifi_configuration"
343+
testdpc:admin="any"
344+
testdpc:minSdkVersion="S" />
345+
<com.afwsamples.testdpc.common.preference.DpcPreference
346+
android:key="remove_not_owned_wifi_configurations"
347+
android:title="@string/remove_not_owned_wifi_configurations"
348+
testdpc:admin="deviceOwner"
349+
testdpc:minSdkVersion="S" />
340350
<com.afwsamples.testdpc.common.preference.DpcPreference
341351
android:key="show_wifi_mac_address"
342352
android:title="@string/show_wifi_mac_address"
@@ -633,7 +643,7 @@
633643
android:key="logout_user"
634644
android:title="@string/logout_user"
635645
testdpc:minSdkVersion="P"
636-
testdpc:user="secondaryUser" />
646+
testdpc:user="secondaryUser" />
637647
<com.afwsamples.testdpc.common.preference.DpcSwitchPreference
638648
android:key="enable_logout"
639649
android:title="@string/enable_logout"

0 commit comments

Comments
 (0)