1/*
2 * Copyright (c) 2015, Motorola Mobility LLC
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *     - Redistributions of source code must retain the above copyright
8 *       notice, this list of conditions and the following disclaimer.
9 *     - Redistributions in binary form must reproduce the above copyright
10 *       notice, this list of conditions and the following disclaimer in the
11 *       documentation and/or other materials provided with the distribution.
12 *     - Neither the name of Motorola Mobility nor the
13 *       names of its contributors may be used to endorse or promote products
14 *       derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MOTOROLA MOBILITY LLC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 * DAMAGE.
27 */
28
29package com.android.service.ims.presence;
30
31import android.content.Context;
32import android.content.SharedPreferences;
33
34public class SharedPrefUtil {
35    public static final String EAB_SHARED_PREF = "com.android.vt.eab";
36    public static final String INIT_DONE = "init_done";
37    private static final String CONTACT_CHANGED_PREF_KEY = "timestamp_change";
38    private static final String CONTACT_DELETE_PREF_KEY = "timestamp_delete";
39    private static final String CONTACT_PROFILE_CHANGED_PREF_KEY = "profile_timestamp_change";
40
41    public static boolean isInitDone(Context context) {
42        SharedPreferences eabPref = context.getSharedPreferences(
43                EAB_SHARED_PREF, Context.MODE_PRIVATE);
44        return eabPref.getBoolean(INIT_DONE, false);
45    }
46
47    public static void setInitDone(Context context, boolean initDone){
48        SharedPreferences.Editor eabPref = context.getSharedPreferences(
49                EAB_SHARED_PREF, Context.MODE_PRIVATE).edit();
50        eabPref.putBoolean(INIT_DONE, initDone).commit();
51    }
52
53    public static long getLastContactChangedTimestamp(Context context, long time) {
54        long contactModifySyncTime = 0;
55        SharedPreferences pref = context.getSharedPreferences(EAB_SHARED_PREF,
56                Context.MODE_PRIVATE);
57        contactModifySyncTime = pref.getLong(CONTACT_CHANGED_PREF_KEY, time);
58        return validateDeviceTimestamp(context, contactModifySyncTime);
59    }
60
61    public static void saveLastContactChangedTimestamp(Context context, long time) {
62        SharedPreferences.Editor eabPref = context.getSharedPreferences(
63                EAB_SHARED_PREF, Context.MODE_PRIVATE).edit();
64        eabPref.putLong(CONTACT_CHANGED_PREF_KEY, time).commit();
65    }
66
67    public static long getLastProfileContactChangedTimestamp(Context context, long time) {
68        long profileModifySyncTime = 0;
69        SharedPreferences pref = context.getSharedPreferences(EAB_SHARED_PREF,
70                Context.MODE_PRIVATE);
71        profileModifySyncTime = pref.getLong(CONTACT_PROFILE_CHANGED_PREF_KEY, time);
72        return validateDeviceTimestamp(context, profileModifySyncTime);
73    }
74
75    public static void saveLastProfileContactChangedTimestamp(Context context, long time) {
76        SharedPreferences.Editor eabPref = context.getSharedPreferences(
77                EAB_SHARED_PREF, Context.MODE_PRIVATE).edit();
78        eabPref.putLong(CONTACT_PROFILE_CHANGED_PREF_KEY, time).commit();
79    }
80
81    public static long getLastContactDeletedTimestamp(Context context, long time) {
82        long contactDeleteSyncTime = 0;
83        SharedPreferences pref = context.getSharedPreferences(EAB_SHARED_PREF,
84                Context.MODE_PRIVATE);
85        contactDeleteSyncTime = pref.getLong(CONTACT_DELETE_PREF_KEY, time);
86        return validateDeviceTimestamp(context, contactDeleteSyncTime);
87    }
88
89    public static void saveLastContactDeletedTimestamp(Context context, long time) {
90        SharedPreferences.Editor eabPref = context.getSharedPreferences(
91                EAB_SHARED_PREF, Context.MODE_PRIVATE).edit();
92        eabPref.putLong(CONTACT_DELETE_PREF_KEY, time).commit();
93    }
94
95    private static long validateDeviceTimestamp(Context context, long lastSyncedTimestamp) {
96        long deviceCurrentTimeMillis = System.currentTimeMillis();
97        if((lastSyncedTimestamp != 0) && lastSyncedTimestamp > deviceCurrentTimeMillis) {
98            SharedPreferences.Editor eabPref = context.getSharedPreferences(
99                    EAB_SHARED_PREF, Context.MODE_PRIVATE).edit();
100            eabPref.putLong(CONTACT_CHANGED_PREF_KEY, 0);
101            eabPref.putLong(CONTACT_DELETE_PREF_KEY, 0);
102            eabPref.putLong(CONTACT_PROFILE_CHANGED_PREF_KEY, 0);
103            eabPref.commit();
104            lastSyncedTimestamp = 0;
105        }
106        return lastSyncedTimestamp;
107    }
108
109    public static void resetEABSharedPref(Context context) {
110        SharedPreferences.Editor eabPref = context.getSharedPreferences(
111                EAB_SHARED_PREF, Context.MODE_PRIVATE).edit();
112        eabPref.putBoolean(INIT_DONE, false);
113        eabPref.putLong(CONTACT_CHANGED_PREF_KEY, 0);
114        eabPref.putLong(CONTACT_DELETE_PREF_KEY, 0);
115        eabPref.putLong(CONTACT_PROFILE_CHANGED_PREF_KEY, 0);
116        eabPref.commit();
117    }
118}
119