Android Open Source - passwords-by-harry Change Master Password Activity






From Project

Back to project page passwords-by-harry.

License

The source code is released under:

Apache License

If you think the Android project passwords-by-harry listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2013. Harry Henry Gebel
 *//from w w  w .  ja v  a  2s .  com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.harryfrommarydelde.passwordsbyharry;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by harry on 9/6/13.
 */
public class ChangeMasterPasswordActivity extends DataServiceConnectedActivity {
    private TextView titleTextView;

    private EditText originalPasswordText;
    private EditText newPasswordText;
    private EditText confirmPasswordText;

    private Button doneButton;
    private CheckBox showPasswordCheck;

    private Boolean isChangingPassword = true;

    protected void localOnServiceConnected(DataService.DataBinder dataBinder) {
        // if the app is not linked to an account, link it, initiate master password
        if (!dataBinder.dataExists()) {
            // change title of activity, remove original password prompt
            originalPasswordText.setVisibility(View.GONE);
            titleTextView.setText(R.string.set_master_password);
            isChangingPassword = false;

            // link with Dropbox account
            dataBinder.linkAccount(this);
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_change_master_password);

        titleTextView = (TextView) findViewById(R.id.titleTextView);

        doneButton = (Button) findViewById(R.id.doneButton);
        showPasswordCheck = (CheckBox) findViewById(R.id.showPasswordCheck);

        // set text fields to confirm valid input and set done button appropriately
        originalPasswordText = (EditText) findViewById(R.id.originalPasswordText);
        newPasswordText = (EditText) findViewById(R.id.newPasswordText);
        confirmPasswordText = (EditText) findViewById(R.id.confirmPasswordText);
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                enableDoneButton();
            }
        };

        originalPasswordText.addTextChangedListener(textWatcher);
        newPasswordText.addTextChangedListener(textWatcher);
        confirmPasswordText.addTextChangedListener(textWatcher);
        enableDoneButton();

        // see if user wants master password shown
        showPasswordCheck.setChecked(settings.getBoolean("show_master_password", false));
        showPassword(showPasswordCheck);
    }

    // Make sure changes in settings are maintained in activity
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("show_master_password")) {
            showPasswordCheck.setChecked(
                    sharedPreferences.getBoolean("show_master_password", false));
            showPassword(showPasswordCheck);
        }
    }

    public void enableDoneButton() {
        boolean enabled = false;

        // first make sure new passwords match, and that there is a new password
        if ((newPasswordText.length() > 0) &&
                (newPasswordText.getText().toString().equals(
                        confirmPasswordText.getText().toString()))) {
            enabled = true;
        }

        // then, if we a changing the password, make sure there is something in the old password
        // box
        if (isChangingPassword && (originalPasswordText.length() == 0)) {
            enabled = false;
        }

        doneButton.setEnabled(enabled);
    }

    public void changePassword(View view) {
        originalPasswordText.setText("");
        newPasswordText.setText("");
        confirmPasswordText.setText("");
    }

    public void showPassword(View view) {
        if (((CheckBox) view).isChecked()) {
            originalPasswordText.setInputType(
                    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            newPasswordText.setInputType(
                    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            confirmPasswordText.setInputType(
                    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else
        {
            originalPasswordText.setInputType(
                    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            newPasswordText.setInputType(
                    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            confirmPasswordText.setInputType(
                    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    }
}




Java Source Code List

com.github.harryfrommarydelde.passwordsbyharry.AccountListActivity.java
com.github.harryfrommarydelde.passwordsbyharry.ChangeMasterPasswordActivity.java
com.github.harryfrommarydelde.passwordsbyharry.DataServiceConnectedActivity.java
com.github.harryfrommarydelde.passwordsbyharry.DataService.java
com.github.harryfrommarydelde.passwordsbyharry.LoginActivity.java
com.github.harryfrommarydelde.passwordsbyharry.NonstopBoundService.java
com.github.harryfrommarydelde.passwordsbyharry.SettingsActivity.java
com.github.harryfrommarydelde.pbhsupport.DropboxSupport.java