Skip to content

Commit 598e983

Browse files
vladmaricapfmaggi
authored andcommitted
No public description
PiperOrigin-RevId: 653352634
1 parent 057cb32 commit 598e983

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/main/java/com/afwsamples/testdpc/policy/FactoryResetProtectionPolicyFragment.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,21 @@
4343
import java.util.ArrayList;
4444
import java.util.List;
4545

46+
@RequiresApi(api = VERSION_CODES.R)
4647
public class FactoryResetProtectionPolicyFragment extends Fragment
4748
implements AdapterView.OnItemSelectedListener, View.OnClickListener {
4849

4950
private static final int DISABLED = 0;
5051
private static final int ENABLED = 1;
5152

53+
/**
54+
* The number of digits in a Google account ID, which includes {@link
55+
* #GOOGLE_ACCOUNT_ID_PREFIX_LENGTH}
56+
*/
57+
private static final int GOOGLE_ACCOUNT_ID_LENGTH = 21;
58+
59+
private static final int GOOGLE_ACCOUNT_ID_PREFIX_LENGTH = 1;
60+
5261
private DevicePolicyManager mDevicePolicyManager;
5362
private ComponentName mAdminComponentName;
5463

@@ -58,7 +67,6 @@ public class FactoryResetProtectionPolicyFragment extends Fragment
5867
private FrpAccountsAdapter mAccountsAdapter;
5968
private Spinner mFrpEnabledSpinner;
6069

61-
@RequiresApi(api = VERSION_CODES.R)
6270
@Override
6371
public void onCreate(Bundle savedInstanceState) {
6472
mDevicePolicyManager =
@@ -68,7 +76,6 @@ public void onCreate(Bundle savedInstanceState) {
6876
getActivity().getActionBar().setTitle(R.string.factory_reset_protection_policy);
6977
}
7078

71-
@RequiresApi(api = VERSION_CODES.R)
7279
@Override
7380
public View onCreateView(
7481
LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
@@ -168,6 +175,7 @@ public void createAddAccountDialog() {
168175
final AlertDialog dialog =
169176
new AlertDialog.Builder(getActivity())
170177
.setTitle(R.string.add_account)
178+
.setMessage(R.string.factory_reset_protection_policy_account_id_msg)
171179
.setView(view)
172180
.setPositiveButton(android.R.string.ok, null)
173181
.setNegativeButton(android.R.string.cancel, null)
@@ -179,7 +187,7 @@ public void createAddAccountDialog() {
179187
.setOnClickListener(
180188
okButtonView -> {
181189
String item = input.getText().toString();
182-
if (TextUtils.isEmpty(item)) {
190+
if (!isValidAccountId(item)) {
183191
showToast(R.string.fail_to_add_account);
184192
return;
185193
}
@@ -211,4 +219,26 @@ public void onNothingSelected(AdapterView<?> adapterView) {
211219
private void showToast(@StringRes int stringResId) {
212220
Toast.makeText(getActivity(), stringResId, Toast.LENGTH_LONG).show();
213221
}
222+
223+
/**
224+
* Returns whether the given string is a valid Google account ID, which are numeric strings
225+
* that are exactly {@value #GOOGLE_ACCOUNT_ID_LENGTH} digits in length.
226+
*/
227+
private boolean isValidAccountId(String accountId) {
228+
if (TextUtils.isEmpty(accountId)) {
229+
return false;
230+
}
231+
232+
if (accountId.length() != GOOGLE_ACCOUNT_ID_LENGTH) {
233+
return false;
234+
}
235+
236+
try {
237+
// Strip the prefix and verify that the rest of the ID can be parsed as a long
238+
Long.parseUnsignedLong(accountId.substring(GOOGLE_ACCOUNT_ID_PREFIX_LENGTH));
239+
return true;
240+
} catch (NumberFormatException ex) {
241+
return false;
242+
}
243+
}
214244
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,8 +1407,11 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
14071407
showSetProfileNameDialog();
14081408
return true;
14091409
} else if (SET_FACTORY_RESET_PROTECTION_POLICY_KEY.equals(key)) {
1410-
showFragment(new FactoryResetProtectionPolicyFragment());
1411-
return true;
1410+
if (Util.SDK_INT >= VERSION_CODES.R) {
1411+
showFragment(new FactoryResetProtectionPolicyFragment());
1412+
return true;
1413+
}
1414+
return false;
14121415
} else if (SET_ORGANIZATION_ID_KEY.equals(key)) {
14131416
showSetOrganizationIdDialog();
14141417
return true;

src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@
142142
<string name="device_owner_removed">This app is no longer a device owner.</string>
143143
<string name="set_factory_reset_protection_policy">Set factory reset protection policy</string>
144144
<string name="factory_reset_protection_policy">Factory reset protection policy</string>
145-
<string name="factory_reset_protection_policy_accounts">Accounts</string>
145+
<string name="factory_reset_protection_policy_accounts">Account IDs</string>
146+
<string name="factory_reset_protection_policy_account_id_msg">Account IDs are exactly 21 decimal digits</string>
146147
<string name="factory_reset_protection_policy_enabled">Enabled</string>
147148
<string-array name="factory_reset_protection_policy_enabled_choices">
148149
<item>Disabled</item>

0 commit comments

Comments
 (0)