The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.res.Configuration; |
| 24 | import android.net.Uri; |
| 25 | import android.os.Bundle; |
| 26 | import android.util.Log; |
| 27 | import android.view.View; |
| 28 | import android.widget.Button; |
| 29 | |
| 30 | import android.graphics.Bitmap; |
| 31 | import android.graphics.BitmapFactory; |
| 32 | |
| 33 | import android.webkit.WebView; |
| 34 | import android.webkit.WebViewClient; |
| 35 | import android.webkit.WebSettings; |
| 36 | |
| 37 | import android.view.KeyEvent; |
| 38 | import android.view.ViewGroup.LayoutParams; |
| 39 | |
| 40 | import android.widget.LinearLayout; |
| 41 | |
| 42 | public class GearsDialog extends Activity { |
| 43 | |
| 44 | private static final String TAG = "GearsDialog"; |
| 45 | |
| 46 | private WebView webview; |
| 47 | |
| 48 | private String htmlContent; |
| 49 | private String dialogArguments; |
| 50 | |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame^] | 51 | private boolean dismissed = false; |
| 52 | |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | @Override |
| 54 | public void onCreate(Bundle icicle) { |
| 55 | super.onCreate(icicle); |
| 56 | webview = new WebView(this); |
| 57 | webview.getSettings().setJavaScriptEnabled(true); |
| 58 | webview.addJavascriptInterface(this, "bridge"); |
| 59 | |
| 60 | setContentView(webview); |
| 61 | setTitle("Gears"); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onStart() { |
| 66 | super.onStart(); |
| 67 | loadHTML(); |
| 68 | } |
| 69 | |
| 70 | @Override |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame^] | 71 | public void onDestroy() { |
| 72 | super.onDestroy(); |
| 73 | // In case we reach this point without |
| 74 | // notifying GearsDialogService, we do it now. |
| 75 | if (!dismissed) { |
| 76 | notifyEndOfDialog(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 81 | public void onConfigurationChanged(Configuration newConfig) { |
| 82 | super.onConfigurationChanged(newConfig); |
| 83 | Intent i = getIntent(); |
| 84 | boolean inSettings = i.getBooleanExtra("inSettings", false); |
| 85 | // If we are called from the settings, we |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame^] | 86 | // dismiss ourselve upon rotation |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 87 | if (inSettings) { |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame^] | 88 | notifyEndOfDialog(); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | finish(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Load the HTML content in the WebView |
| 95 | */ |
| 96 | private void loadHTML() { |
| 97 | Intent i = getIntent(); |
| 98 | htmlContent = i.getStringExtra("htmlContent"); |
| 99 | dialogArguments = i.getStringExtra("dialogArguments"); |
| 100 | webview.loadDataWithBaseURL("", htmlContent, "text/html", "", ""); |
| 101 | } |
| 102 | |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame^] | 103 | /** |
| 104 | * Signal to GearsDialogService that we are done. |
| 105 | */ |
| 106 | private void notifyEndOfDialog() { |
| 107 | GearsDialogService.signalFinishedDialog(); |
| 108 | dismissed = true; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Intercepts the back key to immediately notify |
| 113 | * GearsDialogService that we are done. |
| 114 | */ |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 116 | if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.isDown()) { |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame^] | 117 | notifyEndOfDialog(); |
| 118 | } |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | return super.dispatchKeyEvent(event); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns a json-formatted string containing the information |
| 124 | * about the site. |
| 125 | * This method is accessible through the javascript bridge. |
| 126 | */ |
| 127 | public String getDialogArguments() { |
| 128 | return dialogArguments; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Set the results string and closes the dialog. |
| 133 | * This method is accessible through the javascript bridge. |
| 134 | */ |
| 135 | public void closeDialog(String results) { |
| 136 | GearsDialogService.closeDialog(results); |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame^] | 137 | notifyEndOfDialog(); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | finish(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Debug method outputting a message. |
| 143 | * This method is accessible through the javascript bridge. |
| 144 | */ |
| 145 | public void log(String msg) { |
| 146 | Log.v(TAG, msg); |
| 147 | } |
| 148 | } |