Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 653352634
  • Loading branch information
vladmarica authored and pfmaggi committed Sep 9, 2024
1 parent 057cb32 commit 598e983
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@
import java.util.ArrayList;
import java.util.List;

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

private static final int DISABLED = 0;
private static final int ENABLED = 1;

/**
* The number of digits in a Google account ID, which includes {@link
* #GOOGLE_ACCOUNT_ID_PREFIX_LENGTH}
*/
private static final int GOOGLE_ACCOUNT_ID_LENGTH = 21;

private static final int GOOGLE_ACCOUNT_ID_PREFIX_LENGTH = 1;

private DevicePolicyManager mDevicePolicyManager;
private ComponentName mAdminComponentName;

Expand All @@ -58,7 +67,6 @@ public class FactoryResetProtectionPolicyFragment extends Fragment
private FrpAccountsAdapter mAccountsAdapter;
private Spinner mFrpEnabledSpinner;

@RequiresApi(api = VERSION_CODES.R)
@Override
public void onCreate(Bundle savedInstanceState) {
mDevicePolicyManager =
Expand All @@ -68,7 +76,6 @@ public void onCreate(Bundle savedInstanceState) {
getActivity().getActionBar().setTitle(R.string.factory_reset_protection_policy);
}

@RequiresApi(api = VERSION_CODES.R)
@Override
public View onCreateView(
LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
Expand Down Expand Up @@ -168,6 +175,7 @@ public void createAddAccountDialog() {
final AlertDialog dialog =
new AlertDialog.Builder(getActivity())
.setTitle(R.string.add_account)
.setMessage(R.string.factory_reset_protection_policy_account_id_msg)
.setView(view)
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(android.R.string.cancel, null)
Expand All @@ -179,7 +187,7 @@ public void createAddAccountDialog() {
.setOnClickListener(
okButtonView -> {
String item = input.getText().toString();
if (TextUtils.isEmpty(item)) {
if (!isValidAccountId(item)) {
showToast(R.string.fail_to_add_account);
return;
}
Expand Down Expand Up @@ -211,4 +219,26 @@ public void onNothingSelected(AdapterView<?> adapterView) {
private void showToast(@StringRes int stringResId) {
Toast.makeText(getActivity(), stringResId, Toast.LENGTH_LONG).show();
}

/**
* Returns whether the given string is a valid Google account ID, which are numeric strings
* that are exactly {@value #GOOGLE_ACCOUNT_ID_LENGTH} digits in length.
*/
private boolean isValidAccountId(String accountId) {
if (TextUtils.isEmpty(accountId)) {
return false;
}

if (accountId.length() != GOOGLE_ACCOUNT_ID_LENGTH) {
return false;
}

try {
// Strip the prefix and verify that the rest of the ID can be parsed as a long
Long.parseUnsignedLong(accountId.substring(GOOGLE_ACCOUNT_ID_PREFIX_LENGTH));
return true;
} catch (NumberFormatException ex) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1407,8 +1407,11 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
showSetProfileNameDialog();
return true;
} else if (SET_FACTORY_RESET_PROTECTION_POLICY_KEY.equals(key)) {
showFragment(new FactoryResetProtectionPolicyFragment());
return true;
if (Util.SDK_INT >= VERSION_CODES.R) {
showFragment(new FactoryResetProtectionPolicyFragment());
return true;
}
return false;
} else if (SET_ORGANIZATION_ID_KEY.equals(key)) {
showSetOrganizationIdDialog();
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@
<string name="device_owner_removed">This app is no longer a device owner.</string>
<string name="set_factory_reset_protection_policy">Set factory reset protection policy</string>
<string name="factory_reset_protection_policy">Factory reset protection policy</string>
<string name="factory_reset_protection_policy_accounts">Accounts</string>
<string name="factory_reset_protection_policy_accounts">Account IDs</string>
<string name="factory_reset_protection_policy_account_id_msg">Account IDs are exactly 21 decimal digits</string>
<string name="factory_reset_protection_policy_enabled">Enabled</string>
<string-array name="factory_reset_protection_policy_enabled_choices">
<item>Disabled</item>
Expand Down

0 comments on commit 598e983

Please sign in to comment.