Home | History | Annotate | Download | only in browser
      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.view.LayoutInflater;
     23 import android.view.View;
     24 import android.view.View.OnClickListener;
     25 import android.view.ViewGroup;
     26 import android.view.ViewGroup.LayoutParams;
     27 import android.widget.BaseAdapter;
     28 import android.widget.FrameLayout;
     29 import android.widget.ImageView;
     30 import android.widget.ImageView.ScaleType;
     31 import android.widget.TextView;
     32 
     33 import com.android.browser.view.PieItem;
     34 import com.android.browser.view.PieMenu;
     35 import com.android.browser.view.PieStackView.OnCurrentListener;
     36 
     37 import java.util.ArrayList;
     38 import java.util.List;
     39 
     40 /**
     41  * base controller for Quick Controls pie menu
     42  */
     43 public abstract class PieControlBase implements PieMenu.PieController {
     44 
     45     protected Activity mActivity;
     46     protected UiController mUiController;
     47     protected PieMenu mPie;
     48     protected int mItemSize;
     49     protected TextView mTabsCount;
     50 
     51     public PieControlBase(Activity activity, UiController controller) {
     52         mActivity = activity;
     53         mUiController = controller;
     54         mItemSize = (int) activity.getResources().getDimension(R.dimen.qc_item_size);
     55     }
     56 
     57     protected void attachToContainer(FrameLayout container) {
     58         if (mPie == null) {
     59             mPie = new PieMenu(mActivity);
     60             LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
     61                     LayoutParams.MATCH_PARENT);
     62             mPie.setLayoutParams(lp);
     63             populateMenu();
     64             mPie.setController(this);
     65         }
     66         container.addView(mPie);
     67     }
     68 
     69     protected void removeFromContainer(FrameLayout container) {
     70         container.removeView(mPie);
     71     }
     72 
     73     protected void forceToTop(FrameLayout container) {
     74         if (mPie.getParent() != null) {
     75             container.removeView(mPie);
     76             container.addView(mPie);
     77         }
     78     }
     79 
     80     protected abstract void populateMenu();
     81 
     82     protected void setClickListener(OnClickListener listener, PieItem... items) {
     83         for (PieItem item : items) {
     84             item.getView().setOnClickListener(listener);
     85         }
     86     }
     87 
     88     @Override
     89     public boolean onOpen() {
     90         int n = mUiController.getTabControl().getTabCount();
     91         mTabsCount.setText(Integer.toString(n));
     92         return true;
     93     }
     94 
     95     protected PieItem makeItem(int image, int l) {
     96         ImageView view = new ImageView(mActivity);
     97         view.setImageResource(image);
     98         view.setMinimumWidth(mItemSize);
     99         view.setMinimumHeight(mItemSize);
    100         view.setScaleType(ScaleType.CENTER);
    101         LayoutParams lp = new LayoutParams(mItemSize, mItemSize);
    102         view.setLayoutParams(lp);
    103         return new PieItem(view, l);
    104     }
    105 
    106     protected View makeTabsView() {
    107         View v = mActivity.getLayoutInflater().inflate(R.layout.qc_tabs_view, null);
    108         mTabsCount = (TextView) v.findViewById(R.id.label);
    109         mTabsCount.setText("1");
    110         ImageView image = (ImageView) v.findViewById(R.id.icon);
    111         image.setImageResource(R.drawable.ic_windows_holo_dark);
    112         image.setScaleType(ScaleType.CENTER);
    113         LayoutParams lp = new LayoutParams(mItemSize, mItemSize);
    114         v.setLayoutParams(lp);
    115         return v;
    116     }
    117 
    118     static class TabAdapter extends BaseAdapter implements OnCurrentListener {
    119 
    120         LayoutInflater mInflater;
    121         UiController mUiController;
    122         private List<Tab> mTabs;
    123         private int mCurrent;
    124 
    125         public TabAdapter(Context ctx, UiController ctl) {
    126             mInflater = LayoutInflater.from(ctx);
    127             mUiController = ctl;
    128             mTabs = new ArrayList<Tab>();
    129             mCurrent = -1;
    130         }
    131 
    132         public void setTabs(List<Tab> tabs) {
    133             mTabs = tabs;
    134             notifyDataSetChanged();
    135         }
    136 
    137         @Override
    138         public int getCount() {
    139             return mTabs.size();
    140         }
    141 
    142         @Override
    143         public Tab getItem(int position) {
    144             return mTabs.get(position);
    145         }
    146 
    147         @Override
    148         public long getItemId(int position) {
    149             return position;
    150         }
    151 
    152         @Override
    153         public View getView(int position, View convertView, ViewGroup parent) {
    154             final Tab tab = mTabs.get(position);
    155             View view = mInflater.inflate(R.layout.qc_tab,
    156                     null);
    157             ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
    158             TextView title1 = (TextView) view.findViewById(R.id.title1);
    159             TextView title2 = (TextView) view.findViewById(R.id.title2);
    160             Bitmap b = tab.getScreenshot();
    161             if (b != null) {
    162                 thumb.setImageBitmap(b);
    163             }
    164             if (position > mCurrent) {
    165                 title1.setVisibility(View.GONE);
    166                 title2.setText(tab.getTitle());
    167             } else {
    168                 title2.setVisibility(View.GONE);
    169                 title1.setText(tab.getTitle());
    170             }
    171             view.setOnClickListener(new OnClickListener() {
    172                 @Override
    173                 public void onClick(View v) {
    174                     mUiController.switchToTab(tab);
    175                 }
    176             });
    177             return view;
    178         }
    179 
    180         @Override
    181         public void onSetCurrent(int index) {
    182             mCurrent = index;
    183         }
    184 
    185     }
    186 
    187 }
    188