Suggestion: mobile user agent recognition

jmr

Joined: 2004-02-26
Posts: 15
Posted: Thu, 2004-02-26 11:59

Hi all,
I have a small suggestion to be included in the G2. Since mobile phones with internet access are becoming more popular it would be nice if Gallery was more accessible with mobile devices. By accessible I mean for example restucturing the page layout, different themes, down-scaled images, etc.

I have written few lines of code that allow easy recognition of mobile phones and later usage of that information for example in layout/themes.

In GalleryInitSecondPass() (init.php) right after initializing the session I added the following lines:

/*
 * If we have a mobile user, set the necessary session variables.
 * These variables can later be used, for example, to modify the page 
 * layout to better fit to small screen size.
 */
if (GalleryUtilities::isMobileUser()) {
    $session->put('core.mobile.user', true);
    $screen = GalleryUtilities::getScreenSize();
    $session->put('core.mobile.width', $screen['width']);
    $session->put('core.mobile.height', $screen['height']);
}
else {
    $session->put('core.mobile.user', false);
}

You can probably think of some better names for the session variables.

The two functions mentioned above are defined in GalleryUtilities.class as follows:

/**
 * Return true if the user agent is mobile device. Otherwise return false.
 *
 * @param string The user agent identification string. Optional, if not
 *		provided, then $_SERVER['HTTP_USER_AGENT'] will be used.
 * @return true or false
 */
function isMobileUser($userAgent = null) {
    if ($userAgent == null) {
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
    }

    if (GalleryUtilities::getScreenSize($userAgent) !== null) {
        return true;
    }

    return false;
}

/**
 * Return the screen size of the mobile user agent. If the user is not regocnized 
 * to be a mobile device then return null. File mobileterminals.inc contains a list
 * of known mobile user agents.
 *
 * @param string The user agent identification string. Optional, if not
 *		provided, then $_SERVER['HTTP_USER_AGENT'] will be used.
 * @return array(width, height)
 */
function getScreenSize($userAgent = null) {
    global $gallery;

    /* Get the screen size definitions. */
    $screenSizes = array();
    $basedir = $gallery->getConfig('code.gallery.base');
    include_once($basedir . 'mobileterminals.inc');

    if ($userAgent == null) {
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
    }

    /* Step through the array $screensizes and try to find matching user agent. */
    foreach (array_keys($screenSizes) as $key) {
        /* Check if user agent matches this screen size key. */
        if (preg_match($key, $userAgent)) {
            /* It matched, so return screen size. */
            return $screenSizes[$key];
        }
    }

    return null;
}

The mobileterminals.inc file is just a definition of an array that has different UA strings as keys and screen sizes as values. It is in separate file so that it's easy to update. It looks like this:

<?php
/**
 * This file contains a list of known mobile phone user agent identification 
 * strings. Each key of the $screenSizes array is a regular expression that 
 * matches the UA string and the value is an array that stores the width and
 * the height of the mobile device's screen size.
 *
 * If you want to add more mobile phone models just add new lines following 
 * the same structure than in the existing ones. The more specific regular
 * expression you use to match the US string, the higher up in the list it 
 * should be. If it's very generic then add it at the end of the array.
 */

// Phones that use the Series 60 platform, e.g. Nokia 3650 and 6600.
$screenSizes['/.*?Series60.*?/'] = array('width' => 176, 'height' => 208);
// Phones that use the Series 90 platform, e.g. Nokia 7700.
$screenSizes['/.*?Series90.*?/'] = array('width' => 640, 'height' => 320);

// The generic mobile phone using Symbian OS. All Symbian phones don't necessarily 
// have the same screen size so if you want to include some specific Symbian phones 
// then place them above this line.
$screenSizes['/.*?Symbian.*?/'] = array('width' => 176, 'height' => 208);

?>

What do you think, could this be included in the G2? This would allow the development of mobile friendly themes and layouts and possibly a lot more nice features. As an example take a look at http://mummola.cs.tut.fi/mobgallery/ where we have implemented this in Gallery 1.4.1.

-Jerome
[rannikko at cs dot tut dot fi]

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2004-02-26 21:57

good idea :)
i think G2 is the kind of application predestinated for the mobile use. you're on a party / dining with friends and wanna show them a fantastic pic, or something like that.

@implementation:
having a server side datebase of known mobiles / mobile browsers is not the thing we'd love to administrate, but i didn't think of this solution and i guess it's the only way to go.

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Fri, 2004-02-27 03:32

That's pretty cool.

So what kinds of modifications would we have to make to the layout and theme to support mobile platforms? Is there a best practices guide out there for porting your site to a platform like that?

G2 will be entirely CSS based when we're done with the next round of refactoring the UI so we could obviously do a lot by just changing the stylesheet (which would be controlled by the theme). Since every album can have its own theme, we'd have to have themes that have a mobile vs. desktop version.

What kinds of changes would we have to make to the layout? It looks like you're generating two different versions of the thumbnails (200x150 for desktop and 80x60 for mobile). Thumbnail sizes are controlled by per-album preferences and can be overridden by per-item preferences, so in order for an album to be mobile friendly we'd have to use a mobile-friendly layout, mobile-friendly theme and choose our per thumbnail settings wisely for each album that we want to be viewed that way. It's definitely doable.

 
jmr

Joined: 2004-02-26
Posts: 15
Posted: Fri, 2004-02-27 14:43
bharat wrote:
So what kinds of modifications would we have to make to the layout and theme to support mobile platforms?

Since the screen size is very limited on mobile devices, we should try to use that as efficiently as we can. In practise this could mean for example limiting the number of columns (to minimize the need to scroll horizontally) and leaving out all "not essential" stuff like list of peer items (to minimize the data transfer) and re-arrange the layout/templates so that the main content (pictures) are as high up in the page as possible (so, move the infoset below the picture). Those are the first things that come to mind. Here is an simple example of limiting the number of columns (in layout.inc):

function loadTemplate(&$template, $item) {
  // ...code removed
  if (!isset($params['columns'])) {
    $params['columns'] = 3;
  }

  // THIS IS NEW
  /* Proper number of columns for mobile devices. */
  global $gallery;
  $session =& $gallery->getSession();
  if ($session->get('core.mobile.user')) {
    /* Series 90 phone has 640x320 screen. That's enough to fit in 3 mobile thumbnails. */
    if ($session->get('core.mobile.width') >= 640) {
      if ($params['columns'] > 3) {
        $params['columns'] = 3;
      }
    }
    /* Assume series 60 phone with 176x208 screen. */
    else {
      $params['columns'] = 1;
    }
  }

  // code removed...
}

Maybe something could be done with the templates as well. What I'm thinking is that pathbar.tpl, sidebar.tpl and albumbody.tpl would be stacked one after another so that viewing them wouldn't require any horizontal scrolling. Also, sidebar could be modified so that it didn't contain any peer item info or search. In the albumbody the infoset could be after the image and image properties could be left out. This way the page would be a bit smaller and the main thing, the photo, would be right at top of the page, even on the small screen. This, of course, only applies to the mobile view, the normal desktop view would be just like it is now.

bharat wrote:
Is there a best practices guide out there for porting your site to a platform like that?

We (I'm part of a research group that is studying mobile friendly open source software at Tampere Univ. of Tech.) have actually written a document that states some principles that one should consider when designing mobile friendly web services. You can get the document from
http://mummola.cs.tut.fi/files/principles.pdf

bharat wrote:
It looks like you're generating two different versions of the thumbnails (200x150 for desktop and 80x60 for mobile).

Yes, we have defined the mobile thumbnail as being 80 pixels wide. It looks really small on computer screen but on mobile (176x208) it looks quite good. It maybe could be a bit bigger but the width shouldn't be more than 100 in any case. When G2 detects a mobile user it could use the 80 pixels wide (or the like) thumbnail. And instead of the 640x480 picture the mobile user could get the 150 pixels picture (the normal thumbnail). That would fill quite nicely the 176 pixels screen size. The rest of the image sizes would then be accessible through the dropdown menu just as they are now.

-Jerome
[rannikko at cs dot tut dot fi]

 
nivekiam
nivekiam's picture

Joined: 2002-12-10
Posts: 16504
Posted: Sat, 2004-02-28 21:52

Just an idea, but if something like this does get implemented. Maybe the people working on it should take a look over as OSNews to see what they've done.

Looks like they've done a lot of work to make their site work across a lot of different devices.

http://www.osnews.com/story.php?news_id=6191

http://www.osnews.com/comment.php?news_id=6191

Perhaps Eugenia, if asked nicely ;), would even share some of the work she's done. Like the UA srtrings she's collected (though I hate browser sniffing), and maybe some of the quirks between different platforms. Most of this should be able to be done via CSS as long as the positioning (layout) for G2 is pure CSS and does not use tables ;)

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Mon, 2004-03-29 02:46

jmr, bharat,
Here's another suggestion for how to support mobile devices, based on the original post in this topic..

classes/GalleryMobileLayout.class
Extends GalleryLayout and adds nothing.. simply used as an interface that a module can use to register a mobile layout implementation.

GalleryUtilities::isMobileUserAgent()
Same as described previously (although I didn't put in the screen-size data for my test, and I added /PalmOS/ in the list to test with my TungstenT).

GalleryItemHelper_simple::fetchLayout()
Before retrieving the layout via the normal routine I added this:

      /* Check for mobile device */
      if (GalleryUtilities::isMobileUserAgent()) {
          list ($ret, $layout) =
              GalleryCoreApi::newFactoryInstance('GalleryMobileLayout');
          if ($ret->isError()) {
              return array($ret->wrap(__FILE__, __LINE__), null);
          }
          if (isset($layout)) {
              return array(GalleryStatus::success(), $layout);
          }
      }

That's all the changes in core.. now a module can register an implementation of GalleryMobileLayout and G2 will use it in place of the layout specified for an album whenever viewed with a recognized mobile device. The mobile layout won't appear in the layout list for assigning to an album.. I figured admins wouldn't want to make a layout tailored for mobile devices the primary layout for an album.
I made a module to test this out (incomplete, but it has subalbum links and shows image thumbs), but I think a good design would show only basic info in a single column. It probably doesn't need to offer sidebar or album/item links.. ie, mobile layout can be view-only and not offer admin functions.

 
tokyoman

Joined: 2004-03-29
Posts: 6
Posted: Mon, 2004-03-29 14:59

Hi folks,

I'm writing from the land of mobile phones (=Japan).
If you going to support mobile phones you should consider the following points.

1) size-wise a distinction between mobile phones and pda's
2) there are many, many different types of mobile phones in Japan
3) there are different codesets being used
4) there are different web standards

1) can probably easily solved by defining a 'device category' (PC, PDA, Mobile).

2) The various types and models are commonly distinguished by the its user-agent settings. It would be a good idea to define a *.inc file which may contain actual user-agent strings as well as regex definitions

3) Currently NTT i-mode phones are using Shift-JIS while the website might be in EUC-JP, UTF-8 or Shift-JIS. This means that you might have to do charset conversions.

4) While NTT's i-mode and Vodaphones J-SKY are pretty similiar, KDDI is using WAP.

BTW, this year or next year all main carriers in Japan will be introducing CDMA (UTMS). KDDI is already offering a first version.

Tja, just some pointers.

Bernd

 
monza700
monza700's picture

Joined: 2004-03-02
Posts: 6
Posted: Mon, 2004-03-29 18:02

Could the thumbnail resize be set up in the configuration?
Example configure questions:
----------
1. Detect and display alternate layouts (if available) for mini-browsers (PDA's, Mobile Phones):
[Yes]
[No]

2. Create additional thumbnails for mini-browsers (ignored if 1. is "No"):
(checkbox) a. max width = 80
(checkbox) b. max width = 100
(checkbox) c. max width = 120
(checkbox) a. max width = [User defined] Other
----------
The browser list could be included in a seperate file so that users can add to it/download new specs as they are developed...

A default theme-appendage should be developed that can be used with existing themes, for those users that are upgrading from previous versions of Gallery or are using Older themes (is this even an issue? can users upgrade from previous versions?).

Just some ideas,

 
jmr

Joined: 2004-02-26
Posts: 15
Posted: Mon, 2004-03-29 23:33

mindless
GalleryMobileLayout.class seems like a good idea. Also the fetchLayout() modification looks good but I have a question about that (just to make sure I understood what you ment). You wrote

Quote:
... now a module can register an implementation of
GalleryMobileLayout and G2 will use it in place of the layout specified
for an album whenever viewed with a recognized mobile device.

Do you mean that there would be only one mobile layout? Or didn't I just understand? I think that there probably is need for more than just one different mobile layout, just like there are many normal layouts (and themes).

I think maybe one way to do this is to have in the different layout directories layout.inc and mobilelayout.inc. The latter implementing the MobileLayout class. Then fetchLayout() would in the case of mobile UA first check if it can load an implementation of the MobileLayout and if not then use a default mobile layout, e.g. mobile version of the Matrix layout.

Or, yet another way, just modify the existing layout(s) so that the layout itself can adapt to the mobile device.

Quote:
I think a good design would show only basic info in a single column. It probably doesn't need to offer sidebar or album/item links..ie

Yes, all what's not absolutely necessary info should be left out. It speeds up the download quite a bit on mobile devices and also saves the screen space for the main content. Althoug it all doesn't necessarily have to be in single column since some new phones using Series90 platform have quite wide screens - 640 pixels (height 320). Instead we can figure out the number of columns by reserving, say, 150 pixels for each (thumbnail) image horizontally. Here's an example from loadTemplate() in layout.inc, just before calling _loadAlbumTemplate() or _loadSingleTemplate().

if (GalleryUtilities::isMobileUser()) {
  // give each mobile picture 150 pixels of horizontal space
  $cols = floor(GalleryUtilities::getScreenWidth()/150);
  if ($cols > $params['columns']) {$cols = $params['columns'];}
  // we want approximately the same number of pictures per page
  $params['rows'] = floor($params['rows'] * $params['columns'] / $cols);
  $params['columns'] = $cols;  
}

(The getScreenWidth() function returns (as the name says) the width of the screen. I'll post the new versions of the mobile related functions soon.)

-Jerome
[rannikko at cs dot tut dot fi]

 
jmr

Joined: 2004-02-26
Posts: 15
Posted: Mon, 2004-03-29 23:48

I made some changes to the mobile related functions (see first post in this topic). For example I don't any more use the session to store the screen size but static variables instead. (Any opinions if that's a good or a bad choice? I think it's at least more convenient to use.) Here are the new versions.

function isMobileUser() {
  static $isChecked = false;
  static $isMobile = false;
  if (!$isChecked) {
    if (GalleryUtilities::getScreenSize() !== null) {
      $isMobile = true;
    }
    $isChecked = true;
  }
  return $isMobile;
}
function getScreenSize() {
  static $isChecked = false;
  static $screen = null;
  if (!$isChecked) {
    global $gallery;
    $basedir = $gallery->getConfig('code.gallery.base');
    /* Get the screen size definitions. */
    $screenSizes = array();
    include($basedir . 'mobileterminals.inc');
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    /* Step through the array $screensizes and try to find matching user agent. */
    foreach (array_keys($screenSizes) as $key) {
      /* Check if user agent matches this screen size key. */
      if (preg_match($key, $userAgent)) {
        $screen = $screenSizes[$key];
      }
    }
    $isChecked = true;
  }
  return $screen;
}
function getScreenWidth() {
  // have a reasonable default just in case someone calls this even when UA isn't mobile
  static $width = 1280;
  static $isChecked = false;
  if (!$isChecked) {
    $screen = GalleryUtilities::getScreenSize();
    if ( $screen !== null) {
      $width = $screen['width'];
    }
    $isChecked = true;
  }
  return $width;
}
function getScreenHeight() {
        // have a reasonable default just in case someone calls this even when UA isn't mobile
  static $height = 1024;
  static $isChecked = false;
  if (!$isChecked) {
    $screen = GalleryUtilities::getScreenSize();
    if ( $screen !== null) {
      $height = $screen['height'];
    }
    $isChecked = true;
  }
  return $height;
}

-Jerome
[rannikko at cs dot tut dot fi]

 
jmr

Joined: 2004-02-26
Posts: 15
Posted: Tue, 2004-03-30 00:24

Hi tokyoman

tokyoman wrote:
Hi folks,
If you going to support mobile phones you should consider the following points.
1) size-wise a distinction between mobile phones and pda's
2) there are many, many different types of mobile phones in Japan
3) there are different codesets being used
4) there are different web standards

1&2) As you suggested yourself it is best to take care of these in a separate, easy-to-update file. As you can see from the first post this is just what we've done. And when the screen size is known it doesn't really matter if the device itselft is phone or pda or whatever.

3) The mobile user interface is just meant to present the contents of Gallery in a mobile friendly way, not to make any changes (like language) to it. The Gallery itself, though, can be fully localized language-wise.

4) Like said in the previous point, the aim is not to make any changes in the actual content, only the presentation. Gallery is in HTML and CSS and the mobile user interface is not going to change that. Especially since 'all' new internet enabled mobile phones support (X)HTML and CSS I don't see it very important to worry about other (minor) options like WML.

I hope these comments answered (some of) your concerns.

-Jerome, from another land of mobile phones (=Finland)
[rannikko at cs dot tut dot fi]

 
jmullan
jmullan's picture

Joined: 2002-07-28
Posts: 974
Posted: Tue, 2004-03-30 00:56

About #3:
Gallery2 will be entirely UTF-8. We aren't even thinking about any other character sets.

 
tokyoman

Joined: 2004-03-29
Posts: 6
Posted: Tue, 2004-03-30 01:10

Hi Jerome,

jmr wrote:

1&2) As you suggested yourself it is best to take care of these in a separate, easy-to-update file. As you can see from the first post this is just what we've done. And when the screen size is known it doesn't really matter if the device itselft is phone or pda or whatever.

3) The mobile user interface is just meant to present the contents of Gallery in a mobile friendly way, not to make any changes (like language) to it. The Gallery itself, though, can be fully localized language-wise.

4) Like said in the previous point, the aim is not to make any changes in the actual content, only the presentation. Gallery is in HTML and CSS and the mobile user interface is not going to change that. Especially since 'all' new internet enabled mobile phones support (X)HTML and CSS I don't see it very important to worry about other (minor) options like WML.

A PDA screen is perhaps 5 times as big as a mobile phone screen while mobile phones maybe vary by up to 50%. Maybe you want to use the bigger PDA screen to display additional content (or to structure it differently) than for mobile phones.

3) Sorry, but you're missing the point. The gallery itself may be running in EUC-JP or UTF-8 while the mobile phone needs the content in Shift-JIS. So we're talking about present mobile content and PC content in different codesets.

4) In Japan there is a big installed basis of phones using different versions of i-mode etc. It is correct that new i-mode phones support xhtml (Open Mobile Alliance standard www.openmobilealliance.org) but KDDI phones support a different XHTML type. In addition all variants in Japan support (different) sets of special 1 character pictures showing anything from cars, restaurants, wheather, (NTT i-mode over 200, Vodafone some 500, KDDI up to over 800 - depending on the type).

You should have at least the flexibility to differentiate by carrier and major mobile phone type. If you don't want to go into the details required to present the content in a nice and appealing way - which is very important in Japan - then Japanese users will appreciate if you'd prepare some user-exits allowing us to easily add functionality.

Regards,

Bernd

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Tue, 2004-03-30 03:57
jmr wrote:
Do you mean that there would be only one mobile layout?

Yes, the design I suggested would use only one mobile layout for the entire site. A more extensive design could assign different mobile layouts to different user agents and/or different albums.

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Tue, 2004-03-30 04:30

FYI, I'm monitoring this thread and it looks cool, but I don't have the cycles (and I don't think we have the time) to make a serious effort to get this into G2.0. I think it's a good contender for G2.1 or thereabouts. I don't think that the framework precludes any of this and will keep an eye on the discussion to try to ensure that we leave the door wide open for this kind of approach, but the primary target for G2.0 is web browser based interaction.

 
jmr

Joined: 2004-02-26
Posts: 15
Posted: Wed, 2004-03-31 13:41

Hi Bernd,

tokyoman wrote:
A PDA screen is perhaps 5 times as big as a mobile phone screen while mobile phones maybe vary by up to 50%. Maybe you want to use the bigger PDA screen to display additional content (or to structure it differently) than for mobile phones.

Some (new) mobile phones have quite "big" screens (e.g. Nokia 7700: 640x320, Nokia Communicator: 640x200, Sony Ericsson P900: 208x320) and I doubt PDAs have 5 times that big screens but I see your point and agree with you: all available screen space should be used to display the content. That's why mobileteminals.inc file (first post) has the width and height values. Based on them the content can be scaled / structured properly to fit on the display.

tokyoman wrote:
3) Sorry, but you're missing the point. The gallery itself may be running in EUC-JP or UTF-8 while the mobile phone needs the content in Shift-JIS.

You are right, I did miss your point in this one. Probably because I don't know much about different character encodings (nor Japanese / Asian mobile market). Also, my primary interest in making Gallery mobile friendly is to make it look good on the kind of phones I (might) have so for me UTF-8 works just fine. Of course we should design the mobile interface so that it doesn't prevent anyone from doing more than just restructuring the layout (like change the character set).

br,
Jerome
[rannikko at cs dot tut dot fi]

PS. Just to avoid any confusion: I'm not part of the Gallery development team so my opinions are my own and might be different from their view.

 
jmullan
jmullan's picture

Joined: 2002-07-28
Posts: 974
Posted: Wed, 2004-03-31 17:08
tokyoman wrote:
3) Sorry, but you're missing the point. The gallery itself may be running in EUC-JP or UTF-8 while the mobile phone needs the content in Shift-JIS.

jmr wrote:
You are right, I did miss your point in this one. Probably because I don't know much about different character encodings (nor Japanese / Asian mobile market). Also, my primary interest in making Gallery mobile friendly is to make it look good on the kind of phones I (might) have so for me UTF-8 works just fine. Of course we should design the mobile interface so that it doesn't prevent anyone from doing more than just restructuring the layout (like change the character set).

Again - G2 is entirely UTF-8 based. If there is a need for another character set for mobile phones, we need to arrange some sort of translation - an intermediary for those phones that don't support UTF-8.

We went with UTF-8 so the interface can be in a different language from the metadata. With UTF-8 one can view an album of photos of Greece with the Greek names in Greek, the controls in Japanese, and a few comments in Cyrillic from a Russian friend (provided that you have all of the appropriate fonts).

Behind the scenes we convert input into html entities so that we are only storing ASCII text in the database.

Anyway, putting together a translation layer that translates any UTF-8 characters into Shift-JIS (or whatever character set the phone uses) shouldn't be too terribly difficult. Translating input from a phone into html entities also shouldn't be too difficult, but any admin who wants to make that available would probably have to have extra language libraries installed on their system.

I'd love to keep chatting about this - maybe we could start a new thread to discuss the character set needs of phones/pdas/whatever.

 
wondermanu

Joined: 2004-04-08
Posts: 1
Posted: Thu, 2004-04-08 07:59

Just to confirm that wap support would be a very nice feature. I have been searching for a php gallery that includes this feature without any success for now...And i sure there is a huge need for that.

try to be the first !

 
doomdead

Joined: 2003-04-06
Posts: 174
Posted: Fri, 2006-04-07 22:27

any progress on this? has it been discusses elsewhere since 2004?????

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2006-04-08 02:42

nope

 
gernot.hueller

Joined: 2006-04-26
Posts: 10
Posted: Sat, 2006-10-07 10:56

pity!!! I cant believe it!!!

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2006-10-07 15:22

vote to give this feature more visibility:
http://gallery.menalto.com/sfvote/vote/685827