blob: 302cbc0880e54005bdb2317bac15feb11bf5499d [file] [log] [blame]
Michael Kolb376b5412010-12-15 11:52:57 -08001/*
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
17package com.android.browser;
18
Michael Kolb0860d992011-03-07 15:26:33 -080019import com.android.browser.view.PieItem;
Michael Kolb1acef692011-03-08 14:12:06 -080020import com.android.browser.view.PieListView;
Michael Kolb376b5412010-12-15 11:52:57 -080021import com.android.browser.view.PieMenu;
22
23import android.app.Activity;
Michael Kolb1acef692011-03-08 14:12:06 -080024import android.content.Context;
25import android.view.LayoutInflater;
26import android.view.Menu;
27import android.view.MenuItem;
Michael Kolb376b5412010-12-15 11:52:57 -080028import android.view.View;
29import android.view.View.OnClickListener;
Michael Kolb1acef692011-03-08 14:12:06 -080030import android.view.ViewGroup;
Michael Kolb376b5412010-12-15 11:52:57 -080031import android.view.ViewGroup.LayoutParams;
32import android.webkit.WebView;
Michael Kolb1acef692011-03-08 14:12:06 -080033import android.widget.BaseAdapter;
Michael Kolb376b5412010-12-15 11:52:57 -080034import android.widget.FrameLayout;
35import android.widget.ImageView;
Michael Kolb1acef692011-03-08 14:12:06 -080036import android.widget.TextView;
37
38import java.util.ArrayList;
39import java.util.List;
Michael Kolb376b5412010-12-15 11:52:57 -080040
Michael Kolb376b5412010-12-15 11:52:57 -080041/**
42 * controller for Quick Controls pie menu
43 */
44public class PieControl implements OnClickListener, PieMenu.PieController {
45
46 private Activity mActivity;
47 private UiController mUiController;
48 private XLargeUi mUi;
49 private PieMenu mPie;
Michael Kolb0860d992011-03-07 15:26:33 -080050 private PieItem mBack;
51 private PieItem mForward;
52 private PieItem mRefresh;
53 private PieItem mUrl;
54 private PieItem mOptions;
55 private PieItem mBookmarks;
56 private PieItem mNewTab;
57 private PieItem mClose;
Michael Kolb1acef692011-03-08 14:12:06 -080058 private MenuAdapter mMenuAdapter;
Michael Kolb376b5412010-12-15 11:52:57 -080059
60 public PieControl(Activity activity, UiController controller, XLargeUi ui) {
61 mActivity = activity;
62 mUiController = controller;
63 mUi = ui;
Michael Kolb376b5412010-12-15 11:52:57 -080064 }
65
66 protected void attachToContainer(FrameLayout container) {
67 if (mPie == null) {
68 mPie = new PieMenu(mActivity);
69 LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
70 LayoutParams.MATCH_PARENT);
71 mPie.setLayoutParams(lp);
Michael Kolb0860d992011-03-07 15:26:33 -080072 mBack = makeItem(R.drawable.ic_back_holo_dark, 1);
73 mUrl = makeItem(R.drawable.ic_web_holo_dark, 1);
74 mBookmarks = makeItem(R.drawable.ic_bookmarks_holo_dark, 1);
75 mRefresh = makeItem(R.drawable.ic_refresh_holo_dark, 2);
76 mForward = makeItem(R.drawable.ic_forward_holo_dark, 2);
77 mNewTab = makeItem(R.drawable.ic_new_window_holo_dark, 2);
78 mClose = makeItem(R.drawable.ic_close_window_holo_dark, 2);
79 mOptions = makeItem(
80 com.android.internal.R.drawable.ic_menu_moreoverflow_normal_holo_dark,
81 2);
Michael Kolb1acef692011-03-08 14:12:06 -080082 mMenuAdapter = new MenuAdapter(mActivity, mUiController);
83 PieMenuView menusym = new PieMenuView(mActivity);
84 mOptions.setPieView(menusym);
85 menusym.setAdapter(mMenuAdapter);
Michael Kolb2a56eca2011-02-25 09:45:06 -080086 setClickListener(mBack,
Michael Kolb0860d992011-03-07 15:26:33 -080087 mRefresh,
88 mForward,
Michael Kolb2a56eca2011-02-25 09:45:06 -080089 mUrl,
Michael Kolb2a56eca2011-02-25 09:45:06 -080090 mBookmarks,
Michael Kolb0860d992011-03-07 15:26:33 -080091 mNewTab,
92 mClose
Michael Kolb2a56eca2011-02-25 09:45:06 -080093 );
Michael Kolb0860d992011-03-07 15:26:33 -080094 // level 1
95 mPie.addItem(mBack);
96 mPie.addItem(mUrl);
97 mPie.addItem(mBookmarks);
98 // level 2
99 mPie.addItem(mForward);
100 mPie.addItem(mRefresh);
101 mPie.addItem(mNewTab);
102 mPie.addItem(mClose);
103 mPie.addItem(mOptions);
Michael Kolb376b5412010-12-15 11:52:57 -0800104 mPie.setController(this);
105 }
106 container.addView(mPie);
107 }
108
Michael Kolb1acef692011-03-08 14:12:06 -0800109 protected void onMenuOpened(Menu menu) {
110 mMenuAdapter.setMenu(menu);
111 }
112
Michael Kolb376b5412010-12-15 11:52:57 -0800113 protected void removeFromContainer(FrameLayout container) {
114 container.removeView(mPie);
115 }
116
Michael Kolb0860d992011-03-07 15:26:33 -0800117 private PieItem makeItem(int image, int l) {
118 ImageView view = new ImageView(mActivity);
119 view.setImageResource(image);
120 view.setMinimumWidth(48);
121 view.setMinimumHeight(48);
Michael Kolb376b5412010-12-15 11:52:57 -0800122 LayoutParams lp = new LayoutParams(48, 48);
Michael Kolb0860d992011-03-07 15:26:33 -0800123 view.setLayoutParams(lp);
124 view.setBackgroundResource(R.drawable.qc_item_selector);
125 return new PieItem(view, l);
Michael Kolb376b5412010-12-15 11:52:57 -0800126 }
127
Michael Kolb0860d992011-03-07 15:26:33 -0800128 private void setClickListener(PieItem... items) {
129 for (PieItem item : items) {
130 item.getView().setOnClickListener(this);
Michael Kolb376b5412010-12-15 11:52:57 -0800131 }
132 }
133
134 protected void forceToTop(FrameLayout container) {
135 if (mPie.getParent() != null) {
136 container.removeView(mPie);
137 container.addView(mPie);
138 }
139 }
140
141 @Override
142 public void onClick(View v) {
Michael Kolb376b5412010-12-15 11:52:57 -0800143 Tab tab = mUiController.getTabControl().getCurrentTab();
144 WebView web = tab.getWebView();
Michael Kolb0860d992011-03-07 15:26:33 -0800145 if (mBack.getView() == v) {
Michael Kolb376b5412010-12-15 11:52:57 -0800146 web.goBack();
Michael Kolb0860d992011-03-07 15:26:33 -0800147 } else if (mForward.getView() == v) {
Michael Kolb376b5412010-12-15 11:52:57 -0800148 web.goForward();
Michael Kolb0860d992011-03-07 15:26:33 -0800149 } else if (mRefresh.getView() == v) {
Michael Kolb376b5412010-12-15 11:52:57 -0800150 if (tab.inPageLoad()) {
151 web.stopLoading();
152 } else {
153 web.reload();
154 }
Michael Kolb0860d992011-03-07 15:26:33 -0800155 } else if (mUrl.getView() == v) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800156 mUi.showTitleBarAndEdit();
Michael Kolb0860d992011-03-07 15:26:33 -0800157 } else if (mBookmarks.getView() == v) {
Michael Kolb376b5412010-12-15 11:52:57 -0800158 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolb0860d992011-03-07 15:26:33 -0800159 } else if (mNewTab.getView() == v) {
Michael Kolb376b5412010-12-15 11:52:57 -0800160 mUiController.openTabToHomePage();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800161 mUi.showTitleBarAndEdit();
Michael Kolb0860d992011-03-07 15:26:33 -0800162 } else if (mClose.getView() == v) {
Michael Kolb376b5412010-12-15 11:52:57 -0800163 mUiController.closeCurrentTab();
Michael Kolb376b5412010-12-15 11:52:57 -0800164 }
165 }
166
167 @Override
168 public boolean onOpen() {
Michael Kolb0860d992011-03-07 15:26:33 -0800169 return false;
Michael Kolb376b5412010-12-15 11:52:57 -0800170 }
171
Michael Kolb1acef692011-03-08 14:12:06 -0800172 private class PieMenuView extends PieListView {
173
174 /**
175 * @param ctx
176 */
177 public PieMenuView(Context ctx) {
178 super(ctx);
179 }
180
181 @Override
182 public void layout(int anchorX, int anchorY, boolean left) {
183 mActivity.openOptionsMenu();
184 super.layout(anchorX, anchorY, left);
185 }
186
187 }
188
189 private static class MenuAdapter extends BaseAdapter
190 implements OnClickListener {
191
192 List<MenuItem> mItems;
193 UiController mUiController;
194 LayoutInflater mInflater;
195
196 public MenuAdapter(Context ctx, UiController ctl) {
197 mUiController = ctl;
198 mInflater = LayoutInflater.from(ctx);
199 mItems = new ArrayList<MenuItem>();
200 }
201
202 public void setMenu(Menu menu) {
203 mItems.clear();
204 for (int i = 0; i < menu.size(); i++) {
205 MenuItem item = menu.getItem(i);
206 if (item.isEnabled() && item.isVisible()) {
207 mItems.add(item);
208 }
209 }
210 notifyDataSetChanged();
211 }
212
213 @Override
214 public int getCount() {
215 return mItems.size();
216 }
217
218 @Override
219 public MenuItem getItem(int position) {
220 return mItems.get(position);
221 }
222
223 @Override
224 public long getItemId(int position) {
225 return position;
226 }
227
228 @Override
229 public void onClick(View v) {
230 if (v.getTag() != null) {
231 mUiController.onOptionsItemSelected((MenuItem) v.getTag());
232 }
233 }
234
235 @Override
236 public View getView(int position, View convertView, ViewGroup parent) {
237 final MenuItem item = mItems.get(position);
238 View view = mInflater.inflate(
239 R.layout.qc_menu_item, null);
240 TextView label =
241 (TextView) view.findViewById(R.id.title);
242 label.setText(item.getTitle());
243 label.setTag(item);
244 label.setOnClickListener(this);
245 label.setLayoutParams(new LayoutParams(240, 32));
246 return label;
247 }
248
249 }
250
Michael Kolb376b5412010-12-15 11:52:57 -0800251}