Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts

Tuesday, 23 December 2014

Get device detail in PHP




Been using PHP for sometime and now working on mobile domain? The first hurdle you must have faced is 'how to determine the device type in PHP', unless you used HTACCESS for that, which isn't the right approach.
So, here I'm writing a class that will get you pretty much information about the device. You can get more by processing the USER_AGENT string!

<?php
class Detect
{
 public static function systemInfo()
 {
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $os_platform    = "Unknown OS Platform";
    $os_array       = array('/windows phone 8/i'    =>  'Windows Phone 8',
                            '/windows phone os 7/i' =>  'Windows Phone 7',
                            '/windows nt 6.3/i'     =>  'Windows 8.1',
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile');
    $found = false;
    $addr = new RemoteAddress;
    $device = '';
    foreach ($os_array as $regex => $value) 
    { 
        if($found)
         break;
        else if (preg_match($regex, $user_agent)) 
        {
            $os_platform    =   $value;
            $device = !preg_match('/(windows|mac|linux|ubuntu)/i',$os_platform)
                      ?'MOBILE':(preg_match('/phone/i', $os_platform)?'MOBILE':'SYSTEM');
        }
    }
    $device = !$device? 'SYSTEM':$device;
    return array('os'=>$os_platform,'device'=>$device);
 }

 public static function browser() 
 {
    $user_agent = $_SERVER['HTTP_USER_AGENT'];

    $browser        =   "Unknown Browser";

    $browser_array  = array('/msie/i'       =>  'Internet Explorer',
                            '/firefox/i'    =>  'Firefox',
                            '/safari/i'     =>  'Safari',
                            '/chrome/i'     =>  'Chrome',
                            '/opera/i'      =>  'Opera',
                            '/netscape/i'   =>  'Netscape',
                            '/maxthon/i'    =>  'Maxthon',
                            '/konqueror/i'  =>  'Konqueror',
                            '/mobile/i'     =>  'Handheld Browser');

    foreach ($browser_array as $regex => $value) 
    { 
        if($found)
         break;
        else if (preg_match($regex, $user_agent,$result)) 
        {
            $browser    =   $value;
        }
    }
    return $browser;
 }
}
Now, you can use it like this: 
$detail = Detect::systemInfo();
to check if it's a mobile, you can use,  

if($detect['device']=='MOBILE')
{
/* Your code for mobile devices */ 
}
else 
{
 /* Your code for desktop systems */ 
}

Monday, 31 March 2014

Migrating from Web to Android

Hello there! If you're here, I assume you're a web developer who is getting to learn Android, much like me. When I dipped into Android, the very first thing that confused me was 'how navigation works in Android apps, like we've several pages in a website which we can create an edit, where to do all such stuff in Android?' and 'what is this all heck of activity and XML files?'. While I had a little (yes, little and theoretical only) experience of Java, I knew about concepts of OOP (Polymorphism, Abstraction, Encapsulation and Inheritance). If you don't know about them, you better check out some links on these topics before returning to this page.
So, the first thing to understand in Android is that, 'Activities' are what we typically call 'pages' in Websites and the much likeinteraction that we did with the UI items in websites (such as with 'buttons ' in a webpage), we do so here with the help of Intents. Intents specify an "app's intent to do something with the app". This can, besides, be used to perform a number of tasks, however, we primarily use it perform interaction between activities. We'll discuss them all later.
So, as many pages your want to have in your app, you'll have to create that many activities. In fact, activities are more closely similar to 'views' and that's exactly what we call in Android.
The following post and the upcoming ones will contain important points, using the standard android documentation from it's site. I hope that i do not sound  confusing and that it helps you out in understanding Android.

1. Building a Simple UI
  •  The graphical user interface (GUI) of an app is create using a hierarchy of View and ViewGroup objects. It forms a tree structure where each leaf node can contain either a View object or a ViewGroup object. In simple language, any page can contain a view (page layout) as well as ViewGroup object. And actually, that's how we link Views in Android apps. A basic diagram (above) shows the concept. The LinearLayout and RelativeLayout are the ViewGroup object types which define the type of view layout selected.
  •  View objects are usually UI widgets such as buttons, text-fields, like we have them in a typical web page. However, their attribute declaration is different here. Generally, we define everything in an XML file (which we'll discuss later).
  •  ViewGroup objects are 'invisible' (yes, they're mere a definition) containers that define how the child views are laid out, such as in a grid or a vertical list.
  • UI layout is defined in XML using hierarchy of UI elements.
 Next, we'll discuss how to create a Linear layout in Android. Follow up!