浏览代码

Fix crash when no credentials are available

Hari 7 年之前
父节点
当前提交
6625d4f3b7

+ 23 - 20
src/main/java/com/owncloud/android/ui/activity/Preferences.java

@@ -505,7 +505,7 @@ public class Preferences extends PreferenceActivity
                 accentColor));
 
         boolean fPassCodeEnabled = getResources().getBoolean(R.bool.passcode_enabled);
-        boolean fDeviceCredentialsEnabled = getResources().getBoolean(R.bool.device_credentials);
+        boolean fDeviceCredentialsEnabled = getResources().getBoolean(R.bool.device_credentials_enabled);
         boolean fShowHiddenFilesEnabled = getResources().getBoolean(R.bool.show_hidden_files_enabled);
         boolean fSyncedFolderLightEnabled = getResources().getBoolean(R.bool.syncedFolder_light);
 
@@ -577,30 +577,33 @@ public class Preferences extends PreferenceActivity
     private void setupDeviceCredentialsPreference(PreferenceCategory preferenceCategoryDetails,
                                                   boolean deviceCredentialsEnabled) {
         SwitchPreference useDeviceCredentials = (SwitchPreference) findPreference(PREFERENCE_USE_DEVICE_CREDENTIALS);
+
+        final Activity activity = this;
+
         if (useDeviceCredentials != null && deviceCredentialsEnabled && Build.VERSION.SDK_INT >=
                 Build.VERSION_CODES.M) {
-            if (!DeviceCredentialUtils.areCredentialsAvailable(getApplicationContext())) {
-                DisplayUtils.showSnackMessage(this, R.string.prefs_device_credentials_not_setup);
-                useDeviceCredentials.setChecked(false);
-            } else {
-                useDeviceCredentials.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
-                    @Override
-                    public boolean onPreferenceChange(Preference preference, Object newValue) {
-                        Boolean incoming = (Boolean) newValue;
-                        if (incoming) {
-                            SharedPreferences appPrefs = PreferenceManager
-                                    .getDefaultSharedPreferences(getApplicationContext());
-                            SharedPreferences.Editor editor = appPrefs.edit();
-                            editor.putBoolean(PREFERENCE_USE_DEVICE_CREDENTIALS, true).apply();
-                            return true;
-                        } else {
-                            Intent i = new Intent(getApplicationContext(), RequestCredentialsActivity.class);
-                            startActivityForResult(i, ACTION_CONFIRM_DEVICE_CREDENTIALS);
+            useDeviceCredentials.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
+                @Override
+                public boolean onPreferenceChange(Preference preference, Object newValue) {
+                    Boolean incoming = (Boolean) newValue;
+                    if (incoming) {
+                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
+                                !DeviceCredentialUtils.areCredentialsAvailable(getApplicationContext())) {
+                            DisplayUtils
+                                    .showSnackMessage(activity, R.string.prefs_device_credentials_not_setup);
                             return false;
                         }
+                        SharedPreferences appPrefs = PreferenceManager
+                                .getDefaultSharedPreferences(getApplicationContext());
+                        appPrefs.edit().putBoolean(PREFERENCE_USE_DEVICE_CREDENTIALS, true).apply();
+                        return true;
+                    } else {
+                        Intent i = new Intent(getApplicationContext(), RequestCredentialsActivity.class);
+                        startActivityForResult(i, ACTION_CONFIRM_DEVICE_CREDENTIALS);
+                        return false;
                     }
-                });
-            }
+                }
+            });
         } else {
             preferenceCategoryDetails.removePreference(useDeviceCredentials);
         }

+ 9 - 2
src/main/java/com/owncloud/android/ui/activity/RequestCredentialsActivity.java

@@ -32,6 +32,7 @@ import com.owncloud.android.R;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.utils.AnalyticsUtils;
 import com.owncloud.android.utils.DeviceCredentialUtils;
+import com.owncloud.android.utils.DisplayUtils;
 
 /**
  * Dummy activity that is used to handle the device's default authentication workflow.
@@ -62,8 +63,14 @@ public class RequestCredentialsActivity extends Activity {
     protected void onResume() {
         super.onResume();
         AnalyticsUtils.setCurrentScreenName(this, SCREEN_NAME, TAG);
-        DeviceCredentialUtils.createKey(getApplicationContext());
-        requestCredentials();
+
+        if (DeviceCredentialUtils.areCredentialsAvailable(this)) {
+            DeviceCredentialUtils.createKey(getApplicationContext());
+            requestCredentials();
+        } else {
+            DisplayUtils.showSnackMessage(this, R.string.prefs_device_credentials_not_setup);
+            finishWithResult(true);
+        }
     }
 
     private void requestCredentials() {