BatteryApp
The n0r1sk battery app is an Android App you can install through Google Play.
Contents
About
You may ask yourselves why...
...we wrote another Battery Indicator App ==
- First of all - We wanted to get in tough with Androids SDK
- Second we wanted the App to behave a little different to 'others' on Google Play and
- Third we build up our knowledge on Activities/Services/Broadcast Receivers to get ready for our next App
The main components involved
- The Activity - handles the main screen and menu buttons
- The Intent Service - to put the notification, reschedules himself using the AlarmManager and stops afterwards (frees memory)
- The Receiver - to handle the auto start 'android.intent.action.BOOT_COMPLETED'
Download
Binary
Get it on Google Play (tm):
Source
The Eclipse project source is available under the terms and conditions of the GNU Gpl v. 2 or later.
Package | Download |
---|---|
battery-1.31-src | Link |
battery-1.2-src | Link |
battery-1.1-src | Link |
battery-1.0-src | Link |
Version road map/progress
<mantis>5</mantis>
Used tools
The icon problem
The battery app displays the current battery load in percentage from 0 to 100. Therefore we need 101 icons to display all levels. Android currently supports four types of screen resolutions (beside tablets) with different sizes. As we are using the status bar of Android it is useful to make different color sets.
So you need:
- 101 icons * 4 sizes * 2 colors = 808 icons
It would be possible to make a set of 101 per hand but doing 808 is more than boring. :)
Programming an icon drawing script
In this situation we decided to make a tool that should draw the icons with the correct number in it. This tool should save the icons per default as e.g. 24x24 SVG graphics because they could be easily resized without the loss of details.
Perl and SVG
Perl and SVG are good friends if you only need to write SVG files. SVG files are XML formatted files. What you need is a kind of wrapper around the different types of XML data. PERL::SVG is what we used for that and what is easily available on a lot of Linux distributions.
The script
<source lang=perl>
- !/usr/bin/perl
use SVG;
for (my $i = 0; $i <= 100; $i++){ #createSvg(BatteryFillColor, BatteryStrokeColor, TextFillColor, TextString, FolderName, FilePrefix) createSvg("#000000", "#000000", "#ffffff", $i, "black", "b"); createSvg("#ffffff", "#ffffff", "#000000", $i, "white", "w"); createSvg("#99CC00", "#99CC00", "#000000", $i, "green", "g"); createSvg("#33B5E5", "#33B5E5", "#000000", $i, "blue", "bl"); createSvg("#AA66CC", "#AA66CC", "#ffffff", $i, "violet", "v"); createSvg("#FFBB33", "#FFBB33", "#000000", $i, "orange", "o"); createSvg("#FF4444", "#FF4444", "#000000", $i, "red", "r"); }
sub createSvg{
my $BatteryFillColor = shift; my $BatteryStrokeColor = shift; my $TextFillColor = shift; my $TextString = shift; my $FolderName = shift; my $FolderDensity = "raw"; my $FilePrefix = shift;
#print $BatteryFillColor; #print $BatteryStrokeColor; #print $TextFillColor; #print $TextString; #print $FolderName; #print $FilePrefix;
if ( !(-d './'.$FolderName)){ qx(mkdir -p ./$FolderName/$FolderDensity); }
my $svg= SVG->new(width=>24,height=>24);
my $b_xv = [2,5,5,22,22,5,5,2]; my $b_yv = [9,9,6,6,19,19,16,16];
$points = $svg->get_path( x => $b_xv, y => $b_yv, -type => 'path', -closed => 'true' );
if ( $TextString <= 9){ $tag = $svg->path( %$points, id => 'battery_1', style => { 'fill' => $BatteryFillColor, 'stroke' => $BatteryStrokeColor, 'stroke-width' => '1.0', 'stroke-miterlimit' => '4', #'stroke-opacity' => '1', 'stroke-dasharray' => 'none' } );
my $t_percent = $svg -> text( id=>'text_1', x=>16, y=>20, 'fill' => $TextFillColor, 'font-family'=>'monospace', 'font-weight'=>'bold', 'transform'=>'scale(0.8)' )->cdata($TextString); } elsif( $TextString >= 10 && $TextString <= 99 ) { $tag = $svg->path( %$points, id => 'battery_1', style => { 'fill' => $BatteryFillColor, 'stroke' => $BatteryStrokeColor, 'stroke-width' => '1.0', 'stroke-miterlimit' => '4', #'stroke-opacity' => '1', 'stroke-dasharray' => 'none' } ); my $t_percent = $svg -> text( id=>'text_1', x=>10, y=>20, 'fill'=>$TextFillColor, 'font-family'=>'monospace', 'font-weight'=>'bold', 'transform'=>'scale(0.8)' )->cdata($TextString); } elsif( $TextString == 100 ){ $tag = $svg->path(
%$points, id => 'battery_1', style => { 'fill' => $BatteryFillColor, 'stroke' => $BatteryStrokeColor, 'stroke-width' => '1.0', 'stroke-miterlimit' => '4', #'stroke-opacity' => '1', 'stroke-dasharray' => 'none', } ); my $t_percent = $svg -> text( id=>'text_1', x=>6, y=>20, 'fill'=>$TextFillColor, 'font-family'=>'monospace', 'font-weight'=>'bold',
'transform'=>'scale(0.8)'
)->cdata($TextString);
}
my $out = $svg->xmlify; #print $out; open (MYFILE, '>./'.$FolderName.'/'.$FolderDensity.'/ic_stat_notify_'.$FilePrefix.$TextString.'.svg'); print MYFILE $out; close (MYFILE);
}
</source>
The converter script
<source lang=bash>
- !/bin/bash
FILES=./raw/* DENSITY=(ldpi mdpi hdpi xhdpi)
for DENS in ${DENSITY[@]}; do
mkdir ./$DENS for f in $FILES do FILENAME=`echo $f | awk -F "/" '{print $3}' | awk -F "\." '{print $1}'`
case "$DENS" in "ldpi") rsvg -w 18 $f ./$DENS/$FILENAME.png ;; "mdpi") rsvg -w 24 $f ./$DENS/$FILENAME.png ;; "hdpi") rsvg -w 36 $f ./$DENS/$FILENAME.png ;; "xhdpi") rsvg -w 48 $f ./$DENS/$FILENAME.png ;; esac
done
done </source>
The string problem
If you need text (strings) in different languages (and this is a common task) the Android platform supports you by providing helper calls (R.java). It is very easy to add new translated strings because Android will pick it up automatically. But if you want to format these strings via HTML you have two problems.
The escape char problem
Android needs to have all HTML tags correctly escaped or they will not get parsed from the SDK. On the following site you could convert HTML to escaped HTML.
The Java code to load these strings from resources
<source lang=java> Resources res = getResources(); String text = String.format(res.getString(R.string.>YOURSTRINGRESOURCE<)); CharSequence styledText = Html.fromHtml(text); </source>
After that operation inside the CharSequence is the correct formatted text.
The Java code to make html-links clickable in your TextView
<source lang=java> TextView textview = (TextView) this.findViewById(R.id.>YOURTEXTVIEW<); textview.setMovementMethod(LinkMovementMethod.getInstance()); textview.setText(styledText); </source>
This updates the defined TextView to make html-format shown and linked to the installed browser.
Tested devices
Hardware type | Status |
---|---|
LG P990 / Optimus / Speed 2x | Working |
Motorola DROID / MILESTONE | Working |
Samsung I9000 / Galaxy S | Working |
HTC Desire S | Working |
Licence
GNU General Public Licence, version 2
Copyright (C) 2012 Markus, Bernhard, Martin and Mario
This is how to contact us!
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
You will find a copy of the GNU Library General Public License
here; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Boston, MA 02110-1301, USA.
last edit 16.06.2013 by Mario