Last year I bought an Android smartphone from LG, with Gingerbread 2.3.4 on it. Much to my surprise, it was not very smart, for example I couldn't set it to automatically answer incoming calls.
The good thing about having it run Android, was that I could fix this flaw by developing my own app, so I googled around and found an Auto Answer project on Google Code from Matt Hahnfeld, released under a GNU GPL v3 licence.
Since the author removed it from Google's Play Store, I catered his code to my needs and installed the app on my Optimus HUB.
It works perfectly, I tested it on Ice Cream Sandwich 4.0.3 (LG Optimus L7) without issues, and it should work on Froyo 2.2 too.
At the time there weren't many free apps that did this but now you finally have some choices, note that some phones already come with this feature built-in.
You can find my code on GitHub, the licence remains the same GNU GPL v3. To successfully compile it you'll need an IDE such as Eclipse, and the proper Android SDK for your OS version. The source code I uploaded comes with the Android 4 lib, but you can easily switch it to another version.
23/08/2013
22/08/2013
[Java] String to Date and vice versa
When working with dates in Java you'll often find yourself having to convert them from a String object to a Date object and vice versa. These function snippets will allow you to easily perform the conversion operations, no third-party libraries needed:
Imports
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
string2Date
date2String
Of course, if you prefer, rather than returning NULL if something went wrong, you could always raise an Exception. You can find all accepted formats in the SimpleDateFormat JavaDoc.
Imports
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
string2Date
/** Converts given String in a Date object with specific format
* @param date - a String representing the date to convert. IT MUST BE in the given format
* @param dateFormat - a String specifying the format of the date e.g: dd/MM/yyyy
* @return a Date object, {@null} if the given string is {@null} or if there's been a conversion error
* */
public static Date string2Date(String date, String dateFormat){
Date d = null;
try{
if(date!=null && !date.equalsIgnoreCase("") && dateFormat!=null && !dateFormat.equalsIgnoreCase("")) d = new SimpleDateFormat(dateFormat, Locale.ITALIAN).parse(date);//or whatever Locale you need
}catch(Exception e){}//you could always decide to raise the exception rather than returning simply NULL
return d;
}
date2String
/** Converts given Date in a String object with specific format
* @param date - a Date to be represented as a string. IT MUST BE in the given format
* @param dateFormat - a String specifying the format of the date e.g: dd/MM/yyyy
* @return a String object, {@null} if the given date is {@null} or if there's been a conversion error
* */
public static String date2String(Date date, String dateFormat){
Format formatter = new SimpleDateFormat(dateFormat);
String s = null;
try{
if(date!=null && dateFormat!=null && !dateFormat.equalsIgnoreCase(""))s = formatter.format(date);
}catch(Exception e){}//you could always decide to raise the exception rather than returning simply NULL
return s;
}
Of course, if you prefer, rather than returning NULL if something went wrong, you could always raise an Exception. You can find all accepted formats in the SimpleDateFormat JavaDoc.
28/07/2013
[Java 4] Download file from portlet
Scenario: Java 4, Spring 2, WebSphere 6, JSR-168. How the hell do I let the user download a file from a portlet?
The file is extracted from a database and does not exist on the server's filesystem yet; redirecting to a servlet to create and send the response which contains the file to download fails with a generic:
java.lang.IllegalStateException: Can't invoke sendRedirect() after certain methods have been called
More specifically, that method can not be invoked after any of the following methods of the ActionResponse interface has been called:
The file is extracted from a database and does not exist on the server's filesystem yet; redirecting to a servlet to create and send the response which contains the file to download fails with a generic:
java.lang.IllegalStateException: Can't invoke sendRedirect() after certain methods have been called
More specifically, that method can not be invoked after any of the following methods of the ActionResponse interface has been called:
- setPortletMode
- setWindowState
- setRenderParameter
- setRenderParameters
- removePublicRenderParamter
[Java] Get current user from portlet
To get the user currently authenticated on the portal from a portlet, you may use:
Principal p = request.getUserPrincipal();
you may then call the getName method to get the UID of the user. If the user is not authenticated or the page is not protected, the Principal object is null.
Principal p = request.getUserPrincipal();
you may then call the getName method to get the UID of the user. If the user is not authenticated or the page is not protected, the Principal object is null.
[Java] Get extension from filename
To extract the extension from a given filename in Java, you may try:
This works even if filename contains multiple dots, however it's not an accurate way of determining a file type.
String yourfilename = "something.ext";
String[] tokens = yourfilename.split("\\.(?=[^\\.]+$)");
String extension = tokens[1].toLowerCase();
This works even if filename contains multiple dots, however it's not an accurate way of determining a file type.
[Java] Send mail
Here's an example on how to send HTML emails to multiple recipients, without attachments, via Java.
This Mailer class was written in Java 4 and relies upon the javax.mail.* libraries, providing a method send which takes 4 parameters:
This Mailer class was written in Java 4 and relies upon the javax.mail.* libraries, providing a method send which takes 4 parameters:
- List
:rcptTo - "TO" recipients - List
:ccs - "CC" recipients - String:subject - message subject
- String:body - message body
13/07/2013
[Java 4] Replace placeholder word in string
Should you be unlucky and have to work with Java 4, you may find it lacking many of the convenient features found in later versions.
One of them is the ability to replace a placeholder word in a String with little effort. What you can do is use the replaceAll method:
String str = "some string with a #placeholder# inside";
str=str.replaceAll("#placeholder#", "value");
Note that it will search for and replace ALL matches it finds, making it viable as a placeholder substitution method while it's pretty useless if you have to replace a single word; in that case, you may find the replaceFirst method more suitable.
Remember in any case that the first parameter is parsed as a regular expression meaning that you'll have to escape reserved characters such as "$" if your placeholder contains them. Another very important thing to remember is that, since String objects are immutable in Java, if you don't assign the return value to some variable, the substitution won't have any effect.
Subscribe to:
Posts (Atom)