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 29 package com.android.service.ims.presence; 30 31 import android.content.Context; 32 import android.content.SharedPreferences; 33 import android.telephony.SubscriptionManager; 34 35 public class SharedPrefUtil { 36 public static final String EAB_SHARED_PREF = "com.android.vt.eab"; 37 public static final String INIT_DONE = "init_done"; 38 private static final String CONTACT_CHANGED_PREF_KEY = "timestamp_change"; 39 private static final String CONTACT_DELETE_PREF_KEY = "timestamp_delete"; 40 private static final String CONTACT_PROFILE_CHANGED_PREF_KEY = "profile_timestamp_change"; 41 private static final String LAST_USED_SUB_ID = "last_used_sub_id"; 42 isInitDone(Context context)43 public static boolean isInitDone(Context context) { 44 SharedPreferences eabPref = context.getSharedPreferences( 45 EAB_SHARED_PREF, Context.MODE_PRIVATE); 46 return eabPref.getBoolean(INIT_DONE, false); 47 } 48 setInitDone(Context context, boolean initDone)49 public static void setInitDone(Context context, boolean initDone){ 50 SharedPreferences.Editor eabPref = context.getSharedPreferences( 51 EAB_SHARED_PREF, Context.MODE_PRIVATE).edit(); 52 eabPref.putBoolean(INIT_DONE, initDone).commit(); 53 } 54 getLastContactChangedTimestamp(Context context, long time)55 public static long getLastContactChangedTimestamp(Context context, long time) { 56 long contactModifySyncTime = 0; 57 SharedPreferences pref = context.getSharedPreferences(EAB_SHARED_PREF, 58 Context.MODE_PRIVATE); 59 contactModifySyncTime = pref.getLong(CONTACT_CHANGED_PREF_KEY, time); 60 return validateDeviceTimestamp(context, contactModifySyncTime); 61 } 62 saveLastContactChangedTimestamp(Context context, long time)63 public static void saveLastContactChangedTimestamp(Context context, long time) { 64 SharedPreferences.Editor eabPref = context.getSharedPreferences( 65 EAB_SHARED_PREF, Context.MODE_PRIVATE).edit(); 66 eabPref.putLong(CONTACT_CHANGED_PREF_KEY, time).commit(); 67 } 68 getLastProfileContactChangedTimestamp(Context context, long time)69 public static long getLastProfileContactChangedTimestamp(Context context, long time) { 70 long profileModifySyncTime = 0; 71 SharedPreferences pref = context.getSharedPreferences(EAB_SHARED_PREF, 72 Context.MODE_PRIVATE); 73 profileModifySyncTime = pref.getLong(CONTACT_PROFILE_CHANGED_PREF_KEY, time); 74 return validateDeviceTimestamp(context, profileModifySyncTime); 75 } 76 saveLastProfileContactChangedTimestamp(Context context, long time)77 public static void saveLastProfileContactChangedTimestamp(Context context, long time) { 78 SharedPreferences.Editor eabPref = context.getSharedPreferences( 79 EAB_SHARED_PREF, Context.MODE_PRIVATE).edit(); 80 eabPref.putLong(CONTACT_PROFILE_CHANGED_PREF_KEY, time).commit(); 81 } 82 getLastContactDeletedTimestamp(Context context, long time)83 public static long getLastContactDeletedTimestamp(Context context, long time) { 84 long contactDeleteSyncTime = 0; 85 SharedPreferences pref = context.getSharedPreferences(EAB_SHARED_PREF, 86 Context.MODE_PRIVATE); 87 contactDeleteSyncTime = pref.getLong(CONTACT_DELETE_PREF_KEY, time); 88 return validateDeviceTimestamp(context, contactDeleteSyncTime); 89 } 90 saveLastContactDeletedTimestamp(Context context, long time)91 public static void saveLastContactDeletedTimestamp(Context context, long time) { 92 SharedPreferences.Editor eabPref = context.getSharedPreferences( 93 EAB_SHARED_PREF, Context.MODE_PRIVATE).edit(); 94 eabPref.putLong(CONTACT_DELETE_PREF_KEY, time).commit(); 95 } 96 validateDeviceTimestamp(Context context, long lastSyncedTimestamp)97 private static long validateDeviceTimestamp(Context context, long lastSyncedTimestamp) { 98 long deviceCurrentTimeMillis = System.currentTimeMillis(); 99 if((lastSyncedTimestamp != 0) && lastSyncedTimestamp > deviceCurrentTimeMillis) { 100 SharedPreferences.Editor eabPref = context.getSharedPreferences( 101 EAB_SHARED_PREF, Context.MODE_PRIVATE).edit(); 102 eabPref.putLong(CONTACT_CHANGED_PREF_KEY, 0); 103 eabPref.putLong(CONTACT_DELETE_PREF_KEY, 0); 104 eabPref.putLong(CONTACT_PROFILE_CHANGED_PREF_KEY, 0); 105 eabPref.commit(); 106 lastSyncedTimestamp = 0; 107 } 108 return lastSyncedTimestamp; 109 } 110 resetEABSharedPref(Context context)111 public static void resetEABSharedPref(Context context) { 112 SharedPreferences.Editor eabPref = context.getSharedPreferences( 113 EAB_SHARED_PREF, Context.MODE_PRIVATE).edit(); 114 eabPref.putBoolean(INIT_DONE, false); 115 eabPref.putLong(CONTACT_CHANGED_PREF_KEY, 0); 116 eabPref.putLong(CONTACT_DELETE_PREF_KEY, 0); 117 eabPref.putLong(CONTACT_PROFILE_CHANGED_PREF_KEY, 0); 118 eabPref.commit(); 119 } 120 121 /** 122 * Saves the subscription ID used to get EAB contact presence information. 123 */ saveLastUsedSubscriptionId(Context context, int subId)124 public static void saveLastUsedSubscriptionId(Context context, int subId) { 125 SharedPreferences.Editor lastUsedSubIdPref = context.getSharedPreferences( 126 EAB_SHARED_PREF, Context.MODE_PRIVATE).edit(); 127 lastUsedSubIdPref.putInt(LAST_USED_SUB_ID, subId).commit(); 128 } 129 130 /** 131 * @return The subscription ID used to get EAB contact presence information. 132 */ getLastUsedSubscriptionId(Context context)133 public static int getLastUsedSubscriptionId(Context context) { 134 SharedPreferences lastUsedSubIdPref = context.getSharedPreferences( 135 EAB_SHARED_PREF, Context.MODE_PRIVATE); 136 return lastUsedSubIdPref.getInt(LAST_USED_SUB_ID, 137 SubscriptionManager.INVALID_SUBSCRIPTION_ID); 138 } 139 } 140