forked from Luccifer/FB-Source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoiceMail.java.txt
103 lines (87 loc) · 3.61 KB
/
VoiceMail.java.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.rtc.voicemail;
import javax.inject.Inject;
import java.util.Locale;
import com.facebook.common.util.StringUtil;
import com.facebook.debug.log.BLog;
import com.facebook.http.protocol.SingleMethodRunner;
import com.facebook.inject.AutoBind;
import com.facebook.prefs.shared.FbSharedPreferences;
import com.facebook.prefs.shared.PrefKey;
import com.facebook.rtc.RtcModule;
import com.facebook.rtc.prefs.VoipPrefKeys;
import com.facebook.xconfig.sync.XSyncApiMethodProvider;
import com.facebook.xconfig.sync.XSyncConfigResult;
import com.facebook.xconfig.sync.XSyncParams;
import com.facebook.xconfig.sync.XSyncResult;
import com.google.common.collect.ImmutableMap;
@AutoBind(inModule = RtcModule.class)
public class VoicemailAssetUrlFetcher {
private XSyncApiMethodProvider mXSyncApiMethodProvider;
private SingleMethodRunner mSingleMethodRunner;
private FbSharedPreferences mFbSharedPreferences;
private static final long CACHE_EXPIRATION_TIME_MS = 3*24*60*60*1000;
private static final String CONFIG_NAME = "android_messenger_voicemail";
private static final String LANGUAGE = Locale.getDefault().getLanguage();
private static final Class<?> TAG = VoicemailAssetUrlFetcher.class;
@Inject
public VoicemailAssetUrlFetcher(
XSyncApiMethodProvider xSyncApiMethodProvider,
SingleMethodRunner singleMethodRunner,
FbSharedPreferences fbSharedPreferences) {
mXSyncApiMethodProvider = xSyncApiMethodProvider;
mSingleMethodRunner = singleMethodRunner;
mFbSharedPreferences = fbSharedPreferences;
}
public String getOrFetchAssetUrl() {
String url = getCachedAssetUrl();
BLog.d("VoicemailHandler", "Cached url: " + url);
if (url == null) {
url = fetchAssetUrl();
BLog.d("VoicemailHandler", "Fetched url: " + url);
}
return url;
}
private String fetchAssetUrl() {
// Constructing params for request
String key = CONFIG_NAME + "!!" + LANGUAGE;
String val = "";
ImmutableMap<String, String> map = ImmutableMap.of(key, val);
XSyncParams xSyncParams = new XSyncParams(map, false);
try {
// Making request
XSyncResult xSyncResult = mSingleMethodRunner.run(mXSyncApiMethodProvider.get(null), xSyncParams);
// Parsing result
BLog.d("VoicemailHandler", xSyncResult.configResults.toString());
XSyncConfigResult xSyncConfigResult = xSyncResult.configResults.get(CONFIG_NAME);
BLog.d("VoicemailHandler", xSyncConfigResult.settingNamesToValues.toString());
String url = xSyncConfigResult.settingNamesToValues.get(LANGUAGE);
if (StringUtil.isEmptyOrNull(url)) {
return null;
}
// Caching result
mFbSharedPreferences.edit()
.putString(VoipPrefKeys.VOICEMAIL_ASSET_URL_KEY, url)
.putLong(VoipPrefKeys.VOICEMAIL_DOWNLOAD_TIME_KEY, System.currentTimeMillis())
.commit();
return url;
} catch (Exception e) {
BLog.d("VoicemailHandler", e.toString());
BLog.d(TAG, "Fetching voicemail asset url exception", e);
return null;
}
}
private String getCachedAssetUrl() {
String url = mFbSharedPreferences.getString(VoipPrefKeys.VOICEMAIL_ASSET_URL_KEY, null);
long downloadTime = mFbSharedPreferences.getLong(VoipPrefKeys.VOICEMAIL_DOWNLOAD_TIME_KEY, -1);
if (System.currentTimeMillis() - downloadTime > CACHE_EXPIRATION_TIME_MS) {
// Invalidate cache
mFbSharedPreferences.edit()
.remove(VoipPrefKeys.VOICEMAIL_DOWNLOAD_TIME_KEY)
.remove(VoipPrefKeys.VOICEMAIL_ASSET_URL_KEY)
.commit();
return null;
}
return url;
}
}