1 /*
2  * Copyright (C) 2011 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.Context;
21 import android.graphics.Bitmap;
22 import android.os.Build;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26 import android.view.ViewGroup;
27 import android.view.ViewGroup.LayoutParams;
28 import android.webkit.WebView;
29 import android.widget.BaseAdapter;
30 import android.widget.FrameLayout;
31 import android.widget.ImageView;
32 import android.widget.ImageView.ScaleType;
33 import android.widget.TextView;
34 
35 import com.android.browser.UI.ComboViews;
36 import com.android.browser.view.PieItem;
37 import com.android.browser.view.PieMenu;
38 import com.android.browser.view.PieMenu.PieView.OnLayoutListener;
39 import com.android.browser.view.PieStackView;
40 import com.android.browser.view.PieStackView.OnCurrentListener;
41 
42 import java.util.ArrayList;
43 import java.util.List;
44 
45 /**
46  * Controller for Quick Controls pie menu
47  */
48 public class PieControl implements PieMenu.PieController, OnClickListener {
49 
50     protected Activity mActivity;
51     protected UiController mUiController;
52     protected PieMenu mPie;
53     protected int mItemSize;
54     protected TextView mTabsCount;
55     private BaseUi mUi;
56     private PieItem mBack;
57     private PieItem mForward;
58     private PieItem mRefresh;
59     private PieItem mUrl;
60     private PieItem mOptions;
61     private PieItem mBookmarks;
62     private PieItem mHistory;
63     private PieItem mAddBookmark;
64     private PieItem mNewTab;
65     private PieItem mIncognito;
66     private PieItem mClose;
67     private PieItem mShowTabs;
68     private PieItem mInfo;
69     private PieItem mFind;
70     private PieItem mShare;
71     private PieItem mRDS;
72     private TabAdapter mTabAdapter;
73 
PieControl(Activity activity, UiController controller, BaseUi ui)74     public PieControl(Activity activity, UiController controller, BaseUi ui) {
75         mActivity = activity;
76         mUiController = controller;
77         mItemSize = (int) activity.getResources().getDimension(R.dimen.qc_item_size);
78         mUi = ui;
79     }
80 
stopEditingUrl()81     public void stopEditingUrl() {
82         mUi.stopEditingUrl();
83     }
84 
attachToContainer(FrameLayout container)85     protected void attachToContainer(FrameLayout container) {
86         if (mPie == null) {
87             mPie = new PieMenu(mActivity);
88             LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
89                     LayoutParams.MATCH_PARENT);
90             mPie.setLayoutParams(lp);
91             populateMenu();
92             mPie.setController(this);
93         }
94         container.addView(mPie);
95     }
96 
removeFromContainer(FrameLayout container)97     protected void removeFromContainer(FrameLayout container) {
98         container.removeView(mPie);
99     }
100 
forceToTop(FrameLayout container)101     protected void forceToTop(FrameLayout container) {
102         if (mPie.getParent() != null) {
103             container.removeView(mPie);
104             container.addView(mPie);
105         }
106     }
107 
setClickListener(OnClickListener listener, PieItem... items)108     protected void setClickListener(OnClickListener listener, PieItem... items) {
109         for (PieItem item : items) {
110             item.getView().setOnClickListener(listener);
111         }
112     }
113 
114     @Override
onOpen()115     public boolean onOpen() {
116         int n = mUiController.getTabControl().getTabCount();
117         mTabsCount.setText(Integer.toString(n));
118         Tab tab = mUiController.getCurrentTab();
119         if (tab != null) {
120             mForward.setEnabled(tab.canGoForward());
121         }
122         WebView view = mUiController.getCurrentWebView();
123         if (view != null) {
124             ImageView icon = (ImageView) mRDS.getView();
125             if (mUiController.getSettings().hasDesktopUseragent(view)) {
126                 icon.setImageResource(R.drawable.ic_mobile);
127             } else {
128                 icon.setImageResource(R.drawable.ic_desktop_holo_dark);
129             }
130         }
131         return true;
132     }
133 
populateMenu()134     protected void populateMenu() {
135         mBack = makeItem(R.drawable.ic_back_holo_dark, 1);
136         mUrl = makeItem(R.drawable.ic_web_holo_dark, 1);
137         mBookmarks = makeItem(R.drawable.ic_bookmarks_holo_dark, 1);
138         mHistory = makeItem(R.drawable.ic_history_holo_dark, 1);
139         mAddBookmark = makeItem(R.drawable.ic_bookmark_on_holo_dark, 1);
140         mRefresh = makeItem(R.drawable.ic_refresh_holo_dark, 1);
141         mForward = makeItem(R.drawable.ic_forward_holo_dark, 1);
142         mNewTab = makeItem(R.drawable.ic_new_window_holo_dark, 1);
143         mIncognito = makeItem(R.drawable.ic_new_incognito_holo_dark, 1);
144         mClose = makeItem(R.drawable.ic_close_window_holo_dark, 1);
145         mInfo = makeItem(android.R.drawable.ic_menu_info_details, 1);
146         mFind = makeItem(R.drawable.ic_search_holo_dark, 1);
147         mShare = makeItem(R.drawable.ic_share_holo_dark, 1);
148         View tabs = makeTabsView();
149         mShowTabs = new PieItem(tabs, 1);
150         mOptions = makeItem(R.drawable.ic_settings_holo_dark, 1);
151         mRDS = makeItem(R.drawable.ic_desktop_holo_dark, 1);
152         mTabAdapter = new TabAdapter(mActivity, mUiController);
153         PieStackView stack = new PieStackView(mActivity);
154         stack.setLayoutListener(new OnLayoutListener() {
155             @Override
156             public void onLayout(int ax, int ay, boolean left) {
157                 buildTabs();
158             }
159         });
160         stack.setOnCurrentListener(mTabAdapter);
161         stack.setAdapter(mTabAdapter);
162         mShowTabs.setPieView(stack);
163         setClickListener(this, mBack, mRefresh, mForward, mUrl, mFind, mInfo,
164                 mShare, mBookmarks, mNewTab, mIncognito, mClose, mHistory,
165                 mAddBookmark, mOptions, mRDS);
166         if (!BrowserActivity.isTablet(mActivity)) {
167             mShowTabs.getView().setOnClickListener(this);
168         }
169         // level 1
170         mPie.addItem(mOptions);
171         mOptions.addItem(mRDS);
172         mOptions.addItem(makeFiller());
173         mOptions.addItem(makeFiller());
174         mOptions.addItem(makeFiller());
175         mPie.addItem(mBack);
176         mBack.addItem(mRefresh);
177         mBack.addItem(mForward);
178         mBack.addItem(makeFiller());
179         mBack.addItem(makeFiller());
180         mPie.addItem(mUrl);
181         mUrl.addItem(mFind);
182         mUrl.addItem(mShare);
183         mUrl.addItem(makeFiller());
184         mUrl.addItem(makeFiller());
185         mPie.addItem(mShowTabs);
186         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
187             mShowTabs.addItem(makeFiller());
188             mShowTabs.addItem(mClose);
189         } else {
190             mShowTabs.addItem(mClose);
191             mShowTabs.addItem(mIncognito);
192         }
193         mShowTabs.addItem(mNewTab);
194         mShowTabs.addItem(makeFiller());
195         mPie.addItem(mBookmarks);
196         mBookmarks.addItem(makeFiller());
197         mBookmarks.addItem(makeFiller());
198         mBookmarks.addItem(mAddBookmark);
199         mBookmarks.addItem(mHistory);
200     }
201 
202     @Override
onClick(View v)203     public void onClick(View v) {
204         Tab tab = mUiController.getTabControl().getCurrentTab();
205         WebView web = tab.getWebView();
206         if (mBack.getView() == v) {
207             tab.goBack();
208         } else if (mForward.getView() == v) {
209             tab.goForward();
210         } else if (mRefresh.getView() == v) {
211             if (tab.inPageLoad()) {
212                 web.stopLoading();
213             } else {
214                 web.reload();
215             }
216         } else if (mUrl.getView() == v) {
217             mUi.editUrl(false, true);
218         } else if (mBookmarks.getView() == v) {
219             mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
220         } else if (mHistory.getView() == v) {
221             mUiController.bookmarksOrHistoryPicker(ComboViews.History);
222         } else if (mAddBookmark.getView() == v) {
223             mUiController.bookmarkCurrentPage();
224         } else if (mNewTab.getView() == v) {
225             mUiController.openTabToHomePage();
226             mUi.editUrl(false, true);
227         } else if (mIncognito.getView() == v) {
228             mUiController.openIncognitoTab();
229             mUi.editUrl(false, true);
230         } else if (mClose.getView() == v) {
231             mUiController.closeCurrentTab();
232         } else if (mOptions.getView() == v) {
233             mUiController.openPreferences();
234         } else if (mShare.getView() == v) {
235             mUiController.shareCurrentPage();
236         } else if (mInfo.getView() == v) {
237             mUiController.showPageInfo();
238         } else if (mFind.getView() == v) {
239             mUiController.findOnPage();
240         } else if (mRDS.getView() == v) {
241             mUiController.toggleUserAgent();
242         } else if (mShowTabs.getView() == v) {
243             ((PhoneUi) mUi).showNavScreen();
244         }
245     }
246 
buildTabs()247     private void buildTabs() {
248         final List<Tab> tabs = mUiController.getTabs();
249         mUi.getActiveTab().capture();
250         mTabAdapter.setTabs(tabs);
251         PieStackView sym = (PieStackView) mShowTabs.getPieView();
252         sym.setCurrent(mUiController.getTabControl().getCurrentPosition());
253     }
254 
makeItem(int image, int l)255     protected PieItem makeItem(int image, int l) {
256         ImageView view = new ImageView(mActivity);
257         view.setImageResource(image);
258         view.setMinimumWidth(mItemSize);
259         view.setMinimumHeight(mItemSize);
260         view.setScaleType(ScaleType.CENTER);
261         LayoutParams lp = new LayoutParams(mItemSize, mItemSize);
262         view.setLayoutParams(lp);
263         return new PieItem(view, l);
264     }
265 
makeFiller()266     protected PieItem makeFiller() {
267         return new PieItem(null, 1);
268     }
269 
makeTabsView()270     protected View makeTabsView() {
271         View v = mActivity.getLayoutInflater().inflate(R.layout.qc_tabs_view, null);
272         mTabsCount = (TextView) v.findViewById(R.id.label);
273         mTabsCount.setText("1");
274         ImageView image = (ImageView) v.findViewById(R.id.icon);
275         image.setImageResource(R.drawable.ic_windows_holo_dark);
276         image.setScaleType(ScaleType.CENTER);
277         LayoutParams lp = new LayoutParams(mItemSize, mItemSize);
278         v.setLayoutParams(lp);
279         return v;
280     }
281 
282     static class TabAdapter extends BaseAdapter implements OnCurrentListener {
283 
284         LayoutInflater mInflater;
285         UiController mUiController;
286         private List<Tab> mTabs;
287         private int mCurrent;
288 
TabAdapter(Context ctx, UiController ctl)289         public TabAdapter(Context ctx, UiController ctl) {
290             mInflater = LayoutInflater.from(ctx);
291             mUiController = ctl;
292             mTabs = new ArrayList<Tab>();
293             mCurrent = -1;
294         }
295 
setTabs(List<Tab> tabs)296         public void setTabs(List<Tab> tabs) {
297             mTabs = tabs;
298             notifyDataSetChanged();
299         }
300 
301         @Override
getCount()302         public int getCount() {
303             return mTabs.size();
304         }
305 
306         @Override
getItem(int position)307         public Tab getItem(int position) {
308             return mTabs.get(position);
309         }
310 
311         @Override
getItemId(int position)312         public long getItemId(int position) {
313             return position;
314         }
315 
316         @Override
getView(int position, View convertView, ViewGroup parent)317         public View getView(int position, View convertView, ViewGroup parent) {
318             final Tab tab = mTabs.get(position);
319             View view = mInflater.inflate(R.layout.qc_tab,
320                     null);
321             ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
322             TextView title1 = (TextView) view.findViewById(R.id.title1);
323             TextView title2 = (TextView) view.findViewById(R.id.title2);
324             Bitmap b = tab.getScreenshot();
325             if (b != null) {
326                 thumb.setImageBitmap(b);
327             }
328             if (position > mCurrent) {
329                 title1.setVisibility(View.GONE);
330                 title2.setText(tab.getTitle());
331             } else {
332                 title2.setVisibility(View.GONE);
333                 title1.setText(tab.getTitle());
334             }
335             view.setOnClickListener(new OnClickListener() {
336                 @Override
337                 public void onClick(View v) {
338                     mUiController.switchToTab(tab);
339                 }
340             });
341             return view;
342         }
343 
344         @Override
onSetCurrent(int index)345         public void onSetCurrent(int index) {
346             mCurrent = index;
347         }
348 
349     }
350 
351 }
352