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.adapter;
18
19import android.annotation.TargetApi;
20import android.os.Build;
21
22import java.util.ArrayList;
23import java.util.Collection;
24
25/**
26 * Helper to keep checked state in sync.
27 */
28@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
29public abstract class CheckBroker {
30  private Collection<OnCheckedChangedListener> mListeners =
31      new ArrayList<OnCheckedChangedListener>();
32
33  /**
34   * Listener for item checked state changes.
35   */
36  public interface OnCheckedChangedListener {
37    public void onCheckedChanged(int position, boolean isChecked);
38
39    public void onBulkCheckedChanged();
40  }
41
42  public abstract void setItemChecked(int position, boolean checked);
43
44  public void onCheckedChange(int position, boolean checked) {
45    if (isItemChecked(position) != checked) {
46      for (OnCheckedChangedListener l : mListeners) {
47        l.onCheckedChanged(position, checked);
48      }
49    }
50  }
51
52  public void onBulkCheckedChange() {
53    for (OnCheckedChangedListener l : mListeners) {
54      l.onBulkCheckedChanged();
55    }
56  }
57
58  public abstract boolean isItemChecked(int position);
59
60  public void registerOnCheckedChangeListener(OnCheckedChangedListener l) {
61    mListeners.add(l);
62  }
63
64  public void unregisterOnCheckedChangeListener(OnCheckedChangedListener l) {
65    mListeners.remove(l);
66  }
67}
68