Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.ContentResolver; |
| 22 | import android.content.Intent; |
| 23 | import android.content.res.Resources; |
| 24 | import android.database.Cursor; |
| 25 | import android.graphics.Bitmap; |
| 26 | import android.graphics.drawable.BitmapDrawable; |
| 27 | import android.net.ParseException; |
| 28 | import android.net.Uri; |
| 29 | import android.net.WebAddress; |
| 30 | import android.os.Bundle; |
| 31 | import android.os.Handler; |
| 32 | import android.os.Message; |
| 33 | import android.provider.Browser; |
| 34 | import android.util.Log; |
| 35 | import android.view.View; |
| 36 | import android.view.Window; |
| 37 | import android.widget.Button; |
| 38 | import android.widget.CheckBox; |
| 39 | import android.widget.EditText; |
| 40 | import android.widget.TextView; |
| 41 | import android.widget.Toast; |
| 42 | |
| 43 | import java.io.File; |
| 44 | import java.io.FileOutputStream; |
| 45 | import java.io.IOException; |
| 46 | import java.net.URI; |
| 47 | import java.net.URISyntaxException; |
| 48 | import java.util.Date; |
| 49 | |
| 50 | public class SaveToHomescreenDialog extends Activity { |
| 51 | |
| 52 | private EditText mTitle; |
| 53 | private String mUrl; |
| 54 | private Bitmap mFavicon; |
| 55 | private Bitmap mTouchIcon; |
| 56 | |
| 57 | private View.OnClickListener mOk = new View.OnClickListener() { |
| 58 | public void onClick(View v) { |
| 59 | if (save()) { |
| 60 | finish(); |
| 61 | } |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | private View.OnClickListener mCancel = new View.OnClickListener() { |
| 66 | public void onClick(View v) { |
| 67 | finish(); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | protected void onCreate(Bundle icicle) { |
| 72 | super.onCreate(icicle); |
| 73 | requestWindowFeature(Window.FEATURE_LEFT_ICON); |
| 74 | setContentView(R.layout.browser_add_bookmark_const_url); |
| 75 | setTitle(R.string.create_shortcut_bookmark); |
| 76 | getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, |
| 77 | R.drawable.ic_list_bookmark); |
| 78 | |
| 79 | String title = null; |
| 80 | String url = null; |
| 81 | Bundle map = getIntent().getExtras(); |
| 82 | if (map != null) { |
| 83 | title = map.getString("title"); |
| 84 | } |
| 85 | |
| 86 | mUrl = map.getString("url"); |
| 87 | mFavicon = (Bitmap)map.getParcelable("favicon"); |
| 88 | mTouchIcon = (Bitmap)map.getParcelable("touchIcon"); |
| 89 | |
| 90 | Bitmap icon = BookmarkUtils.createIcon(this, mTouchIcon, mFavicon, |
| 91 | BookmarkUtils.BookmarkIconType.ICON_HOME_SHORTCUT); |
| 92 | getWindow().setFeatureDrawable(Window.FEATURE_LEFT_ICON, new BitmapDrawable(icon)); |
| 93 | |
| 94 | mTitle = (EditText) findViewById(R.id.title); |
| 95 | mTitle.setText(title); |
| 96 | |
| 97 | Button okButton = (Button) findViewById(R.id.OK); |
| 98 | okButton.setOnClickListener(mOk); |
| 99 | |
| 100 | Button cancelButton = (Button) findViewById(R.id.cancel); |
| 101 | cancelButton.setOnClickListener(mCancel); |
| 102 | |
| 103 | if (!getWindow().getDecorView().isInTouchMode()) { |
| 104 | okButton.requestFocus(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Parse the data entered in the dialog and send an intent to create an |
| 110 | * icon on the homescreen. |
| 111 | */ |
| 112 | private boolean save() { |
| 113 | String title = mTitle.getText().toString().trim(); |
| 114 | String unfilteredUrl = BrowserActivity.fixUrl(mUrl); |
| 115 | if (title.length() == 0) { |
| 116 | mTitle.setError(getResources().getText(R.string.bookmark_needs_title)); |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | String url = unfilteredUrl.trim(); |
| 121 | |
| 122 | sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url, title, |
| 123 | mTouchIcon, mFavicon)); |
| 124 | setResult(RESULT_OK); |
| 125 | return true; |
| 126 | } |
| 127 | } |