If you have a time like 24:00 or even 28:12 in SQL and want to convert it to a normal time this is the way to do it:
SELECT [time] = RIGHT(’0′+ RTRIM(CAST(substring(’28:12′,1,2)%24 as NCHAR(2))),2)+’:'+substring(’28:12′,4,5)
Sample results:
08:00=08:00
23:45=23:45
24:00=00:00
28:12=04:12
If you know a better way, please let me know!
Continue reading...
If you have some activities in an android application and what to close all activities without having to worry about the back button, you can register a broadcast receiver that get triggered when you push the logout button.
Lets create a new class that we can inherit on all activities that requires login:
public class LogedInActivity extends Activity {
public class LogoutReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.package.ACTION_LOGOUT")) {
finish();
}
}
}
private LogoutReceiver logoutReceiver;
@Override
protected void onDestroy() {
// Unregister the logout receiver
unregisterReceiver(logoutReceiver);
super.onDestroy();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
logoutReceiver = new LogoutReceiver();
// Register the logout receiver
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_LOGOUT");
registerReceiver(logoutReceiver, intentFilter);
}
}
Then when you press the logout button you can close all open activities by just sending a broadcast intent:
// on your logout method:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_LOGOUT");
c.sendBroadcast(broadcastIntent);
Continue reading...
This is a PHP script to determine if an image is an valid 9 patch PNG image that can be used in an android application
<?php
$src = dirname(__FILE__).'/btn.9.png'; //change me
if(function_exists('imagecreatefrompng') && function_exists('imagecolorat')) {
$isvalid = IsValid9PatchImage($src);
print $src . ' is a ' . ($isvalid ? 'valid':'invalid') . ' 9patch image';
}
else {
print "Could not check if ".$src." is a valid 9patch image. the php functions imagecreatefrompng and imagecolorat is required";
}
function IsValid9PatchImage($pngRel) {
$im = imagecreatefrompng($pngRel);
$height = imagesy($im);
$width = imagesx($im);
//Not a valid png image?
if($height <= 0 || $width <= 0) {
return false;
}
$anyBlack = false;
for($x = 0; $x < $width; $x++){
$y = 0;
$rgba = imagecolorat($im,$x,$y);
$alpha = ($rgba & 0x7F000000) >> 24;
$red = ($rgba & 0xFF0000) >> 16;
$green = ($rgba & 0x00FF00) >> 8;
$blue = ($rgba & 0x0000FF);
$isAlpha = false;
$isBlack = false;
if($alpha == 0 && $red == 0 && $green == 0 && $blue == 0) {
$isBlack = true;
$anyBlack = true;
}
else if($alpha == 127) {
$isAlpha = true;
}
//If the top left pixel is not transparent then false
if($x == 0 && $isAlpha == false) {
//die('If the top left pixel is not transparent then false. x:' . $x . ' y:' . $y);
return false;
}
//If the top right pixel is not transparent then false
if($x == $width-1 && $isAlpha == false) {
//die('If the top right pixel is not transparent then false. x:' . $x . ' y:' . $y);
return false;
}
//If it is not completly transparent or black, then false
if(!$isAlpha && !$isBlack) {
//die('If it is not completly transparent or black, then false. x:' . $x . ' y:' . $y);
return false;
}
}
if($!anyBlack) {
return false;
}
$anyBlack = false;
for($y = 0; $y < $height; $y++){
$x = 0;
$rgba = imagecolorat($im,$x,$y);
$alpha = ($rgba & 0x7F000000) >> 24;
$red = ($rgba & 0xFF0000) >> 16;
$green = ($rgba & 0x00FF00) >> 8;
$blue = ($rgba & 0x0000FF);
$isAlpha = false;
$isBlack = false;
if($alpha == 0 && $red == 0 && $green == 0 && $blue == 0) {
$isBlack = true;
$anyBlack = true;
}
else if($alpha == 127) {
$isAlpha = true;
}
//If the top left pixel is not transparent then false
if($y == 0 && $isAlpha == false) {
//die('If the top left pixel is not transparent then false. x:' . $x . ' y:' . $y);
return false;
}
//If the top right pixel is not transparent then false
if($y == $width-1 && $isAlpha == false) {
//die('If the top right pixel is not transparent then false. x:' . $x . ' y:' . $y);
return false;
}
//If it is not completly transparent or black, then false
if(!$isAlpha && !$isBlack) {
//die('If it is not completly transparent or black, then false. x:' . $x . ' y:' . $y);
return false;
}
}
if($!anyBlack) {
return false;
}
return true;
}
?>
Continue reading...
To make Eclipse always use utf-8 even when creating new workspace you can just add a single line in eclipse.ini
-Dfile.encoding=UTF-8
Continue reading...
With a few lines of code you can easily display image objects when debugging image object in visual studio.
I have compile a DLL (with the source code taken from codeproject)
Download ImageVisualizer.dll and put the dll in “C:\Users\{Username}\Documents\Visual Studio {Version}\Visualizers”

Continue reading...
In EPiServers public template they have given me a nice way to redirect the user after a XForm have been submitted by hooking up on the AfterSubmitPostedData event.
public void XForm_ControlSetup(object sender, EventArgs e)
{
var control = (XFormControl)sender;
control.AfterSubmitPostedData += XForm_AfterSubmitPostedData;
}
public void XForm_AfterSubmitPostedData(object sender, SaveFormDataEventArgs e)
{
var control = (XFormControl)sender;
if (control.FormDefinition.PageGuidAfterPost != Guid.Empty)
{
var pageMap = PermanentLinkMapStore.Find(control.FormDefinition.PageGuidAfterPost) as PermanentPageLinkMap;
if (pageMap != null)
{
control.Page.Response.Redirect(pageMap.MappedUrl.ToString());
return;
}
}
}
If XForm_AfterSubmitPostedData is never triggered this is probably because the SMTP settings is wrong in web.config(!!?).
IMOHO this is a bug and a very stupid way to handle this error, episerver should throw an exception!
Anyway… go ahead and download smtp4dev which is a great dummy SMTP server for developers. And then change the SMTP settings in web.config to something like this:
<mailSettings>
<smtp from="noreply@dummy.com" deliveryMethod="Network">
<network host="localhost" port="25" userName="" password="" defaultCredentials="false" />
</smtp>
</mailSettings>
Voila!
Continue reading...
If you want to authenticate a user based on an ip or whatever this is a good method to use in global.asax:
void Session_Start(object sender, EventArgs e)
{
string ip = ConfigurationManager.AppSettings["AutoLoginIp"];
if (Request.UserHostAddress != null && Request.UserHostAddress.Equals(ip))
{
LoginAs("theUserName");
}
}
public void LoginAs(string username)
{
FormsAuthentication.SetAuthCookie(username, false);
System.Web.HttpCookie authCookie = System.Web.HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket != null && !authTicket.Expired)
{
FormsAuthenticationTicket newAuthTicket = authTicket;
if (FormsAuthentication.SlidingExpiration)
{
newAuthTicket = FormsAuthentication.RenewTicketIfOld(authTicket);
}
if (newAuthTicket != null)
{
string userData = newAuthTicket.UserData;
string[] roles = userData.Split(',');
System.Web.HttpContext.Current.User =
new System.Security.Principal.GenericPrincipal(new FormsIdentity(newAuthTicket), roles);
}
}
}
}
You could use only “FormsAuthentication.SetAuthCookie" but in that case you are not logged in before next request.
Continue reading...
When creating a page in EPiServer the page inherits the parent’s ACL.
To remove a role in the on create page event you need the following:
private static void Instance_CreatedPage(object sender, PageEventArgs e)
{
if (e.Page != null)
{
var kb = e.Page;
var acl = new PageAccessControlList(kb.PageLink)
{
new AccessControlEntry("Everyone", AccessLevel.NoAccess, SecurityEntityType.Role),
new AccessControlEntry("AnotherRole", AccessLevel.NoAccess, SecurityEntityType.Role),
new AccessControlEntry("UserName", AccessLevel.NoAccess, SecurityEntityType.User)
};
acl.Save();
}
}
Continue reading...
Har äntligen blivit klar med mitt senaste projekt. Det är en sida som tar din blogs feed och gör en Android applikation av den.
Sök på feed.nu i android market för att se exempel.
Prova gärna: http://feed.nu/
Continue reading...
2011-12-19
0 Comments