PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.ideal.webaccess;
 18
 19import java.io.ByteArrayOutputStream;
 20import java.io.File;
 21import java.io.IOException;
 22import java.io.InputStream;
 23import android.app.Activity;
 24import android.content.ContentValues;
 25import android.content.res.AssetFileDescriptor;
 26import android.content.res.Resources;
 27import android.database.Cursor;
 28import android.os.Bundle;
 29import android.os.Environment;
 30import android.provider.BaseColumns;
 31import android.provider.Browser;
 32import android.webkit.WebView;
 33
 34/**
 35 * Main activity for IDEAL Web Access. Adds the bookmarklet + unzips the
 36 * necessary JavaScript files, then displays the tutorial HTML WebView.
 37 */
 38public class StartIdealWebAccess extends Activity {
 39    private static final String[] mColumnStrings = {
 40            BaseColumns._ID, Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL,
 41            Browser.BookmarkColumns.BOOKMARK, Browser.BookmarkColumns.VISITS
 42    };
 43
 44    private static final String bookmarkletTitle = "IDEAL Web Access";
 45
 46    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);})();";
 47
 48    /** Called when the activity is first created. */
 49    @Override
 50    public void onCreate(Bundle icicle) {
 51        super.onCreate(icicle);
 52        setContentView(R.layout.installing);
 53        loadBookmarklet();
 54        runInstaller();
 55    }
 56
 57    private void runInstaller() {
 58        try {
 59            Resources res = getResources();
 60            // Yes, it is weird to name the js zip file as .mp3, but that
 61            // seems to be the only way to keep AAPT from compressing it
 62            // and screwing up the build.
 63            // http://code.google.com/p/android/issues/detail?id=3122
 64            AssetFileDescriptor jsZipFd = res.openRawResourceFd(R.raw.ideal_js);
 65            InputStream stream = jsZipFd.createInputStream();
 66            (new Thread(new dataCheckAndUnzip(stream))).start();
 67        } catch (IOException e) {
 68            e.printStackTrace();
 69        }
 70    }
 71
 72    private class dataCheckAndUnzip implements Runnable {
 73        public InputStream stream;
 74
 75        public dataCheckAndUnzip(InputStream is) {
 76            stream = is;
 77        }
 78
 79        public void run() {
 80            File idealLoaderScript = new File(Environment.getExternalStorageDirectory()
 81                    + "/ideal-webaccess/js/ideal-webaccess.user.js");
 82            boolean result = idealLoaderScript.exists();
 83            if (!result) {
 84                result = Unzipper.doDataCheckAndUnzip(stream);
 85            }
 86            if (result) {
 87                runOnUiThread(new Runnable() {
 88                    @Override
 89                    public void run() {
 90                        showTutorial();
 91                    }
 92                });
 93            }
 94        }
 95    }
 96
 97    private void showTutorial() {
 98        setContentView(R.layout.main);
 99        final String mimeType = "text/html";
100        final String encoding = "UTF-8";
101
102        try {
103            InputStream is = getResources().openRawResource(R.raw.tutorial);
104
105            byte[] buffer = new byte[4096];
106            ByteArrayOutputStream baos = new ByteArrayOutputStream();
107
108            while (true) {
109                int read = is.read(buffer);
110
111                if (read == -1) {
112                    break;
113                }
114
115                baos.write(buffer, 0, read);
116            }
117
118            baos.close();
119            is.close();
120
121            String data = baos.toString();
122
123            WebView tutorialWebView = (WebView) findViewById(R.id.tutorial);
124            tutorialWebView.getSettings().setJavaScriptEnabled(true);
125            tutorialWebView.loadData(data, mimeType, encoding);
126
127        } catch (IOException e) {
128            // TODO Auto-generated catch block
129            e.printStackTrace();
130        }
131    }
132
133    private void loadBookmarklet() {
134        boolean hasBookmarklet = false;
135        Cursor c = getContentResolver().query(Browser.BOOKMARKS_URI, mColumnStrings, null, null,
136                null);
137        if (c != null) {
138            boolean keepGoing = c.moveToFirst();
139            while (keepGoing) {
140                if ((c.getString(3) != null) && (c.getString(3).equals("1"))
141                        && (c.getString(1) != null) && (c.getString(1).equals(bookmarkletTitle))
142                        && (c.getString(2) != null) && (c.getString(2).equals(bookmarkletUrl))) {
143                    hasBookmarklet = true;
144                }
145                keepGoing = c.moveToNext();
146            }
147        }
148        if (!hasBookmarklet) {
149            ContentValues bookmarkletContent = new ContentValues();
150            bookmarkletContent.put(Browser.BookmarkColumns.TITLE, bookmarkletTitle);
151            bookmarkletContent.put(Browser.BookmarkColumns.URL, bookmarkletUrl);
152            bookmarkletContent.put(Browser.BookmarkColumns.BOOKMARK, 1);
153            bookmarkletContent.put(Browser.BookmarkColumns.VISITS, 9999);
154            getContentResolver().insert(Browser.BOOKMARKS_URI, bookmarkletContent);
155        }
156    }
157
158}