diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/IntroActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/IntroActivity.java index f638df3..ad10adb 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/IntroActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/IntroActivity.java @@ -1,36 +1,16 @@ package cy.agorise.crystalwallet.activities; -import android.arch.lifecycle.LifecycleActivity; -import android.arch.lifecycle.LiveData; -import android.arch.lifecycle.Observer; -import android.arch.lifecycle.ViewModelProvider; import android.arch.lifecycle.ViewModelProviders; -import android.arch.paging.PagedList; import android.content.Intent; import android.os.Bundle; -import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; -import android.view.View; -import android.widget.Button; - -import java.util.List; import cy.agorise.crystalwallet.R; -import cy.agorise.crystalwallet.dao.CrystalDatabase; -import cy.agorise.crystalwallet.models.AccountSeed; -import cy.agorise.crystalwallet.models.CryptoCoinBalance; -import cy.agorise.crystalwallet.models.CryptoCoinTransaction; -import cy.agorise.crystalwallet.models.CryptoNetAccount; -import cy.agorise.crystalwallet.randomdatagenerators.RandomCryptoCoinBalanceGenerator; -import cy.agorise.crystalwallet.randomdatagenerators.RandomCryptoNetAccountGenerator; -import cy.agorise.crystalwallet.randomdatagenerators.RandomSeedGenerator; -import cy.agorise.crystalwallet.randomdatagenerators.RandomTransactionsGenerator; +import cy.agorise.crystalwallet.application.CrystalSecurityMonitor; import cy.agorise.crystalwallet.viewmodels.AccountSeedListViewModel; import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; import cy.agorise.crystalwallet.views.TransactionListView; -import static cy.agorise.crystalwallet.R.string.transactions; - public class IntroActivity extends AppCompatActivity { TransactionListViewModel transactionListViewModel; @@ -41,6 +21,9 @@ public class IntroActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intro); + this.getApplication().registerActivityLifecycleCallbacks(new CrystalSecurityMonitor(this)); + + //Checks if the user has any seed created AccountSeedListViewModel accountSeedListViewModel = ViewModelProviders.of(this).get(AccountSeedListViewModel.class); diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/PinRequestActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/PinRequestActivity.java index ecc82eb..a95210a 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/PinRequestActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/PinRequestActivity.java @@ -11,6 +11,8 @@ import android.widget.Button; import android.widget.EditText; import android.widget.TextView; +import java.util.List; + import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; @@ -18,9 +20,17 @@ import butterknife.OnTextChanged; import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.models.AccountSeed; import cy.agorise.crystalwallet.models.GeneralSetting; +import cy.agorise.crystalwallet.util.PasswordManager; import cy.agorise.crystalwallet.viewmodels.AccountSeedViewModel; +import cy.agorise.crystalwallet.viewmodels.GeneralSettingListViewModel; public class PinRequestActivity extends AppCompatActivity { + private String passwordEncrypted; + + @Override + public void onBackPressed() { + //Do nothing to prevent the user to use the back button + } @BindView(R.id.etPassword) EditText etPassword; @@ -30,12 +40,34 @@ public class PinRequestActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pin_request); ButterKnife.bind(this); + + GeneralSettingListViewModel generalSettingListViewModel = ViewModelProviders.of(this).get(GeneralSettingListViewModel.class); + LiveData> generalSettingsLiveData = generalSettingListViewModel.getGeneralSettingList(); + generalSettingsLiveData.observe(this, new Observer>() { + @Override + public void onChanged(@Nullable List generalSettings) { + passwordEncrypted = ""; + + if (generalSettings != null){ + for (GeneralSetting generalSetting:generalSettings) { + if (generalSetting.getName().equals(GeneralSetting.SETTING_PASSWORD)){ + if (!generalSetting.getValue().isEmpty()){ + passwordEncrypted = generalSetting.getValue(); + } + break; + } + } + } + } + }); } @OnTextChanged(value = R.id.etPassword, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) void afterPasswordChanged(Editable editable) { - this.finish(); + if (PasswordManager.checkPassword(passwordEncrypted, etPassword.getText().toString())) { + this.finish(); + } } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java index 64e7345..fc5f6c7 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java +++ b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java @@ -26,7 +26,5 @@ public class CrystalApplication extends Application { Intent intent = new Intent(getApplicationContext(), CrystalWalletService.class); startService(intent); - - registerActivityLifecycleCallbacks(new CrystalSecurityMonitor()); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalSecurityMonitor.java b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalSecurityMonitor.java index c99729d..852daa9 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalSecurityMonitor.java +++ b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalSecurityMonitor.java @@ -1,25 +1,62 @@ package cy.agorise.crystalwallet.application; import android.app.Activity; + import android.app.Application; +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.Observer; +import android.arch.lifecycle.ViewModelProviders; import android.content.Intent; import android.os.Bundle; -import android.widget.Toast; +import android.support.annotation.Nullable; +import android.support.v4.app.FragmentActivity; + +import java.util.List; -import cy.agorise.crystalwallet.activities.CreateSeedActivity; import cy.agorise.crystalwallet.activities.PinRequestActivity; +import cy.agorise.crystalwallet.models.GeneralSetting; +import cy.agorise.crystalwallet.viewmodels.GeneralSettingListViewModel; /** * Created by Henry Varona on 27/1/2018. */ -class CrystalSecurityMonitor implements Application.ActivityLifecycleCallbacks { +public class CrystalSecurityMonitor implements Application.ActivityLifecycleCallbacks { private int numStarted = 0; + private String passwordEncrypted; + + public CrystalSecurityMonitor(final FragmentActivity fragmentActivity){ + GeneralSettingListViewModel generalSettingListViewModel = ViewModelProviders.of(fragmentActivity).get(GeneralSettingListViewModel.class); + LiveData> generalSettingsLiveData = generalSettingListViewModel.getGeneralSettingList(); + + generalSettingsLiveData.observe(fragmentActivity, new Observer>() { + @Override + public void onChanged(@Nullable List generalSettings) { + boolean founded = false; + passwordEncrypted = ""; + + if (generalSettings != null){ + for (GeneralSetting generalSetting:generalSettings) { + if (generalSetting.getName().equals(GeneralSetting.SETTING_PASSWORD)){ + founded = true; + if (!generalSetting.getValue().isEmpty()){ + passwordEncrypted = generalSetting.getValue(); + callPasswordRequest(fragmentActivity); + } + break; + } + } + } + } + }); + } @Override public void onActivityStarted(Activity activity) { if (numStarted == 0) { - callPasswordRequest(activity); + if ((this.passwordEncrypted != null) && (!this.passwordEncrypted.equals(""))) { + callPasswordRequest(activity); + } } numStarted++; } @@ -28,15 +65,17 @@ class CrystalSecurityMonitor implements Application.ActivityLifecycleCallbacks { public void onActivityStopped(Activity activity) { numStarted--; if (numStarted == 0) { - callPasswordRequest(activity); + if ((this.passwordEncrypted != null) && (!this.passwordEncrypted.equals(""))) { + callPasswordRequest(activity); + } } } public void callPasswordRequest(Activity activity){ if ((!activity.getIntent().hasExtra("ACTIVITY_TYPE")) || (!activity.getIntent().getStringExtra("ACTIVITY_TYPE").equals("PASSWORD_REQUEST"))) { - //Intent intent = new Intent(activity, PinRequestActivity.class); - //intent.putExtra("ACTIVITY_TYPE", "PASSWORD_REQUEST"); - //activity.startActivity(intent); + Intent intent = new Intent(activity, PinRequestActivity.class); + intent.putExtra("ACTIVITY_TYPE", "PASSWORD_REQUEST"); + activity.startActivity(intent); } } diff --git a/app/src/main/res/layout/activity_pin_request.xml b/app/src/main/res/layout/activity_pin_request.xml index 58fa56f..d05df7d 100644 --- a/app/src/main/res/layout/activity_pin_request.xml +++ b/app/src/main/res/layout/activity_pin_request.xml @@ -18,9 +18,11 @@ android:padding="10dp" android:background="@color/colorPrimary" android:text="Enter Pin:"/> + + android:layout_height="wrap_content" + android:inputType="numberPassword" /> \ No newline at end of file