packages/apps/Settings
Revision | 72c60f0d6a869158943961a1d8b2d1d4539a32e2 (tree) |
---|---|
Zeit | 2021-08-16 13:15:31 |
Autor | Android Build Coastguard Worker <android-build-coastguard-worker@goog...> |
Commiter | Android Build Coastguard Worker |
Merge cherrypicks of [15581311, 15581312, 15581313, 15581314, 15581326, 15581327, 15581138, 15581139, 15581140, 15581341, 15581342, 15581343, 15581344, 15581095, 15581315, 15581316, 15581317, 15581345, 15581328] into rvc-d2-release
Change-Id: I2cb951d02f087df3bef53d54679b0ae786a1ebfb
@@ -2711,6 +2711,10 @@ | ||
2711 | 2711 | </intent-filter> |
2712 | 2712 | </activity> |
2713 | 2713 | |
2714 | + <!-- | |
2715 | + The Wi-Fi result data will only be returned from WifiDialogActivity if the calling | |
2716 | + package has ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission. (see b/185126813) | |
2717 | + --> | |
2714 | 2718 | <activity |
2715 | 2719 | android:name=".wifi.WifiDialogActivity" |
2716 | 2720 | android:label="" |
@@ -16,14 +16,19 @@ | ||
16 | 16 | |
17 | 17 | package com.android.settings.wifi; |
18 | 18 | |
19 | +import static android.Manifest.permission.ACCESS_COARSE_LOCATION; | |
20 | +import static android.Manifest.permission.ACCESS_FINE_LOCATION; | |
21 | + | |
19 | 22 | import android.app.Activity; |
20 | 23 | import android.content.DialogInterface; |
21 | 24 | import android.content.Intent; |
25 | +import android.content.pm.PackageManager; | |
22 | 26 | import android.net.NetworkInfo; |
23 | 27 | import android.net.wifi.WifiConfiguration; |
24 | 28 | import android.net.wifi.WifiManager; |
25 | 29 | import android.net.wifi.WifiManager.ActionListener; |
26 | 30 | import android.os.Bundle; |
31 | +import android.util.EventLog; | |
27 | 32 | import android.util.Log; |
28 | 33 | |
29 | 34 | import androidx.annotation.VisibleForTesting; |
@@ -53,10 +58,12 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo | ||
53 | 58 | |
54 | 59 | public static final String KEY_WIFI_CONFIGURATION = "wifi_configuration"; |
55 | 60 | |
56 | - private static final int RESULT_CONNECTED = RESULT_FIRST_USER; | |
61 | + @VisibleForTesting | |
62 | + static final int RESULT_CONNECTED = RESULT_FIRST_USER; | |
57 | 63 | private static final int RESULT_FORGET = RESULT_FIRST_USER + 1; |
58 | 64 | |
59 | - private static final int REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER = 0; | |
65 | + @VisibleForTesting | |
66 | + static final int REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER = 0; | |
60 | 67 | |
61 | 68 | private WifiDialog mDialog; |
62 | 69 |
@@ -156,17 +163,22 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo | ||
156 | 163 | } |
157 | 164 | } |
158 | 165 | |
159 | - Intent resultData = new Intent(); | |
166 | + Intent resultData = hasPermissionForResult() ? createResultData(config, accessPoint) : null; | |
167 | + setResult(RESULT_CONNECTED, resultData); | |
168 | + finish(); | |
169 | + } | |
170 | + | |
171 | + protected Intent createResultData(WifiConfiguration config, AccessPoint accessPoint) { | |
172 | + Intent result = new Intent(); | |
160 | 173 | if (accessPoint != null) { |
161 | 174 | Bundle accessPointState = new Bundle(); |
162 | 175 | accessPoint.saveWifiState(accessPointState); |
163 | - resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState); | |
176 | + result.putExtra(KEY_ACCESS_POINT_STATE, accessPointState); | |
164 | 177 | } |
165 | 178 | if (config != null) { |
166 | - resultData.putExtra(KEY_WIFI_CONFIGURATION, config); | |
179 | + result.putExtra(KEY_WIFI_CONFIGURATION, config); | |
167 | 180 | } |
168 | - setResult(RESULT_CONNECTED, resultData); | |
169 | - finish(); | |
181 | + return result; | |
170 | 182 | } |
171 | 183 | |
172 | 184 | @Override |
@@ -192,9 +204,44 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo | ||
192 | 204 | if (resultCode != RESULT_OK) { |
193 | 205 | return; |
194 | 206 | } |
195 | - | |
196 | - setResult(RESULT_CONNECTED, data); | |
207 | + if (hasPermissionForResult()) { | |
208 | + setResult(RESULT_CONNECTED, data); | |
209 | + } else { | |
210 | + setResult(RESULT_CONNECTED); | |
211 | + } | |
197 | 212 | finish(); |
198 | 213 | } |
199 | 214 | } |
215 | + | |
216 | + protected boolean hasPermissionForResult() { | |
217 | + final String callingPackage = getCallingPackage(); | |
218 | + if (callingPackage == null) { | |
219 | + Log.d(TAG, "Failed to get the calling package, don't return the result."); | |
220 | + EventLog.writeEvent(0x534e4554, "185126813", -1 /* UID */, "no calling package"); | |
221 | + return false; | |
222 | + } | |
223 | + | |
224 | + if (getPackageManager().checkPermission(ACCESS_COARSE_LOCATION, callingPackage) | |
225 | + == PackageManager.PERMISSION_GRANTED) { | |
226 | + Log.d(TAG, "The calling package has ACCESS_COARSE_LOCATION permission for result."); | |
227 | + return true; | |
228 | + } | |
229 | + | |
230 | + if (getPackageManager().checkPermission(ACCESS_FINE_LOCATION, callingPackage) | |
231 | + == PackageManager.PERMISSION_GRANTED) { | |
232 | + Log.d(TAG, "The calling package has ACCESS_FINE_LOCATION permission for result."); | |
233 | + return true; | |
234 | + } | |
235 | + | |
236 | + Log.d(TAG, "The calling package does not have the necessary permissions for result."); | |
237 | + try { | |
238 | + EventLog.writeEvent(0x534e4554, "185126813", | |
239 | + getPackageManager().getPackageUid(callingPackage, 0 /* flags */), | |
240 | + "no permission"); | |
241 | + } catch (PackageManager.NameNotFoundException e) { | |
242 | + EventLog.writeEvent(0x534e4554, "185126813", -1 /* UID */, "no permission"); | |
243 | + Log.w(TAG, "Cannot find the UID, calling package: " + callingPackage, e); | |
244 | + } | |
245 | + return false; | |
246 | + } | |
200 | 247 | } |
@@ -16,17 +16,31 @@ | ||
16 | 16 | |
17 | 17 | package com.android.settings.wifi; |
18 | 18 | |
19 | +import static android.Manifest.permission.ACCESS_COARSE_LOCATION; | |
20 | +import static android.Manifest.permission.ACCESS_FINE_LOCATION; | |
21 | + | |
22 | +import static com.android.settings.wifi.WifiDialogActivity.REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER; | |
23 | +import static com.android.settings.wifi.WifiDialogActivity.RESULT_CONNECTED; | |
24 | +import static com.android.settings.wifi.WifiDialogActivity.RESULT_OK; | |
25 | + | |
19 | 26 | import static com.google.common.truth.Truth.assertThat; |
20 | 27 | |
28 | +import static org.mockito.ArgumentMatchers.any; | |
21 | 29 | import static org.mockito.Mockito.doReturn; |
30 | +import static org.mockito.Mockito.spy; | |
31 | +import static org.mockito.Mockito.verify; | |
32 | +import static org.mockito.Mockito.when; | |
22 | 33 | |
23 | 34 | import android.content.Intent; |
35 | +import android.content.pm.PackageManager; | |
24 | 36 | import android.net.wifi.WifiConfiguration; |
37 | +import android.net.wifi.WifiManager; | |
25 | 38 | |
26 | 39 | import com.android.settings.R; |
27 | 40 | import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; |
28 | 41 | import com.android.settings.testutils.shadow.ShadowConnectivityManager; |
29 | 42 | import com.android.settings.testutils.shadow.ShadowWifiManager; |
43 | +import com.android.settingslib.wifi.AccessPoint; | |
30 | 44 | |
31 | 45 | import com.google.android.setupcompat.util.WizardManagerHelper; |
32 | 46 |
@@ -48,13 +62,30 @@ import org.robolectric.util.ReflectionHelpers; | ||
48 | 62 | }) |
49 | 63 | public class WifiDialogActivityTest { |
50 | 64 | |
65 | + private static final String CALLING_PACKAGE = "calling_package"; | |
51 | 66 | private static final String AP1_SSID = "\"ap1\""; |
67 | + | |
68 | + @Mock | |
69 | + PackageManager mPackageManager; | |
70 | + @Mock | |
71 | + WifiManager mWifiManager; | |
72 | + @Mock | |
73 | + WifiDialog mWifiDialog; | |
74 | + @Mock | |
75 | + WifiConfiguration mWifiConfiguration; | |
76 | + @Mock | |
77 | + AccessPoint mAccessPoint; | |
78 | + @Mock | |
79 | + Intent mResultData; | |
52 | 80 | @Mock |
53 | 81 | private WifiConfigController mController; |
54 | 82 | |
55 | 83 | @Before |
56 | 84 | public void setUp() { |
57 | 85 | MockitoAnnotations.initMocks(this); |
86 | + when(mWifiDialog.getController()).thenReturn(mController); | |
87 | + when(mController.getConfig()).thenReturn(mWifiConfiguration); | |
88 | + when(mController.getAccessPoint()).thenReturn(mAccessPoint); | |
58 | 89 | |
59 | 90 | WifiConfiguration wifiConfig = new WifiConfiguration(); |
60 | 91 | wifiConfig.SSID = AP1_SSID; |
@@ -75,6 +106,29 @@ public class WifiDialogActivityTest { | ||
75 | 106 | } |
76 | 107 | |
77 | 108 | @Test |
109 | + public void onSubmit_noPermissionForResult_setResultWithoutData() { | |
110 | + WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class)); | |
111 | + when(activity.hasPermissionForResult()).thenReturn(false); | |
112 | + when(activity.getSystemService(WifiManager.class)).thenReturn(mWifiManager); | |
113 | + | |
114 | + activity.onSubmit(mWifiDialog); | |
115 | + | |
116 | + verify(activity).setResult(RESULT_CONNECTED, null); | |
117 | + } | |
118 | + | |
119 | + @Test | |
120 | + public void onSubmit_hasPermissionForResult_setResultWithData() { | |
121 | + WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class)); | |
122 | + when(activity.hasPermissionForResult()).thenReturn(true); | |
123 | + when(activity.createResultData(any(), any())).thenReturn(mResultData); | |
124 | + when(activity.getSystemService(WifiManager.class)).thenReturn(mWifiManager); | |
125 | + | |
126 | + activity.onSubmit(mWifiDialog); | |
127 | + | |
128 | + verify(activity).setResult(RESULT_CONNECTED, mResultData); | |
129 | + } | |
130 | + | |
131 | + @Test | |
78 | 132 | public void onSubmit_whenConnectForCallerIsFalse_shouldNotConnectToNetwork() { |
79 | 133 | WifiDialogActivity activity = |
80 | 134 | Robolectric.buildActivity( |
@@ -111,4 +165,97 @@ public class WifiDialogActivityTest { | ||
111 | 165 | assertThat(dialog.getContext().getThemeResId()) |
112 | 166 | .isEqualTo(R.style.SuwAlertDialogThemeCompat_Light); |
113 | 167 | } |
168 | + | |
169 | + @Test | |
170 | + public void onActivityResult_noPermissionForResult_setResultWithoutData() { | |
171 | + WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class)); | |
172 | + when(activity.hasPermissionForResult()).thenReturn(false); | |
173 | + final Intent data = new Intent(); | |
174 | + | |
175 | + activity.onActivityResult(REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER, RESULT_OK, | |
176 | + data); | |
177 | + | |
178 | + verify(activity).setResult(RESULT_CONNECTED); | |
179 | + } | |
180 | + | |
181 | + @Test | |
182 | + public void onActivityResult_hasPermissionForResult_setResultWithData() { | |
183 | + WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class)); | |
184 | + when(activity.hasPermissionForResult()).thenReturn(true); | |
185 | + final Intent data = new Intent(); | |
186 | + | |
187 | + activity.onActivityResult(REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER, RESULT_OK, | |
188 | + data); | |
189 | + | |
190 | + verify(activity).setResult(RESULT_CONNECTED, data); | |
191 | + } | |
192 | + | |
193 | + @Test | |
194 | + public void hasPermissionForResult_noCallingPackage_returnFalse() { | |
195 | + WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class)); | |
196 | + when(activity.getCallingPackage()).thenReturn(null); | |
197 | + | |
198 | + final boolean result = activity.hasPermissionForResult(); | |
199 | + | |
200 | + assertThat(result).isFalse(); | |
201 | + } | |
202 | + | |
203 | + @Test | |
204 | + public void hasPermissionForResult_noPermission_returnFalse() { | |
205 | + WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class)); | |
206 | + when(activity.getCallingPackage()).thenReturn(null); | |
207 | + when(mPackageManager.checkPermission(ACCESS_COARSE_LOCATION, CALLING_PACKAGE)) | |
208 | + .thenReturn(PackageManager.PERMISSION_DENIED); | |
209 | + when(mPackageManager.checkPermission(ACCESS_FINE_LOCATION, CALLING_PACKAGE)) | |
210 | + .thenReturn(PackageManager.PERMISSION_DENIED); | |
211 | + | |
212 | + final boolean result = activity.hasPermissionForResult(); | |
213 | + | |
214 | + assertThat(result).isFalse(); | |
215 | + } | |
216 | + | |
217 | + @Test | |
218 | + public void hasPermissionForResult_hasCoarseLocationPermission_returnTrue() { | |
219 | + WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class)); | |
220 | + when(activity.getCallingPackage()).thenReturn(CALLING_PACKAGE); | |
221 | + when(activity.getPackageManager()).thenReturn(mPackageManager); | |
222 | + when(mPackageManager.checkPermission(ACCESS_COARSE_LOCATION, CALLING_PACKAGE)) | |
223 | + .thenReturn(PackageManager.PERMISSION_GRANTED); | |
224 | + when(mPackageManager.checkPermission(ACCESS_FINE_LOCATION, CALLING_PACKAGE)) | |
225 | + .thenReturn(PackageManager.PERMISSION_DENIED); | |
226 | + | |
227 | + final boolean result = activity.hasPermissionForResult(); | |
228 | + | |
229 | + assertThat(result).isTrue(); | |
230 | + } | |
231 | + | |
232 | + @Test | |
233 | + public void hasPermissionForResult_hasFineLocationPermission_returnTrue() { | |
234 | + WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class)); | |
235 | + when(activity.getCallingPackage()).thenReturn(CALLING_PACKAGE); | |
236 | + when(activity.getPackageManager()).thenReturn(mPackageManager); | |
237 | + when(mPackageManager.checkPermission(ACCESS_COARSE_LOCATION, CALLING_PACKAGE)) | |
238 | + .thenReturn(PackageManager.PERMISSION_DENIED); | |
239 | + when(mPackageManager.checkPermission(ACCESS_FINE_LOCATION, CALLING_PACKAGE)) | |
240 | + .thenReturn(PackageManager.PERMISSION_GRANTED); | |
241 | + | |
242 | + final boolean result = activity.hasPermissionForResult(); | |
243 | + | |
244 | + assertThat(result).isTrue(); | |
245 | + } | |
246 | + | |
247 | + @Test | |
248 | + public void hasPermissionForResult_haveBothLocationPermissions_returnTrue() { | |
249 | + WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class)); | |
250 | + when(activity.getCallingPackage()).thenReturn(CALLING_PACKAGE); | |
251 | + when(activity.getPackageManager()).thenReturn(mPackageManager); | |
252 | + when(mPackageManager.checkPermission(ACCESS_COARSE_LOCATION, CALLING_PACKAGE)) | |
253 | + .thenReturn(PackageManager.PERMISSION_GRANTED); | |
254 | + when(mPackageManager.checkPermission(ACCESS_FINE_LOCATION, CALLING_PACKAGE)) | |
255 | + .thenReturn(PackageManager.PERMISSION_GRANTED); | |
256 | + | |
257 | + final boolean result = activity.hasPermissionForResult(); | |
258 | + | |
259 | + assertThat(result).isTrue(); | |
260 | + } | |
114 | 261 | } |