RequestCredentialsActivity.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.support.annotation.RequiresApi;
  28. import android.widget.Toast;
  29. import com.owncloud.android.R;
  30. import com.owncloud.android.lib.common.utils.Log_OC;
  31. import com.owncloud.android.utils.DeviceCredentialUtils;
  32. import com.owncloud.android.utils.DisplayUtils;
  33. /**
  34. * Dummy activity that is used to handle the device's default authentication workflow.
  35. */
  36. @RequiresApi(Build.VERSION_CODES.M)
  37. public class RequestCredentialsActivity extends Activity {
  38. private static final String TAG = RequestCredentialsActivity.class.getSimpleName();
  39. public final static String KEY_CHECK_RESULT = "KEY_CHECK_RESULT";
  40. public final static int KEY_CHECK_RESULT_TRUE = 1;
  41. public final static int KEY_CHECK_RESULT_FALSE = 0;
  42. public final static int KEY_CHECK_RESULT_CANCEL = -1;
  43. private static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 1;
  44. @Override
  45. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  46. if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
  47. if (resultCode == Activity.RESULT_OK && DeviceCredentialUtils.tryEncrypt(getApplicationContext())) {
  48. finishWithResult(KEY_CHECK_RESULT_TRUE);
  49. } else if (resultCode == Activity.RESULT_CANCELED) {
  50. finishWithResult(KEY_CHECK_RESULT_CANCEL);
  51. } else {
  52. Toast.makeText(this, R.string.default_credentials_wrong, Toast.LENGTH_SHORT).show();
  53. requestCredentials();
  54. }
  55. }
  56. }
  57. @Override
  58. protected void onResume() {
  59. super.onResume();
  60. if (DeviceCredentialUtils.areCredentialsAvailable(this)) {
  61. DeviceCredentialUtils.createKey(getApplicationContext());
  62. requestCredentials();
  63. } else {
  64. DisplayUtils.showSnackMessage(this, R.string.prefs_lock_device_credentials_not_setup);
  65. finishWithResult(KEY_CHECK_RESULT_TRUE);
  66. }
  67. }
  68. private void requestCredentials() {
  69. KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
  70. if (keyguardManager != null) {
  71. Intent i = keyguardManager.createConfirmDeviceCredentialIntent(null, null);
  72. startActivityForResult(i, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
  73. } else {
  74. Log_OC.e(TAG, "Keyguard manager is null");
  75. finishWithResult(KEY_CHECK_RESULT_FALSE);
  76. }
  77. }
  78. private void finishWithResult(int success) {
  79. Intent resultIntent = new Intent();
  80. resultIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
  81. resultIntent.putExtra(KEY_CHECK_RESULT, success);
  82. setResult(Activity.RESULT_OK, resultIntent);
  83. finish();
  84. }
  85. }