43
43
import java .util .ArrayList ;
44
44
import java .util .List ;
45
45
46
+ @ RequiresApi (api = VERSION_CODES .R )
46
47
public class FactoryResetProtectionPolicyFragment extends Fragment
47
48
implements AdapterView .OnItemSelectedListener , View .OnClickListener {
48
49
49
50
private static final int DISABLED = 0 ;
50
51
private static final int ENABLED = 1 ;
51
52
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
+
52
61
private DevicePolicyManager mDevicePolicyManager ;
53
62
private ComponentName mAdminComponentName ;
54
63
@@ -58,7 +67,6 @@ public class FactoryResetProtectionPolicyFragment extends Fragment
58
67
private FrpAccountsAdapter mAccountsAdapter ;
59
68
private Spinner mFrpEnabledSpinner ;
60
69
61
- @ RequiresApi (api = VERSION_CODES .R )
62
70
@ Override
63
71
public void onCreate (Bundle savedInstanceState ) {
64
72
mDevicePolicyManager =
@@ -68,7 +76,6 @@ public void onCreate(Bundle savedInstanceState) {
68
76
getActivity ().getActionBar ().setTitle (R .string .factory_reset_protection_policy );
69
77
}
70
78
71
- @ RequiresApi (api = VERSION_CODES .R )
72
79
@ Override
73
80
public View onCreateView (
74
81
LayoutInflater inflater , final ViewGroup container , Bundle savedInstanceState ) {
@@ -168,6 +175,7 @@ public void createAddAccountDialog() {
168
175
final AlertDialog dialog =
169
176
new AlertDialog .Builder (getActivity ())
170
177
.setTitle (R .string .add_account )
178
+ .setMessage (R .string .factory_reset_protection_policy_account_id_msg )
171
179
.setView (view )
172
180
.setPositiveButton (android .R .string .ok , null )
173
181
.setNegativeButton (android .R .string .cancel , null )
@@ -179,7 +187,7 @@ public void createAddAccountDialog() {
179
187
.setOnClickListener (
180
188
okButtonView -> {
181
189
String item = input .getText ().toString ();
182
- if (TextUtils . isEmpty (item )) {
190
+ if (! isValidAccountId (item )) {
183
191
showToast (R .string .fail_to_add_account );
184
192
return ;
185
193
}
@@ -211,4 +219,26 @@ public void onNothingSelected(AdapterView<?> adapterView) {
211
219
private void showToast (@ StringRes int stringResId ) {
212
220
Toast .makeText (getActivity (), stringResId , Toast .LENGTH_LONG ).show ();
213
221
}
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
+ }
214
244
}
0 commit comments