summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/ImageAdapter.java
blob: e6eaa75ad9e98b5985ff9e1b7a21929ae3ef298e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.browser;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.webkit.WebView;
import android.widget.*;

import java.util.ArrayList;

/**
 * Adapter used by ImageGrid.
 */
public class ImageAdapter implements ListAdapter {
    
    ArrayList<TabControl.Tab> mItems;  // Items shown in the grid
    ArrayList<DataSetObserver> mDataObservers; // Data change listeners
    Context mContext;  // Context to use to inflate views
    boolean mMaxedOut;
    boolean mLandScape;
    ImageGrid mImageGrid;
    boolean mIsLive;

    ImageAdapter(Context context, ImageGrid grid,
            ArrayList<TabControl.Tab> items, boolean live) {
        mContext = context;
        mIsLive = live;
        if (items == null) {
            mItems = new ArrayList<TabControl.Tab>();
        } else {
            mItems = items;
            if (items.size() == TabControl.MAX_TABS) {
                mMaxedOut = true;
            }
        }
        mImageGrid = grid;
        mDataObservers = new ArrayList<DataSetObserver>();
        mLandScape = context.getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_LANDSCAPE;
    }
    
    /**
     *  Whether the adapter is at its limit, determined by TabControl.MAX_TABS
     *
     *  @return True if the number of Tabs represented in this Adapter is at its
     *          maximum.
     */
    public boolean maxedOut() {
        return mMaxedOut;
    }

    /**
     * Clear the internal WebViews and remove their picture listeners.
     */
    public void clear() {
        for (TabControl.Tab t : mItems) {
            clearPictureListeners(t);
        }
        mItems.clear();
        notifyObservers();
    }

    private void clearPictureListeners(TabControl.Tab t) {
        if (t.getWebView() != null) {
            t.getWebView().setPictureListener(null);
            if (t.getSubWebView() != null) {
                t.getSubWebView().setPictureListener(null);
            }
        }
    }

    /**
     * Add a new window web page to the grid
     * 
     * @param t The tab to display
     */
    public void add(TabControl.Tab t) {
        if (mMaxedOut) {
            return;
        }
        mItems.add(t);
        notifyObservers();
        if (mItems.size() == TabControl.MAX_TABS) {
            mMaxedOut = true;
        }
    }
    
    /**
     * Remove a window from the list. At this point, the window
     * has already gone. It just needs to be removed from the screen
     * 
     * @param index window to remove
     */
    public void remove(int index) {
        if (index >= 0 && index < mItems.size()) {
            clearPictureListeners(mItems.remove(index));
            notifyObservers();
            mMaxedOut = false;
        }
    }

    /* (non-Javadoc)
     * @see android.widget.ListAdapter#areAllItemsSelectable()
     */
    public boolean areAllItemsEnabled() {
        return true;
    }

    /* (non-Javadoc)
     * @see android.widget.ListAdapter#isSelectable(int)
     */
    public boolean isEnabled(int position) {
        if (position >= 0 && position <= mItems.size()) {
            return true;
        }
        return false;
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getCount()
     */
    public int getCount() {
        // Include the New Window button if we have not reached the tab limit
        if (!mMaxedOut) {
            return mItems.size()+1;
        }
        return mItems.size();
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getItem(int)
     */
    public Object getItem(int position) {
        if (!mMaxedOut) {
            if (0 == position) {
                return null;
            }
            return mItems.get(position);
        }
        return mItems.get(position);
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getItemId(int)
     */
    public long getItemId(int position) {
        return position;
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getView(int, android.view.View, 
     * android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = null;
        if (convertView != null) {
            v = convertView;
        } else {
            LayoutInflater factory = LayoutInflater.from(mContext);
            v = factory.inflate(R.layout.tabitem, null);
        }
        FakeWebView img = (FakeWebView) v.findViewById(R.id.icon);
        ImageView close = (ImageView) v.findViewById(R.id.close);
        TextView tv = (TextView) v.findViewById(R.id.label);

        // position needs to be in the range of Tab indices.
        if (!mMaxedOut) {
            position--;
        }

        // Create the View for actual tabs
        if (position != ImageGrid.NEW_TAB) {
            TabControl.Tab t = mItems.get(position);
            img.setTab(t);
            tv.setText(t.getTitle());
            // Do not put the 'X' for a single tab or if the tab picker isn't
            // "live" (meaning the user cannot click on a tab)
            if (mItems.size() == 1 || !mIsLive) {
                close.setVisibility(View.GONE);
            } else {
                close.setVisibility(View.VISIBLE);
                final int pos = position;
                close.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            ImageAdapter.this.confirmClose(pos);
                        }
                    });
            }
        } else {
            img.setBackgroundColor(Color.BLACK);
            img.setImageResource(R.drawable.ic_new_window);
            img.setScaleType(ImageView.ScaleType.CENTER);
            img.setPadding(0, 0, 0, 34);
            tv.setText(R.string.new_window);
            close.setVisibility(View.GONE);
        }
        if (mLandScape) {
            ViewGroup.LayoutParams lp = img.getLayoutParams();
            lp.width = 225;
            lp.height = 120;
            img.requestLayout();
        }
        return v;
    }

    /*
     * Pop a confirmation dialog to the user asking if they want to close this
     * tab.
     */
    private void confirmClose(final int position) {
        final ImageGrid.Listener l = mImageGrid.getListener();
        if (l == null) {
            return;
        }
        DialogInterface.OnClickListener confirm =
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {
                        l.remove(position);
                    }
                };
        new AlertDialog.Builder(mContext)
                .setTitle(R.string.close)
                .setIcon(R.drawable.ssl_icon)
                .setMessage(R.string.close_window)
                .setPositiveButton(R.string.ok, confirm)
                .setNegativeButton(R.string.cancel, null)
                .show();
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
     */
    public void registerDataSetObserver(DataSetObserver observer) {
        mDataObservers.add(observer);

    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#hasStableIds()
     */
    public boolean hasStableIds() {
        return true;
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
     */
    public void unregisterDataSetObserver(DataSetObserver observer) {
        mDataObservers.remove(observer);

    }
    
    /**
     * Notify all the observers that a change has happened.
     */
    void notifyObservers() {
        for (DataSetObserver observer : mDataObservers) {
            observer.onChanged();
        }
    }

    public int getItemViewType(int position) {
        return 0;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public boolean isEmpty() {
        return getCount() == 0;
    }
}