123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644 |
- /**
- * ownCloud Android client application
- *
- * Copyright (C) 2011 Bartek Przybylski
- * Copyright (C) 2015 ownCloud Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- package com.owncloud.android.ui.activity;
- import java.util.Arrays;
- import com.actionbarsherlock.app.ActionBar;
- import com.actionbarsherlock.app.SherlockFragmentActivity;
- import com.owncloud.android.R;
- import com.owncloud.android.utils.DisplayUtils;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnFocusChangeListener;
- import android.view.View.OnKeyListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class PinCodeActivity extends SherlockFragmentActivity {
-
- public final static String EXTRA_ACTIVITY = "com.owncloud.android.ui.activity.PinCodeActivity.ACTIVITY";
- public final static String EXTRA_NEW_STATE = "com.owncloud.android.ui.activity.PinCodeActivity.NEW_STATE";
-
- private Button mBCancel;
- private TextView mPinHdr;
- private TextView mPinHdrExplanation;
- private EditText mText1;
- private EditText mText2;
- private EditText mText3;
- private EditText mText4;
-
- private String [] mTempText ={"","","",""};
-
- private String mActivity;
-
- private boolean mConfirmingPinCode = false;
- private boolean mPinCodeChecked = false;
- private boolean mNewPasswordEntered = false;
- private boolean mBChange = true; // to control that only one blocks jump
- //private int mTCounter ; // Count the number of attempts an user could introduce the PIN code
-
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.pincodelock);
-
- Intent intent = getIntent();
- mActivity = intent.getStringExtra(EXTRA_ACTIVITY);
-
- mBCancel = (Button) findViewById(R.id.cancel);
- mPinHdr = (TextView) findViewById(R.id.pinHdr);
- mPinHdrExplanation = (TextView) findViewById(R.id.pinHdrExpl);
- mText1 = (EditText) findViewById(R.id.txt1);
- mText1.requestFocus();
- getWindow().setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
- mText2 = (EditText) findViewById(R.id.txt2);
- mText3 = (EditText) findViewById(R.id.txt3);
- mText4 = (EditText) findViewById(R.id.txt4);
-
- SharedPreferences appPrefs = PreferenceManager
- .getDefaultSharedPreferences(getApplicationContext());
-
-
- // Not PIN Code defined yet.
- // In a previous version settings is allow from start
- if ( (appPrefs.getString("PrefPinCode1", null) == null ) ){
- setChangePincodeView(true);
- mPinCodeChecked = true;
- mNewPasswordEntered = true;
-
- }else{
-
- if (appPrefs.getBoolean("set_pincode", false)){
- // pincode activated
- if (mActivity.equals("preferences")){
- // PIN has been activated yet
- mPinHdr.setText(R.string.pincode_configure_your_pin);
- mPinHdrExplanation.setVisibility(View.VISIBLE);
- mPinCodeChecked = true ; // No need to check it
- setChangePincodeView(true);
- }else{
- // PIN active
- mBCancel.setVisibility(View.INVISIBLE);
- mBCancel.setVisibility(View.GONE);
- mPinHdr.setText(R.string.pincode_enter_pin_code);
- mPinHdrExplanation.setVisibility(View.INVISIBLE);
- setChangePincodeView(false);
- }
-
- }else {
- // pincode removal
- mPinHdr.setText(R.string.pincode_remove_your_pincode);
- mPinHdrExplanation.setVisibility(View.INVISIBLE);
- mPinCodeChecked = false;
- setChangePincodeView(true);
- }
-
- }
- setTextListeners();
-
- ActionBar actionBar = getSupportActionBar();
- actionBar.setIcon(DisplayUtils.getSeasonalIconId());
- }
-
-
- protected void setInitVars(){
- mConfirmingPinCode = false;
- mPinCodeChecked = false;
- mNewPasswordEntered = false;
- }
-
- protected void setInitView(){
- mBCancel.setVisibility(View.INVISIBLE);
- mBCancel.setVisibility(View.GONE);
- mPinHdr.setText(R.string.pincode_enter_pin_code);
- mPinHdrExplanation.setVisibility(View.INVISIBLE);
- }
-
-
- protected void setChangePincodeView(boolean state){
-
- if(state){
- mBCancel.setVisibility(View.VISIBLE);
- mBCancel.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
-
- SharedPreferences.Editor appPrefsE = PreferenceManager
- .getDefaultSharedPreferences(getApplicationContext()).edit();
-
- SharedPreferences appPrefs = PreferenceManager
- .getDefaultSharedPreferences(getApplicationContext());
-
- boolean state = appPrefs.getBoolean("set_pincode", false);
- appPrefsE.putBoolean("set_pincode",!state);
- appPrefsE.commit();
- setInitVars();
- finish();
- }
- });
- }
-
- }
-
-
-
- /*
- *
- */
- protected void setTextListeners(){
-
- /*------------------------------------------------
- * FIRST BOX
- -------------------------------------------------*/
-
- mText1.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- if (s.length() > 0) {
- if (!mConfirmingPinCode){
- mTempText[0] = mText1.getText().toString();
-
- }
- mText2.requestFocus();
- }
- }
- });
-
-
- /*------------------------------------------------
- * SECOND BOX
- -------------------------------------------------*/
- mText2.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- if (s.length() > 0) {
- if (!mConfirmingPinCode){
- mTempText[1] = mText2.getText().toString();
- }
-
- mText3.requestFocus();
- }
- }
- });
-
- mText2.setOnKeyListener(new OnKeyListener() {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_DEL && mBChange) {
- mText1.setText("");
- mText1.requestFocus();
- if (!mConfirmingPinCode)
- mTempText[0] = "";
- mBChange= false;
-
- }else if(!mBChange){
- mBChange=true;
-
- }
- return false;
- }
- });
-
- mText2.setOnFocusChangeListener(new OnFocusChangeListener() {
-
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- mText2.setCursorVisible(true);
- if (mText1.getText().toString().equals("")){
- mText2.setSelected(false);
- mText2.setCursorVisible(false);
- mText1.requestFocus();
- mText1.setSelected(true);
- mText1.setSelection(0);
- }
-
- }
- });
-
-
- /*------------------------------------------------
- * THIRD BOX
- -------------------------------------------------*/
- mText3.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- if (s.length() > 0) {
- if (!mConfirmingPinCode){
- mTempText[2] = mText3.getText().toString();
- }
- mText4.requestFocus();
- }
- }
- });
-
- mText3.setOnKeyListener(new OnKeyListener() {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_DEL && mBChange) {
- mText2.requestFocus();
- if (!mConfirmingPinCode)
- mTempText[1] = "";
- mText2.setText("");
- mBChange= false;
-
- }else if(!mBChange){
- mBChange=true;
-
- }
- return false;
- }
- });
-
- mText3.setOnFocusChangeListener(new OnFocusChangeListener() {
-
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- mText3.setCursorVisible(true);
- if (mText1.getText().toString().equals("")){
- mText3.setSelected(false);
- mText3.setCursorVisible(false);
- mText1.requestFocus();
- mText1.setSelected(true);
- mText1.setSelection(0);
- }else if (mText2.getText().toString().equals("")){
- mText3.setSelected(false);
- mText3.setCursorVisible(false);
- mText2.requestFocus();
- mText2.setSelected(true);
- mText2.setSelection(0);
- }
-
- }
- });
-
- /*------------------------------------------------
- * FOURTH BOX
- -------------------------------------------------*/
- mText4.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- if (s.length() > 0) {
-
- if (!mConfirmingPinCode){
- mTempText[3] = mText4.getText().toString();
- }
- mText1.requestFocus();
- if (!mPinCodeChecked){
- mPinCodeChecked = checkPincode();
- }
-
- if (mPinCodeChecked &&
- ( mActivity.equals("FileDisplayActivity") || mActivity.equals("PreviewImageActivity") ) ){
- finish();
- } else if (mPinCodeChecked){
-
- Intent intent = getIntent();
- String newState = intent.getStringExtra(EXTRA_NEW_STATE);
-
- if (newState.equals("false")){
- SharedPreferences.Editor appPrefs = PreferenceManager
- .getDefaultSharedPreferences(getApplicationContext()).edit();
- appPrefs.putBoolean("set_pincode",false);
- appPrefs.commit();
-
- setInitVars();
- pinCodeEnd(false);
-
- }else{
-
- if (!mConfirmingPinCode){
- pinCodeChangeRequest();
-
- } else {
- confirmPincode();
- }
- }
-
-
- }
- }
- }
- });
-
-
- mText4.setOnKeyListener(new OnKeyListener() {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_DEL && mBChange) {
- mText3.requestFocus();
- if (!mConfirmingPinCode)
- mTempText[2]="";
- mText3.setText("");
- mBChange= false;
-
- }else if(!mBChange){
- mBChange=true;
- }
- return false;
- }
- });
-
- mText4.setOnFocusChangeListener(new OnFocusChangeListener() {
-
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- mText4.setCursorVisible(true);
-
- if (mText1.getText().toString().equals("")){
- mText4.setSelected(false);
- mText4.setCursorVisible(false);
- mText1.requestFocus();
- mText1.setSelected(true);
- mText1.setSelection(0);
- }else if (mText2.getText().toString().equals("")){
- mText4.setSelected(false);
- mText4.setCursorVisible(false);
- mText2.requestFocus();
- mText2.setSelected(true);
- mText2.setSelection(0);
- }else if (mText3.getText().toString().equals("")){
- mText4.setSelected(false);
- mText4.setCursorVisible(false);
- mText3.requestFocus();
- mText3.setSelected(true);
- mText3.setSelection(0);
- }
-
- }
- });
-
-
-
- } // end setTextListener
-
-
- protected void pinCodeChangeRequest(){
-
- clearBoxes();
- mPinHdr.setText(R.string.pincode_reenter_your_pincode);
- mPinHdrExplanation.setVisibility(View.INVISIBLE);
- mConfirmingPinCode =true;
-
- }
-
-
- protected boolean checkPincode(){
-
-
- SharedPreferences appPrefs = PreferenceManager
- .getDefaultSharedPreferences(getApplicationContext());
-
- String pText1 = appPrefs.getString("PrefPinCode1", null);
- String pText2 = appPrefs.getString("PrefPinCode2", null);
- String pText3 = appPrefs.getString("PrefPinCode3", null);
- String pText4 = appPrefs.getString("PrefPinCode4", null);
- if ( mTempText[0].equals(pText1) &&
- mTempText[1].equals(pText2) &&
- mTempText[2].equals(pText3) &&
- mTempText[3].equals(pText4) ) {
-
- return true;
-
-
- }else {
- Arrays.fill(mTempText, null);
- AlertDialog aDialog = new AlertDialog.Builder(this).create();
- CharSequence errorSeq = getString(R.string.common_error);
- aDialog.setTitle(errorSeq);
- CharSequence cseq = getString(R.string.pincode_wrong);
- aDialog.setMessage(cseq);
- CharSequence okSeq = getString(R.string.common_ok);
- aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- return;
- }
-
- });
- aDialog.show();
- clearBoxes();
- mPinHdr.setText(R.string.pincode_enter_pin_code);
- mPinHdrExplanation.setVisibility(View.INVISIBLE);
- mNewPasswordEntered = true;
- mConfirmingPinCode = false;
-
- }
-
-
- return false;
- }
-
- protected void confirmPincode(){
-
- mConfirmingPinCode = false;
-
- String rText1 = mText1.getText().toString();
- String rText2 = mText2.getText().toString();
- String rText3 = mText3.getText().toString();
- String rText4 = mText4.getText().toString();
-
- if ( mTempText[0].equals(rText1) &&
- mTempText[1].equals(rText2) &&
- mTempText[2].equals(rText3) &&
- mTempText[3].equals(rText4) ) {
-
- savePincodeAndExit();
-
- } else {
-
- Arrays.fill(mTempText, null);
- AlertDialog aDialog = new AlertDialog.Builder(this).create();
- CharSequence errorSeq = getString(R.string.common_error);
- aDialog.setTitle(errorSeq);
- CharSequence cseq = getString(R.string.pincode_mismatch);
- aDialog.setMessage(cseq);
- CharSequence okSeq = getString(R.string.common_ok);
- aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- return;
- }
-
- });
- aDialog.show();
- mPinHdr.setText(R.string.pincode_configure_your_pin);
- mPinHdrExplanation.setVisibility(View.VISIBLE);
- clearBoxes();
- }
-
- }
-
-
- protected void pinCodeEnd(boolean state){
- AlertDialog aDialog = new AlertDialog.Builder(this).create();
-
- if (state){
- CharSequence saveSeq = getString(R.string.common_save_exit);
- aDialog.setTitle(saveSeq);
- CharSequence cseq = getString(R.string.pincode_stored);
- aDialog.setMessage(cseq);
-
- }else{
- CharSequence saveSeq = getString(R.string.common_save_exit);
- aDialog.setTitle(saveSeq);
- CharSequence cseq = getString(R.string.pincode_removed);
- aDialog.setMessage(cseq);
-
- }
- CharSequence okSeq = getString(R.string.common_ok);
- aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- finish();
- return;
- }
-
- });
- aDialog.show();
- }
-
- protected void savePincodeAndExit(){
- SharedPreferences.Editor appPrefs = PreferenceManager
- .getDefaultSharedPreferences(getApplicationContext()).edit();
-
- appPrefs.putString("PrefPinCode1", mTempText[0]);
- appPrefs.putString("PrefPinCode2",mTempText[1]);
- appPrefs.putString("PrefPinCode3", mTempText[2]);
- appPrefs.putString("PrefPinCode4", mTempText[3]);
- appPrefs.putBoolean("set_pincode",true);
- appPrefs.commit();
-
- pinCodeEnd(true);
-
-
-
- }
-
-
- protected void clearBoxes(){
-
- mText1.setText("");
- mText2.setText("");
- mText3.setText("");
- mText4.setText("");
- mText1.requestFocus();
- }
-
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event){
- if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()== 0){
-
- if (mActivity.equals("preferences")){
- SharedPreferences.Editor appPrefsE = PreferenceManager
-
- .getDefaultSharedPreferences(getApplicationContext()).edit();
-
- SharedPreferences appPrefs = PreferenceManager
- .getDefaultSharedPreferences(getApplicationContext());
-
- boolean state = appPrefs.getBoolean("set_pincode", false);
- appPrefsE.putBoolean("set_pincode",!state);
- appPrefsE.commit();
- setInitVars();
- finish();
- }
- return true;
-
- }
-
- return super.onKeyDown(keyCode, event);
- }
-
-
-
-
- }
|