Skip to content

3 step process to being a ‘tab pirate’ with google chrome

Image

I don’t know about you, but I am a tab pirate. You can be a tab pirate too, because hey, I used to be like you too. 

What does it mean to be a tab pirate? Well you effectively make use of having millions (or at least a few) tabs open at a time.

Example:

  1. You’re you’re studying, looking for articles, or doing some fun programming stuff on your own
  2. You open up every tab that looks like it might be relevant
  3. Something comes up, distracting you from your work
  4. You leave those tabs (and chrome) open because you don’t want to bookmark them all

Guess what you have to do? Either stop getting distracted and do one thing to completion (but lets be honest, thats not always realistic) or become a tab pirate!

Steps to becoming a tab pirate:

  1. Get a parrot
  2. Download ‘The great suspender’: https://chrome.google.com/webstore/detail/the-great-suspender/klbibkeccnjlkjkiokjodocebajanakg?hl=en
  3. When you get distracted (or when you finally remember that you got distracted) click on the cool little face in the top corner of your browser and click suspend all. 

It keeps all tabs there but it makes them stop running until you go to them and click reload.

I’ve seen my battery life double almost instantly from doing this to all my un-used tabs.

Another solution is to download ‘Read it later’, which lets your quickly save the url in a ‘queue’ that you can quickly retrieve later through the app.

[Read it later: https://chrome.google.com/webstore/detail/read-later-fast/decdfngdidijkdjgbknlnepdljfaepji?hl=en]

Thanks for reading!

The easy solution – Problems with ListView and Selecting Items – Day in the life of android development

Today I woke up and said, “I really would love to create a custom adapter today,” for my android app. 

Recently I’ve been running into some problems simply creating a list view that would do what I wanted it to:

1. Display a small or large list of events from my SQLite Database

2. Have each event clickable

3. Be able to do whatever I want (e.g. edit, delete, move, etc.) with a clicked event.

If you’re new to android, know that THERE IS A DIFFERENCE BETWEEN SELECTIONS AND CLICKS! With that out of the way, if you’re using a listView or arrayAdapter selections happen naturally and are part of the framework, i.e. they come built in with those views and does not need to be implemented in the XML or the .java files. BUT if I’m not mistaken, clicking does not come naturally and that is exactly what you need when you are dealing with a touch screen device because selections can only be activated with a pointer or a keyboard.

Luckily I stopped myself, and you can too,  from creating a new adapter like many websites suggest. Creating a new array adapter can easily introduces new problems. The solution I found to this was to do some editing in the XML layout file and the paired activity file. Without adding any extra class files, or many lines of code. 

We’ll call my activity class ClassyList and my XML file classy.xml.

First I implemented the arrayAdapter in ClassyList, check below for the link to my post on that. Second I went to work on the XML layout file to add simple few fields to my classy.xml ListView. 

<ListView

     android:drawSelectorOnTop=”false”

     android:choiceMode=”singleChoice”

/>

Once these have been added, your listView is ready to receive clicked events. The rest is up to your activity class.

When you create your array adapter you unfortunately have to use android.R.layout.simple_list_item_single_choice to display your items (at least immediately). Second in your onClick method (whether it is one large on click method for all of your items or if its a separate listener for each) you need to write two lines of code to get your position in your list. The following code will illustrate.

In onCreate(Bundle ….)

ArrayAdapter<YourDataType> adapter = new ArrayAdapter<YourDataType>(this, android.R.layout.simple_list_item_single_choice, your_list_of_YourDataTypes);

setListAdapter(adapter); 

}

 

In onClick(View view){…

        ListView list = (ListView)findViewById(android.R.id.list);

        int selectedPosition = list.getCheckedItemPosition();

then when you want to manipulate the selected piece of data do this: 

        YourDataType yourSelectedData = (YourDataType) getListAdapter().getItem(selectedPosition);

Voila! The magic happens here, and you heard it first! 

In case you want multiple rows or you want your list of selected items to look differently, I’ll post up two different posts to show you what to do. Multiple rows is easy but I separate it anyways.

A day in the life with ANTLR

This summer I’ve gotten a great opportunity to work with some amazing people. Currently I’m developing a grammar (for parsing) for an obscure file extension (.sbs and not the SPSS file or the random farming stats extension). Because of the amazing people around me, I was told to use ANTLR to make the grammar.

Let me first say ANTLR is amazing, but it can be hard to use. So when I find something I think is really useful, I’ll probably post it up here on the blog because the ANTLR website sucks for newbies.

So predicates are pretty much questions and they can help you do things with a lot less typing than you’d have to do otherwise. Here are the different types in ANTLR:

There are 3 types of semantic predicates:

validating semantic predicates;
gated semantic predicates;
disambiguating semantic predicates.
Example grammar

Let’s say you have a block of text consisting of only numbers separated by comma’s, ignoring any white spaces. You would like to parse this input making sure that the numbers are at most 3 digits “long” (at most 999). The following grammar (Numbers.g) would do such a thing:

grammar Numbers;

// entry point of this parser: it parses an input string consisting of at least
// one number, optionally followed by zero or more comma’s and numbers
parse
: number (‘,’ number)* EOF
;

// matches a number that is between 1 and 3 digits long
number
: Digit Digit Digit
| Digit Digit
| Digit
;

// matches a single digit
Digit
: ‘0’..’9′
;

// ignore spaces
WhiteSpace
: (‘ ‘ | ‘\t’ | ‘\r’ | ‘\n’) {skip();}
;
Testing

The grammar can be tested with the following class:

import org.antlr.runtime.*;

public class Main {
public static void main(String[] args) throws Exception {
ANTLRStringStream in = new ANTLRStringStream(“123, 456, 7 , 89”);
NumbersLexer lexer = new NumbersLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
NumbersParser parser = new NumbersParser(tokens);
parser.parse();
}
}
Test it by generating the lexer and parser, compiling all .java files and running the Main class:

java -cp antlr-3.2.jar org.antlr.Tool Numbers.g
javac -cp antlr-3.2.jar *.java
java -cp .:antlr-3.2.jar Main
When doing so, nothing is printed to the console, which indicates that nothing went wrong. Try changing:

ANTLRStringStream in = new ANTLRStringStream(“123, 456, 7 , 89”);
into:

ANTLRStringStream in = new ANTLRStringStream(“123, 456, 7777 , 89”);
and do the test again: you will see an error appearing on the console right after the string 777.

Semantic Predicates

This brings us to the semantic predicates. Let’s say you want to parse numbers between 1 and 10 digits long. A rule like:

number
: Digit Digit Digit Digit Digit Digit Digit Digit Digit Digit
| Digit Digit Digit Digit Digit Digit Digit Digit Digit
/* … */
| Digit Digit Digit
| Digit Digit
| Digit
;
would become cumbersome. Semantic predicates can help simplify this type of rule.

1. Validating Semantic Predicates

A validating semantic predicate is nothing more than a block of code followed by a question mark:

RULE { /* a boolean expression in here */ }?
To solve the problem above using a validating semantic predicate, change the number rule in the grammar into:

number
@init { int N = 0; }
: (Digit { N++; } )+ { N <= 10 }?
;
The parts { int N = 0; } and { N++; } are plain Java statements of which the first is initialized when the parser “enters” the number rule. The actual predicate is: { N <= 10 }?, which causes the parser to throw a FailedPredicateException whenever a number is more than 10 digits long.

Test it by using the following ANTLRStringStream:

// all equal or less than 10 digits
ANTLRStringStream in = new ANTLRStringStream(“1,23,1234567890”);
which produces no exception, while the following does thow an exception:

// ‘12345678901’ is more than 10 digits
ANTLRStringStream in = new ANTLRStringStream(“1,23,12345678901”);
2. Gated Semantic Predicates

A gated semantic predicate is similar to a validating semantic predicate, only the gated version produces a syntax error instead of a FailedPredicateException.

The syntax of a gated semantic predicate is:

{ /* a boolean expression in here */ }?=> RULE
To instead solve the above problem using gated predicates to match numbers up to 10 digits long you would write:

number
@init { int N = 1; }
: ( { N <= 10 }?=> Digit { N++; } )+
;
Test it again with both:

// all equal or less than 10 digits
ANTLRStringStream in = new ANTLRStringStream(“1,23,1234567890”);
and:

// ‘12345678901’ is more than 10 digits
ANTLRStringStream in = new ANTLRStringStream(“1,23,12345678901”);
and you will see the last on will throw an error.

3. Disambiguating Semantic Predicates

The final type of predicate is a disambiguating semantic predicate, which looks a bit like a validating predicate ({boolean-expression}?), but acts more like a gated semantic predicate (no exception is thrown when the boolean expression evaluates to false). You can use it at the start of a rule to check some property of a rule and let the parser match said rule or not.

Let’s say the example grammar creates Number tokens (a lexer rule instead of a parser rule) that will match numbers in the range of 0..999. Now in the parser, you’d like to make a distinction between low- and hight numbers (low: 0..500, high: 501..999). This could be done using a disambiguating semantic predicate where you inspect the token next in the stream (input.LT(1)) to check if it’s either low or high.

A demo:

grammar Numbers;

parse
: atom (‘,’ atom)* EOF
;

atom
: low {System.out.println(“low = ” + $low.text);}
| high {System.out.println(“high = ” + $high.text);}
;

low
: {Integer.valueOf(input.LT(1).getText()) <= 500}? Number
;

high
: Number
;

Number
: Digit Digit Digit
| Digit Digit
| Digit
;

fragment Digit
: ‘0’..’9′
;

WhiteSpace
: (‘ ‘ | ‘\t’ | ‘\r’ | ‘\n’) {skip();}
;
If you now parse the string “123, 999, 456, 700, 89, 0”, you’d see the following output:

low = 123
high = 999
low = 456
high = 700
low = 89
low = 0