Android uses webview to customize browsers with history and download Functions

Source: Internet
Author: User

Package com. mysession;

Import java. util. arraylist;
Import java. util. List;

Import Android. App. activity;
Import Android. content. intent;
Import Android. Graphics. Bitmap;
Import Android. Graphics. Canvas;
Import Android. Graphics. matrix;
Import Android. Graphics. picture;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. WebKit. cookiemanager;
Import Android. WebKit. cookiesyncmanager;
Import Android. WebKit. downloadlistener;
Import Android. WebKit. valuecallback;
Import Android. WebKit. webchromeclient;
Import Android. WebKit. webhistoryitem;
Import Android. WebKit. websettings;
Import Android. WebKit. webview;
Import Android. WebKit. webviewclient;
Import Android. widget. Button;
Import Android. widget. edittext;
Import Android. widget. gridview;
Import Android. widget. progressbar;

Import com. mysession. model. historymodel;

Public class d_sessionactivity extends activity implements onclicklistener,
Downloadlistener {

Private Static final string tag = "tag ";
// Private string detailurl =
// "Http://www.google.com ";
// Private string detailurl =
// "Http: // 172.20.230.67: 8900/iportal/servlet/getmobilecontent? & User = administrator & Vol = allen_iserver & profile = myserver & Password = uaabd7az0thxe6wfm2fdqa % 3d % 3d & respformat = HTML & subid = login & ENC = 1 ";

// Private string detailurl = "http://music.sogou.com /";
Private webview;
Private button btnload, btnreset, btnhistory, btnback, btnforward;
Private edittext eturl;
Private progressbar mprogressbar;
Private gridview mgridview;
Private cookiemanager;
Private browserhistoryadapter;
Private list

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Cookiesyncmanager. createinstance (this );
Cookiemanager = cookiemanager. getinstance ();

Webview = (webview) findviewbyid (R. Id. webview1 );
Btnload = (button) findviewbyid (R. Id. btnload );
Btnreset = (button) findviewbyid (R. Id. btnreset );
Btnhistory = (button) findviewbyid (R. Id. btnhistory );
Mprogressbar = (progressbar) findviewbyid (R. Id. progressbar1 );
Eturl = (edittext) findviewbyid (R. Id. eturl );
Btnback = (button) findviewbyid (R. Id. btnback );
Btnforward = (button) findviewbyid (R. Id. btnforward );
Mgridview = (gridview) findviewbyid (R. Id. gridview1 );

Btnload. setonclicklistener (this );
Btnreset. setonclicklistener (this );
Btnhistory. setonclicklistener (this );
Btnback. setonclicklistener (this );
Btnforward. setonclicklistener (this );

Webview. getsettings (). setjavascriptenabled (true );
Webview. setwebviewclient (New birtmobilewebviewclient ());
Webview. setwebchromeclient (New geoclient ());
Webview. setdownloadlistener (this );
Webview. getsettings (). setcachemode (websettings. load_cache_else_network );

// Webbackforwardlist history = webview. copybackforwardlist ();
// If (history! = NULL ){
// Browserhistoryadapter = new browserhistoryadapter (history, this );
// Mgridview. setadapter (browserhistoryadapter );
//}

Webview. setvisibility (view. Visible );
Mgridview. setvisibility (view. Gone );
}

@ Override
Public void onclick (view v ){
If (V = btnload ){
Cookiemanager. setacceptcookie (true );
Webview. setvisibility (view. Visible );
Mgridview. setvisibility (view. Gone );

If (! "". Inclusignorecase (eturl. gettext (). tostring ())){
Webview. loadurl (eturl. gettext (). tostring ());
}

} Else if (V = btnreset ){
Cookiemanager. removeallcookie ();
} Else if (V = btnhistory ){
If (webview. copybackforwardlist () = NULL ){
Return;
} Else {
Webview. setvisibility (view. Gone );
Mgridview. setvisibility (view. Visible );

If (browserhistoryadapter = NULL ){
Browserhistoryadapter = new browserhistoryadapter (
Historymodels, this );
Mgridview. setadapter (browserhistoryadapter );

} Else {
Browserhistoryadapter. sethostory (historymodels );
Browserhistoryadapter. notifydatasetchanged ();
}
}

} Else if (V = btnback ){
Webview. Goback ();
} Else if (V = btnforward ){
Webview. goforward ();
}
}

// Create a webview client that handles mailto links within the webview
Private class birtmobilewebviewclient extends webviewclient {
@ Override
Public Boolean shouldoverrideurlloading (webview view, string URL ){
Log. D ("------ redirect: -----", URL );
Return false;
}

@ Override
Public void onpagestarted (webview view, string URL, bitmap favicon ){
Eturl. settext (URL );
Log. D (TAG + "Start:", URL );
}

@ Override
Public void onpagefinished (webview view, string URL ){

Historymodel = new historymodel ();

// Obtain the history of the browser, including the ID, name, and URL.

Webhistoryitem item = webview. copybackforwardlist ()

. Getcurrentitem ();

// Get a browser page snapshot for browser history

Picture snapshot = view. capturepicture ();

If (snapshot. getwidth ()> 0 & snapshot. getheight ()> 0 ){
Bitmap B = bitmap. createbitmap (snapshot. getwidth (),
Snapshot. getheight (), bitmap. config. argb_8888 );

Canvas c = new canvas (B );

Snapshot. Draw (C );

Bitmap newbitmap = zoombitmap (B, 250,250 );
Historymodel. setsnapshot (newbitmap );
 

// Write the SD card of the mobile phone

// Fileoutputstream Fos = NULL;
// Try {
// Fos = new fileoutputstream ("/sdcard/" + item. gettitle ()
// + ". Jpg ");
// If (FOS! = NULL ){
// B. Compress (bitmap. compressformat. JPEG, 90, FOS );
// FOS. Close ();
//}
//} Catch (exception e ){
//
//}
//
// B = bitmapfactory. decodefile ("/sdcard/" + item. gettitle ()
// + ". Jpg ");
}

Historymodel. setid (item. GETID ());
Historymodel. setname (item. gettitle ());
Historymodel. seturl (item. geturl ());
Historymodels. Add (historymodel );

Log. D (TAG + "finish:", URL );
}
};

/**
* For #43616 from hyron
*
* @ Author allen_fang
*
*/
Private class geoclient extends webchromeclient {

@ Override
Public void ongeolocationpermissionsshowprompt (string origin,
Android. WebKit. geolocationpermissions. Callback callback ){
Super. ongeolocationpermissionsshowprompt (origin, callback );
Callback. Invoke (origin, true, false );
}

@ Override
Public void onprogresschanged (webview view, int newprogress ){
Mprogressbar. setprogress (newprogress );
Super. onprogresschanged (view, newprogress );
}

@ Override
Public void getvisitedhistory (valuecallback <string []> callback ){
Super. getvisitedhistory (callback );
}
}

@ Override
Public void ondownloadstart (string URL, string useragent,
String contentdisposition, string mimetype, long contentlength ){

Log. D ("--------------------------", mimetype );
Log. D ("--------------------------", URL );
Uri uri = URI. parse (URL );
Intent intent = new intent (intent. action_view, Uri );
Startactivity (intent );

}

Public static bitmap zoombitmap (Bitmap bitmap, int width, int height ){
Int W = bitmap. getwidth ();
Int H = bitmap. getheight ();
Matrix matrix = new matrix ();
Float scalewidth = (float) width/W );
Float scaleheight = (float) height/h );
Matrix. postscale (scalewidth, scaleheight );
Bitmap newbmp = bitmap. createbitmap (bitmap, 0, 0, W, H, matrix, true );
Return newbmp;
}
}

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.