• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.deskclock.timer;
18 
19 import android.app.Fragment;
20 import android.app.FragmentManager;
21 import android.content.SharedPreferences;
22 import android.support.v4.view.PagerAdapter;
23 
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 
28 public class TimerFragmentAdapter extends FragmentStatePagerAdapter2 {
29 
30     private final ArrayList<TimerObj> mTimerList = new ArrayList<TimerObj>();
31     private final SharedPreferences mSharedPrefs;
32 
TimerFragmentAdapter(FragmentManager fm, SharedPreferences sharedPreferences)33     public TimerFragmentAdapter(FragmentManager fm, SharedPreferences sharedPreferences) {
34         super(fm);
35         mSharedPrefs = sharedPreferences;
36     }
37 
38     @Override
getItemPosition(Object object)39     public int getItemPosition(Object object) {
40         // Force return NONE so that the adapter always assumes the item position has changed
41         return PagerAdapter.POSITION_NONE;
42     }
43 
44     @Override
getCount()45     public int getCount() {
46         return mTimerList.size();
47     }
48 
49     @Override
getItem(int position)50     public Fragment getItem(int position) {
51         return TimerItemFragment.newInstance(mTimerList.get(position));
52     }
53 
addTimer(TimerObj timer)54     public void addTimer(TimerObj timer) {
55         // Newly created timer should always show on the top of the list
56         mTimerList.add(0, timer);
57         notifyDataSetChanged();
58     }
59 
getTimerAt(int position)60     public TimerObj getTimerAt(int position) {
61         return mTimerList.get(position);
62     }
63 
saveTimersToSharedPrefs()64     public void saveTimersToSharedPrefs() {
65         TimerObj.putTimersInSharedPrefs(mSharedPrefs, mTimerList);
66     }
67 
populateTimersFromPref()68     public void populateTimersFromPref() {
69         mTimerList.clear();
70         TimerObj.getTimersFromSharedPrefs(mSharedPrefs, mTimerList);
71         Collections.sort(mTimerList, new Comparator<TimerObj>() {
72             @Override
73             public int compare(TimerObj o1, TimerObj o2) {
74                 return (o2.mTimerId < o1.mTimerId) ? -1 : 1;
75             }
76         });
77 
78         notifyDataSetChanged();
79     }
80 
deleteTimer(int id)81     public void deleteTimer(int id) {
82         for (int i = 0; i < mTimerList.size(); i++) {
83             TimerObj timer = mTimerList.get(i);
84             if (timer.mTimerId == id) {
85                 if (timer.mView != null) {
86                     timer.mView.stop();
87                 }
88                 timer.deleteFromSharedPref(mSharedPrefs);
89                 mTimerList.remove(i);
90                 break;
91             }
92         }
93 
94         notifyDataSetChanged();
95         return;
96     }
97 }
98