forked from Justin42/XVoicePlus
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGoogleVoiceManager.java
More file actions
270 lines (237 loc) · 10.1 KB
/
GoogleVoiceManager.java
File metadata and controls
270 lines (237 loc) · 10.1 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package io.behindthemath.xvoiceplus.gv;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.koushikdutta.ion.Ion;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import io.behindthemath.xvoiceplus.XVoicePlusService;
import io.behindthemath.xvoiceplus.gv.GvResponse.Conversation;
import io.behindthemath.xvoiceplus.gv.GvResponse.Payload;
public class GoogleVoiceManager {
private static final String TAG = GoogleVoiceManager.class.getSimpleName();
public static final String ACCOUNT_CHANGED = "io.behindthemath.xvoiceplus.ACCOUNT_CHANGED";
public static final int MARK_MESSAGE_UNREAD = 0;
public static final int MARK_MESSAGE_READ = 1;
private final Context mContext;
private String mRnrse = null;
public GoogleVoiceManager(Context context) {
mContext = context;
}
private SharedPreferences getSettings() {
return mContext.getSharedPreferences("io.behindthemath.xvoiceplus_preferences", Context.MODE_PRIVATE);
}
private String getAccount() {
return getSettings().getString("account", null);
}
public boolean refreshAuth() {
return getRnrse(true) != null;
}
private void saveRnrse(String rnrse) {
getSettings().edit().putString("_rnr_se", rnrse).apply();
}
private String getRnrse() {
return getRnrse(false);
}
private String getRnrse(boolean force) {
if (force || mRnrse == null) {
try {
mRnrse = fetchRnrSe();
} catch (Exception e) {
mRnrse = null;
}
}
return mRnrse;
}
/**
* Fetch the weirdo opaque token Google Voice needs...
*
* @return
*
* @throws Exception
*/
private String fetchRnrSe() throws Exception {
final String authToken = getAuthToken();
JsonObject userInfo = Ion.with(mContext).load("https://www.google.com/voice/request/user")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36")
.setHeader("Authorization", "GoogleLogin auth=" + authToken)
.asJsonObject()
.get();
Log.d(TAG, "fetchRnrSe: "+userInfo.getAsString());
String rnrse = userInfo.get("r").getAsString();
verifySmsForwarding(userInfo, authToken, rnrse);
saveRnrse(rnrse);
return rnrse;
}
private void verifySmsForwarding(JsonObject userInfo, String authToken, String rnrse) {
try {
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
String number = telephonyManager.getLine1Number();
if (number != null) {
JsonObject phones = userInfo.getAsJsonObject("phones");
for (Map.Entry<String, JsonElement> entry: phones.entrySet()) {
JsonObject phone = entry.getValue().getAsJsonObject();
if (!phone.get("smsEnabled").getAsBoolean()) {
break;
}
if (PhoneNumberUtils.compare(number, phone.get("phoneNumber").getAsString())) {
Log.i(TAG, "Disabling SMS forwarding to phone.");
Ion.with(mContext).load("https://www.google.com/voice/settings/editForwardingSms/")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36")
.setHeader("Authorization", "GoogleLogin auth=" + authToken)
.setBodyParameter("phoneId", entry.getKey())
.setBodyParameter("enabled", "0")
.setBodyParameter("_rnr_se", rnrse)
.asJsonObject();
break;
}
}
}
}
catch (Exception e) {
Log.e(TAG, "Error verifying GV SMS forwarding", e);
}
}
private static Bundle getAccountBundle(Context context, String account) throws Exception {
AccountManager accountManager = AccountManager.get(context);
if (accountManager != null) {
return accountManager.getAuthToken(new Account(account, "com.google"), "grandcentral", null, true, null, null)
.getResult();
}
return null;
}
private String getAuthToken() throws Exception {
Bundle bundle = getAccountBundle(mContext, getAccount());
return bundle.getString(AccountManager.KEY_AUTHTOKEN);
}
/**
* Hit the Google Voice API to send a text
*
* @param number
* @param text
*
* @throws Exception
*/
public void sendGvMessage(String number, String text) throws Exception {
final String authToken = getAuthToken();
String response = Ion.with(mContext).load("https://www.google.com/voice/sms/send/")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36")
.onHeaders(new GvHeadersCallback(mContext, authToken))
.setHeader("Authorization", "GoogleLogin auth=" + authToken)
.setBodyParameter("phoneNumber", number)
.setBodyParameter("sendErrorSms", "0")
.setBodyParameter("text", text)
.setBodyParameter("_rnr_se", getRnrse())
.asString()
.get();
try {
boolean isOk = new JsonParser().parse(response).getAsJsonObject().get("ok").getAsBoolean();
if (!isOk){
throw new Exception(response);
}
} catch (JsonParseException e){
throw new Exception(response);
}
Log.d(TAG, "Message sent with Google Voice successfully");
}
/**
* Update the read state on GV
*
* @param id - GV message id
* @param read - 0 = unread, 1 = read
*
* @throws Exception
*/
public void markGvMessageRead(String id, int read) throws Exception {
final String authToken = getAuthToken();
Log.d(TAG, "Marking messsage " + id + " as read");
Ion.with(mContext).load("https://www.google.com/voice/inbox/mark/")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36")
.onHeaders(new GvHeadersCallback(mContext, authToken))
.setHeader("Authorization", "GoogleLogin auth=" + authToken)
.setBodyParameter("messages", id)
.setBodyParameter("read", String.valueOf(read))
.setBodyParameter("_rnr_se", getRnrse());
}
/**
* Refresh the messages that were on the server
*
* @return
*
* @throws Exception
*/
public List<Conversation> retrieveMessages() throws Exception {
String account = getAccount();
if (account == null) {
Log.d(TAG, "Account not set");
return new ArrayList<>();
}
Log.i(TAG, "Refreshing messages");
// tokens!
final String authToken = getAuthToken();
try {
return Ion.with(mContext).load("https://www.google.com/voice/request/messages")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36")
.onHeaders(new GvHeadersCallback(mContext, authToken))
.setHeader("Authorization", "GoogleLogin auth=" + authToken)
.as(Payload.class).get().conversations;
} catch (Throwable e){
Log.e(TAG, "Unable to retrieve messages: "+e.getMessage(), e);
throw e;
}
}
public static void invalidateToken(final Context context, final String account) {
if (account == null) return;
new Thread() {
@Override
public void run() {
try {
// Grab the auth token
Bundle bundle = getAccountBundle(context, account);
String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
AccountManager accountManager = AccountManager.get(context);
if (accountManager != null) {
accountManager.invalidateAuthToken("com.google", authToken);
Log.i(TAG, "Token invalidated.");
} else {
throw new Exception("No account manager found");
}
}
catch (Exception e) {
Log.e(TAG, "error invalidating token", e);
}
}
}.start();
}
public static void getToken(final Context context, final Account account) {
AccountManager accountManager = AccountManager.get(context);
if (accountManager == null) return;
accountManager.getAuthToken(account, "grandcentral", null, false, new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
try {
Intent intent = new Intent(context, XVoicePlusService.class);
intent.setAction(ACCOUNT_CHANGED);
context.startService(intent);
Log.i(TAG, "Token retrieved.");
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Handler());
}
}