|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.afwsamples.testdpc.policy; |
| 18 | + |
| 19 | +import android.app.AlertDialog; |
| 20 | +import android.app.Fragment; |
| 21 | +import android.app.admin.DevicePolicyManager; |
| 22 | +import android.app.admin.FactoryResetProtectionPolicy; |
| 23 | +import android.content.ComponentName; |
| 24 | +import android.content.Context; |
| 25 | +import android.os.Bundle; |
| 26 | +import android.text.TextUtils; |
| 27 | +import android.view.LayoutInflater; |
| 28 | +import android.view.View; |
| 29 | +import android.view.ViewGroup; |
| 30 | +import android.widget.AdapterView; |
| 31 | +import android.widget.ArrayAdapter; |
| 32 | +import android.widget.Button; |
| 33 | +import android.widget.EditText; |
| 34 | +import android.widget.ListView; |
| 35 | +import android.widget.Spinner; |
| 36 | +import android.widget.TextView; |
| 37 | +import android.widget.Toast; |
| 38 | + |
| 39 | +import androidx.annotation.RequiresApi; |
| 40 | +import androidx.annotation.StringRes; |
| 41 | + |
| 42 | +import com.afwsamples.testdpc.DeviceAdminReceiver; |
| 43 | +import com.afwsamples.testdpc.R; |
| 44 | +import com.afwsamples.testdpc.common.Util; |
| 45 | + |
| 46 | +import java.util.ArrayList; |
| 47 | +import java.util.List; |
| 48 | + |
| 49 | +public class FactoryResetProtectionPolicyFragment extends Fragment |
| 50 | + implements AdapterView.OnItemSelectedListener, View.OnClickListener { |
| 51 | + |
| 52 | + private static final int DISABLED = 0; |
| 53 | + private static final int ENABLED = 1; |
| 54 | + |
| 55 | + private DevicePolicyManager mDevicePolicyManager; |
| 56 | + private ComponentName mAdminComponentName; |
| 57 | + |
| 58 | + private List<String> mAccounts = new ArrayList<>(); |
| 59 | + private boolean mEnabled; |
| 60 | + |
| 61 | + private FrpAccountsAdapter mAccountsAdapter; |
| 62 | + private Spinner mFrpEnabledSpinner; |
| 63 | + |
| 64 | + @RequiresApi(api = Util.R_VERSION_CODE) |
| 65 | + @Override |
| 66 | + public void onCreate(Bundle savedInstanceState) { |
| 67 | + mDevicePolicyManager = (DevicePolicyManager) |
| 68 | + getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 69 | + mAdminComponentName = DeviceAdminReceiver.getComponentName(getActivity()); |
| 70 | + super.onCreate(savedInstanceState); |
| 71 | + getActivity().getActionBar().setTitle(R.string.factory_reset_protection_policy); |
| 72 | + } |
| 73 | + |
| 74 | + @RequiresApi(api = Util.R_VERSION_CODE) |
| 75 | + @Override |
| 76 | + public View onCreateView(LayoutInflater inflater, final ViewGroup container, |
| 77 | + Bundle savedInstanceState) { |
| 78 | + super.onCreateView(inflater, container, savedInstanceState); |
| 79 | + View view = inflater.inflate(R.layout.factory_reset_protection_policy, container, false); |
| 80 | + |
| 81 | + ListView frpAccounts = view.findViewById(R.id.frp_accounts); |
| 82 | + mAccountsAdapter = new FrpAccountsAdapter(); |
| 83 | + frpAccounts.setAdapter(mAccountsAdapter); |
| 84 | + |
| 85 | + Button addAccountButton = view.findViewById(R.id.add_account_button); |
| 86 | + addAccountButton.setOnClickListener(this); |
| 87 | + Button clearButton = view.findViewById(R.id.clear_frp_button); |
| 88 | + clearButton.setOnClickListener(this); |
| 89 | + Button saveButton = view.findViewById(R.id.save_frp_button); |
| 90 | + saveButton.setOnClickListener(this); |
| 91 | + |
| 92 | + mFrpEnabledSpinner = view.findViewById(R.id.frp_enabled); |
| 93 | + mFrpEnabledSpinner.setOnItemSelectedListener(this); |
| 94 | + ArrayAdapter<CharSequence> enabledAdapter = ArrayAdapter.createFromResource(getActivity(), |
| 95 | + R.array.factory_reset_protection_policy_enabled_choices, |
| 96 | + android.R.layout.simple_spinner_item); |
| 97 | + enabledAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
| 98 | + mFrpEnabledSpinner.setAdapter(enabledAdapter); |
| 99 | + |
| 100 | + refreshUi(); |
| 101 | + return view; |
| 102 | + } |
| 103 | + |
| 104 | + private void refreshUi() { |
| 105 | + mAccountsAdapter.clear(); |
| 106 | + mFrpEnabledSpinner.setSelection(DISABLED); |
| 107 | + |
| 108 | + FactoryResetProtectionPolicy mFrpPolicy = mDevicePolicyManager |
| 109 | + .getFactoryResetProtectionPolicy(mAdminComponentName); |
| 110 | + if (mFrpPolicy != null) { |
| 111 | + mAccountsAdapter.addAll(mFrpPolicy.getFactoryResetProtectionAccounts()); |
| 112 | + mFrpEnabledSpinner.setSelection( |
| 113 | + mFrpPolicy.isFactoryResetProtectionEnabled() ? ENABLED : DISABLED); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private class FrpAccountsAdapter extends ArrayAdapter<String> { |
| 118 | + |
| 119 | + public FrpAccountsAdapter() { |
| 120 | + super(getActivity(), R.layout.factory_reset_protection_policy_account, mAccounts); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public View getView(int position, View view, ViewGroup parent) { |
| 125 | + if (view == null) { |
| 126 | + view = getActivity().getLayoutInflater().inflate( |
| 127 | + R.layout.factory_reset_protection_policy_account, parent, false); |
| 128 | + } |
| 129 | + TextView listItemText = view.findViewById(R.id.frp_account); |
| 130 | + listItemText.setText(mAccounts.get(position)); |
| 131 | + |
| 132 | + Button removeAccountButton = view.findViewById(R.id.remove_account_button); |
| 133 | + removeAccountButton.setOnClickListener(v -> { |
| 134 | + mAccounts.remove(position); |
| 135 | + notifyDataSetChanged(); |
| 136 | + }); |
| 137 | + |
| 138 | + return view; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void onClick(View view) { |
| 144 | + switch (view.getId()) { |
| 145 | + case R.id.add_account_button: |
| 146 | + createAddAccountDialog(); |
| 147 | + break; |
| 148 | + case R.id.clear_frp_button: |
| 149 | + mDevicePolicyManager.setFactoryResetProtectionPolicy(mAdminComponentName, null); |
| 150 | + refreshUi(); |
| 151 | + showToast(R.string.cleared_factory_reset_protection_policy); |
| 152 | + break; |
| 153 | + case R.id.save_frp_button: |
| 154 | + mDevicePolicyManager.setFactoryResetProtectionPolicy(mAdminComponentName, |
| 155 | + new FactoryResetProtectionPolicy.Builder() |
| 156 | + .setFactoryResetProtectionAccounts(mAccounts) |
| 157 | + .setFactoryResetProtectionEnabled(mEnabled) |
| 158 | + .build()); |
| 159 | + showToast(R.string.saved_factory_reset_protection_policy); |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + public void createAddAccountDialog() { |
| 165 | + View view = LayoutInflater.from(getActivity()).inflate(R.layout.simple_edittext, null); |
| 166 | + final EditText input = view.findViewById(R.id.input); |
| 167 | + |
| 168 | + final AlertDialog dialog = new AlertDialog.Builder(getActivity()) |
| 169 | + .setTitle(R.string.add_account) |
| 170 | + .setView(view) |
| 171 | + .setPositiveButton(android.R.string.ok, null) |
| 172 | + .setNegativeButton(android.R.string.cancel, null) |
| 173 | + .create(); |
| 174 | + dialog.setOnShowListener( |
| 175 | + dialogInterface -> dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener( |
| 176 | + okButtonView -> { |
| 177 | + String item = input.getText().toString(); |
| 178 | + if (TextUtils.isEmpty(item)) { |
| 179 | + showToast(R.string.fail_to_add_account); |
| 180 | + return; |
| 181 | + } |
| 182 | + mAccountsAdapter.add(item); |
| 183 | + dialog.dismiss(); |
| 184 | + })); |
| 185 | + dialog.show(); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
| 190 | + if (parent.getId() == R.id.frp_enabled) { |
| 191 | + switch (pos) { |
| 192 | + case DISABLED: |
| 193 | + mEnabled = false; |
| 194 | + break; |
| 195 | + case ENABLED: |
| 196 | + mEnabled = true; |
| 197 | + break; |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public void onNothingSelected(AdapterView<?> adapterView) { |
| 204 | + // do nothing |
| 205 | + } |
| 206 | + |
| 207 | + private void showToast(@StringRes int stringResId) { |
| 208 | + Toast.makeText(getActivity(), stringResId, Toast.LENGTH_LONG).show(); |
| 209 | + } |
| 210 | + |
| 211 | +} |
0 commit comments