diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainIntroActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainIntroActivity.java index d45abf60..6c82517f 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainIntroActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainIntroActivity.java @@ -1,15 +1,22 @@ package org.shadowice.flocke.andotp.Activities; +import android.app.KeyguardManager; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.design.widget.TextInputEditText; +import android.text.Editable; +import android.text.TextWatcher; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; +import android.widget.Toast; import com.heinrichreimersoftware.materialintro.app.IntroActivity; import com.heinrichreimersoftware.materialintro.app.SlideFragment; @@ -91,6 +98,14 @@ public class MainIntroActivity extends IntroActivity { encryptionChangedCallback = cb; } + private void generateSelectionMapping() { + String[] encValues = getResources().getStringArray(R.array.settings_values_encryption); + + selectionMapping = new SparseArray<>(); + for (int i = 0; i < encValues.length; i++) + selectionMapping.put(i, Constants.EncryptionType.valueOf(encValues[i].toUpperCase())); + } + @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -99,11 +114,7 @@ public class MainIntroActivity extends IntroActivity { selection = root.findViewById(R.id.introEncryptionSelection); desc = root.findViewById(R.id.introEncryptionDesc); - String[] encValues = getResources().getStringArray(R.array.settings_values_encryption); - - selectionMapping = new SparseArray<>(); - for (int i = 0; i < encValues.length; i++) - selectionMapping.put(i, Constants.EncryptionType.valueOf(encValues[i].toUpperCase())); + generateSelectionMapping(); selection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override @@ -137,6 +148,10 @@ public class MainIntroActivity extends IntroActivity { private TextView desc = null; private Spinner selection = null; + private TextView authWarnings = null; + private LinearLayout credentialsLayout = null; + private TextInputEditText passwordInput = null; + private EditText passwordConfirm = null; private SparseArray selectionMapping; @@ -159,6 +174,27 @@ public class MainIntroActivity extends IntroActivity { } } + private void generateSelectionMapping() { + Constants.AuthMethod[] authValues = Constants.AuthMethod.values(); + + selectionMapping = new SparseArray<>(); + for (int i = 0; i < authValues.length; i++) + selectionMapping.put(i, authValues[i]); + } + + private void displayWarning(int resId) { + displayWarning(getString(resId)); + } + + private void displayWarning(String warning) { + authWarnings.setVisibility(View.VISIBLE); + authWarnings.setText(warning); + } + + private void hideWarning() { + authWarnings.setVisibility(View.GONE); + } + @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -166,14 +202,14 @@ public class MainIntroActivity extends IntroActivity { desc = root.findViewById(R.id.introAuthDesc); selection = root.findViewById(R.id.introAuthSelection); + authWarnings = root.findViewById(R.id.introAuthWarnings); + credentialsLayout = root.findViewById(R.id.introCredentialsLayout); + passwordInput = root.findViewById(R.id.introPasswordEdit); + passwordConfirm = root.findViewById(R.id.introPasswordConfirm); - Constants.AuthMethod[] authValues = Constants.AuthMethod.values(); - String[] authEntries = getResources().getStringArray(R.array.settings_entries_auth); - - selectionMapping = new SparseArray<>(); - for (int i = 0; i < authValues.length; i++) - selectionMapping.put(i, authValues[i]); + generateSelectionMapping(); + final String[] authEntries = getResources().getStringArray(R.array.settings_entries_auth); ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(getIntroActivity(), android.R.layout.simple_spinner_item, authEntries) { @Override public boolean isEnabled(int position){ @@ -198,7 +234,74 @@ public class MainIntroActivity extends IntroActivity { spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); selection.setAdapter(spinnerArrayAdapter); + selection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView adapterView, View view, int i, long l) { + Constants.AuthMethod authMethod = selectionMapping.get(i); + + if (authMethod == Constants.AuthMethod.PASSWORD || authMethod == Constants.AuthMethod.PIN) { + credentialsLayout.setVisibility(View.VISIBLE); + } else { + credentialsLayout.setVisibility(View.INVISIBLE); + } + + updateNavigation(); + } + + @Override + public void onNothingSelected(AdapterView adapterView) { + } + }); + + TextWatcher textWatcher = new TextWatcher() { + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + updateNavigation(); + } + + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + } + + @Override + public void afterTextChanged(Editable editable) { + } + }; + + passwordInput.addTextChangedListener(textWatcher); + passwordConfirm.addTextChangedListener(textWatcher); + return root; } + + @Override + public boolean canGoForward() { + Constants.AuthMethod authMethod = selectionMapping.get(selection.getSelectedItemPosition()); + + if (authMethod == Constants.AuthMethod.PIN || authMethod == Constants.AuthMethod.PASSWORD) { + String password = passwordInput.getText().toString(); + String confirm = passwordConfirm.getText().toString(); + + return ! password.isEmpty() && ! confirm.isEmpty() && confirm.equals(password); + } else if (authMethod == Constants.AuthMethod.DEVICE) { + KeyguardManager km = (KeyguardManager) getContext().getSystemService(KEYGUARD_SERVICE); + + if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { + displayWarning(R.string.settings_toast_auth_device_pre_lollipop); + return false; + } else if (! km.isKeyguardSecure()) { + displayWarning(R.string.settings_toast_auth_device_not_secure); + return false; + } + + hideWarning(); + + return true; + } else { + hideWarning(); + + return true; + } + } } } diff --git a/app/src/main/res/layout/component_intro_authentication.xml b/app/src/main/res/layout/component_intro_authentication.xml index dc6cd41e..5f0d65f8 100644 --- a/app/src/main/res/layout/component_intro_authentication.xml +++ b/app/src/main/res/layout/component_intro_authentication.xml @@ -1,79 +1,85 @@ - - - - - - - + android:overScrollMode="never"> - - - - - - - - + android:layout_gravity="center" + android:gravity="center" + android:padding="@dimen/activity_margin_large"> + + + + + + + android:textStyle="bold" /> + + + + + + + + + + + + - \ No newline at end of file + \ No newline at end of file