Scenarios for handling touch icon in Android

Source: Internet
Author: User

Apple's touch icon is relatively familiar to us, it is Apple to support Web applications (or Web pages) to add to the desktop needs of the icon, with these touch icon Web links more and native application more similar. Because of the wide range of devices such as Apple device Ipod,iphone,ipad, many Web pages provide the icon resource for touch icon. Since there is not an early standard in Android, we still need to use Apple's touch Icon when we want to add a webpage to the desktop.
?
Touch Icon

When we want a Web page to be perfectly added to the desktop, usually we need to set up a PNG image file as a apple-touch-icon. Like what

1 <span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><link rel= "Apple-touch-icon" href= "/custom_icon.png" ></span>

If you want to support the iphone and ipad, we need to use the Sizes property to create multiple images, and the default sizes value is 60 x.

1 <span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><link rel= "Apple-touch-icon" href= "Touch-icon-iphone.png" >2 <link rel= "Apple-touch-icon" Sizes= "76x76" href= "Touch-icon-ipad.png" >3 <link rel= "Apple-touch-icon" sizes= "120x120" href= " Touch-icon-iphone-retina.png ">4 <link rel=" Apple-touch-icon "sizes=" 152x152 "href=" Touch-icon-ipad-retina.png "></span>

Before the IOS7, the Apple system will be added to the desktop of the icon to fillet and other visual processing, in order not to allow processing, we can use apple-touch-icon-precomposed as the value of the REL implementation.

For more information on Touch icon, visit the Fruit Developer website to learn more.

defective implementations in Android
The Android WebView provides a callback to handle touch icon, Onreceivedtouchiconurl (WebView view, String Url,boolean precomposed) This method returns the URL of the touch icon that is useful to us, and whether it is pre-assembled (no visual processing is required in iOS). Although there is such data, we can deal with it, but there is a problem, that is, we do not determine the size of the file, to select the appropriate image.

For example, the next page of the source code, where the order of sizes is irregular

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><link rel= "apple-touch-icon-precomposed" sizes= "72x72" href= "http://www.qiyipic.com/20130423143600/fix/ H5-72x72.png "><link rel=" apple-touch-icon-precomposed "sizes=" 114x114 "href=" http://www.qiyipic.com/ 20130423143600/fix/h5-114x114.png "><link rel=" apple-touch-icon-precomposed "sizes=" 57x57 "href="/HTTP/ Www.qiyipic.com/20130423143600/fix/H5-57x57.png "><link rel=" apple-touch-icon-precomposed "  href=" http ://www.qiyipic.com/20130423143600/fix/h5-0x0.png "></span>

Load Web page, Onreceivedtouchiconurl output log

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><link rel= "apple-touch-icon-precomposed" sizes= "72x72" href= "http://www.qiyipic.com/20130423143600/fix/ H5-72x72.png "><link rel=" apple-touch-icon-precomposed "sizes=" 114x114 "href=" http://www.qiyipic.com/ 20130423143600/fix/h5-114x114.png "><link rel=" apple-touch-icon-precomposed "sizes=" 57x57 "href="/HTTP/ Www.qiyipic.com/20130423143600/fix/H5-57x57.png "><link rel=" apple-touch-icon-precomposed "  href=" http ://www.qiyipic.com/20130423143600/fix/h5-0x0.png "></span>

From the above output, basically the back (writing) elements are printed first, so the defect of this callback is as follows

1. Since the touch icon URL address does not have a hard rule, you cannot determine which Icon to use according to the URL contains some dimensions

2. Since the Web authoring Touch icon element is relatively arbitrary, it is not possible to decide which icon to use according to the Onreceivedtouchiconurl call

3. There is no sizes attribute value in the callback, it is not OK to use which icon

4. If we choose the highest quality picture, then the appropriate compression processing may be able to solve the problem, but the full icon download or according to head header information always feel not good.

Improvement Methods

Since WebView does not have a ready-made method to meet our needs, we have to implement it ourselves. In fact, the implementation of the method is relatively simple is the JS script injection detection page elements of touch icon, return JSON data.

JavaScript Methods

The following JS code functions to find all the link elements for touch icon, including the normal and marked as precomposed. The properties of these link elements are then stored in the JSON data and finally returned to the corresponding callback in the Java code.

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >var touchicons =[];function gathertouchicons (elements) {var normaltouchiconlength=elements.length;  var currentelement;  for(var i =0; i < normaltouchiconlength;i++) {currentelement=Elements[i];      var size; if(Currentelement.hasattribute (' sizes ') ) {size= Currentelement.sizes[0]; } Else{size= ' '; } var info= {' Sizes ': size, ' rel ': Currentelement.rel, ' href ': Currentelement.href};  Touchicons.push (info); }}function obtaintouchicons () {normalelements= Document.queryselectorall ("link[rel= ' Apple-touch-icon ')"); Precomposedelements= Document.queryselectorall ("link[rel= ' apple-touch-icon-precomposed ')");  Gathertouchicons (normalelements);  Gathertouchicons (precomposedelements); var info=json.stringify (touchicons); Window.app_native.onReceivedTouchIcons (document. URL, info);} Obtaintouchicons ();</span>

Java Code

Here in order to facilitate understanding or all put out the demo source code, the demo when the page loading completed after injecting the above JS code to get touch icon information, and then returned to the Java callback method. If you are not sure how Java code implements the improvements, you can click on the Java code to write an Android app to achieve security

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" > PackageCom.example.obtaintouchicon;ImportJava.io.BufferedReader;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.webkit.JavascriptInterface;Importandroid.webkit.WebChromeClient;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient; Public classMainactivityextendsActivity {protectedString LogTag = "Mainactivity"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); WebView WebView=NewWebView ( This); Webview.getsettings (). setjavascriptenabled (true); Webview.setwebviewclient (Newwebviewclient () {@Override Public voidonpagefinished (WebView view, String URL) {Super. onpagefinished (view, URL); FinalString Touchiconjscode =Gettouchiconjscode (); LOG.I (LogTag,"Onpagefinished url =" + URL + "; touchiconjscode=" +Touchiconjscode); View.loadurl ("javascript:" +Touchiconjscode);      }      }); Webview.addjavascriptinterface (NewJsobject (), "app_native"); Webview.loadurl ("Http://192.168.1.5:8000/html/touchicon.html"); }    Private classJsobject {@JavascriptInterface Public voidonreceivedtouchicons (string URL, string json) {log.i (LogTag,"Onreceivedtouchicons url=" + URL + "; json=" +JSON); }  }    PrivateString Gettouchiconjscode () {StringBuilder total=NewStringBuilder (); InputStream InputStream=NULL; BufferedReader Bufferreader=NULL; Try{InputStream= Getassets (). Open ("Touchicon.js"); Bufferreader=NewBufferedReader (NewInputStreamReader (InputStream));          String Line;  while(line = Bufferreader.readline ())! =NULL) {total.append (line); }      } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally {          if(NULL!=InputStream) {              Try{inputstream.close (); } Catch(IOException e) {e.printstacktrace (); }          }      }      returntotal.tostring (); }}</span>

The JSON data returned

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >[  {      "Sizes": "72x72",      "rel": "Apple-touch-icon-precomposed",      "href": "Http://www.qiyipic.com/20130423143600/fix/H5-72x72.png"  },  {      "Sizes": "114x114",      "rel": "Apple-touch-icon-precomposed",      "href": "Http://www.qiyipic.com/20130423143600/fix/H5-114x114.png"  },  {      "Sizes": "57x57",      "rel": "Apple-touch-icon-precomposed",      "href": "Http://www.qiyipic.com/20130423143600/fix/H5-57x57.png"  },  {      "Sizes": "",      "rel": "Apple-touch-icon-precomposed",      "href": "Http://www.qiyipic.com/20130423143600/fix/H5-0x0.png"  }]</span>

We can handle the resulting JSON data as needed.

Will Google improve?

The answer is yes, and it has improved, but Google is not modifying the onreceivedtouchiconurl approach, but Google is pushing its own set of rules.

On Chrome, Google adds an element that Google provides as a way to define metadata for a Web program.

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><link rel= "manifest" href= "Manifest.json" ></span>

In the metadata JSON, you can customize the title, the start page, whether the program is horizontal or vertical screen display. A simple JSON example is as follows, where we can see several icons like touch icon in icons, src for icon path, sizes for size, type is mimetype, Density refers to the screen density in Android (so it's more Android).

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >{  "Name": "Web application Manifest Sample",  "Icons": [    {      "src": "Launcher-icon-0-75x.png",      "Sizes": "36x36",      "Type": "Image/png",      "Density": "0.75"    },    {      "src": "Launcher-icon-1x.png",      "Sizes": "48x48",      "Type": "Image/png",      "Density": "1.0"    },    {      "src": "Launcher-icon-1-5x.png",      "Sizes": "72x72",      "Type": "Image/png",      "Density": "1.5"    },    {      "src": "Launcher-icon-2x.png",      "Sizes": "96x96",      "Type": "Image/png",      "Density": "2.0"    },    {      "src": "Launcher-icon-3x.png",      "Sizes": "144x144",      "Type": "Image/png",      "Density": "3.0"    },    {      "src": "Launcher-icon-4x.png",      "Sizes": "192x192",      "Type": "Image/png",      "Density": "4.0"    }  ],  "Start_url": "Index.html",  "Display": "Standalone",  "Orientation": "Landscape"}</span>

But because the standard implementation rate is relatively low at the moment, we still need to use Apple's touch icon.

SOURCE Download :Http://pan.baidu.com/s/1dDD3gZZ

Scenarios for handling touch icon in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.