<commit_before>/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")savedInstanceState;
 * 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 info.guardianproject.notepadbot;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import info.guardianproject.database.sqlcipher.SQLiteDatabase;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;


public class Notepadbot extends ListActivity {
    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;
    
    private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private static final int REKEY_ID = Menu.FIRST + 2;
    private static final int SHARE_ID = Menu.FIRST + 3;
    private static final int VIEW_ID = Menu.FIRST + 4;
    
    private NotesDbAdapter mDbHelper;
    
    private Uri dataStream;
    private Uri tmpImageUri;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if (getIntent() != null)
		{
			
			if(getIntent().hasExtra(Intent.EXTRA_STREAM)) {
				dataStream = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
			}
			else
				dataStream = getIntent().getData();
			
		}
        
        SQLiteDatabase.loadLibs(this);
        
        setContentView(R.layout.notes_list);

        registerForContextMenu(getListView());

		
    }
    
    
    private void loadData ()
    {
    	
    	Intent passingIntent = new Intent(this,ImageStore.class);

		passingIntent.setData(dataStream);
		startActivityForResult(passingIntent, 1);
    }
    
    @Override
	protected void onResume() {
		super.onResume();
		
		if (mDbHelper == null)
			showPassword();
		
	
	}


	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		
		 findViewById(R.id.listlayout).setOnTouchListener(new OnTouchListener ()
	        {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					createNote();
					return false;
				}
	        	
	        }
	        		
	        );
	}



	private void showPassword ()
    {
    	 // This example shows how to add a custom layout to an AlertDialog
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
        new AlertDialog.Builder(this)
            .setTitle(getString(R.string.app_name))
            .setView(textEntryView)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                	String password = ((android.widget.EditText)textEntryView.findViewById(R.id.password_edit)).getText().toString();
                	
                	unlockDatabase(password);
                	
                	if (dataStream != null)
        				loadData();
                	
                	System.gc();
                	
                }
            })
            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked cancel so do some stuff */
                }
            })
            .create().show();
    }
	
	private void showRekeyDialog ()
    {
    	 // This example shows how to add a custom layout to an AlertDialog
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
        new AlertDialog.Builder(this)
            .setTitle(getString(R.string.app_name))
            .setView(textEntryView)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                	String newPassword = ((android.widget.EditText)textEntryView.findViewById(R.id.password_edit)).getText().toString();
                	
                	rekeyDatabase(newPassword);
                	
                	System.gc();
                	
                }
            })
            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked cancel so do some stuff */
                }
            })
            .create().show();
    }
    
    private void unlockDatabase (String password)
    {
    	if (mDbHelper == null)
    		mDbHelper = NotesDbAdapter.getInstance(this);

    	try
    	{
    	
    		mDbHelper.open(password);
    		fillData();
    	}
    	catch (Exception e)
    	{
    		Toast.makeText(this, "Unable to unlock your notes. Are you sure you entered the right PIN?", Toast.LENGTH_LONG).show();
    		showPassword();
    	}
    }<commit_msg>edittext dialogs for PIN entry are now reset to empty after value is retrieved<commit_after>/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")savedInstanceState;
 * 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 info.guardianproject.notepadbot;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import info.guardianproject.database.sqlcipher.SQLiteDatabase;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;


public class Notepadbot extends ListActivity {
    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;
    
    private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private static final int REKEY_ID = Menu.FIRST + 2;
    private static final int SHARE_ID = Menu.FIRST + 3;
    private static final int VIEW_ID = Menu.FIRST + 4;
    
    private NotesDbAdapter mDbHelper;
    
    private Uri dataStream;
    private Uri tmpImageUri;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if (getIntent() != null)
		{
			
			if(getIntent().hasExtra(Intent.EXTRA_STREAM)) {
				dataStream = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
			}
			else
				dataStream = getIntent().getData();
			
		}
        
        SQLiteDatabase.loadLibs(this);
        
        setContentView(R.layout.notes_list);

        registerForContextMenu(getListView());

		
    }
    
    
    private void loadData ()
    {
    	
    	Intent passingIntent = new Intent(this,ImageStore.class);

		passingIntent.setData(dataStream);
		startActivityForResult(passingIntent, 1);
    }
    
    @Override
	protected void onResume() {
		super.onResume();
		
		if (mDbHelper == null)
			showPassword();
		
	
	}


	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		
		 findViewById(R.id.listlayout).setOnTouchListener(new OnTouchListener ()
	        {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					createNote();
					return false;
				}
	        	
	        }
	        		
	        );
	}



	private void showPassword ()
    {
    	 // This example shows how to add a custom layout to an AlertDialog
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
        new AlertDialog.Builder(this)
            .setTitle(getString(R.string.app_name))
            .setView(textEntryView)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                	EditText eText = ((android.widget.EditText)textEntryView.findViewById(R.id.password_edit));
                	String password = eText.getText().toString();
                	
                	unlockDatabase(password);
                	
                	if (dataStream != null)
        				loadData();
                	
                	eText.setText("");
                	System.gc();
                	
                }
            })
            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked cancel so do some stuff */
                }
            })
            .create().show();
    }
	
	private void showRekeyDialog ()
    {
    	 // This example shows how to add a custom layout to an AlertDialog
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
        new AlertDialog.Builder(this)
            .setTitle(getString(R.string.app_name))
            .setView(textEntryView)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                	EditText eText = ((android.widget.EditText)textEntryView.findViewById(R.id.password_edit));

                	String newPassword = eText.getText().toString();
                	
                	rekeyDatabase(newPassword);
                	
                	eText.setText("");
                	System.gc();
                	
                }
            })
            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked cancel so do some stuff */
                }
            })
            .create().show();
    }
    
    private void unlockDatabase (String password)
    {
    	if (mDbHelper == null)
    		mDbHelper = NotesDbAdapter.getInstance(this);

    	try
    	{
    	
    		mDbHelper.open(password);
    		fillData();
    	}
    	catch (Exception e)
    	{
    		Toast.makeText(this, "Unable to unlock your notes. Are you sure you entered the right PIN?", Toast.LENGTH_LONG).show();
    		showPassword();
    	}
    }
{'fraction_non_alphanumeric': 0.08881172839506173, 'fraction_numerical': 0.0023148148148148147, 'mean_word_length': 2.48507663350363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1211242', 'n_tokens_mistral': 3696, 'n_tokens_neox': 3397, 'n_words': 871}