4343import java .util .ArrayList ;
4444import java .util .List ;
4545
46+ @ RequiresApi (api = VERSION_CODES .R )
4647public 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}
0 commit comments