|
| 1 | +package com.android.settings.security; |
| 2 | + |
| 3 | +import android.app.AlertDialog; |
| 4 | +import android.content.Intent; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.util.Log; |
| 7 | + |
| 8 | +import androidx.annotation.DrawableRes; |
| 9 | +import androidx.annotation.Nullable; |
| 10 | +import androidx.annotation.StringRes; |
| 11 | + |
| 12 | +import com.android.internal.widget.LockPatternUtils; |
| 13 | +import com.android.internal.widget.LockscreenCredential; |
| 14 | +import com.android.settings.R; |
| 15 | +import com.android.settings.password.ChooseLockSettingsHelper; |
| 16 | +import com.google.android.setupdesign.GlifRecyclerLayout; |
| 17 | +import com.google.android.setupdesign.items.IItem; |
| 18 | +import com.google.android.setupdesign.items.Item; |
| 19 | +import com.google.android.setupdesign.items.ItemGroup; |
| 20 | +import com.google.android.setupdesign.items.RecyclerItemAdapter; |
| 21 | + |
| 22 | +public class DuressPasswordMainActivity extends DuressPasswordActivity implements RecyclerItemAdapter.OnItemSelectedListener { |
| 23 | + private static final String TAG = DuressPasswordMainActivity.class.getSimpleName(); |
| 24 | + private static final String KEY_USER_CREDENTIAL = "user_credential"; |
| 25 | + |
| 26 | + private GlifRecyclerLayout layout; |
| 27 | + private LockscreenCredential userCredential; |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 31 | + super.onCreate(savedInstanceState); |
| 32 | + |
| 33 | + var layout = new GlifRecyclerLayout(this); |
| 34 | + this.layout = layout; |
| 35 | + layout.setIcon(getDrawable(R.drawable.ic_lock)); |
| 36 | + layout.setHeaderText(R.string.duress_pwd_pref_title); |
| 37 | + adjustDescriptionStyle(layout); |
| 38 | + layout.setDescriptionText(R.string.duress_pwd_description); |
| 39 | + |
| 40 | + setContentView(layout); |
| 41 | + |
| 42 | + if (savedInstanceState != null) { |
| 43 | + userCredential = savedInstanceState.getParcelable(KEY_USER_CREDENTIAL, LockscreenCredential.class); |
| 44 | + } |
| 45 | + |
| 46 | + if (userCredential == null) { |
| 47 | + userCredential = LockscreenCredential.createNone(); |
| 48 | + |
| 49 | + var b = new ChooseLockSettingsHelper.Builder(this); |
| 50 | + b.setRequestCode(REQ_CODE_OBTAIN_USER_CREDENTIALS); |
| 51 | + b.setReturnCredentials(true); |
| 52 | + b.setForegroundOnly(true); |
| 53 | + b.show(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected boolean hasUserCredential() { |
| 59 | + return !userCredential.isNone(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void onSaveInstanceState(Bundle outState) { |
| 64 | + super.onSaveInstanceState(outState); |
| 65 | + outState.putParcelable(KEY_USER_CREDENTIAL, userCredential); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void onResume() { |
| 70 | + super.onResume(); |
| 71 | + updateActionList(); |
| 72 | + } |
| 73 | + |
| 74 | + private void updateActionList() { |
| 75 | + var g = new ItemGroup(); |
| 76 | + |
| 77 | + if (getLockPatternUtils().hasDuressCredentials()) { |
| 78 | + g.addChild(createItem(R.string.duress_pwd_action_update, R.drawable.ic_edit)); |
| 79 | + g.addChild(createItem(R.string.duress_pwd_action_delete, R.drawable.ic_delete)); |
| 80 | + } else { |
| 81 | + g.addChild(createItem(R.string.duress_pwd_action_add, R.drawable.ic_add_24dp)); |
| 82 | + } |
| 83 | + |
| 84 | + var adapter = new RecyclerItemAdapter(g); |
| 85 | + adapter.setOnItemSelectedListener(this); |
| 86 | + layout.setAdapter(adapter); |
| 87 | + } |
| 88 | + |
| 89 | + private Item createItem(@StringRes int title, @DrawableRes int icon) { |
| 90 | + var i = new Item(); |
| 91 | + i.setId(title); |
| 92 | + i.setTitle(getText(title)); |
| 93 | + i.setIcon(getDrawable(icon)); |
| 94 | + return i; |
| 95 | + } |
| 96 | + |
| 97 | + // RecyclerItemAdapter.OnItemSelectedListener |
| 98 | + @Override |
| 99 | + public void onItemSelected(IItem iitem) { |
| 100 | + Item item = (Item) iitem; |
| 101 | + int id = item.getId(); |
| 102 | + |
| 103 | + if (id == R.string.duress_pwd_action_add || id == R.string.duress_pwd_action_update) { |
| 104 | + var i = new Intent(this, DuressPasswordSetupActivity.class); |
| 105 | + i.putExtra(DuressPasswordSetupActivity.EXTRA_TITLE_TEXT, id); |
| 106 | + i.putExtra(DuressPasswordSetupActivity.EXTRA_USER_CREDENTIAL, userCredential); |
| 107 | + allowNextOnStop = true; |
| 108 | + startActivityForResult(i, REQ_CODE_SETUP); |
| 109 | + } else if (id == R.string.duress_pwd_action_delete) { |
| 110 | + var b = new AlertDialog.Builder(this); |
| 111 | + b.setMessage(R.string.duress_pwd_action_delete_confirmation); |
| 112 | + b.setPositiveButton(R.string.duress_pwd_delete_button, (dialog, which) -> { |
| 113 | + LockPatternUtils lpu = getLockPatternUtils(); |
| 114 | + try { |
| 115 | + lpu.deleteDuressCredentials(userCredential); |
| 116 | + } catch (Exception e) { |
| 117 | + Log.e(TAG, "deleteDuressCredentials failed", e); |
| 118 | + |
| 119 | + var d = new AlertDialog.Builder(this); |
| 120 | + d.setMessage(getString(R.string.duress_pwd_delete_error, e.toString())); |
| 121 | + d.setNeutralButton(R.string.duress_pwd_error_dialog_dismiss, null); |
| 122 | + d.show(); |
| 123 | + return; |
| 124 | + } |
| 125 | + updateActionList(); |
| 126 | + }); |
| 127 | + b.setNegativeButton(R.string.duress_pwd_cancel_button, null); |
| 128 | + b.show(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + static final int REQ_CODE_OBTAIN_USER_CREDENTIALS = 1; |
| 133 | + static final int REQ_CODE_SETUP = 2; |
| 134 | + |
| 135 | + @Override |
| 136 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 137 | + if (requestCode == REQ_CODE_SETUP) { |
| 138 | + if (resultCode == RESULT_CANCELED) { |
| 139 | + finish(); |
| 140 | + } |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + if (requestCode != REQ_CODE_OBTAIN_USER_CREDENTIALS) { |
| 145 | + throw new IllegalStateException(Integer.toString(resultCode)); |
| 146 | + } |
| 147 | + |
| 148 | + Log.d(TAG, "onActivityResult"); |
| 149 | + |
| 150 | + if (resultCode != RESULT_OK) { |
| 151 | + finish(); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + if (data == null) { |
| 156 | + throw new IllegalStateException("data == null"); |
| 157 | + } |
| 158 | + |
| 159 | + var credential = data.getParcelableExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD, LockscreenCredential.class); |
| 160 | + if (credential == null) { |
| 161 | + throw new IllegalStateException("no returned credential"); |
| 162 | + } |
| 163 | + userCredential = credential; |
| 164 | + } |
| 165 | +} |
0 commit comments