2013-07-15

Popup a AlertDialog in Android Service

If you want to popup a dialog in Android Service, you have two ways:
1. Use an Activity as Dialog
a. Create an Activity
b. Config theme as Theme.Dialog in manifest
c. Define your layout in res
2. Use AlertDialog.Builder, but you'll need to config dialog as System Alert
Here is the sample code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Test dialog"));
builder.setIcon(R.drawable.icon);
builder.setMessage("Content");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //Do something
        dialog.dismiss();
});
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        dialog.dismiss();
    }
});
AlertDialog alert = builder.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
Also, remember to add permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

2013-07-09

Install Openfire (Instant Messaging Server) on Ubuntu

Openfire is an open source instant messaging server, based on the XMPP protocol.

Prepare JRE
sudo apt-get install openjdk-7-jre
Download package
Make sure the latest version here: http://www.igniterealtime.org/downloads/index.jsp#openfire
In my case, for example, latest version is 3.8.2
wget http://www.igniterealtime.org/downloadServlet?filename=openfire/openfire_3_8_2.tar.gz -O openfire_3_8_2.tar.gz
Untar gz file, you'll get a openfire folder
tar -xvzf openfire_3_8_2.tar.gz

Edit openfire/conf/openfire.xml, enter your public server IP here:
<interface>127.0.0.1</interface> 
 Add a link to /etc/init.d so that you can start the daemon with a call to service:
ln -s YourOpenfirePath/bin/openfire /etc/init.d/
Start/stop service
service openfire start/stop 
Server ports:
3478 - STUN Service (NAT connectivity)
3479 - STUN Service (NAT connectivity)
5222 - Client to Server (standard and encrypted)
5223 - Client to Server (legacy SSL support)
5229 - Flash Cross Domain (Flash client support)
7070 - HTTP Binding (unsecured HTTP connecitons)
7443 - HTTP Binding (secured HTTP connections)
7777 - File Transfer Proxy (XMPP file transfers)
9090 - Admin Console (unsecured)
9091 - Admin Console (secured)

Now, you can start to config your server from admin console port 9090 or 9091

Process timeout in C# WebRequest

WebRequest in C# default will use IE proxy setting as its proxy server. Usually its a internet proxy.
So if your web request is intent to access local network (intranet), you should disable proxy to get a better performance.

How to disable proxy when using WebRequest, it's quite simple:
request.Proxy = new WebProxy();

2013-07-01

Fix asmack invitationListener does not working on Android

Smack is a good java based library for XMPP client.
http://www.igniterealtime.org/projects/smack/

Android patched version here, called asmack
https://code.google.com/p/asmack/

But there is one issue I met when using MultiUserChat, the invitationListener won't work by using these code:
XMPPConnection mConnection = new XMPPConnection(config);
mConnection.connect();
mConnection.login(login, password, resource);
MultiUserChat mcu = new MultiUserChat();
...
mcu.addInvitationListener(mConnection, mInvitationListener);
mInvitationListener won't get called. So I cannot receive any invitation for group chat.

After done a research from google:
https://code.google.com/p/asmack/issues/detail?id=36

Solution is to add a ProviderManager before register InvitationListener
ProviderManager pm = ProviderManager.getInstance();
pm.addExtensionProvider("x", "http://jabber.org/protocol/muc#user", new MUCUserProvider());

2013-06-20

Use Style definition to remove title bar when using Activity as Dialog

When using Activity as Dialog, you'll use the dialog theme in AndroidManifest.xml
For example:
<activity
    android:label="@string/app_name"
    android:name=".MyDialog"
    android:theme="@android:style/Theme.Dialog"
    android:configChanges="orientation" >
</activity>
If you want to make this dialog without title, one solution is to add
requestWindowFeature(Window.FEATURE_NO_TITLE);
into you Activity onCreate() method, just right after super.onCreate()


Today I'm gonna use another way to remove title on this activity dialog.
Add a new style definition into your styles.xml (sometimes located in res/values/)
<style name="NoTitleDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
</style>
And then goto your AndroidManifest.xml, change your activity dialog theme to this new style:
 <activity
    android:label="@string/app_name"
    android:name=".MyDialog"
    android:theme="@style/NoTitleDialog"
    android:configChanges="orientation" >
</activity>
Done!!

2013-06-19

Adnroid get current fragment when using ViewPager

1. Get current select view page

ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
int id = viewPager.getCurrentItem();
2. Get Fragment by tag
Fragment frag = getFragmentManager().findFragmentByTag("android:switcher:"+R.id.pager+":"+id); 
Then you can access your fragment reference.

2013-06-18

Solve Django show error: database is locked when use SQLite as database

SQLite is meant to be a lightweight database, and thus can’t support a high level of concurrency.OperationalError: database is locked errors indicate that your application is experiencing more concurrency than sqlite can handle in default configuration. This error means that one thread or process has an exclusive lock on the database connection and another thread timed out waiting for the lock the be released.

Python’s SQLite wrapper has a default timeout value that determines how long the second thread is allowed to wait on the lock before it times out and raises the OperationalError: database is locked error.

If you’re getting this error, you can solve it by:

  • Switching to another database backend. At a certain point SQLite becomes too “lite” for real-world applications, and these sorts of concurrency errors indicate you’ve reached that point.
  • Rewriting your code to reduce concurrency and ensure that database transactions are short-lived.
  • Increase the default timeout value by setting the timeout database option option:

'OPTIONS': {
# ...
'timeout': 20,
# ...
}
This will simply make SQLite wait a bit longer before throwing “database is locked” errors; it won’t really do anything to solve them.

Please refer to https://docs.djangoproject.com/en/dev/ref/databases/#database-is-locked-errors


In my case, I took solution 3 add timeout option to solve this problem.