<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pieces of Life... &#187; Programming</title>
	<atom:link href="http://www.bogdanirimia.ro/category/technology/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bogdanirimia.ro</link>
	<description>Bogdan Irimia&#039;s Web Space</description>
	<lastBuildDate>Fri, 18 Mar 2011 20:11:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Android widget, click event, multiple instances</title>
		<link>http://www.bogdanirimia.ro/android-widget-click-event-multiple-instances/269</link>
		<comments>http://www.bogdanirimia.ro/android-widget-click-event-multiple-instances/269#comments</comments>
		<pubDate>Fri, 09 Jul 2010 13:03:42 +0000</pubDate>
		<dc:creator>Myself</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[click]]></category>
		<category><![CDATA[instances]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://www.bogdanirimia.ro/?p=269</guid>
		<description><![CDATA[Ok&#8230; I started to do some Android devel&#8230; And I started to like it, even if it is based on Java, works in Eclipse and has a ton of classes. Now, the first project is a widget (weather widget). For this, there&#8217;s this &#8220;AppWidgetProvider&#8221; class that implements the design and functions for data retrieval (using [...]]]></description>
			<content:encoded><![CDATA[<p>Ok&#8230; I started to do some Android devel&#8230; And I started to like it, even if it is based on Java, works in Eclipse and has a ton of classes.</p>
<p>Now, the first project is a widget (weather widget). For this, there&#8217;s this &#8220;AppWidgetProvider&#8221; class that implements the design and functions for data retrieval (using threads), and also a configuration Activity. When the configuration activity finishes (a &#8220;Save&#8221; button is pressed), a static function of the AppWidgetProvider class is called. This static function updates the settings of the widget and also does something of interest: it sets the event handler for &#8220;onClick&#8221; &#8211; so that when I click (touch) the widget, to be able to modify the layout somehow (to rotate a needle).</p>
<p>This is a part of the static function that updates the widget:</p>
<pre class="brush: java; title: ; notranslate">
...
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent clickIntent = new Intent(context, DigiStation.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, clickIntent, 0);
views.setOnClickPendingIntent(R.id.widget_normal_relativelayout, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
...
</pre>
<p>The AppWidgetProvider class is called &#8220;DigiStation&#8221; &#8211; we&#8217;re using it for creating an intent that targets it.</p>
<p>Ok. For handling the events, the DigiStation class (which is of type AppWidgetProvider) implements the function &#8220;onReceive&#8221;. The function is called with two parameters: the context and the intent.</p>
<p>Now, the issue was to be able to distinguish between multiple instances of the same AppWidgetProvider class. To be able to do that, I had to put some extra information in the intent (clickIntent.putExtra). The field contains the appWidgetId &#8211; the widget ID &#8211; of the instance.</p>
<p>The onReceive function looks like this:</p>
<pre class="brush: java; title: ; notranslate">
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction()==null) {
        Bundle extras = intent.getExtras();
        if(extras!=null) {
            int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
            // do something for the widget that has appWidgetId = widgetId
        }
    }
    else {
        super.onReceive(context, intent);
    }
 }
</pre>
<p>All looks simple enough, but there are some little details that I must underline, because I spend a few days with them:</p>
<p style="text-align: left;">1. To be able to distinguish between multiple instances of the same AppWidgetProvider, when registering the &#8220;onClick&#8221; event (intent) you must add an extra value with the widget ID (appWidgetId)</p>
<p style="text-align: left;">2. Update only the views of the current instance! &#8211; appWidgetManager.updateAppWidget(appWidgetId, views);</p>
<p style="text-align: left;">3. <strong>Android reuses intents, so when you create an intent, make sure you put an unique ID, or else the same intent used before will be triggered for all instances! </strong> Only with this detail I spent half a day! ( PendingIntent pendingIntent = PendingIntent.getBroadcast(context, <strong>appWidgetId</strong>, clickIntent, 0); )</p>
<p style="text-align: left;">4. When handling the click event, get the appWidgetId from the &#8220;extras&#8221; payload of the intent.</p>
<p>Hope this helps people not spending so much time for little undocumented details! <img src='http://www.bogdanirimia.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bogdanirimia.ro/android-widget-click-event-multiple-instances/269/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>C application with MySQL, built with NetBeans, on CygWin</title>
		<link>http://www.bogdanirimia.ro/c-application-with-mysql-built-with-netbeans-on-cygwin/262</link>
		<comments>http://www.bogdanirimia.ro/c-application-with-mysql-built-with-netbeans-on-cygwin/262#comments</comments>
		<pubDate>Mon, 22 Feb 2010 12:38:23 +0000</pubDate>
		<dc:creator>Myself</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[CygWin]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[NetBeans]]></category>

		<guid isPermaLink="false">http://www.bogdanirimia.ro/?p=262</guid>
		<description><![CDATA[Because MySQL became one of the most used database engine in the world and because of the ease of development in PHP with MySQL, most of the prototype application can be developed at first using this combination (PHP+MySQL). But then, mainly because of performance issues, one might want to implement that project in C &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>Because MySQL became one of the most used database engine in the world and because of the ease of development in PHP with MySQL, most of the prototype application can be developed at first using this combination (PHP+MySQL). But then, mainly because of performance issues, one might want to implement that project in C &#8211; so there&#8217;s the need to connect from a C application to the MySQL server. Nothing easier &#8211; you say. MySQL provides us the mysql-connector that has a C implementation and binaries for the most important platforms. But I wanted to develop my application in a full-fledged IDE, and because my workstation runs Windows (7 &#8211; big fan) I needed to make it compile somehow on Windows. But the application will run on a Linux server (or Unix), so it had to be POSIX compatible. This took Visual Studio out of the equation.</p>
<p>So, to build a POSIX C application on Windows I recommend using NetBeans IDE (6.8 at this time) &#8211; it has lots of features, making it a good development environment. It has a C/C++ plugin implementing support for these two languages.</p>
<p>Now, to make it compile, I needed gcc, so I installed CygWin. It works fine on Windows 7 x64. When installing CygWin, you must make sure you install <strong>gcc </strong>(and all that it depends on), <strong>libncurses-devel</strong>, <strong>readline </strong>and <strong>libreadline </strong>(from the Devel category). Of course I didn&#8217;t unselect anything from the default list. I just added these libraries.</p>
<p>To make CygWin more friendly (I have this friendliness obsession  <img src='http://www.bogdanirimia.ro/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  ) I&#8217;m using <a href="http://code.google.com/p/puttycyg/" target="_blank">PuTTYcyg</a>, a great expansion of PuTTY. I am also editing the <strong>.vimrc</strong> file just like in the post for Solaris. The <strong>.bashrc</strong> file should exist and contain also the line</p>
<pre class="brush: bash; title: ; notranslate">

export PS1='[\u@\h \w]\$ '
</pre>
<p>Now, I&#8217;m having this Linux-like environment running on my Windows. All I have to do is to install the mysql-connector and to configure NetBeans to compile on CygWin.</p>
<p>To install mysql-connector on CygWin I had to download the sources of MySQL (not only mysql-client, I couldn&#8217;t compile it). MySQL includes the client when compiling. These are the steps to follow if you want mysql-client on CygWin</p>
<ul>
<li>make sure you have gcc, libncurses-devel, readline and libreadline</li>
<li>download the mysql sources and extract them in a folder in CygWin</li>
<li>copy the file <strong>ttydefaults.h</strong> (from a working Unix system) in <strong>/usr/include/sys/</strong></li>
<li>go into the sources folder and run<strong> ./configure &#8211;without-readline CFLAGS=-O2 </strong>(you can add &#8211;without-server, but I didn&#8217;t test that)</li>
<li>wait for it to finish (took me about 20 minutes). Hopefully you won&#8217;t have any errors</li>
<li>run <strong>make &amp; make install</strong> and wait for it to finish (should take more than half an hour, but maybe your PC is faster)</li>
</ul>
<p>This should be it. Of course, if you run configure and have a missing library error, install that library and rerun configure. If you have an error during compilation, make sure to run <strong>make clean</strong> before retrying!</p>
<p>Ok. Now to make sure that NetBeans compiles your C application successfully, follow these steps:</p>
<ul>
<li>create a new C project and paste the following code</li>
</ul>
<pre class="brush: cpp; title: ; notranslate">

/*
 * File:   main.c
 * Author: Bogdan
 *
 * Created on February 18, 2010, 1:09 PM
 */

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#include &lt;my_global.h&gt;
#include &lt;mysql.h&gt;

int main(int argc, char** argv) {

 printf(&quot;Running just fine!\n&quot;);
 printf(&quot;MySQL client version: %s\n&quot;, mysql_get_client_info());

 return (EXIT_SUCCESS);
}
</pre>
<ul>
<li>go to Tools-&gt;Options and select C/C++ tab. Click the &#8220;Add&#8221; button and select the &#8220;bin&#8221; folder of your CygWin installation (mine is C:\cygwin\bin). The other two fields should auto-complete. Click OK, make sure all paths are correct and then click OK again.</li>
<li>right-click your project in Projects pane. Go to Properties</li>
<li>in the Build-&gt;C Compiler section, add the value &#8220;c:/cygwin/usr/local/include/mysql&#8221; in the &#8220;Include directories&#8221; field (supposing that you installed CygWin in C:\cygwin)</li>
<li>in the Build-&gt;Linker section, add the value &#8220;c:/cygwin/usr/local/lib/mysql&#8221;</li>
<li>in the same section (Linker) click on the button near &#8220;Libraries&#8221;, click &#8220;Add Library&#8221;, go to &#8220;/usr/local/lib/mysql&#8221; and add &#8220;libmysqlclient.a&#8221;</li>
<li>after clicking &#8220;Select&#8221;, press again &#8220;Add Library&#8221;, go to &#8220;/lib&#8221; and add &#8220;libz.a&#8221;</li>
<li>click OK and then OK again</li>
</ul>
<p>Now your application should compile and run successfully. So now I&#8217;m ready to develop and test a C application with MySQL connection on my PC and then to deploy it on a UNIX server (of course it will need to be recompiled).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bogdanirimia.ro/c-application-with-mysql-built-with-netbeans-on-cygwin/262/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

