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
17package com.android.providers.downloads.ui;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.MotionEvent;
22import android.view.accessibility.AccessibilityEvent;
23import android.widget.CheckBox;
24import android.widget.Checkable;
25import android.widget.GridLayout;
26
27/**
28 * This class customizes RelativeLayout to directly handle clicks on the left part of the view and
29 * treat them at clicks on the checkbox. This makes rapid selection of many items easier. This class
30 * also keeps an ID associated with the currently displayed download and notifies a listener upon
31 * selection changes with that ID.
32 */
33public class DownloadItem extends GridLayout implements Checkable {
34    private static float CHECKMARK_AREA = -1;
35
36    private boolean mIsInDownEvent = false;
37    private CheckBox mCheckBox;
38    private long mDownloadId;
39    private String mFileName;
40    private String mMimeType;
41    private DownloadList mDownloadList;
42    private int mPosition;
43
44    public DownloadItem(Context context, AttributeSet attrs, int defStyle) {
45        super(context, attrs, defStyle);
46        initialize();
47    }
48
49    public DownloadItem(Context context, AttributeSet attrs) {
50        super(context, attrs);
51        initialize();
52    }
53
54    public DownloadItem(Context context) {
55        super(context);
56        initialize();
57    }
58
59    private void initialize() {
60        if (CHECKMARK_AREA == -1) {
61            CHECKMARK_AREA = getResources().getDimensionPixelSize(R.dimen.checkmark_area);
62        }
63    }
64
65    @Override
66    protected void onFinishInflate() {
67        super.onFinishInflate();
68        mCheckBox = (CheckBox) findViewById(R.id.download_checkbox);
69    }
70
71    public void setData(long downloadId, int position, String fileName, String mimeType) {
72        mDownloadId = downloadId;
73        mPosition = position;
74        mFileName = fileName;
75        mMimeType = mimeType;
76        if (mDownloadList.isDownloadSelected(downloadId)) {
77            setChecked(true);
78        }
79    }
80
81    public void setDownloadListObj(DownloadList downloadList) {
82        mDownloadList = downloadList;
83    }
84
85    private boolean inCheckArea(MotionEvent event) {
86        if (isLayoutRtl()) {
87            return event.getX() > getWidth() - CHECKMARK_AREA;
88        } else {
89            return event.getX() < CHECKMARK_AREA;
90        }
91    }
92
93    @Override
94    public boolean onTouchEvent(MotionEvent event) {
95        boolean handled = false;
96        switch(event.getAction()) {
97            case MotionEvent.ACTION_DOWN:
98                if (inCheckArea(event)) {
99                    mIsInDownEvent = true;
100                    handled = true;
101                }
102                break;
103
104            case MotionEvent.ACTION_CANCEL:
105                mIsInDownEvent = false;
106                break;
107
108            case MotionEvent.ACTION_UP:
109                if (mIsInDownEvent && inCheckArea(event)) {
110                    toggle();
111                    sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
112                    handled = true;
113                }
114                mIsInDownEvent = false;
115                break;
116        }
117
118        if (handled) {
119            postInvalidate();
120        } else {
121            handled = super.onTouchEvent(event);
122        }
123
124        return handled;
125    }
126
127    @Override
128    public boolean isChecked() {
129        return mCheckBox.isChecked();
130    }
131
132    @Override
133    public void setChecked(boolean checked) {
134        mCheckBox.setChecked(checked);
135        mDownloadList.onDownloadSelectionChanged(mDownloadId, mCheckBox.isChecked(),
136                mFileName, mMimeType);
137        mDownloadList.getCurrentView().setItemChecked(mPosition, mCheckBox.isChecked());
138    }
139
140    @Override
141    public void toggle() {
142        setChecked(!isChecked());
143    }
144
145    public CheckBox getCheckBox() {
146        return this.mCheckBox;
147    }
148
149    public String getFileName() {
150        return mFileName;
151    }
152
153    public String getMimeType() {
154        return mMimeType;
155    }
156}
157