Category Archives: Development

This is where i blog about my developer projects. I try to keep it in english.

Launch Boxer mail client from Android App

I spent a good few hours trying to launch Boxer e-mail client from an app i’m currently working on. I thought it would be piece of cake when i read throught their documentation, and sure, it was easy on iOS using the url scheme awemailclient://

The documentation did not mention the Android version of Boxer don’t support this. It took me a few hours to realize url scheme was a dead end when it comes to Android, and another bunch of hours to find out how to do it.

The solution, as always, is quite simple and also make sence. You pretty much do a regular intent of type message/rfc822. What you need to add to that is code to show only Boxer, not all apps on the phone that can handle e-mails. And that is done with one line of code.

This example is in C# using Xamarin, but i’m sure it’s easy to translate to Java if needed:

//Create your intent
var intent = new Intent(Intent.ActionSend);
//This is what makes boxer the only option!
intent.SetPackage("com.boxer.email");
//Set the type
intent.SetType("message/rfc822");
//Put in recipient, subject and body
intent.PutExtra(Intent.ExtraEmail, new string[] { "recipient@contoso.com" });
intent.PutExtra(Intent.ExtraSubject, "Hello World");
intent.PutExtra(Intent.ExtraText, "This is a message!"));
Android.App.Application.Context.StartActivity(intent);

 

JonikaBot – Möt min Twitterbot

Lite då och får man lust att hitta på något kul projekt hemma, något annorlunda än det man gör dagligen på jobbet. Men ofta är det väldigt svårt att komma på bra idéer, och utan bra idéer blir det ofta tråkigt att lägga tid på saker.

Men plötsligt kom jag på vad jag ville bygga, en Twitterbot! Det dök genast upp den hel del idéer på vad den ska göra (men jag avslöjar bara dom som är klara just nu 🙂 ). Min första idé var en “Smurfalizer”. Det har ju funnits en som försmurfar webbsidor i flera år, så varför inte göra det samma med tweets? Just nu används en ordlista med knappt 400 verb. Det är dessutom viss slump i mina beräkningar, så inte alla verb översätts. Om någon vill testa den, se till att ha gott om verb i texten. Tagga en tweet med #jonikabotsmurf, så bör det komma ett svar inom ca 3 minuter. Så här kan ett svar se ut:

Den andra idén var att kombinera mitt intresse för programmering med mitt intresse för hockey, och i synnerhet Färjestad.  Jag har en hel del idéer inom ämnet, men den första funktionen är att ta den tweetande användarens profilbild i storleken 400x400px, lägga på ett Färjestademblem i nedre vänstra hörnet och skicka tillbaka bilden i en tweet. Om någon känner ett behov av en snyggare profilbild så skicka en tweet med vilket innehåll som helst och taga den med #jonikabotfbk

Boten kommer säkert byggas på med funktioner, så stay tuned 🙂

När boten väl var i hyfsat skick insåg jag nästa bekymmer: Var ska jag köra den? Tiden då hemmet kryllar av servrar och datorer är förbi, och nu för tiden är det mest paddor och telefoner i huset, förutom iMac:en (som jag för övrigt utvecklar på hemma). En tur till NetOnNet gjorde mig en Raspberry Pi 3 rikare, och efter 1 knapp timma var Raspbian Jessie Lite installerat, och burken uppkopplad på Wifi. Eftersom jag lever med C# så föll valet på Mono, vilket var lätt att installera även på Raspberry.

OBS! Boten är högst experimentell och innehåller säker en del buggar, så kommer det inget svar kan den antingen vara nere för tillfället, eller så har du lyckats hitta en bugg 🙂

Så här ser den extremt snygga konsolen ut:

How i automate website creation with Powershell

If you, like me, create lots and lots of websites almost every day you probably noticed that a lot of the things you do are repetitive.

I have a lot of rules that almost all my websites follow.

  • I name the pool, the site and the folder the same (like site.domain.com)
  • I always add a binding following the pattern 17000 + site id, so a site with site id 13 has a binding for localhost:17013
  • I set idle time-out to 0
  • I add a scheduled recycle at 02:00 (for load balanced environments att set it at 03:00 for the other node)
  • I always create a scheduled job to take care of log rotation

To avoid making misstakes, and to make sure it gets done the same way every time, i use this Powershell. It’s not perfect, but it works for me, and it does exactly what i need it to.

In the end of the script, where the scheduled job is created, i use a little application called LogRotate which i may publish on the internet. In the mean time, you can use it as a way to add a scheduled task.

Maybe it will work for you as well, or maybe you can pick the parts you need. Enjoy!

#
# PowerShell Script used to create new websites. Requires Eleveted PowerShell Console!
# This script: 
# 	- Creates a new Applicatiopn Pool with our preferred settings
#	- Creates a new website using that pool
#	- Create a scheduled job to manage logfiles
#
# Make sure to edit these parameters before you run the script:
# - $AppPoolName Sets the name of AppPool and WebSite
# - $AppPoolIdentityName and $AppPoolIdentityPwd to set the identity
# - $SiteDirectory is the base path to the folder holding websites
#

#########################
# Import modules needed #
#########################

Import-Module WebAdministration

######################
# Set all parameters #
######################

#AppPool parameters

$AppPoolName 			= "Test.Domain.Com"	            #Name of ApplicationPool
$AppPoolDotNetVersion 	= "v4.0"						#.NET version for pool
$UseAppPoolIdentity     = 0                             #Set to 1 to user app-pool identity instead of domain account
$AppPoolIdentityName 	= "domain\sysAccount"			#Identity to execute pool
$AppPoolIdentityPwd 	= "P@ssw0rd"					#Identity password
$AppPoolRecycleTime		= "02:00"						#Default time is 02:00, on load balanced environments, set first host to 02:00 and second to 03:00

#WebSite parameters
$SiteName 				= $AppPoolName					#WebSite name, by default the same as name of pool
$SiteDirectory 			= "C:\WebSite\WebSite\Sites\"	#Base folder, where the site should be created. Site folder is created automatically

###########################
# Create application pool #
###########################

#Create Pool and store it in $AppPool
New-WebAppPool ñName $AppPoolName
$AppPool = Get-Item IIS:\AppPools\$($AppPoolName) 

#Stop AppPool
$AppPool | Stop-WebAppPool 

#Set additional properties
#Set AppPool Identity and password
if($UseAppPoolIdentity -eq 0)
{
    $AppPool.ProcessModel.identityType = 3
    $AppPool.ProcessModel.Username = $AppPoolIdentityName 
    $AppPool.ProcessModel.Password = $AppPoolIdentityPwd
}

#Set idle time-out to zero
$AppPool.ProcessModel.IdleTimeout = "0"

#Set desired .NET version
$AppPool.ManagedRuntimeVersion = $AppPoolDotNetVersion

#Set periodic recycle schedule
Set-ItemProperty -Path IIS:\AppPools\$($AppPoolName) -Name Recycling.periodicRestart.schedule -Value @{value=$AppPoolRecycleTime}

#Save and start AppPool
$AppPool | Set-Item
$AppPool | Start-WebAppPool

#Clear periodic restart time (defaults to 1740 minutes)
Set-ItemProperty -Path IIS:\AppPools\$($AppPoolName) -Name Recycling.periodicRestart.time -Value "00:00:00"


##################
# Create website #
##################

#Create Website Directory
New-Item -ItemType directory -Path "$($SiteDirectory)$($SiteName)"

#Create Website and set default binding
New-Website -Name $SiteName -PhysicalPath "$($SiteDirectory)$($SiteName)" -ApplicationPool $AppPoolName -Port "170$($WebSite.id)" -HostHeader "" -IPAddress "*"

#Get the newly created website and store it in $WebSite
$WebSite = Get-Item IIS:\Sites\$($SiteName) 

#Set additional binding based on AppPool name
New-WebBinding -Name $SiteName -IPAddress "*" -Port 80 -HostHeader $AppPoolName.ToLower()

#Start Website
$WebSite | Start-WebSite


######################################
# Create scheduled job for LogRotate #
# This only works on Win2012         #
######################################
#Scheduled task, LogRotate
#Theese values shouldn't be neccesarry to edit. 
#Make sure LogRotate exists under Program Files and that you WebSite-folder point to the correct location.
$TaskName 				= "LogRotate W3SVC$($WebSite.id)"
$TaskDescription 		= "Archives old logfiles for site $($SiteName)"
$TaskActionCommand		= "C:\Program Files\LogRotate\LogRotator.App.exe"
$TaskArguments			= "C:\WebSite\Logs\W3SVC$($WebSite.id) C:\WebSite\Logs\Backup W3SVC$($WebSite.id) *.log 31 true"
$TaskPath				= "LogRotate"

$PsHost = host
$OsVer = [environment]::OSVersion.Version

If(($OsVer.Major -ge 6) -and ($OsVer.Minor -ge 2) -and ($PsHost.Version.Major -ge 4))
{
    Import-Module ScheduledTasks 
	$TaskAction = New-ScheduledTaskAction -Execute $TaskActionCommand -Argument $TaskArguments
	$TaskTrigger = New-ScheduledTaskTrigger -Daily -AT "05:00"
	$Task = New-ScheduledTask -Action $TaskAction -Trigger $TaskTrigger -Description $TaskDescription
	Register-ScheduledTask $TaskName -InputObject $Task -TaskPath $TaskPath
}
Else
{
	"You need a PowerShell 4 and Windows Server 2012 to create Scheduled Task!"
	"You'll need to configure LogRotate manually."
}

 

A10 aFlex: Temporary blacklist clients based on failed logins

I was asked if i could find a way to temporary blacklist clients after x failed logins. The reason was to avoid lockout in AD when users changed their passwords, but forgot to change the password in their phones connected through ActiveSync.

If a phone connects to ActiveSync with the wrong password, resulting in a error 401, i should be able to catch that and save the IP in a table. If it re-occurs 4 times or more in less then 10 minutes than the A10 should remember this and drop the traffic, resulting in request not being sent to the mail server and the AD account not being locked out.

Thats the theory. Below is my current solution, very much in POC state but my initial testing looks promising 🙂 But i’m sure there are many aFlex/TCL gurus out there to correct me. I’m certainly not one of them!

A few notes:

  • Because i write failed attempt to temp-table, the blacklist is 10 minutes after the last failed logins. This is probably longer than needed.
  • maxfadiledrequests is set to 5, because it always start at 2 for me.. and i’m not sure why. I did expect it to start at 1 since the initial status from server is 401, but not 2.
  • holdtime is the the in seconds the address is blocked
  • You should probably not write to log in a production environment
when RULE_INIT {
    set ::maxfailedrequests 5
    set ::holdtime 600
}

when HTTP_REQUEST {
    set key [IP::client_addr]
    if { [table lookup "blacklist" $key] != "" } {
        reject
        log "$first_key is blocked"
        return
    }

    if { [table lookup tmp_table $key] == "" } {
        table set tmp_table $key 1 indef $::holdtime
        log "$key's session table created."
        return
    }
}

when HTTP_RESPONSE {
    if { ([HTTP::status] == 401) } {
        set count [table incr tmp_table $key]
        log "failed request count: $count"
        if { $count > $::maxfailedrequests } {
            table add "blacklist" $key "blocked" indef $::holdtime
            log "$key blacklisted for $::holdtime seconds "
            table delete tmp_table $key
            reject
            return
        }
    }
}

 

Xamarin Droid: Putting LocationManager in a separate class

I almost always put all logic in som sort of separate classes in my projects. I simply think it looks better, is way more easy to understand and the better way.

When using Xamarin to create cross-plattform apps, you probably use dependency injection to minimize ugly code blocks like this.

if (Device.OS == TargetPlatform.iOS) {
}else{
}

That also means you pretty much need to create interfaces that you can use for all plattforms, which leads to this topic: You need to put logic in classes.

I had a bit of a struggle to implement location services on Android this way, hence this post. Most bloggs and articles i found used the main activity. For iOS it was very straight forward, so I’m leaving that part out.

And to be clear: This is my POC-code, not intended to copy and paste into production!

First of all, i had to create an interface i could use for iOS and Droid. I kept it simple, and only included stuff i needed: The current location, a method to initialize everything and a event to trigger when position changes.

public interface ILocationManager
{
	Tuple<double, double> GetCurrentLocation();
	void StartLocationUpdates();
	event EventHandler LocationUpdated;
}

I also created i class holding the main activity context. I needed this to be able to use the context from my class.

public class Config {
	public static MainActivity context = null;
}

In my MainActivity i set the context of the class above:

Config.context = this;

From my cross-plattform code i need to get a instance of ILocationManager for the current plattform. For this, i use the DependencyService. I Also call my StartLocationUpdates method to start getting location updates:

private ILocationManager LocManager = DependencyService.Get<ILocationManager> ();
LocManager.StartLocationUpdates ();

So far, all code except the config class is the same for both iOS and Android. But this is where i hit the wall on Android. I spent days trying to figure out how to create a class using my interface that would work in the same way my iOS code did. This is my solution

First, make sure to put this line above the namespace in your class to get dependency injection to work:

[assembly: Xamarin.Forms.Dependency (typeof(NautiCalc.Droid.LocationManager))]

The second thing i found important was to decorate your class with the Activity attribute. This makes sure your activity is inserted into the Manifest, which is important. You also need to inherit Activity class, and implement your interface.

[Activity]
public class LocationManager : Activity, NautiCalc.ILocationManager, ILocationListener

The rest is kind of straight forward. You need to implement a few methods because of the inheritance of Activity, and of course your own interface.

Here is my complete code:

using System;
using Android.Content;
using Android.App;
using Android.Locations;
using Android.OS;
using Android.Widget;

[assembly: Xamarin.Forms.Dependency (typeof(NautiCalc.Droid.LocationManager))]
namespace NautiCalc.Droid
{
	[Activity]
	public class LocationManager : Activity, NautiCalc.ILocationManager, ILocationListener
	{
		// event for the location changing
		public event EventHandler LocationUpdated = delegate {};

		protected Android.Locations.LocationManager locMgr;

		public Android.Locations.LocationManager LocMgr {
			get { return this.locMgr; }
		}

		public Tuple<double, double> GetCurrentLocation ()
		{
			Location lastKnown = locMgr.GetLastKnownLocation (Android.Locations.LocationManager.NetworkProvider);
			if (lastKnown != null) {
				return Tuple.Create (lastKnown.Latitude, lastKnown.Longitude);
			}
			return null;
		}

		public void StartLocationUpdates ()
		{
			locMgr = (Android.Locations.LocationManager)Config.context.GetSystemService (Context.LocationService);

			if (locMgr.AllProviders.Contains (Android.Locations.LocationManager.GpsProvider) && locMgr.IsProviderEnabled (Android.Locations.LocationManager.GpsProvider)) {
				locMgr.RequestLocationUpdates (Android.Locations.LocationManager.GpsProvider, 2000, 1, this);
			} 
		}

		protected override void OnPause ()
		{
			base.OnPause ();
			locMgr.RemoveUpdates (this);
		}

		public void OnProviderEnabled (string provider)
		{
		}

		public void OnProviderDisabled (string provider)
		{
		}

		public void OnStatusChanged (string provider, Availability status, Bundle extras)
		{
		}

		public void OnLocationChanged (Location location)
		{
			LocationData CurrentLocation = new LocationData ();
			CurrentLocation.Altitude = location.Altitude;
			CurrentLocation.Course = (double)location.Bearing;
			CurrentLocation.Latitude = location.Latitude;
			CurrentLocation.Longitude = location.Longitude;
			CurrentLocation.Speed = (double)location.Speed;
			LocationUpdated (this, new LocationUpdatedEventArgs (CurrentLocation));
		}
	}
}

Not that hard, once you figured it out. Happy coding!