Back to project page Gcal_Importer.
The source code is released under:
Apache License
If you think the Android project Gcal_Importer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package net.crappo.android.androics; //from ww w . j a v a 2 s . c o m import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.StringTokenizer; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.cookie.BasicClientCookie; import android.os.Bundle; import android.os.StrictMode; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Bitmap; import android.view.View; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.Toast; /* * Webkit????????google???????????????????????????URL???????????Activity? * ????????????????????????????????gmail????????????????????????????????????? * (2??????Webkit????????????????????????????????????) */ @SuppressLint("SetJavaScriptEnabled") public class BrowserForDownLoadActivity extends Activity { private static final String firstUrl = "file:///android_asset/redirect.html"; private static String obtainedName = "tmp.zip"; // ????????????????????? private WebView webView; private Intent intent; private String downloadStatus = ""; // ????????????????????????????????????? private BrowserForDownLoadActivity activityObj; private CookieManager cookMgr; private CookieStore cookStore; private HttpClient client; private URL urlObj; private HttpGet httpGet; private ProgressDialog loading; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_browser_for_download); intent = getIntent(); activityObj = this; // for inner class's Toast RelativeLayout rl = (RelativeLayout)findViewById(R.id.browser_layout); loading = new ProgressDialog(this); // WebView?layout??????????????????????????????? webView = new WebView(this); webView.setVerticalScrollbarOverlay(true); webView.setWebViewClient(new CustomWebViewClient()); rl.addView(webView); WebSettings webSettings = webView.getSettings(); webSettings.setSupportMultipleWindows(true); webSettings.setLoadsImagesAutomatically(true); webSettings.setJavaScriptEnabled(true); // asset???????HTML????????????google????????????URL??????????? webView.loadUrl(firstUrl); // WebView????????????????????????????????? webView.setWebChromeClient(new ShowProgressBar()); webView.setDownloadListener(new WebDownloadListener()); } @Override protected void onResume() { // ??????????????????????????????????????????????????????????????????? super.onResume(); intent = getIntent(); if (downloadStatus.equals("Download FAILD")) { setResult(RESULT_CANCELED, intent); finish(); } } @Override protected void onDestroy() { // ??????Activity???????????????????????????? webView.clearHistory(); if(cookStore != null) cookStore.clear(); cookMgr.removeAllCookie(); super.onDestroy(); } /* xml?????????Button???????????????? android:onClick ????????????????????????????????? */ public void onClickReloadBtn(View v) { webView.reload(); } /* xml?????????Button???????????????? android:onClick ????????????????????????????????? */ public void onClickFinish(View v) { setResult(RESULT_CANCELED, intent); finish(); } /* icalzip??????????????????TopActivity??????????????????? */ private void successFinish() { Intent intent = getIntent(); setResult(RESULT_OK, intent); intent.putExtra("fileName", obtainedName); finish(); } /* * onCreate????webView???setWebChromeClient????????new????????? * progressBar??/????????????????????????????????????????????? */ class ShowProgressBar extends WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { View pBarWrapper = findViewById(R.id.browser_rogress_bar_wrapper); ProgressBar pBar = (ProgressBar) findViewById(R.id.browser_progress_bar); pBar.setProgress(newProgress); if (newProgress == 100) pBarWrapper.setVisibility(View.GONE); else pBarWrapper.setVisibility(View.VISIBLE); } } /* * Google Calendar????icalzip????????????????????????????????????????? * ?????????????????????????WebView??????????????????????????????? */ class WebDownloadListener implements DownloadListener { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { // ??????????HttpClient????????????DownloadManager???????????????????????????????????? // google???????DownloadManager???????????????????????????????????????? // ?????????????????????????????????????????????????????????????????????????? if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } // cookie??????????? String cookStr = ""; cookMgr = CookieManager.getInstance(); cookStr = cookMgr.getCookie(url); // HTTP???????????response????header????????????????????????????? httpGet = new HttpGet(url); httpGet.setHeader("Cookie", cookStr); client = new DefaultHttpClient(); try { client.execute(httpGet, new ResponseHandler<String>(){ @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: InputStream inContent = response.getEntity().getContent(); String[] str = null; for (Header header : response.getAllHeaders()) { String name = header.getName(); String val = header.getValue(); // ?????????????????????????????????? if(name.equals("Content-disposition")) { str = val.split(";"); str = str[1].split("="); StringTokenizer st = new StringTokenizer(str[1],"\""); obtainedName = st.nextToken(); } } // ??????????icalzip??????path????????????(???????????????????????zip?????????????TopActivity??????????????) DataInputStream dataInStream =new DataInputStream(inContent); String toPath = TopActivity.pathOfDataDir + ZipInOutMethods.outputDir + obtainedName; FileOutputStream outStream = new FileOutputStream(toPath); DataOutputStream dataOutStream = new DataOutputStream(new BufferedOutputStream(outStream)); // Read/Write Data byte[] b= new byte[4096]; int readByte = 0; while(-1 != (readByte = dataInStream.read(b))){ dataOutStream.write(b, 0, readByte); } dataInStream.close(); dataOutStream.close(); downloadStatus = "Success"; return null; case HttpStatus.SC_NOT_FOUND: throw new RuntimeException("HTTP Status : 404 Not Found."); default: throw new RuntimeException("HTTP Status : Error."); } } }); } catch (ClientProtocolException e) { e.printStackTrace(); downloadStatus = "Download FAILD"; } catch (IOException e) { e.printStackTrace(); downloadStatus = "Download FAILD"; } finally { client.getConnectionManager().shutdown(); } } } class CustomWebViewClient extends WebViewClient { String fileName = ""; String pathName = ""; String hostName = ""; String protocolName = ""; @Override public void onPageStarted(WebView webView, String urlStr, Bitmap favicon) { super.onPageStarted(webView, urlStr, favicon); urlParse(urlStr); loading.show(); // dispCookie(urlStr); // for Debug } @Override public void onPageFinished(WebView view, String urlStr) { super.onPageFinished(view, urlStr); saveCookie(urlStr); if (loading.isShowing()) { loading.dismiss(); } if(downloadStatus.equals("Success")) { successFinish(); } } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { urlParse(url); if ( hostName.equals("accounts.google.com") && pathName.equals("/SignUp")) { // ???????????????????(???????????)????????????????????????????? view.loadUrl(BrowserForDownLoadActivity.firstUrl); Toast.makeText(activityObj, R.string.browser_toast, Toast.LENGTH_LONG).show(); return false; } else if ( hostName.equals("www.google.com") && pathName.equals("/accounts/recovery")) { // ???????????????????(??????????????????recovery????)????????????????????????????? webView.loadUrl("file:///android_asset/announce.html"); return false; } view.loadUrl(url); return false; } /* ???????????????????????URL?parse????????????????????????????????????????? */ private void urlParse(String url) { try { urlObj = new URL(url); fileName = urlObj.getFile(); pathName = urlObj.getPath(); hostName = urlObj.getHost(); protocolName = urlObj.getProtocol(); } catch (MalformedURLException e) { e.printStackTrace(); } } /* DownloadListener???HttpClient???icalzip????????????????????????????????cookie????????????????? */ private void saveCookie(String urlStr) { urlParse(urlStr); // WebView???Cookie????? cookMgr = CookieManager.getInstance(); String cookie = cookMgr.getCookie(urlStr); if (cookie != null) { String[] cookies = cookie.split(";"); for (String keyValue : cookies) { keyValue = keyValue.trim(); int index = keyValue.indexOf("="); String[] cookieSet = new String[] { keyValue.substring(0, index), keyValue.substring(index + 1) }; // Cookie???? BasicClientCookie bCookie = new BasicClientCookie(cookieSet[0], cookieSet[1]); bCookie.setDomain(urlObj.getHost()); bCookie.setPath("/"); // CookieStore????? DefaultHttpClient defHttpClient; if (client != null) defHttpClient = (DefaultHttpClient)client; else defHttpClient = new DefaultHttpClient(); cookStore = defHttpClient.getCookieStore(); // Cookie??? cookStore.addCookie(bCookie); } } } // private void dispCookie(String urlStr) { // cookMgr = CookieManager.getInstance(); // String cookie = cookMgr.getCookie(urlStr); // Log.v("dispCookie", "cookie - " + cookie); // if (cookie != null) { // String[] cookies = cookie.split(";"); // for (String keyValue : cookies) { // keyValue = keyValue.trim(); // int index = keyValue.indexOf("="); // String[] cookieSet = new String[] { keyValue.substring(0, index), keyValue.substring(index + 1) }; // Log.v("dispCookie", "cookieSet - " + cookieSet[0] + " = " + cookieSet[1]); // } // } // } } }