Home | History | Annotate | Download | only in phototable
      1 /*
      2  * Copyright (C) 2012 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 package com.android.dreams.phototable;
     17 
     18 import android.content.Context;
     19 import android.content.SharedPreferences;
     20 import android.util.Log;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.View.OnClickListener;
     24 import android.view.ViewGroup;
     25 import android.widget.ArrayAdapter;
     26 import android.widget.CheckBox;
     27 import android.widget.TextView;
     28 
     29 import java.util.Comparator;
     30 import java.util.HashSet;
     31 import java.util.List;
     32 
     33 /**
     34  * Settings panel for photo flipping dream.
     35  */
     36 public class AlbumDataAdapter extends ArrayAdapter<PhotoSource.AlbumData> {
     37     private static final String TAG = "AlbumDataAdapter";
     38     private static final boolean DEBUG = false;
     39 
     40     public static final String ALBUM_SET = "Enabled Album Set";
     41 
     42     private final AlbumSettings mSettings;
     43     private final LayoutInflater mInflater;
     44     private final int mLayout;
     45     private final ItemClickListener mListener;
     46     private final HashSet<String> mValidAlbumIds;
     47 
     48     public AlbumDataAdapter(Context context, SharedPreferences settings,
     49             int resource, List<PhotoSource.AlbumData> objects) {
     50         super(context, resource, objects);
     51         mSettings = AlbumSettings.getAlbumSettings(settings);
     52         mLayout = resource;
     53         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     54         mListener = new ItemClickListener();
     55 
     56         mValidAlbumIds = new HashSet<String>(objects.size());
     57         for (PhotoSource.AlbumData albumData: objects) {
     58             mValidAlbumIds.add(albumData.id);
     59         }
     60         mSettings.pruneObsoleteSettings(mValidAlbumIds);
     61     }
     62 
     63     public boolean isSelected(int position) {
     64         PhotoSource.AlbumData data = getItem(position);
     65         return mSettings.isAlbumEnabled(data.id);
     66     }
     67 
     68     public boolean areAllSelected() {
     69         return mSettings.areAllEnabled(mValidAlbumIds);
     70     }
     71 
     72     public void selectAll(boolean select) {
     73         if (select) {
     74             mSettings.enableAllAlbums(mValidAlbumIds);
     75         } else {
     76             mSettings.disableAllAlbums();
     77         }
     78         notifyDataSetChanged();
     79     }
     80 
     81     @Override
     82     public View getView(int position, View convertView, ViewGroup parent) {
     83         View item = convertView;
     84         if (item == null) {
     85             item = mInflater.inflate(mLayout, parent, false);
     86         }
     87         PhotoSource.AlbumData data = getItem(position);
     88 
     89         View vCheckBox = item.findViewById(R.id.enabled);
     90         if (vCheckBox != null && vCheckBox instanceof CheckBox) {
     91             CheckBox checkBox = (CheckBox) vCheckBox;
     92             checkBox.setChecked(isSelected(position));
     93             checkBox.setTag(R.id.data_payload, data);
     94         }
     95 
     96         View vTextView = item.findViewById(R.id.title);
     97         if (vTextView != null && vTextView instanceof TextView) {
     98             TextView textView = (TextView) vTextView;
     99             textView.setText(data.title);
    100         }
    101 
    102         item.setOnClickListener(mListener);
    103         return item;
    104     }
    105 
    106     public static class AccountComparator implements Comparator<PhotoSource.AlbumData> {
    107         private final RecencyComparator recency;
    108         public AccountComparator() {
    109             recency = new RecencyComparator();
    110         }
    111 
    112         @Override
    113         public int compare(PhotoSource.AlbumData a, PhotoSource.AlbumData b) {
    114             if (a.account == b.account) {
    115                 return recency.compare(a, b);
    116             } else {
    117                 String typeAString = a.getType();
    118                 String typeBString = b.getType();
    119                 int typeA = 1;
    120                 int typeB = 1;
    121 
    122                 if (typeAString.equals(LocalSource.class.getName())) {
    123                     typeA = 0;
    124                 }
    125                 if (typeBString.equals(LocalSource.class.getName())) {
    126                     typeB = 0;
    127                 }
    128 
    129                 if (typeAString.equals(StockSource.class.getName())) {
    130                     typeA = 2;
    131                 }
    132                 if (typeBString.equals(StockSource.class.getName())) {
    133                     typeB = 2;
    134                 }
    135 
    136                 if (typeA == typeB) {
    137                     return a.account.compareTo(b.account);
    138                 } else {
    139                     return (int) Math.signum(typeA - typeB);
    140                 }
    141             }
    142         }
    143     }
    144 
    145     public static class RecencyComparator implements Comparator<PhotoSource.AlbumData> {
    146         private final TitleComparator title;
    147         public RecencyComparator() {
    148             title = new TitleComparator();
    149         }
    150 
    151         @Override
    152         public int compare(PhotoSource.AlbumData a, PhotoSource.AlbumData b) {
    153             if (a.updated == b.updated) {
    154                 return title.compare(a, b);
    155             } else {
    156                 return (int) Math.signum(b.updated - a.updated);
    157             }
    158         }
    159     }
    160 
    161     public static class TitleComparator implements Comparator<PhotoSource.AlbumData> {
    162         @Override
    163         public int compare(PhotoSource.AlbumData a, PhotoSource.AlbumData b) {
    164             return a.title.compareTo(b.title);
    165         }
    166     }
    167 
    168     private class ItemClickListener implements OnClickListener {
    169         @Override
    170         public void onClick(View v) {
    171             final View vCheckBox = v.findViewById(R.id.enabled);
    172             if (vCheckBox != null && vCheckBox instanceof CheckBox) {
    173                 final CheckBox checkBox = (CheckBox) vCheckBox;
    174                 final PhotoSource.AlbumData data =
    175                     (PhotoSource.AlbumData) checkBox.getTag(R.id.data_payload);
    176                 final boolean isChecked = !checkBox.isChecked();
    177                 checkBox.setChecked(isChecked);
    178                 mSettings.setAlbumEnabled(data.id, isChecked);
    179                 notifyDataSetChanged();
    180                 if (DEBUG) Log.i(TAG, data.title + " is " +
    181                                  (isChecked ? "" : "not") + " enabled");
    182             } else {
    183                 if (DEBUG) Log.w(TAG, "no checkbox found in settings row!");
    184             }
    185             v.setPressed(true);
    186         }
    187     }
    188 }
    189