Tuesday, May 22, 2012

Android Sample Code: Battery Widget

*If you are new to Android, you should read this post first: Sample Code Battery Status

For a widget to work, below are some important items:
  • AndroidManifest.xml must be altered
  • Must create a layout file for widget provider
  • Must create a layout for widget
  • Main class source code
First, let see the AndroidManifest.xml:
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.sample.batterystatuswidget"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk android:minSdkVersion="7" />  
   <application  
     android:icon="@drawable/widget_battery01"  
     android:label="@string/app_name" >  
     <receiver android:name=".BatteryStatusWidgetActivity" android:label="@string/app_name">  
       <intent-filter>  
         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
       </intent-filter>  
       <meta-data android:name="android.appwidget.provider"  
                            android:resource="@xml/widget_main" />  
     </receiver>  
   </application>  
 </manifest>  
Take note that the different is on the android.appwidget.action.APPWIDGET_UPDATE and additional "<meta-data ..".

Next, since in the AndroidManifest.xml above it refers to the @xml/widget_main, then widget_main.xml must exist:
 <?xml version="1.0" encoding="utf-8"?>  
 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
   android:initialLayout="@layout/widget_layout"  
   android:minWidth="40dp"  
   android:minHeight="40dp"  
   android:updatePeriodMillis="30000">  
 </appwidget-provider>  
Take note that updatePeriodMillis is referring to how frequent the widget will trigger itself to be refreshed.

Next, since in widget_main.xml above it refers to @layout/widget_layout, then widget_layout.xml must exist:
 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/relativeLayout1"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:gravity="center" >  
     <ImageView  
       xmlns:android="http://schemas.android.com/apk/res/android"  
       android:id="@+id/imageView1"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_alignParentLeft="true"  
       android:layout_alignParentTop="true"  
       android:src="@drawable/widget_batt_frame01" />  
           <LinearLayout  
             xmlns:android="http://schemas.android.com/apk/res/android"  
             android:id="@+id/layout"  
             android:layout_width="wrap_content"  
             android:layout_height="wrap_content"  
             android:layout_centerHorizontal="true"  
             android:layout_centerVertical="true"  
             android:layout_margin="10dip"  
             android:gravity="center_vertical"  
             android:orientation="vertical"  
             android:paddingTop="0dip" >  
             <TextView  
               android:id="@+id/widget_text"  
               style="@android:style/TextAppearance.Medium"  
               android:layout_width="wrap_content"  
               android:layout_height="0dp"  
               android:layout_gravity="center_vertical"  
               android:layout_weight="1"  
               android:gravity="center_horizontal|center_vertical"  
               android:text="@string/battery_level"  
               android:textSize="12dp"  
               android:textColor="#000000"/>  
           </LinearLayout>  
 </RelativeLayout>  
This layout will create a drawable image and a text on top of it. In this case, the text will display the remaining battery in percentage.

Finally, the source code for the main class:
 public class BatteryStatusWidgetActivity extends AppWidgetProvider {  
   /** Called when the activity is first created. */  
      private String batteryLevel = "init";  
      private int widgetImageFrame = R.drawable.widget_batt_frame01;  
      @Override  
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
           context.getApplicationContext().registerReceiver(this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));  
           updateView(context);  
      }  
      @Override  
      public void onReceive(Context context, Intent intent) {  
           int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);  
           int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);  
           int level = -1;  
           level = (rawlevel * 100) / scale;  
           batteryLevel = Integer.toString(level);  
           widgetImageFrame = R.drawable.widget_batt_frame01;  
           updateView(context);  
           super.onReceive(context, intent);  
      }  
      public void updateView(Context context) {  
           RemoteViews thisViews = new RemoteViews(context.getApplicationContext().getPackageName(), R.layout.widget_layout);  
           thisViews.setTextViewText(R.id.widget_text, batteryLevel);  
           thisViews.setImageViewResource(R.id.imageView1, widgetImageFrame);  
           ComponentName thisWidget = new ComponentName(context, BatteryStatusWidgetActivity.class);  
           AppWidgetManager.getInstance(context).updateAppWidget(thisWidget, thisViews);  
      }  
Take note the following:
  • updateView(context) function is needed to refresh the widget
  • R.drawable.widget_batt_frame01 is referring to the image that registered to layout widgetImageFrame

Happy Coding Android!

That's all for now!
*I use http://codeformatter.blogspot.com/ to place codes in this post.

:: aerobrainTech ::

Thursday, April 26, 2012

Google Drive vs Microsoft SkyDrive vs Dropbox


Just 2 days after Microsoft announced the SkyDrive, Google rise with the new Google Drive.

Below are couple of pictures during the installation:



Here is my comparison:


Free Storage
5GB
7GB
(25GB for existing user!)
2GB
Maximum File Size
10GB
2GB
Unlimited (desktop)
300MB (online)
Sync Immediately upon File Change
YES
YES
YES
Notification upon File Change
NO
NO
YES
Sync over LAN
NO
NO
YES
Speed Notification during Sync
NO
NO
YES

:: aerobrainTech ::

Tuesday, April 24, 2012

SkyDrive: Free 25GB, Sync Like Dropbox

Yesterday, the official Windows 8 Blog announced that SkyDrive now can be synchronized to your desktop thru a client application. The good news is that for the existing Windows Live account, you will get FREE upgrade from 7GB to 25GB!

Scroll down below to see my comparison the SkyDrive vs Dropbox.

Below are my screenshot during the installation of SkyDrive client application on Windows 7.





 Here is my comparison:


Free Storage

7GB
(25GB for existing user!)
2GB
Maximum File Size
2GB
Unlimited (desktop)
300MB (online)
Sync Immediately upon File Change
YES
YES
Notification upon File Change
NO
YES
Sync over LAN

NO
YES
Speed Notification during Sync
NO
YES

SkyDrive won on the storage size but lack of minor things compared to Dropbox. Hopefully SkyDrive team can continuously improve this cool service!

Source: Making personal cloud storage for Windows available anywhere, with the new SkyDrive

:: aerobrainTech ::

Monday, April 16, 2012

How Strong is Your Password?


I've read an interesting article about "Realistic Password Strength Estimation" from Dropbox tech blog.

Interesting quote:
"Through 20 years of effort, we've successfully trained everyone to use passwords that are HARD for humans to REMEMBER, but EASY for computers to GUESS."


In short, according to the writer's estimation complicated password such as "Tr0ub4dor&3" is easier for the computer to guess in comparison to "correcthorsebatterystaple".


source: http://tech.dropbox.com/?p=165


:: aerobrainTech ::

Friday, April 6, 2012

Bitcasa (Beta) Cloud Storage Preview

Yesterday I received an email from Bitcasa telling me that I have been selected to test the beta version. The good news is that I will be able to invite 10 people to join me as the beta tester.

In this preview, I will show some basic function of Bitcasa.
Bitcasa Folder List (client)

  Bitcasa Folder List (on web or also called portal)

 Right Click to Cloudify a folder
Once the Bitcasa Client is installed, you can turn any folder to cloud folder (cloudify). The folders that has been cloudify will appear in the Bitcasa online portal. If you have multiple machines, you have the choice to sync with the cloudified folder via the Bitcasa Folder List on the client application as shown above.

9 exabytes folder size!
Once you cloudify a folder you will see the size of the folder is now about 9 exabytes!! This is equivalent to 9000 petabytes or 9000000TB!! (you need me to keep converting for you?)

In other words, you storage space now become almost infinity. However, since this is on the cloud, your internet connection must be fast enough to download and upload the files.

I have shown the great features, now lets talk about the weaknesses on the current beta versions:
  • No upload/download/sync indicator - The client application does not tell whether a folder/file is being uploaded, downloaded or synchronized. This situation leave me at a confusion state.
  • Files appear in folder but cannot open or hang when picked - In the connected folder, which you cloudify from othe machines but you want to access from another, upon opening the file or folder, the windows hangs for quite sometime. It confuse me again whether the files has been downloaded or it directly pulled from the cloud.

I also get this error upon opening pdf file in the cloudified folder.

In conclusion, I would say that the Bitcasa carries very good idea in terms of storing in the cloud. Their server also quite fast as I've been able to download a 30MB file at 1.3MB/s (that's megabytes not megabits). But those little things like indication and smoothness of synchronization will please customer more as what Dropbox has been good at doing it.

Look forward to the improvement of this cool service! Oh ya, for those who interested to test the Beta, please leave your email in the comment.

Thanks!

:: aerobrainTech ::