|
1 | 1 | package nl.jpelgrm.movienotifier.ui.settings; |
2 | 2 |
|
| 3 | +import android.app.KeyguardManager; |
3 | 4 | import android.content.Context; |
4 | 5 | import android.content.Intent; |
5 | 6 | import android.content.SharedPreferences; |
6 | 7 | import android.os.AsyncTask; |
7 | 8 | import android.os.Build; |
8 | 9 | import android.os.Bundle; |
9 | 10 | import android.provider.Settings; |
10 | | -import android.util.Log; |
11 | 11 | import android.view.LayoutInflater; |
12 | 12 | import android.view.View; |
13 | 13 | import android.view.ViewGroup; |
|
47 | 47 | import retrofit2.Callback; |
48 | 48 | import retrofit2.Response; |
49 | 49 |
|
| 50 | +import static android.app.Activity.RESULT_OK; |
| 51 | + |
50 | 52 | public class SettingsAccountOverviewFragment extends Fragment { |
| 53 | + public static final int INTENT_AUTHENTICATE_PASSWORD = 160; |
| 54 | + public static final int INTENT_AUTHENTICATE_DELETE = 161; |
| 55 | + |
51 | 56 | @BindView(R.id.accountCoordinator) CoordinatorLayout coordinator; |
52 | 57 |
|
53 | 58 | @BindView(R.id.progress) ProgressBar progress; |
@@ -120,7 +125,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat |
120 | 125 |
|
121 | 126 | accountSwitch.setOnClickListener(v -> switchToThis()); |
122 | 127 | accountName.setOnClickListener(v -> editDetail(SettingsAccountUpdateFragment.UpdateMode.NAME)); |
123 | | - accountPassword.setOnClickListener(v -> editDetail(SettingsAccountUpdateFragment.UpdateMode.PASSWORD)); |
| 128 | + accountPassword.setOnClickListener(v -> editPassword()); |
124 | 129 | accountLogout.setOnClickListener(v -> logout()); |
125 | 130 | accountDelete.setOnClickListener(v -> delete()); |
126 | 131 | accountSwitch.setVisibility(isCurrentUser ? View.GONE : View.VISIBLE); |
@@ -179,6 +184,22 @@ private void editDetail(SettingsAccountUpdateFragment.UpdateMode mode) { |
179 | 184 | ((SettingsActivity) getActivity()).editUserDetail(user.getId(), mode); |
180 | 185 | } |
181 | 186 |
|
| 187 | + private void editPassword() { |
| 188 | + if(getContext() != null) { |
| 189 | + KeyguardManager keyguardManager = (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE); |
| 190 | + if(keyguardManager != null) { |
| 191 | + Intent authenticationIntent = keyguardManager.createConfirmDeviceCredentialIntent(null, null); |
| 192 | + if(authenticationIntent != null) { |
| 193 | + startActivityForResult(authenticationIntent, INTENT_AUTHENTICATE_PASSWORD); |
| 194 | + } else { |
| 195 | + editDetail(SettingsAccountUpdateFragment.UpdateMode.PASSWORD); |
| 196 | + } |
| 197 | + } else { |
| 198 | + editDetail(SettingsAccountUpdateFragment.UpdateMode.PASSWORD); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
182 | 203 | private void togglePushNotifications() { |
183 | 204 | String token = notificationSettings.getString("token", ""); |
184 | 205 | User toUpdate = new User(); |
@@ -400,6 +421,22 @@ private void logoutLocal() { |
400 | 421 | } |
401 | 422 |
|
402 | 423 | private void delete() { |
| 424 | + if(getContext() != null) { |
| 425 | + KeyguardManager keyguardManager = (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE); |
| 426 | + if(keyguardManager != null) { |
| 427 | + Intent authenticationIntent = keyguardManager.createConfirmDeviceCredentialIntent(null, null); |
| 428 | + if(authenticationIntent != null) { |
| 429 | + startActivityForResult(authenticationIntent, INTENT_AUTHENTICATE_DELETE); |
| 430 | + } else { |
| 431 | + doDelete(); |
| 432 | + } |
| 433 | + } else { |
| 434 | + doDelete(); |
| 435 | + } |
| 436 | + } |
| 437 | + } |
| 438 | + |
| 439 | + private void doDelete() { |
403 | 440 | error.setVisibility(View.GONE); |
404 | 441 |
|
405 | 442 | new AlertDialog.Builder(getContext()).setMessage(R.string.user_settings_security_delete_confirm).setPositiveButton(R.string.yes, (dialogInterface, i) -> { |
@@ -487,6 +524,27 @@ private void setFieldsEnabled(boolean enabled) { |
487 | 524 | notificationsEmailAddress.setClickable(enabled); |
488 | 525 | } |
489 | 526 |
|
| 527 | + @Override |
| 528 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 529 | + switch(requestCode) { |
| 530 | + case INTENT_AUTHENTICATE_PASSWORD: |
| 531 | + case INTENT_AUTHENTICATE_DELETE: |
| 532 | + if(resultCode == RESULT_OK) { |
| 533 | + if(requestCode == INTENT_AUTHENTICATE_PASSWORD) { |
| 534 | + editDetail(SettingsAccountUpdateFragment.UpdateMode.PASSWORD); |
| 535 | + } else { |
| 536 | + doDelete(); |
| 537 | + } |
| 538 | + } else { |
| 539 | + Snackbar.make(coordinator, R.string.user_settings_security_authenticate, Snackbar.LENGTH_LONG).show(); |
| 540 | + } |
| 541 | + break; |
| 542 | + default: |
| 543 | + super.onActivityResult(requestCode, resultCode, data); |
| 544 | + break; |
| 545 | + } |
| 546 | + } |
| 547 | + |
490 | 548 | private interface OnUpdatedListener { |
491 | 549 | void onResult(boolean success); |
492 | 550 | } |
|
0 commit comments