RequestCredentialsActivity.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Harikrishnan Rajan
  5. * Copyright (C) 2017
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see http://www.gnu.org/licenses/.
  19. *
  20. */
  21. package com.owncloud.android.ui.activity;
  22. import android.app.Activity;
  23. import android.app.KeyguardManager;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.os.Build;
  27. import android.os.SystemClock;
  28. import android.widget.Toast;
  29. import com.nextcloud.client.preferences.AppPreferencesImpl;
  30. import com.owncloud.android.R;
  31. import com.owncloud.android.lib.common.utils.Log_OC;
  32. import com.owncloud.android.utils.DeviceCredentialUtils;
  33. import com.owncloud.android.utils.DisplayUtils;
  34. import androidx.annotation.RequiresApi;
  35. /**
  36. * Dummy activity that is used to handle the device's default authentication workflow.
  37. */
  38. @RequiresApi(Build.VERSION_CODES.M)
  39. public class RequestCredentialsActivity extends Activity {
  40. private static final String TAG = RequestCredentialsActivity.class.getSimpleName();
  41. public final static String KEY_CHECK_RESULT = "KEY_CHECK_RESULT";
  42. public final static int KEY_CHECK_RESULT_TRUE = 1;
  43. public final static int KEY_CHECK_RESULT_FALSE = 0;
  44. public final static int KEY_CHECK_RESULT_CANCEL = -1;
  45. private static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 1;
  46. @Override
  47. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  48. if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
  49. if (resultCode == Activity.RESULT_OK) {
  50. AppPreferencesImpl.fromContext(this).setLockTimestamp(SystemClock.elapsedRealtime());
  51. finishWithResult(KEY_CHECK_RESULT_TRUE);
  52. } else if (resultCode == Activity.RESULT_CANCELED) {
  53. finishWithResult(KEY_CHECK_RESULT_CANCEL);
  54. } else {
  55. Toast.makeText(this, R.string.default_credentials_wrong, Toast.LENGTH_SHORT).show();
  56. requestCredentials();
  57. }
  58. }
  59. }
  60. @Override
  61. protected void onResume() {
  62. super.onResume();
  63. if (DeviceCredentialUtils.areCredentialsAvailable(this)) {
  64. requestCredentials();
  65. } else {
  66. DisplayUtils.showSnackMessage(this, R.string.prefs_lock_device_credentials_not_setup);
  67. finishWithResult(KEY_CHECK_RESULT_CANCEL);
  68. }
  69. }
  70. private void requestCredentials() {
  71. KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
  72. if (keyguardManager != null) {
  73. Intent i = keyguardManager.createConfirmDeviceCredentialIntent(null, null);
  74. i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  75. startActivityForResult(i, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
  76. } else {
  77. Log_OC.e(TAG, "Keyguard manager is null");
  78. finishWithResult(KEY_CHECK_RESULT_FALSE);
  79. }
  80. }
  81. private void finishWithResult(int success) {
  82. Intent resultIntent = new Intent();
  83. resultIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
  84. resultIntent.putExtra(KEY_CHECK_RESULT, success);
  85. setResult(Activity.RESULT_OK, resultIntent);
  86. finish();
  87. }
  88. }