/WebAccess/src/com/ideal/webaccess/StartIdealWebAccess.java

http://eyes-free.googlecode.com/ · Java · 158 lines · 111 code · 22 blank · 25 comment · 18 complexity · 8997edb406e314a99244fa03f9bfc5f5 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The IDEAL Group
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.ideal.webaccess;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import android.app.Activity;
  22. import android.content.ContentValues;
  23. import android.content.res.AssetFileDescriptor;
  24. import android.content.res.Resources;
  25. import android.database.Cursor;
  26. import android.os.Bundle;
  27. import android.os.Environment;
  28. import android.provider.BaseColumns;
  29. import android.provider.Browser;
  30. import android.webkit.WebView;
  31. /**
  32. * Main activity for IDEAL Web Access. Adds the bookmarklet + unzips the
  33. * necessary JavaScript files, then displays the tutorial HTML WebView.
  34. */
  35. public class StartIdealWebAccess extends Activity {
  36. private static final String[] mColumnStrings = {
  37. BaseColumns._ID, Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL,
  38. Browser.BookmarkColumns.BOOKMARK, Browser.BookmarkColumns.VISITS
  39. };
  40. private static final String bookmarkletTitle = "IDEAL Web Access";
  41. private static final String bookmarkletUrl = "javascript:(function(){loaderScript=document.createElement('SCRIPT');loaderScript.type='text/javascript';loaderScript.src='content://com.ideal.webaccess.localjs/ideal-loader.js';document.getElementsByTagName('head')[0].appendChild(loaderScript);})();";
  42. /** Called when the activity is first created. */
  43. @Override
  44. public void onCreate(Bundle icicle) {
  45. super.onCreate(icicle);
  46. setContentView(R.layout.installing);
  47. loadBookmarklet();
  48. runInstaller();
  49. }
  50. private void runInstaller() {
  51. try {
  52. Resources res = getResources();
  53. // Yes, it is weird to name the js zip file as .mp3, but that
  54. // seems to be the only way to keep AAPT from compressing it
  55. // and screwing up the build.
  56. // http://code.google.com/p/android/issues/detail?id=3122
  57. AssetFileDescriptor jsZipFd = res.openRawResourceFd(R.raw.ideal_js);
  58. InputStream stream = jsZipFd.createInputStream();
  59. (new Thread(new dataCheckAndUnzip(stream))).start();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. private class dataCheckAndUnzip implements Runnable {
  65. public InputStream stream;
  66. public dataCheckAndUnzip(InputStream is) {
  67. stream = is;
  68. }
  69. public void run() {
  70. File idealLoaderScript = new File(Environment.getExternalStorageDirectory()
  71. + "/ideal-webaccess/js/ideal-webaccess.user.js");
  72. boolean result = idealLoaderScript.exists();
  73. if (!result) {
  74. result = Unzipper.doDataCheckAndUnzip(stream);
  75. }
  76. if (result) {
  77. runOnUiThread(new Runnable() {
  78. @Override
  79. public void run() {
  80. showTutorial();
  81. }
  82. });
  83. }
  84. }
  85. }
  86. private void showTutorial() {
  87. setContentView(R.layout.main);
  88. final String mimeType = "text/html";
  89. final String encoding = "UTF-8";
  90. try {
  91. InputStream is = getResources().openRawResource(R.raw.tutorial);
  92. byte[] buffer = new byte[4096];
  93. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  94. while (true) {
  95. int read = is.read(buffer);
  96. if (read == -1) {
  97. break;
  98. }
  99. baos.write(buffer, 0, read);
  100. }
  101. baos.close();
  102. is.close();
  103. String data = baos.toString();
  104. WebView tutorialWebView = (WebView) findViewById(R.id.tutorial);
  105. tutorialWebView.getSettings().setJavaScriptEnabled(true);
  106. tutorialWebView.loadData(data, mimeType, encoding);
  107. } catch (IOException e) {
  108. // TODO Auto-generated catch block
  109. e.printStackTrace();
  110. }
  111. }
  112. private void loadBookmarklet() {
  113. boolean hasBookmarklet = false;
  114. Cursor c = getContentResolver().query(Browser.BOOKMARKS_URI, mColumnStrings, null, null,
  115. null);
  116. if (c != null) {
  117. boolean keepGoing = c.moveToFirst();
  118. while (keepGoing) {
  119. if ((c.getString(3) != null) && (c.getString(3).equals("1"))
  120. && (c.getString(1) != null) && (c.getString(1).equals(bookmarkletTitle))
  121. && (c.getString(2) != null) && (c.getString(2).equals(bookmarkletUrl))) {
  122. hasBookmarklet = true;
  123. }
  124. keepGoing = c.moveToNext();
  125. }
  126. }
  127. if (!hasBookmarklet) {
  128. ContentValues bookmarkletContent = new ContentValues();
  129. bookmarkletContent.put(Browser.BookmarkColumns.TITLE, bookmarkletTitle);
  130. bookmarkletContent.put(Browser.BookmarkColumns.URL, bookmarkletUrl);
  131. bookmarkletContent.put(Browser.BookmarkColumns.BOOKMARK, 1);
  132. bookmarkletContent.put(Browser.BookmarkColumns.VISITS, 9999);
  133. getContentResolver().insert(Browser.BOOKMARKS_URI, bookmarkletContent);
  134. }
  135. }
  136. }