Home | History | Annotate | Download | only in adapter
      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 
     17 package com.android.gallery3d.ingest.adapter;
     18 
     19 import java.util.ArrayList;
     20 import java.util.Collection;
     21 
     22 public abstract class CheckBroker {
     23     private Collection<OnCheckedChangedListener> mListeners =
     24             new ArrayList<OnCheckedChangedListener>();
     25 
     26     public interface OnCheckedChangedListener {
     27         public void onCheckedChanged(int position, boolean isChecked);
     28         public void onBulkCheckedChanged();
     29     }
     30 
     31     public abstract void setItemChecked(int position, boolean checked);
     32 
     33     public void onCheckedChange(int position, boolean checked) {
     34         if (isItemChecked(position) != checked) {
     35             for (OnCheckedChangedListener l : mListeners) {
     36                 l.onCheckedChanged(position, checked);
     37             }
     38         }
     39     }
     40 
     41     public void onBulkCheckedChange() {
     42         for (OnCheckedChangedListener l : mListeners) {
     43             l.onBulkCheckedChanged();
     44         }
     45     }
     46 
     47     public abstract boolean isItemChecked(int position);
     48 
     49     public void registerOnCheckedChangeListener(OnCheckedChangedListener l) {
     50         mListeners.add(l);
     51     }
     52 
     53     public void unregisterOnCheckedChangeListener(OnCheckedChangedListener l) {
     54         mListeners.remove(l);
     55     }
     56 }
     57