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