summaryrefslogtreecommitdiffstats
path: root/src/com/android/dreams/phototable/AlbumDataAdapter.java
blob: a0c039be02a3399aef5f07719e16833f04017cb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.dreams.phototable;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.SpannableString;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Settings panel for photo flipping dream.
 */
public class AlbumDataAdapter extends ArrayAdapter<PhotoSource.AlbumData> {
    private static final String TAG = "AlbumDataAdapter";
    private static final boolean DEBUG = false;

    public static final String ALBUM_SET = "Enabled Album Set";

    private final AlbumSettings mSettings;
    private final LayoutInflater mInflater;
    private final int mLayout;
    private final ItemClickListener mListener;

    public AlbumDataAdapter(Context context, SharedPreferences settings,
            int resource, List<PhotoSource.AlbumData> objects) {
        super(context, resource, objects);
        mSettings = AlbumSettings.getAlbumSettings(settings);
        mLayout = resource;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mListener = new ItemClickListener();

        HashSet<String> validAlbumIds = new HashSet<String>(objects.size());
        for (PhotoSource.AlbumData albumData: objects) {
            validAlbumIds.add(albumData.id);
        }
        mSettings.pruneObsoleteSettings(validAlbumIds);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View item = convertView;
        if (item == null) {
            item = mInflater.inflate(mLayout, parent, false);
        }
        PhotoSource.AlbumData data = getItem(position);

        View vCheckBox = item.findViewById(R.id.enabled);
        if (vCheckBox != null && vCheckBox instanceof CheckBox) {
            CheckBox checkBox = (CheckBox) vCheckBox;
            checkBox.setChecked(mSettings.isAlbumEnabled(data.id));
            checkBox.setTag(R.id.data_payload, data);
        }

        View vTextView = item.findViewById(R.id.title);
        if (vTextView != null && vTextView instanceof TextView) {
            TextView textView = (TextView) vTextView;
            textView.setText(data.title);
        }

        item.setOnClickListener(mListener);
        return item;
    }

    public static class AccountComparator implements Comparator<PhotoSource.AlbumData> {
        private final RecencyComparator recency;
        public AccountComparator() {
            recency = new RecencyComparator();
        }

        @Override
        public int compare(PhotoSource.AlbumData a, PhotoSource.AlbumData b) {
            if (a.account == b.account) {
                return recency.compare(a, b);
            } else {
                String typeAString = a.getType();
                String typeBString = b.getType();
                int typeA = 1;
                int typeB = 1;

                if (typeAString.equals(LocalSource.class.getName())) {
                    typeA = 0;
                }
                if (typeBString.equals(LocalSource.class.getName())) {
                    typeB = 0;
                }

                if (typeAString.equals(StockSource.class.getName())) {
                    typeA = 2;
                }
                if (typeBString.equals(StockSource.class.getName())) {
                    typeB = 2;
                }

                if (typeA == typeB) {
                    return a.account.compareTo(b.account);
                } else {
                    return (int) Math.signum(typeA - typeB);
                }
            }
        }
    }

    public static class RecencyComparator implements Comparator<PhotoSource.AlbumData> {
        private final TitleComparator title;
        public RecencyComparator() {
            title = new TitleComparator();
        }

        @Override
        public int compare(PhotoSource.AlbumData a, PhotoSource.AlbumData b) {
            if (a.updated == b.updated) {
                return title.compare(a, b);
            } else {
                return (int) Math.signum(b.updated - a.updated);
            }
        }
    }

    public static class TitleComparator implements Comparator<PhotoSource.AlbumData> {
        @Override
        public int compare(PhotoSource.AlbumData a, PhotoSource.AlbumData b) {
            return a.title.compareTo(b.title);
        }
    }

    private class ItemClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            final View vCheckBox = v.findViewById(R.id.enabled);
            if (vCheckBox != null && vCheckBox instanceof CheckBox) {
                final CheckBox checkBox = (CheckBox) vCheckBox;
                final PhotoSource.AlbumData data =
                    (PhotoSource.AlbumData) checkBox.getTag(R.id.data_payload);
                final boolean isChecked = !checkBox.isChecked();
                checkBox.setChecked(isChecked);
                mSettings.setAlbumEnabled(data.id, isChecked);
                if (DEBUG) Log.i(TAG, data.title + " is " +
                                 (isChecked ? "" : "not") + " enabled");
            } else {
                if (DEBUG) Log.w(TAG, "no checkbox found in settings row!");
            }
            v.setPressed(true);
        }
    }
}