Merge remote-tracking branch 'origin/master'

This commit is contained in:
hvarona 2018-05-16 22:07:58 -04:00
commit dd78076906
11 changed files with 169 additions and 211 deletions

View file

@ -76,7 +76,7 @@ public class IntroActivity extends AppCompatActivity {
}
});
this.getApplication().registerActivityLifecycleCallbacks(new CrystalSecurityMonitor(this));
this.getApplication().registerActivityLifecycleCallbacks(CrystalSecurityMonitor.getInstance(this));
//Checks if the user has any seed created

View file

@ -19,6 +19,7 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
import cy.agorise.crystalwallet.BuildConfig;
import cy.agorise.crystalwallet.R;
import cy.agorise.crystalwallet.application.CrystalSecurityMonitor;
import cy.agorise.crystalwallet.fragments.AccountsSettingsFragment;
import cy.agorise.crystalwallet.fragments.BackupsSettingsFragment;
import cy.agorise.crystalwallet.fragments.BalanceFragment;

View file

@ -16,6 +16,7 @@ import java.util.List;
import cy.agorise.crystalwallet.activities.PatternRequestActivity;
import cy.agorise.crystalwallet.activities.PinRequestActivity;
import cy.agorise.crystalwallet.models.GeneralSetting;
import cy.agorise.crystalwallet.notifiers.CrystalWalletNotifier;
import cy.agorise.crystalwallet.viewmodels.GeneralSettingListViewModel;
/**
@ -27,44 +28,77 @@ public class CrystalSecurityMonitor implements Application.ActivityLifecycleCall
private String passwordEncrypted;
private String patternEncrypted;
public CrystalSecurityMonitor(final FragmentActivity fragmentActivity){
GeneralSettingListViewModel generalSettingListViewModel = ViewModelProviders.of(fragmentActivity).get(GeneralSettingListViewModel.class);
LiveData<List<GeneralSetting>> generalSettingsLiveData = generalSettingListViewModel.getGeneralSettingList();
private static CrystalSecurityMonitor instance;
private GeneralSettingListViewModel generalSettingListViewModel;
generalSettingsLiveData.observe(fragmentActivity, new Observer<List<GeneralSetting>>() {
@Override
public void onChanged(@Nullable List<GeneralSetting> generalSettings) {
boolean founded = false;
passwordEncrypted = "";
private CrystalSecurityMonitor(final FragmentActivity fragmentActivity){
//For security reasons, this code is not asynchronous because the pattern or password data
//must be retrieved before the principal activity starts
generalSettingListViewModel = ViewModelProviders.of(fragmentActivity).get(GeneralSettingListViewModel.class);
if (generalSettings != null){
for (GeneralSetting generalSetting:generalSettings) {
if (generalSetting.getName().equals(GeneralSetting.SETTING_PASSWORD)){
if (!generalSetting.getValue().isEmpty()){
passwordEncrypted = generalSetting.getValue();
callPasswordRequest(fragmentActivity);
}
break;
} else if (generalSetting.getName().equals(GeneralSetting.SETTING_PATTERN)){
if (!generalSetting.getValue().isEmpty()){
patternEncrypted = generalSetting.getValue();
callPasswordRequest(fragmentActivity);
}
break;
}
}
}
}
});
this.passwordEncrypted = "";
this.patternEncrypted = "";
GeneralSetting passwordGeneralSetting = generalSettingListViewModel.getGeneralSettingByName(GeneralSetting.SETTING_PASSWORD);;
GeneralSetting patternGeneralSetting = generalSettingListViewModel.getGeneralSettingByName(GeneralSetting.SETTING_PATTERN);;
if (passwordGeneralSetting != null){
this.passwordEncrypted = passwordGeneralSetting.getValue();
}
if (patternGeneralSetting != null){
this.patternEncrypted = patternGeneralSetting.getValue();
}
}
public static CrystalSecurityMonitor getInstance(final FragmentActivity fragmentActivity){
if (instance == null){
instance = new CrystalSecurityMonitor(fragmentActivity);
}
return instance;
}
public void clearSecurity(){
this.patternEncrypted = "";
this.passwordEncrypted = "";
generalSettingListViewModel.deleteGeneralSettingByName(GeneralSetting.SETTING_PASSWORD);
generalSettingListViewModel.deleteGeneralSettingByName(GeneralSetting.SETTING_PATTERN);
}
public void setPasswordSecurity(String password){
clearSecurity();
this.passwordEncrypted = password;
GeneralSetting passwordGeneralSetting = new GeneralSetting();
passwordGeneralSetting.setName(GeneralSetting.SETTING_PASSWORD);
passwordGeneralSetting.setValue(password);
generalSettingListViewModel.saveGeneralSetting(passwordGeneralSetting);
}
public void setPatternEncrypted(String pattern){
clearSecurity();
this.patternEncrypted = pattern;
GeneralSetting patternGeneralSetting = new GeneralSetting();
patternGeneralSetting.setName(GeneralSetting.SETTING_PATTERN);
patternGeneralSetting.setValue(pattern);
generalSettingListViewModel.saveGeneralSetting(patternGeneralSetting);
}
public String actualSecurity(){
if ((this.patternEncrypted != null) && (!this.patternEncrypted.equals(""))){
return GeneralSetting.SETTING_PATTERN;
} else if ((this.passwordEncrypted != null) && (!this.passwordEncrypted.equals(""))){
return GeneralSetting.SETTING_PASSWORD;
}
return "";
}
@Override
public void onActivityStarted(Activity activity) {
if (numStarted == 0) {
if (
((this.passwordEncrypted != null) && (!this.passwordEncrypted.equals("")))
|| ((this.patternEncrypted != null) && (!this.patternEncrypted.equals("")))
) {
if (!actualSecurity().equals("")){
callPasswordRequest(activity);
}
}
@ -75,10 +109,7 @@ public class CrystalSecurityMonitor implements Application.ActivityLifecycleCall
public void onActivityStopped(Activity activity) {
numStarted--;
if (numStarted == 0) {
if (
((this.passwordEncrypted != null) && (!this.passwordEncrypted.equals("")))
|| ((this.patternEncrypted != null) && (!this.patternEncrypted.equals("")))
) {
if (!actualSecurity().equals("")){
callPasswordRequest(activity);
}
}

View file

@ -2,6 +2,7 @@ package cy.agorise.crystalwallet.dao;
import android.arch.lifecycle.LiveData;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;
@ -32,4 +33,10 @@ public interface GeneralSettingDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
public long insertGeneralSetting(GeneralSetting generalSetting);
@Delete
public void deleteGeneralSettings(GeneralSetting... generalSettings);
@Query("DELETE FROM general_setting WHERE name = :name")
public void deleteByName(String name);
}

View file

@ -1,13 +1,23 @@
package cy.agorise.crystalwallet.fragments;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import butterknife.ButterKnife;
import cy.agorise.crystalwallet.R;
import cy.agorise.crystalwallet.application.CrystalSecurityMonitor;
import cy.agorise.crystalwallet.models.GeneralSetting;
import cy.agorise.crystalwallet.viewmodels.GeneralSettingListViewModel;
import cy.agorise.crystalwallet.viewmodels.validators.PinSecurityValidator;
/**
* Created by xd on 1/18/18.
@ -15,6 +25,11 @@ import cy.agorise.crystalwallet.R;
public class NoneSecurityFragment extends Fragment {
GeneralSetting passwordGeneralSetting = new GeneralSetting();
GeneralSetting patternGeneralSetting = new GeneralSetting();
GeneralSettingListViewModel generalSettingListViewModel;
public NoneSecurityFragment() {
// Required empty public constructor
}
@ -33,6 +48,39 @@ public class NoneSecurityFragment extends Fragment {
View v = inflater.inflate(R.layout.fragment_none_security, container, false);
ButterKnife.bind(this, v);
generalSettingListViewModel = ViewModelProviders.of(this).get(GeneralSettingListViewModel.class);
LiveData<List<GeneralSetting>> generalSettingsLiveData = generalSettingListViewModel.getGeneralSettingList();
generalSettingsLiveData.observe(this, new Observer<List<GeneralSetting>>() {
@Override
public void onChanged(@Nullable List<GeneralSetting> generalSettings) {
if (generalSettings != null){
for (GeneralSetting generalSetting:generalSettings) {
if (generalSetting.getName().equals(GeneralSetting.SETTING_PASSWORD)){
passwordGeneralSetting = generalSetting;
} else if (generalSetting.getName().equals(GeneralSetting.SETTING_PATTERN)){
patternGeneralSetting = generalSetting;
}
}
}
}
});
return v;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
setSecurityOff();
}
}
public void setSecurityOff(){
generalSettingListViewModel.deleteGeneralSettings(passwordGeneralSetting,patternGeneralSetting);
CrystalSecurityMonitor.getInstance(null).setPasswordSecurity("");
CrystalSecurityMonitor.getInstance(null).setPatternEncrypted("");
}
}

View file

@ -23,6 +23,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnTextChanged;
import cy.agorise.crystalwallet.R;
import cy.agorise.crystalwallet.application.CrystalSecurityMonitor;
import cy.agorise.crystalwallet.models.GeneralSetting;
import cy.agorise.crystalwallet.util.PasswordManager;
import cy.agorise.crystalwallet.viewmodels.GeneralSettingListViewModel;
@ -36,10 +37,6 @@ import cy.agorise.crystalwallet.viewmodels.validators.validationfields.Validatio
public class PatternSecurityFragment extends Fragment {
GeneralSettingListViewModel generalSettingListViewModel;
GeneralSetting patternGeneralSetting;
//PatternSecurityValidator patternSecurityValidator;
@BindView(R.id.patternLockView)
PatternLockView patternLockView;
@BindView(R.id.tvPatternText)
@ -66,35 +63,7 @@ public class PatternSecurityFragment extends Fragment {
View v = inflater.inflate(R.layout.fragment_pattern_security, container, false);
ButterKnife.bind(this, v);
generalSettingListViewModel = ViewModelProviders.of(this).get(GeneralSettingListViewModel.class);
LiveData<List<GeneralSetting>> generalSettingsLiveData = generalSettingListViewModel.getGeneralSettingList();
generalSettingsLiveData.observe(this, new Observer<List<GeneralSetting>>() {
@Override
public void onChanged(@Nullable List<GeneralSetting> generalSettings) {
boolean founded = false;
if (generalSettings != null){
for (GeneralSetting generalSetting:generalSettings) {
if (generalSetting.getName().equals(GeneralSetting.SETTING_PATTERN)){
founded = true;
if (!generalSetting.getValue().isEmpty()){
patternGeneralSetting = generalSetting;
showEnterPatternUI();
} else {
showNewPatternUI();
}
break;
}
}
if (!founded){
showNewPatternUI();
}
} else {
showNewPatternUI();
}
}
});
showNewPatternUI();
return v;
}
@ -166,7 +135,7 @@ public class PatternSecurityFragment extends Fragment {
public void onComplete(List<PatternLockView.Dot> pattern) {
if (patternEntered.equals(patternToString(pattern))){
savePattern(patternEntered);
showEnterPatternUI();
showNewPatternUI();
}
}
@ -180,44 +149,7 @@ public class PatternSecurityFragment extends Fragment {
public void savePattern(String pattern){
String patternEncripted = PasswordManager.encriptPassword(pattern);
if (patternGeneralSetting == null) {
patternGeneralSetting = new GeneralSetting();
patternGeneralSetting.setName(GeneralSetting.SETTING_PATTERN);
}
patternGeneralSetting.setValue(patternEncripted);
generalSettingListViewModel.saveGeneralSetting(patternGeneralSetting);
}
public void showEnterPatternUI(){
removePatternListener();
patternLockView.clearPattern();
tvPatternText.setText("Enter old pattern");
actualPatternListener = new PatternLockViewListener() {
@Override
public void onStarted() {
}
@Override
public void onProgress(List<PatternLockView.Dot> progressPattern) {
}
@Override
public void onComplete(List<PatternLockView.Dot> pattern) {
if (PasswordManager.checkPassword(patternGeneralSetting.getValue(),patternToString(pattern))){
showNewPatternUI();
}
}
@Override
public void onCleared() {
}
};
patternLockView.addPatternLockListener(actualPatternListener);
CrystalSecurityMonitor.getInstance(null).setPatternEncrypted(patternEncripted);
CrystalSecurityMonitor.getInstance(null).callPasswordRequest(this.getActivity());
}
}

View file

@ -22,6 +22,7 @@ import butterknife.ButterKnife;
import butterknife.OnTextChanged;
import cy.agorise.crystalwallet.R;
import cy.agorise.crystalwallet.activities.CreateSeedActivity;
import cy.agorise.crystalwallet.application.CrystalSecurityMonitor;
import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.models.GeneralSetting;
import cy.agorise.crystalwallet.util.PasswordManager;
@ -36,17 +37,11 @@ import cy.agorise.crystalwallet.viewmodels.validators.validationfields.Validatio
public class PinSecurityFragment extends Fragment implements UIValidatorListener {
@BindView(R.id.tvCurrentPin)
TextView tvCurrentPin;
@BindView(R.id.etCurrentPin)
EditText etCurrentPin;
@BindView(R.id.etNewPin)
EditText etNewPin;
@BindView(R.id.etConfirmPin)
EditText etConfirmPin;
@BindView(R.id.tvCurrentPinError)
TextView tvCurrentPinError;
@BindView(R.id.tvNewPinError)
TextView tvNewPinError;
@BindView(R.id.tvConfirmPinError)
@ -77,55 +72,12 @@ public class PinSecurityFragment extends Fragment implements UIValidatorListener
generalSettingListViewModel = ViewModelProviders.of(this).get(GeneralSettingListViewModel.class);
LiveData<List<GeneralSetting>> generalSettingsLiveData = generalSettingListViewModel.getGeneralSettingList();
pinSecurityValidator = new PinSecurityValidator(this.getContext(), etCurrentPin, etNewPin, etConfirmPin);
pinSecurityValidator = new PinSecurityValidator(this.getContext(), etNewPin, etConfirmPin);
pinSecurityValidator.setListener(this);
generalSettingsLiveData.observe(this, new Observer<List<GeneralSetting>>() {
@Override
public void onChanged(@Nullable List<GeneralSetting> generalSettings) {
boolean founded = false;
if (generalSettings != null){
for (GeneralSetting generalSetting:generalSettings) {
if (generalSetting.getName().equals(GeneralSetting.SETTING_PASSWORD)){
founded = true;
if (!generalSetting.getValue().isEmpty()){
passwordGeneralSetting = generalSetting;
showCurrentPinUI(true);
} else {
showCurrentPinUI(false);
}
break;
}
}
if (!founded){
showCurrentPinUI(false);
}
} else {
showCurrentPinUI(false);
}
}
});
return v;
}
public void showCurrentPinUI(Boolean visible){
if (visible){
tvCurrentPin.setVisibility(View.VISIBLE);
etCurrentPin.setVisibility(View.VISIBLE);
} else {
tvCurrentPin.setVisibility(View.GONE);
etCurrentPin.setVisibility(View.GONE);
}
}
@OnTextChanged(value = R.id.etCurrentPin,
callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void afterCurrentPinChanged(Editable editable) {
this.pinSecurityValidator.validate();
}
@OnTextChanged(value = R.id.etNewPin,
callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void afterNewPinChanged(Editable editable) {
@ -139,9 +91,6 @@ public class PinSecurityFragment extends Fragment implements UIValidatorListener
}
public void clearFields(){
if (!this.etCurrentPin.getText().toString().equals("")) {
this.etCurrentPin.setText("");
}
if (!this.etNewPin.getText().toString().equals("")) {
this.etNewPin.setText("");
}
@ -157,9 +106,7 @@ public class PinSecurityFragment extends Fragment implements UIValidatorListener
this.getActivity().runOnUiThread(new Runnable() {
public void run() {
if (field.getView() == etCurrentPin) {
tvCurrentPinError.setText("");
} else if (field.getView() == etNewPin){
if (field.getView() == etNewPin){
tvNewPinError.setText("");
} else if (field.getView() == etConfirmPin){
tvConfirmPinError.setText("");
@ -171,7 +118,6 @@ public class PinSecurityFragment extends Fragment implements UIValidatorListener
Toast toast = Toast.makeText(getContext(), text, duration);
toast.show();
//showCurrentPinUI(true);
savePassword(etNewPin.getText().toString());
@ -184,14 +130,8 @@ public class PinSecurityFragment extends Fragment implements UIValidatorListener
public void savePassword(String password) {
String passwordEncripted = PasswordManager.encriptPassword(password);
if (passwordGeneralSetting == null) {
passwordGeneralSetting = new GeneralSetting();
passwordGeneralSetting.setName(GeneralSetting.SETTING_PASSWORD);
}
passwordGeneralSetting.setValue(passwordEncripted);
generalSettingListViewModel.saveGeneralSetting(passwordGeneralSetting);
CrystalSecurityMonitor.getInstance(null).setPasswordSecurity(passwordEncripted);
CrystalSecurityMonitor.getInstance(null).callPasswordRequest(this.getActivity());
}
@Override
@ -200,9 +140,7 @@ public class PinSecurityFragment extends Fragment implements UIValidatorListener
@Override
public void run() {
if (field.getView() == etCurrentPin) {
tvCurrentPinError.setText(field.getMessage());
} else if (field.getView() == etNewPin){
if (field.getView() == etNewPin){
tvNewPinError.setText(field.getMessage());
} else if (field.getView() == etConfirmPin){
tvConfirmPinError.setText(field.getMessage());

View file

@ -12,6 +12,8 @@ import android.view.ViewGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import cy.agorise.crystalwallet.R;
import cy.agorise.crystalwallet.application.CrystalSecurityMonitor;
import cy.agorise.crystalwallet.models.GeneralSetting;
import cy.agorise.crystalwallet.util.ChildViewPager;
/**
@ -46,6 +48,17 @@ public class SecuritySettingsFragment extends Fragment {
securityPagerAdapter = new SecurityPagerAdapter(getChildFragmentManager());
mPager.setAdapter(securityPagerAdapter);
switch(CrystalSecurityMonitor.getInstance(null).actualSecurity()) {
case GeneralSetting.SETTING_PASSWORD:
mPager.setCurrentItem(1);
break;
case GeneralSetting.SETTING_PATTERN:
mPager.setCurrentItem(2);
break;
default:
mPager.setCurrentItem(0);
}
mPager.setSwipeLocked(true);
TabLayout tabLayout = v.findViewById(R.id.tabs);
@ -56,6 +69,14 @@ public class SecuritySettingsFragment extends Fragment {
return v;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
CrystalSecurityMonitor.getInstance(null).callPasswordRequest(this.getActivity());
}
}
private class SecurityPagerAdapter extends FragmentPagerAdapter {
SecurityPagerAdapter(FragmentManager fm) {
super(fm);

View file

@ -45,4 +45,16 @@ public class GeneralSettingListViewModel extends AndroidViewModel {
public LiveData<GeneralSetting> getGeneralSettingLiveDataByName(String name){
return this.db.generalSettingDao().getByName(name);
}
public void deleteGeneralSettings(GeneralSetting... generalSettings){
this.db.generalSettingDao().deleteGeneralSettings(generalSettings);
}
public void deleteGeneralSettingByName(String name){
this.db.generalSettingDao().deleteByName(name);
}
//public void addGeneralSetting(String name, String value){
// this.db.generalSettingDao().addGeneralSetting(name,value);
//}
}

View file

@ -14,9 +14,8 @@ import cy.agorise.crystalwallet.viewmodels.validators.validationfields.PinValida
public class PinSecurityValidator extends UIValidator {
public PinSecurityValidator(Context context, EditText currentPinEdit, EditText newPinEdit, EditText newPinConfirmationEdit){
public PinSecurityValidator(Context context, EditText newPinEdit, EditText newPinConfirmationEdit){
super(context);
this.addField(new CurrentPinValidationField(currentPinEdit));
this.addField(new PinValidationField(newPinEdit));
this.addField(new PinConfirmationValidationField(newPinEdit,newPinConfirmationEdit));
}

View file

@ -6,35 +6,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvCurrentPin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/current_pin"
app:layout_constraintBottom_toTopOf="@+id/etCurrentPin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<EditText
android:id="@+id/etCurrentPin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="numberPassword"
app:layout_constraintBottom_toTopOf="@+id/etNewPin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvCurrentPin"
tools:ignore="LabelFor" />
<TextView
android:id="@+id/tvCurrentPinError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/red"
app:layout_constraintTop_toBottomOf="@+id/etCurrentPin" />
<EditText
android:id="@+id/etNewPin"
android:layout_width="0dp"
@ -42,11 +13,9 @@
android:layout_marginTop="16dp"
android:hint="@string/new_pin"
android:inputType="numberPassword"
app:layout_constraintBottom_toTopOf="@+id/etConfirmPin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvCurrentPinError" />
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/tvNewPinError"
android:layout_width="wrap_content"