• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.deskclock.timer;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.util.Log;
24 import android.view.View;
25 
26 import com.android.deskclock.R;
27 import com.android.deskclock.Utils;
28 
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.Set;
35 
36 public class TimerObj implements Parcelable {
37 
38     private static final String TAG = "TimerObj";
39     // Max timer length is 9 hours + 99 minutes + 9 seconds
40     public static final long MAX_TIMER_LENGTH = (9 * 3600 + 99 * 60  + 99) * 1000;
41     public static final long MINUTE_IN_MILLIS = 60 * 1000;
42 
43     public int mTimerId;             // Unique id
44     public long mStartTime;          // With mTimeLeft , used to calculate the correct time
45     public long mTimeLeft;           // in the timer.
46     public long mOriginalLength;     // length set at start of timer and by +1 min after times up
47     public long mSetupLength;        // length set at start of timer
48     public TimerListItem mView;
49     public int mState;
50     public String mLabel;
51     public boolean mDeleteAfterUse;
52 
53     public static final int STATE_RUNNING = 1;
54     public static final int STATE_STOPPED = 2;
55     public static final int STATE_TIMESUP = 3;
56     public static final int STATE_DONE = 4;
57     public static final int STATE_RESTART = 5;
58     public static final int STATE_DELETED = 6;
59 
60     private static final String PREF_TIMER_ID = "timer_id_";
61     private static final String PREF_START_TIME  = "timer_start_time_";
62     private static final String PREF_TIME_LEFT = "timer_time_left_";
63     private static final String PREF_ORIGINAL_TIME = "timer_original_timet_";
64     private static final String PREF_SETUP_TIME = "timer_setup_timet_";
65     private static final String PREF_STATE = "timer_state_";
66     private static final String PREF_LABEL = "timer_label_";
67     private static final String PREF_DELETE_AFTER_USE = "delete_after_use_";
68 
69     private static final String PREF_TIMERS_LIST = "timers_list";
70 
71     public static final Parcelable.Creator<TimerObj> CREATOR = new Parcelable.Creator<TimerObj>() {
72         @Override
73         public TimerObj createFromParcel(Parcel p) {
74             return new TimerObj(p);
75         }
76 
77         @Override
78         public TimerObj[] newArray(int size) {
79             return new TimerObj[size];
80         }
81     };
82 
writeToSharedPref(SharedPreferences prefs)83     public void writeToSharedPref(SharedPreferences prefs) {
84         final SharedPreferences.Editor editor = prefs.edit();
85         final String id = Integer.toString(mTimerId);
86         editor.putInt(PREF_TIMER_ID + id, mTimerId);
87         editor.putLong(PREF_START_TIME + id, mStartTime);
88         editor.putLong (PREF_TIME_LEFT + id, mTimeLeft);
89         editor.putLong (PREF_ORIGINAL_TIME + id, mOriginalLength);
90         editor.putLong (PREF_SETUP_TIME + id, mSetupLength);
91         editor.putInt(PREF_STATE + id, mState);
92         final Set <String> timersList = prefs.getStringSet(PREF_TIMERS_LIST, new HashSet<String>());
93         timersList.add(id);
94         editor.putStringSet(PREF_TIMERS_LIST, timersList);
95         editor.putString(PREF_LABEL + id, mLabel);
96         editor.putBoolean(PREF_DELETE_AFTER_USE + id, mDeleteAfterUse);
97         editor.apply();
98     }
99 
readFromSharedPref(SharedPreferences prefs)100     public void readFromSharedPref(SharedPreferences prefs) {
101         String id = Integer.toString(mTimerId);
102         String key = PREF_START_TIME + id;
103         mStartTime = prefs.getLong(key, 0);
104         key = PREF_TIME_LEFT + id;
105         mTimeLeft = prefs.getLong(key, 0);
106         key = PREF_ORIGINAL_TIME + id;
107         mOriginalLength = prefs.getLong(key, 0);
108         key = PREF_SETUP_TIME + id;
109         mSetupLength = prefs.getLong(key, 0);
110         key = PREF_STATE + id;
111         mState = prefs.getInt(key, 0);
112         key = PREF_LABEL + id;
113         mLabel = prefs.getString(key, "");
114         key = PREF_DELETE_AFTER_USE + id;
115         mDeleteAfterUse = prefs.getBoolean(key, false);
116     }
117 
deleteFromSharedPref(SharedPreferences prefs)118     public void deleteFromSharedPref(SharedPreferences prefs) {
119         SharedPreferences.Editor editor = prefs.edit();
120         String key = PREF_TIMER_ID + Integer.toString(mTimerId);
121         String id = Integer.toString(mTimerId);
122         editor.remove (key);
123         key = PREF_START_TIME + id;
124         editor.remove (key);
125         key = PREF_TIME_LEFT + id;
126         editor.remove (key);
127         key = PREF_ORIGINAL_TIME + id;
128         editor.remove (key);
129         key = PREF_SETUP_TIME + id;
130         editor.remove (key);
131         key = PREF_STATE + id;
132         editor.remove (key);
133         Set <String> timersList = prefs.getStringSet(PREF_TIMERS_LIST, new HashSet<String>());
134         timersList.remove(id);
135         editor.putStringSet(PREF_TIMERS_LIST, timersList);
136         key = PREF_LABEL + id;
137         editor.remove(key);
138         key = PREF_DELETE_AFTER_USE + id;
139         editor.remove(key);
140         editor.commit();
141         //dumpTimersFromSharedPrefs(prefs);
142     }
143 
144 
145     @Override
describeContents()146     public int describeContents() {
147         return 0;
148     }
149 
150     @Override
writeToParcel(Parcel dest, int flags)151     public void writeToParcel(Parcel dest, int flags) {
152         dest.writeInt(mTimerId);
153         dest.writeLong(mStartTime);
154         dest.writeLong(mTimeLeft);
155         dest.writeLong(mOriginalLength);
156         dest.writeLong(mSetupLength);
157         dest.writeInt(mState);
158         dest.writeString(mLabel);
159     }
160 
TimerObj(Parcel p)161     public TimerObj(Parcel p) {
162         mTimerId = p.readInt();
163         mStartTime = p.readLong();
164         mTimeLeft = p.readLong();
165         mOriginalLength = p.readLong();
166         mSetupLength = p.readLong();
167         mState = p.readInt();
168         mLabel = p.readString();
169     }
170 
TimerObj()171     public TimerObj() {
172         this(0);
173     }
174 
TimerObj(long timerLength)175     public TimerObj(long timerLength) {
176       init(timerLength);
177     }
178 
TimerObj(long length, String label)179     public TimerObj(long length, String label) {
180         this(length);
181         mLabel = label != null ? label : "";
182     }
183 
init(long length)184     private void init (long length) {
185         /* TODO: mTimerId must avoid StopwatchService.NOTIFICATION_ID,
186          * TimerReceiver.IN_USE_NOTIFICATION_ID, and alarm ID's (which seem to be 1, 2, ..)
187          */
188         mTimerId = (int) Utils.getTimeNow();
189         mStartTime = Utils.getTimeNow();
190         mTimeLeft = mOriginalLength = mSetupLength = length;
191         mLabel = "";
192     }
193 
updateTimeLeft(boolean forceUpdate)194     public long updateTimeLeft(boolean forceUpdate) {
195         if (isTicking() || forceUpdate) {
196             long millis = Utils.getTimeNow();
197             mTimeLeft = mOriginalLength - (millis - mStartTime);
198         }
199         return mTimeLeft;
200     }
201 
getLabelOrDefault(Context context)202     public String getLabelOrDefault(Context context) {
203         return (mLabel == null || mLabel.length() == 0) ? context.getString(
204                 R.string.timer_notification_label)
205                 : mLabel;
206     }
207 
isTicking()208     public boolean isTicking() {
209         return mState == STATE_RUNNING || mState == STATE_TIMESUP;
210     }
211 
isInUse()212     public boolean isInUse() {
213         return mState == STATE_RUNNING || mState == STATE_STOPPED;
214     }
215 
addTime(long time)216     public void addTime(long time) {
217         mTimeLeft = mOriginalLength - (Utils.getTimeNow() - mStartTime);
218         if (mTimeLeft < MAX_TIMER_LENGTH - time) {
219                 mOriginalLength += time;
220         }
221     }
222 
getDeleteAfterUse()223     public boolean getDeleteAfterUse() {
224         return mDeleteAfterUse;
225     }
226 
getTimesupTime()227     public long getTimesupTime() {
228         return mStartTime + mOriginalLength;
229     }
230 
231 
getTimersFromSharedPrefs( SharedPreferences prefs, ArrayList<TimerObj> timers)232     public static void getTimersFromSharedPrefs(
233             SharedPreferences prefs, ArrayList<TimerObj> timers) {
234         Object[] timerStrings =
235                 prefs.getStringSet(PREF_TIMERS_LIST, new HashSet<String>()).toArray();
236         if (timerStrings.length > 0) {
237             for (int i = 0; i < timerStrings.length; i++) {
238                 TimerObj t = new TimerObj();
239                 t.mTimerId = Integer.parseInt((String)timerStrings[i]);
240                 t.readFromSharedPref(prefs);
241                 timers.add(t);
242             }
243             Collections.sort(timers, new Comparator<TimerObj>() {
244                 @Override
245                 public int compare(TimerObj timerObj1, TimerObj timerObj2) {
246                    return timerObj1.mTimerId - timerObj2.mTimerId;
247                 }
248             });
249         }
250     }
251 
getTimersFromSharedPrefs( SharedPreferences prefs, ArrayList<TimerObj> timers, int match)252     public static void getTimersFromSharedPrefs(
253             SharedPreferences prefs, ArrayList<TimerObj> timers, int match) {
254         Object[] timerStrings = prefs.getStringSet(PREF_TIMERS_LIST, new HashSet<String>())
255                 .toArray();
256         if (timerStrings.length > 0) {
257             for (int i = 0; i < timerStrings.length; i++) {
258                 TimerObj t = new TimerObj();
259                 t.mTimerId = Integer.parseInt((String) timerStrings[i]);
260                 t.readFromSharedPref(prefs);
261                 if (t.mState == match) {
262                     timers.add(t);
263                 }
264             }
265         }
266     }
267 
putTimersInSharedPrefs( SharedPreferences prefs, ArrayList<TimerObj> timers)268     public static void putTimersInSharedPrefs(
269             SharedPreferences prefs, ArrayList<TimerObj> timers) {
270         if (timers.size() > 0) {
271             for (int i = 0; i < timers.size(); i++) {
272                 timers.get(i).writeToSharedPref(prefs);
273             }
274         }
275     }
276 
dumpTimersFromSharedPrefs( SharedPreferences prefs)277     public static void dumpTimersFromSharedPrefs(
278             SharedPreferences prefs) {
279         Object[] timerStrings =
280                 prefs.getStringSet(PREF_TIMERS_LIST, new HashSet<String>()).toArray();
281         Log.v(TAG,"--------------------- timers list in shared prefs");
282         if (timerStrings.length > 0) {
283             for (int i = 0; i < timerStrings.length; i++) {
284                 int id = Integer.parseInt((String)timerStrings[i]);
285                 Log.v(TAG,"---------------------timer  " + (i + 1) + ": id - " + id);
286             }
287         }
288     }
289 
resetTimersInSharedPrefs(SharedPreferences prefs)290     public static void resetTimersInSharedPrefs(SharedPreferences prefs) {
291         ArrayList<TimerObj> timers = new  ArrayList<TimerObj>();
292         getTimersFromSharedPrefs(prefs, timers);
293         Iterator<TimerObj> i = timers.iterator();
294         while(i.hasNext()) {
295             TimerObj t = i.next();
296             t.mState = TimerObj.STATE_RESTART;
297             t.mTimeLeft = t. mOriginalLength = t.mSetupLength;
298             t.writeToSharedPref(prefs);
299         }
300     }
301 
302 }
303