<commit_before>/*
 * Copyright (©) 2010 <NAME> <<EMAIL>>
 * All rights reserved. Use of the code is allowed under the
 * Artistic License 2.0 terms, as specified in the LICENSE file
 * distributed with this code, or available from
 * http://www.opensource.org/licenses/artistic-license-2.0.php
 */
package com.jefftharris.passwdsafe;

import java.io.File;

import org.pwsafe.lib.file.PwsRecord;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class RecordEditActivity extends Activity
{
    private static final String TAG = "RecordEditActivity";

    private static final int DIALOG_PROGRESS = 0;

    private ActivityPasswdFile itsFile;
    private String itsUUID;
    private SaveTask itsSaveTask;

    private TextWatcher itsTextWatcher = new TextWatcher()
    {
        public void afterTextChanged(Editable s)
        {
            validate();
        }

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after)
        {
        }

        public void onTextChanged(CharSequence s, int start,
                                  int before, int count)
        {
        }
    };

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        PasswdSafeApp.dbginfo(TAG, "onCreate intent:" + getIntent());

        File file = new File(intent.getData().getPath());
        itsUUID = intent.getData().getQueryParameter("rec");
        if (itsUUID == null) {
            // TODO edit new entry
            return;
        }

        PasswdSafeApp app = (PasswdSafeApp)getApplication();
        itsFile = app.accessPasswdFile(file, this);
        PasswdFileData fileData = itsFile.getFileData();
        if (fileData == null) {
            PasswdSafeApp.showFatalMsg("File not open: " + file, this);
            return;
        }

        PwsRecord record = fileData.getRecord(itsUUID);
        if (record == null) {
            PasswdSafeApp.showFatalMsg("Unknown record: " + itsUUID, this);
            return;
        }

        setContentView(R.layout.record_edit);
        setTitle(PasswdSafeApp.getAppFileTitle(itsFile, this));

        // TODO hide fields that aren't valid for version

        setText(R.id.rec_title, "Edit " + fileData.getTitle(record));
        setText(R.id.title, fileData.getTitle(record));
        // TODO editable combo-box for groups??
        setText(R.id.group, fileData.getGroup(record));
        setText(R.id.url, fileData.getURL(record));
        setText(R.id.email, fileData.getEmail(record));
        setText(R.id.user, fileData.getUsername(record));
        setText(R.id.notes, fileData.getNotes(record));
        // TODO password edit

        Button button = (Button)findViewById(R.id.done_btn);
        button.setOnClickListener(new OnClickListener()
        {
            public final void onClick(View v)
            {
                saveRecord();
            }
        });

        button = (Button)findViewById(R.id.cancel_btn);
        button.setOnClickListener(new OnClickListener()
        {
            public final void onClick(View v)
            {
                finish();
            }
        });

        validate();
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onPause()
     */
    @Override
    protected void onPause()
    {
        super.onPause();
        PasswdSafeApp.dbginfo(TAG, "onPause");
        if (itsSaveTask != null) {
            try {
                itsSaveTask.get();
            } catch (Exception e) {
                PasswdSafeApp.showFatalMsg(e.toString(), this);
            }
            itsSaveTask = null;
            removeDialog(DIALOG_PROGRESS);
        }
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume()
    {
        super.onResume();
        itsFile.touch();
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onCreateDialog(int)
     */
    @Override
    protected Dialog onCreateDialog(int id)
    {
        Dialog dialog = null;
        switch (id) {
        case DIALOG_PROGRESS:
        {
            ProgressDialog dlg = new ProgressDialog(this);
            dlg.setTitle(PasswdSafeApp.getAppTitle(this));
            dlg.setMessage("Saving " +
                           itsFile.getFileData().getFile().getName() + "...");
            dlg.setIndeterminate(true);
            dlg.setCancelable(false);
            dialog = dlg;
            break;
        }
        default:
        {
            dialog = super.onCreateDialog(id);
            break;
        }
        }
        return dialog;
    }

    private final void validate()
    {
        String errorMsg = null;
        do {
            if (getTextViewStr(R.id.title).length() == 0) {
                errorMsg = "Empty title";
                break;
            }

            if (getTextViewStr(R.id.group).length() == 0) {
                errorMsg = "Empty group";
                break;
            }
        } while(false);

        TextView errorMsgView = (TextView)findViewById(R.id.error_msg);
        if (errorMsg == null) {
            errorMsgView.setVisibility(View.GONE);
        } else {
            errorMsgView.setVisibility(View.VISIBLE);

            String errorFmt = getResources().getString(R.string.error_msg);
            errorMsgView.setText(
                Html.fromHtml(String.format(errorFmt, errorMsg)));
        }
<commit_msg>Remove validation for empty group - it is allowed<commit_after>/*
 * Copyright (©) 2010 <NAME> <<EMAIL>>
 * All rights reserved. Use of the code is allowed under the
 * Artistic License 2.0 terms, as specified in the LICENSE file
 * distributed with this code, or available from
 * http://www.opensource.org/licenses/artistic-license-2.0.php
 */
package com.jefftharris.passwdsafe;

import java.io.File;

import org.pwsafe.lib.file.PwsRecord;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class RecordEditActivity extends Activity
{
    private static final String TAG = "RecordEditActivity";

    private static final int DIALOG_PROGRESS = 0;

    private ActivityPasswdFile itsFile;
    private String itsUUID;
    private SaveTask itsSaveTask;

    private TextWatcher itsTextWatcher = new TextWatcher()
    {
        public void afterTextChanged(Editable s)
        {
            validate();
        }

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after)
        {
        }

        public void onTextChanged(CharSequence s, int start,
                                  int before, int count)
        {
        }
    };

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        PasswdSafeApp.dbginfo(TAG, "onCreate intent:" + getIntent());

        File file = new File(intent.getData().getPath());
        itsUUID = intent.getData().getQueryParameter("rec");
        if (itsUUID == null) {
            // TODO edit new entry
            return;
        }

        PasswdSafeApp app = (PasswdSafeApp)getApplication();
        itsFile = app.accessPasswdFile(file, this);
        PasswdFileData fileData = itsFile.getFileData();
        if (fileData == null) {
            PasswdSafeApp.showFatalMsg("File not open: " + file, this);
            return;
        }

        PwsRecord record = fileData.getRecord(itsUUID);
        if (record == null) {
            PasswdSafeApp.showFatalMsg("Unknown record: " + itsUUID, this);
            return;
        }

        setContentView(R.layout.record_edit);
        setTitle(PasswdSafeApp.getAppFileTitle(itsFile, this));

        // TODO hide fields that aren't valid for version

        setText(R.id.rec_title, "Edit " + fileData.getTitle(record));
        setText(R.id.title, fileData.getTitle(record));
        // TODO editable combo-box for groups??
        setText(R.id.group, fileData.getGroup(record));
        setText(R.id.url, fileData.getURL(record));
        setText(R.id.email, fileData.getEmail(record));
        setText(R.id.user, fileData.getUsername(record));
        setText(R.id.notes, fileData.getNotes(record));
        // TODO password edit

        Button button = (Button)findViewById(R.id.done_btn);
        button.setOnClickListener(new OnClickListener()
        {
            public final void onClick(View v)
            {
                saveRecord();
            }
        });

        button = (Button)findViewById(R.id.cancel_btn);
        button.setOnClickListener(new OnClickListener()
        {
            public final void onClick(View v)
            {
                finish();
            }
        });

        validate();
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onPause()
     */
    @Override
    protected void onPause()
    {
        super.onPause();
        PasswdSafeApp.dbginfo(TAG, "onPause");
        if (itsSaveTask != null) {
            try {
                itsSaveTask.get();
            } catch (Exception e) {
                PasswdSafeApp.showFatalMsg(e.toString(), this);
            }
            itsSaveTask = null;
            removeDialog(DIALOG_PROGRESS);
        }
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume()
    {
        super.onResume();
        itsFile.touch();
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onCreateDialog(int)
     */
    @Override
    protected Dialog onCreateDialog(int id)
    {
        Dialog dialog = null;
        switch (id) {
        case DIALOG_PROGRESS:
        {
            ProgressDialog dlg = new ProgressDialog(this);
            dlg.setTitle(PasswdSafeApp.getAppTitle(this));
            dlg.setMessage("Saving " +
                           itsFile.getFileData().getFile().getName() + "...");
            dlg.setIndeterminate(true);
            dlg.setCancelable(false);
            dialog = dlg;
            break;
        }
        default:
        {
            dialog = super.onCreateDialog(id);
            break;
        }
        }
        return dialog;
    }

    private final void validate()
    {
        String errorMsg = null;
        do {
            if (getTextViewStr(R.id.title).length() == 0) {
                errorMsg = "Empty title";
                break;
            }
        } while(false);

        TextView errorMsgView = (TextView)findViewById(R.id.error_msg);
        if (errorMsg == null) {
            errorMsgView.setVisibility(View.GONE);
        } else {
            errorMsgView.setVisibility(View.VISIBLE);

            String errorFmt = getResources().getString(R.string.error_msg);
            errorMsgView.setText(
                Html.fromHtml(String.format(errorFmt, errorMsg)));
        }

{'fraction_non_alphanumeric': 0.10809209412561367, 'fraction_numerical': 0.0017775520568816658, 'mean_word_length': 1.970832285642444, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6739499', 'n_tokens_mistral': 3415, 'n_tokens_neox': 3280, 'n_words': 686}