How to check password with String in Android - java

For my application login I want use this library : https://github.com/alphamu/PinEntryEditText
In this library for check password use below method :
final PinEntryEditText pinEntry = (PinEntryEditText) findViewById(R.id.txt_pin_entry);
if (pinEntry != null) {
pinEntry.setOnPinEnteredListener(new PinEntryEditText.OnPinEnteredListener() {
#Override
public void onPinEntered(CharSequence str) {
if (str.toString().equals("1234")) {
Toast.makeText(AnimatedEditTextWidgetsActivity.this, "SUCCESS", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AnimatedEditTextWidgetsActivity.this, "FAIL", Toast.LENGTH_SHORT).show();
pinEntry.setText(null);
}
}
});
}
Check with String constructor :
public void onPinEntered(CharSequence str) { ... }
But my query for check password this :
public Boolean login(String password, SQLiteDatabase db) throws SQLException {
Cursor cursor = db.rawQuery("SELECT * FROM " + UserContract.NewUserInfo.TABLE_NAME +
" WHERE " + UserContract.NewUserInfo.USER_PASSWORD + "=?", new String[]{password});
if (cursor != null) {
if (cursor.getCount() > 0) {
return true;
}
}
return false;
}
How can I check password from SQLite with CharSequence str in above library ?!
Please edit my code, because I am amateur and I really need this tutorial. Thanks all <3

As String implements CharSequence you can use a String anywhere you need CharSequence -- to get a String from a CharSequence use toString():
public void onPinEntered(CharSequence str) {
if( login( str.toString() ) {
Toast.makeText(AnimatedEditTextWidgetsActivity.this, "SUCCESS", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AnimatedEditTextWidgetsActivity.this, "FAIL", Toast.LENGTH_SHORT).show();
pinEntry.setText(null);
}
}
Tips:
Use boolean instead of Boolean if you don't need a nullable result.
You generally don't want to do heavy lifting on the UI thread. You'll probably get away with running a quick sqlite query, but your UI will become unresponsive if you do longer operations.

Related

How to cache search results retrieved from auto-complete API

I've been working on a searching auto-complete feature with Bootstrap-3-Typeahead ,GWT and data comes from http://dawa.aws.dk/dok/api by JsonP request..
The Typahead creation method is below
private Typeahead<Location> createTypeAhead() {
typeAhead = new Typeahead<>(new Dataset<Location>() {
#Override
public void findMatches(final String query, final SuggestionCallback<Location> callback) {
requestCounter--;
startSendingRequest = true;
clear.setIcon(IconType.SPINNER);
clear.setIconSpin(true);
final Set<Suggestion<Location>> suggestions = new HashSet<>();
queryLower = query.toLowerCase();
JsonpRequestBuilder jsonpRequestBuilder;
if (!streetSelected) {
jsonpRequestBuilder = new JsonpRequestBuilder();
jsonpRequestBuilder.requestObject("https://dawa.aws.dk/vejnavne/autocomplete?side=1&per_side=500&noformat=1&q=" + queryLower + "*", new AsyncCallback<MyJsArray<VejAutocomplete>>() {
#Override
public void onFailure(Throwable caught) {
Notify.notify("suggestion matches failed");
}
#Override
public void onSuccess(MyJsArray<VejAutocomplete> result) {
Set<Location> locationSet = new LinkedHashSet<>();
for (VejAutocomplete item : result.getAsList()) {
String lowerCase = item.getTekst().toLowerCase();
if (lowerCase.startsWith(queryLower)) {
locationSet.add(new Location(Location.LocationType.STREET, item.getTekst(), item));
locationArrayList.clear();
locationArrayList.addAll(locationSet);
}
}
}
});
}
for (Location address : locationArrayList) {
String value = address.getValue();
Suggestion<Location> s = Suggestion.create(value, address, this);
if (address.getValue().toLowerCase().startsWith(queryLower)) {
suggestions.add(s);
}
}
callback.execute(suggestions);
if (typeAhead.getValue().length() != 0 && queryLower.length() <= 5 && requestCounter < 5 && requestCounter > 0) {
new Timer() {
#Override
public void run() {
findMatches(queryLower, callback);
}
}.schedule(500);
} else {
clear.setIconSpin(false);
clear.setIcon(IconType.CLOSE);
requestCounter = 5;
}
}
});
return typeAhead;
}
The result is like below:
I used recursion to send 4-5 times of request because it's not showing the suggestion list with the keyword of single letter. And It still won't work with some single letters like "s" or "e". Data successfully retrieved from the API but doesn't show in the suggestion list like below:
I assume that I should cache all search results then recreate the auto-complete from scratch, it becomes complicated in that case.
Any good idea to solve this problem?

Mockito test overwritten void method

I'm new to java, unit-testing and mockito but I want to test something from my class.
Class I want to test:
abstract class Irc {
Command received(String data) {
// parsing
receivedCommand(prefix, command, parameters, trailing, tags);
return new Command(prefix, command, parameters, trailing, tags);
}
private void receivedCommand(String prefix, String command, String[] parameters, String trailing, Map<String, String> tags) {
String nick = getNickFromPrefix(prefix);
parsed(prefix, command, parameters, trailing);
if (command.equals("MODE")) {
if (parameters.length == 3) {
String chan = parameters[0];
String mode = parameters[1];
String name = parameters[2];
if (mode.length() == 2) {
String modeChar = mode.substring(1, 2);
if (mode.startsWith("+")) {
onModeChange(chan, name, true, modeChar, prefix);
} else if (mode.startsWith("-")) {
onModeChange(chan, name, false, modeChar, prefix);
}
}
}
return;
}
}
void onModeChange(String channel, String nick, boolean modeAdded, String mode, String prefix) {
}
}
EDIT:
I want to make sure that onModeChange is called after received was called.
What I have so far:
#Test
public void modeChangeReceivedRightSyntax() {
try {
irc = new Irc("Test") {
#Override
public void debug(String line) {
System.err.println(line);
}
#Override
void onModeChange(String channel, String nick, boolean modeAdded, String mode, String prefix) {
System.out.println("Mode Change: " + channel + " " + nick + " " + mode + " " + prefix + " " + modeAdded);
}
};
ircMock = spy(irc);
when(
ircMock.received(":jtv MODE #channel +o user")
).thenReturn(
new Command("jtv", "MODE", new String[]{"#channel", "+o", "user"}, "", null)
);
verify(ircMock).onModeChange("#channel", "user", true, "o", "jtv");
} catch (Exception ex) {
fail("Exception: " + ex.toString());
}
}
The when is working but the verify fails with Actually, there were zero interactions with this mock.
Your code is not calling any business methods. It only mocks how the received method should behave but that does not actually call the method.
Since your Irc class is abstract, your approach to use spy is good but you should not mock the received method but tell the spy to delegate to the real method. Then you can verify that onModeChange is called.
Irc irc = spy(Irc.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));
// Business method
irc.received(":jtv MODE #channel +o user");
// Asserts
verify(irc).onModeChange("#channel", "user", true, "o", "jtv");

Creating 140,000-row SQLite database in AsyncTask doInBackground taking many, many minutes

I haven't dealt with SQLite databases before last week. I last dealt with SQL many years ago, but I still have the gist of it.
The code below reads 140,000 words from an asset named dictionary.dic and inserts each into a SQLite database along with its status. My expectation was that it would take a good while, but it's been like 25 minutes on a 7" tablet and still not near finished (on P).
Should I say, "Hey, it's 1/7 of a million rows. It's gonna take awhile." But I can read all 140,000 words into an ArrayList<String> in 30 seconds. I realize there's overhead in creating the database, but many, many minutes?
Should I say, "Well, think how long it would take if not using AsyncTask" and accept it since it's a one-time task? But it's really obnoxious, taking so long. It's off-putting.
Should I say, "Why are you using a Scanner? No wonder it's taking so long?" and do some other asset access? Or is that not the real problem?
I also have never used AsyncTask. Am I misusing doInBackground? I've got a lot of code in there; not all MUST go there, but the loop is what it is and there's the hangup.
Is using database.Insert, which is called a "convenience method", what's causing the hangup? Should I be using a Cursor and query instead? I'm not entirely sure how I'd do that. Got my idea from Deitel's "Address Book" app in "Android for Programmers--App Driven...", but his database is empty at the outset.
I've given this plenty of thought. I just need someone with experience to look and say, "Well, HERE'S your problem." I can't justify starting redoing all the things I've thought of without some guidance about whether any of it is going to help.
public class DatabaseConnector //extends ArrayList<String>
{
public static Cursor cursor ;
Scanner scDict;
InputStream stream = null;
Context mContext;
AssetManager mAssets;
public static final String DATABASE_NAME = "Dictionary";
public static final String TABLE_NAME = "wordlist";
public static final String WORD_COLUMN_NAME = "word";
public static final String STATUS_COLUMN_NAME = "status";
public static final String [] columns = new String[]{WORD_COLUMN_NAME, STATUS_COLUMN_NAME};
private DatabaseOpenHelper ___databaseOpenHelper; // creates the database
private SQLiteDatabase ___database; // for interacting with the database
public DatabaseConnector(Context _context, AssetManager assets)
{
mContext = _context;
mAssets = assets;
___databaseOpenHelper = new DatabaseOpenHelper(_context, DATABASE_NAME, null, 1);
Log.w("DB connected", ___databaseOpenHelper.getDatabaseName());
createDbIfNecessary();
};
public void open() throws SQLException // opens/creates
{
___database = ___databaseOpenHelper.getWritableDatabase(); // create OR open
}
public void createDbIfNecessary(){
this.open();
if(getDbCount() < 140000){
try { stream = mAssets.open("dictionary.dic"); }
catch (IOException e) { System.out.println(Arrays.toString(e.getStackTrace())); }
MainActivity.setLblProgress("This one-time task takes awhile: loading letter... ");
LoadWords loadWords = new LoadWords();
loadWords.execute((Object[]) null);
this.close();
}
}
public void close(){
if(___database != null)
___database.close();
}
public int getDbCount(){
this.open();
return ___database.query(TABLE_NAME, columns, null, null, null, null, null).getCount();
}
public long insertWord(String _word)
{
ContentValues
__newWord;
__newWord = new ContentValues();
__newWord.put(WORD_COLUMN_NAME, _word);
__newWord.put(STATUS_COLUMN_NAME, true);
long __row = ___database.insert(TABLE_NAME, null, __newWord);
return __row; // -1 if can't insert
}
//////////////////////////////////////////////////////////////////////////////////////////////////
private class DatabaseOpenHelper extends SQLiteOpenHelper
{
public DatabaseOpenHelper(Context _context, String _name, CursorFactory _factory, int _version)
{ super(_context, _name, _factory, _version); }
#Override public void onCreate(SQLiteDatabase _db)
{
_db.execSQL( "CREATE TABLE " + TABLE_NAME +
"("
+ WORD_COLUMN_NAME + " TEXT primary key , " //not null, "
+ STATUS_COLUMN_NAME + " BOOLEAN" +
");"
); // execute query to create the ___database
}
} // end class DatabaseOpenHelper
//////////////////////////////////////////////////////////////////////////////////////////////////
private class LoadWords extends AsyncTask<Object, Integer, Void>
{
#Override
protected Void doInBackground(Object... params) {
long k = 0;
scDict = new Scanner(stream).useDelimiter("\r\n");
long count = getDbCount();
Log.w("Start load at " , "" + count);
String s = "";
while(k++ < count){
s = scDict.next();
}
Log.w("Add after " , s);
while (scDict.hasNext())
{
s = scDict.next();
publishProgress((Integer)(int)s.charAt(0));
insertWord(s) ;
return null;
}
protected void onProgressUpdate(Integer... progress) {
int c = (int)progress[0];
MainActivity.setLastLetterProcessed((char) c);
}
#Override
protected void onPostExecute(Void x)
{
MainActivity.popupMessage("Database has been created", mContext);
}
}
} // end class DatabaseConnector
You are attempting to do 140,000 individual database transactions. That might take weeks.
Instead, either wrap your entire thing in a single transaction, or batch the inserts into transactions (e.g., every 1000 words). You can create your own transaction bounds using this pseudo-Java:
db.beginTransaction();
try {
// do your SQL work here
db.setTransactionSuccesful();
}
catch (Exception e) {
// logging, event bus message to UI, whatever
}
finally {
db.endTransaction();
}
Thanks to #Commonsware, the 140,000 records now load in under a minute, as opposed to under an HOUR. All I did was use his "p-code" to surround my insert with a 1000-count loop:
protected Void doInBackground(Object... params) {
...
scDict = new Scanner(stream).useDelimiter("\r\n");
long count = getDbCount();
while (k++ < count)
s = scDict.next();
while (scDict.hasNext())
{
//////////////////////////////////////////////////////// start insert
int ki = 0;
try
{
___database.beginTransaction();
while (ki < MAX_TRANSACTIONS && scDict.hasNext())
{
//////////////////////////////////////////////////////// end
insertWord(scDict.next());
//////////////////////////////////////////////////////// start insert
++ki;
}
___database.setTransactionSuccessful();
}
catch(Exception e){ Log.w("Exception",e);}
finally
{
___database.endTransaction();
publishProgress((Integer) (int) s.charAt(0));
}
//////////////////////////////////////////////////////// end
}
return null;
}
...
}

My android app stop I donĀ“t know how get a value of SQLite

I'm new programmer, I need just one value ("IdUser") of SQLite and parse to String variable, but I don't have any idea to catch it and return that value. My app stopped because of this.
Activity.java
public void onClick(View arg0) {
String LOGIN2 ="Users";
String eti = loginDataBaseAdapter.CheckEtiqueta(LOGIN2);
Toast.makeText(getApplicationContext(), eti, Toast.LENGTH_LONG).show();
}
UsersDatabaseHelper.java
public String CheckEtiqueta(String LOGIN2) {
Cursor c = db.rawQuery("SELECT * FROM "+LOGIN2+" WHERE IdUser = 20 ORDER BY IdUser LIMIT1", null);
while (c.moveToNext()) {
//Sample operation with the Database
if (c.getString(c.getColumnIndex("IdUser"))!=null) {
LOGIN2=c.getString(c.getColumnIndex("IdUser"));
return LOGIN2;
}
}
return LOGIN2;
}
Thanks
First of all please correct your SQL query. and why you are returning the same variable that function received. You also havn't opened your database.
To open database.
public void openDatabase()
{
database = dbHelper.getWritableDatabase();
}
And To close :
public void closeDatabase()
{
if (database != null && database.isOpen())
database.close();
}
Add openDatabase() call before Cursor statement and Don't forget to closeDatabase() once you done with it.
EDIT :
Ideal Code :
public User getUserByID(int userID)
{
openDatabase();
Cursor userByID= database.rawQuery("select * from UsersTable where UserID = " + userID, null);
User user = new User();
userByID.moveToFirst();
user.setName(userByID.getString(userByID.getColumnIndex("UserName")));
userByID.close();
closeDatabase();
return User;
}

Searchbar "if" statement keeps showing "else" result

I'm trying to make a custom search bar for my app and it works fine but I need to include a if/else statement to let my user know that the user he searched for does not exist.I tried implementing a if/else statement but when I try to search a user that's in my database it shows me my else statement of "Username not found" when I know I'm putting in the right Username.
From the code I provided I would like to know what am I doing wrong?
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String Username = sbar.getText().toString();
String foundplayer = db.getUsername();
if (Username.equals(foundplayer)) {
ResultFrame.setVisibility(View.VISIBLE);
ResultText.setText(foundplayer);
Toast.makeText(getApplicationContext(), "Player Found", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Username Not Found", Toast.LENGTH_LONG).show();
}
}
});
public String getUsername() {
String[] userName = new String[] { "USERNAME" };
Cursor c = db.query( "NFDB", null, null, null, null, null, null, null);
String result = "";
int iName = c.getColumnIndex("USERNAME");
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = c.getString(iName);
}
return result;
}
First make sure that your getUsername() method returning value..
If it returns value then do like this.
String foundplayer = db.getUsername();
this will take sometime to process as it involves getting the data from Database so your compiler will not wait for it to complete because of that compiler will go to if condition before completing getUsername() method. so you will get your foundplayer null
so it will never satisfy the condition.
so call that in a separate thread and let the other statement wait until it completes
String foundplayer = "";
Thread t=new Thread()
{
public void run()
{
foundplayer = db.getUsername();
}
};
t.start();
t.join();
or
equals is case sensitive try equalsIgnoreCase
Your getUsername function actually returns some random user name from the database.
You have to give it the user name to be searched for as a parameter:
public String getUsername(String searchName) {
Cursor c = db.query("NFDB",
new String[] { "USERNAME" },
"USERNAME = ?",
new String[] { searchName },
null, null, null);
if (c.moveToNext())
return c.getString(0);
else
return "";
}
For that matter, it's quite pointless to return the user name from that function, because the caller already knows what name was searched for. You should better return a boolean.

Categories