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