aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/wallpapers/photophase/preferences/ChoosePicturesFragment.java
blob: f0f3522c719c533da3aa99fee9282350f8d98709 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Copyright (C) 2013 The CyanogenMod 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 org.cyanogenmod.wallpapers.photophase.preferences;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import org.cyanogenmod.wallpapers.photophase.R;
import org.cyanogenmod.wallpapers.photophase.animations.AlbumsFlip3dAnimationController;
import org.cyanogenmod.wallpapers.photophase.model.Album;
import org.cyanogenmod.wallpapers.photophase.widgets.AlbumInfo;
import org.cyanogenmod.wallpapers.photophase.widgets.AlbumPictures;
import org.cyanogenmod.wallpapers.photophase.widgets.CardLayout;

import java.io.File;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * A fragment class for select the picture that will be displayed on the wallpaper
 */
public class ChoosePicturesFragment extends PreferenceFragment {

    private final AsyncTask<Void, Album, Void> mAlbumsLoaderTask = new AsyncTask<Void, Album, Void>() {
        /**
         * {@inheritDoc}
         */
        @Override
        protected Void doInBackground(Void... params) {
            // Query all the external content and classify the pictures in albums and load the cards
            DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
            Album album = null;
            mAlbums.clear();
            Cursor c = mContentResolver.query(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            new String[]{ MediaStore.MediaColumns.DATA },
                            null,
                            null,
                            MediaStore.MediaColumns.DATA);
            if (c != null) {
                try {
                    while (c.moveToNext()) {
                        // Only valid files (those i can read)
                        String p = c.getString(0);
                        if (p != null) {
                            File f = new File(p);
                            if (f.exists() && f.isFile() && f.canRead()) {
                                  File path = f.getParentFile();
                                  String name = path.getName();
                                  if (album == null || album.getPath().compareTo(path.getAbsolutePath()) != 0) {
                                      if (album != null) {
                                          mAlbums.add(album);
                                          this.publishProgress(album);
                                          try {
                                              Thread.sleep(50L);
                                          } catch (InterruptedException e) {
                                              // Ignore
                                          }
                                      }
                                      album = new Album();
                                      album.setPath(path.getAbsolutePath());
                                      album.setName(name);
                                      album.setDate(df.format(new Date(path.lastModified())));
                                      album.setSelected(isSelectedItem(album.getPath()));
                                      album.setItems(new ArrayList<String>());
                                      album.setSelectedItems(new ArrayList<String>());
                                  }
                                  album.getItems().add(f.getAbsolutePath());
                                  if (isSelectedItem(f.getAbsolutePath())) {
                                      album.getSelectedItems().add(f.getAbsolutePath());
                                  }
                            }
                        }
                    }
                } finally {
                    c.close();
                }
            }

            return null;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected void onProgressUpdate(Album... values) {
            for (Album album : values) {
                addAlbum(album);
            }
        }
    };



    List<Album> mAlbums;
    ContentResolver mContentResolver;

    private CardLayout mAlbumsPanel;

    /*package*/ Set<String> mSelectedAlbums;

    /*package*/ boolean mSelectionChanged;

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContentResolver = getActivity().getContentResolver();

        // Create an empty album
        mAlbums = new ArrayList<Album>();

        // Change the preference manager
        getPreferenceManager().setSharedPreferencesName(PreferencesProvider.PREFERENCES_FILE);
        getPreferenceManager().setSharedPreferencesMode(Context.MODE_PRIVATE);

        // Load the albums user selection
        mSelectedAlbums = PreferencesProvider.Preferences.Media.getSelectedAlbums();
        mSelectionChanged = false;

        setHasOptionsMenu(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mAlbumsLoaderTask.getStatus().compareTo(Status.PENDING) == 0) {
            mAlbumsLoaderTask.cancel(true);
        }
        unbindDrawables(mAlbumsPanel);

        // Notify that the settings was changed
        Intent intent = new Intent(PreferencesProvider.ACTION_SETTINGS_CHANGED);
        if (mSelectionChanged) {
            intent.putExtra(PreferencesProvider.EXTRA_FLAG_MEDIA_RELOAD, Boolean.TRUE);
        }
        getActivity().sendBroadcast(intent);
    }

    /**
     * Method that unbind all the drawables for a view
     *
     * @param view The root view
     */
    private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            ((ViewGroup) view).removeAllViews();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.choose_picture_fragment, container, false);
        mAlbumsPanel = (CardLayout) v.findViewById(R.id.albums_panel);

        // Load the albums
        mAlbumsLoaderTask.execute();

        return v;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.albums, menu);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.mnu_ok:
                getActivity().finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * Method that adds a new album to the card layout
     *
     * @param album The album to add
     */
    void addAlbum(Album album) {
        LayoutInflater li = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View albumView = li.inflate(R.layout.album, mAlbumsPanel, false);
        final AlbumInfo albumInfo = (AlbumInfo)albumView.findViewById(R.id.album_info);
        final AlbumPictures albumPictures = (AlbumPictures)albumView.findViewById(R.id.album_pictures);

        // Load the album info
        albumInfo.updateView(album);
        if (album.isSelected()) {
            albumInfo.setSelected(true);
        }
        albumInfo.addCallBackListener(new AlbumInfo.CallbacksListener() {
            @Override
            public void onAlbumSelected(Album ref) {
                // Remove all pictures of the album and add the album reference
                removeAlbumItems(ref);
                mSelectedAlbums.add(ref.getPath());
                ref.setSelected(true);
                albumPictures.updateView(ref);

                PreferencesProvider.Preferences.Media.setSelectedAlbums(getActivity(), mSelectedAlbums);
                mSelectionChanged = true;
            }

            @Override
            public void onAlbumDeselected(Album ref) {
                // Remove all pictures of the album
                removeAlbumItems(ref);
                ref.setSelected(false);
                albumPictures.updateView(ref);

                PreferencesProvider.Preferences.Media.setSelectedAlbums(getActivity(), mSelectedAlbums);
                mSelectionChanged = true;
            }


        });

        // Load the album picture data
        albumPictures.updateView(album);
        albumPictures.addCallBackListener(new AlbumPictures.CallbacksListener() {
            @Override
            public void onBackButtonClick(View v) {
                // Ignored
            }

            @Override
            public void onSelectionChanged(Album ref) {
                // Remove, add, and persist the selection
                removeAlbumItems(ref);
                mSelectedAlbums.addAll(ref.getSelectedItems());
                ref.setSelected(false);
                albumInfo.updateView(ref);

                PreferencesProvider.Preferences.Media.setSelectedAlbums(getActivity(), mSelectedAlbums);
                mSelectionChanged = true;
            }
        });

        // Register the animation controller
        AlbumsFlip3dAnimationController controller = new AlbumsFlip3dAnimationController(albumInfo, albumPictures);
        controller.register();

        // Add to the panel of cards
        mAlbumsPanel.addCard(albumView);
    }

    /**
     * Method that checks if an item is selected
     *
     * @param item The item
     * @return boolean if an item is selected
     */
    /*package*/ boolean isSelectedItem(String item) {
        Iterator<String> it = mSelectedAlbums.iterator();
        while (it.hasNext()) {
            String albumPath = it.next();
            if (item.compareTo(albumPath) == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * Method that removes the reference to all the items and itself
     *
     * @param ref The album
     */
    /*package*/ void removeAlbumItems(Album ref) {
        Iterator<String> it = mSelectedAlbums.iterator();
        while (it.hasNext()) {
            String item = it.next();
            String parent = new File(item).getParent();
            if (parent.compareTo(ref.getPath()) == 0) {
                it.remove();
            } else if (item.compareTo(ref.getPath()) == 0) {
                it.remove();
            }
        }
    }
}