1/*
2 * Copyright (C) 2013 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.gallery3d.ingest.ui;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.widget.CheckBox;
22import android.widget.Checkable;
23import android.widget.CompoundButton;
24import android.widget.CompoundButton.OnCheckedChangeListener;
25import android.widget.RelativeLayout;
26
27import com.android.gallery3d.R;
28import com.android.gallery3d.ingest.adapter.CheckBroker;
29
30public class MtpFullscreenView extends RelativeLayout implements Checkable,
31    CompoundButton.OnCheckedChangeListener, CheckBroker.OnCheckedChangedListener {
32
33    private MtpImageView mImageView;
34    private CheckBox mCheckbox;
35    private int mPosition = -1;
36    private CheckBroker mBroker;
37
38    public MtpFullscreenView(Context context) {
39        super(context);
40    }
41
42    public MtpFullscreenView(Context context, AttributeSet attrs) {
43        super(context, attrs);
44    }
45
46    public MtpFullscreenView(Context context, AttributeSet attrs, int defStyle) {
47        super(context, attrs, defStyle);
48    }
49
50    @Override
51    protected void onFinishInflate() {
52        super.onFinishInflate();
53        mImageView = (MtpImageView) findViewById(R.id.ingest_fullsize_image);
54        mCheckbox = (CheckBox) findViewById(R.id.ingest_fullsize_image_checkbox);
55        mCheckbox.setOnCheckedChangeListener(this);
56    }
57
58    @Override
59    public boolean isChecked() {
60        return mCheckbox.isChecked();
61    }
62
63    @Override
64    public void setChecked(boolean checked) {
65        mCheckbox.setChecked(checked);
66    }
67
68    @Override
69    public void toggle() {
70        mCheckbox.toggle();
71    }
72
73    @Override
74    public void onDetachedFromWindow() {
75        setPositionAndBroker(-1, null);
76        super.onDetachedFromWindow();
77    }
78
79    public MtpImageView getImageView() {
80        return mImageView;
81    }
82
83    public int getPosition() {
84        return mPosition;
85    }
86
87    public void setPositionAndBroker(int position, CheckBroker b) {
88        if (mBroker != null) {
89            mBroker.unregisterOnCheckedChangeListener(this);
90        }
91        mPosition = position;
92        mBroker = b;
93        if (mBroker != null) {
94            setChecked(mBroker.isItemChecked(position));
95            mBroker.registerOnCheckedChangeListener(this);
96        }
97    }
98
99    @Override
100    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
101        if (mBroker != null) mBroker.setItemChecked(mPosition, isChecked);
102    }
103
104    @Override
105    public void onCheckedChanged(int position, boolean isChecked) {
106        if (position == mPosition) {
107            setChecked(isChecked);
108        }
109    }
110
111    @Override
112    public void onBulkCheckedChanged() {
113        if(mBroker != null) setChecked(mBroker.isItemChecked(mPosition));
114    }
115}
116