Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Helpers/SMSHelper.java
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Simon Redman <simon@ergotech.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | package org.kde.kdeconnect.Helpers; | ||||
22 | | ||||
23 | import android.content.Context; | ||||
24 | import android.database.Cursor; | ||||
25 | import android.net.Uri; | ||||
26 | import android.os.Build; | ||||
27 | import android.provider.Telephony; | ||||
28 | import android.support.annotation.RequiresApi; | ||||
29 | | ||||
30 | import org.json.JSONException; | ||||
31 | import org.json.JSONObject; | ||||
32 | | ||||
33 | import java.util.ArrayList; | ||||
34 | import java.util.HashMap; | ||||
35 | import java.util.List; | ||||
36 | import java.util.Map; | ||||
37 | | ||||
38 | public class SMSHelper { | ||||
39 | | ||||
40 | /** | ||||
41 | * Get the base address for the SMS content | ||||
42 | * <p> | ||||
43 | * If we want to support API < 19, it seems to be possible to read via this query | ||||
44 | * This is highly undocumented and very likely varies between vendors but appears to work | ||||
45 | */ | ||||
46 | protected static Uri getSMSURIBad() { | ||||
47 | return Uri.parse("content://sms/"); | ||||
48 | } | ||||
49 | | ||||
50 | /** | ||||
51 | * Get the base address for the SMS content | ||||
52 | * <p> | ||||
53 | * Use the new API way which should work on any phone API >= 19 | ||||
54 | */ | ||||
55 | @RequiresApi(Build.VERSION_CODES.KITKAT) | ||||
56 | protected static Uri getSMSURIGood() { | ||||
57 | // TODO: Why not use Telephony.MmsSms.CONTENT_URI? | ||||
58 | return Telephony.Sms.CONTENT_URI; | ||||
59 | } | ||||
60 | | ||||
61 | protected static Uri getSMSUri() { | ||||
62 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | ||||
63 | return getSMSURIGood(); | ||||
64 | } else { | ||||
65 | return getSMSURIBad(); | ||||
66 | } | ||||
67 | } | ||||
68 | | ||||
69 | /** | ||||
70 | * Get the base address for all message conversations | ||||
71 | */ | ||||
72 | protected static Uri getConversationUri() { | ||||
73 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | ||||
74 | return Telephony.MmsSms.CONTENT_CONVERSATIONS_URI; | ||||
75 | } else { | ||||
76 | // As with getSMSUriBad, this is potentially unsafe depending on whether a specific | ||||
77 | // manufacturer decided to do their own thing | ||||
78 | return Uri.parse("content://mms-sms/conversations"); | ||||
79 | } | ||||
80 | } | ||||
81 | | ||||
82 | /** | ||||
83 | * Get all the messages in a requested thread | ||||
84 | * | ||||
85 | * @param context android.content.Context running the request | ||||
86 | * @param threadID Thread to look up | ||||
87 | * @return List of all messages in the thread | ||||
88 | */ | ||||
89 | public static List<Message> getMessagesInThread(Context context, ThreadID threadID) { | ||||
90 | List<Message> toReturn = new ArrayList<>(); | ||||
91 | | ||||
92 | Uri smsUri = getSMSUri(); | ||||
93 | | ||||
94 | final String selection = ThreadID.lookupColumn + " == ?"; | ||||
95 | final String[] selectionArgs = new String[] { threadID.toString() }; | ||||
96 | | ||||
97 | Cursor smsCursor = context.getContentResolver().query( | ||||
98 | smsUri, | ||||
99 | Message.smsColumns, | ||||
100 | selection, | ||||
101 | selectionArgs, | ||||
102 | null); | ||||
103 | | ||||
104 | if (smsCursor != null && smsCursor.moveToFirst()) { | ||||
105 | int threadColumn = smsCursor.getColumnIndexOrThrow(ThreadID.lookupColumn); | ||||
106 | do { | ||||
107 | int thread = smsCursor.getInt(threadColumn); | ||||
108 | | ||||
109 | HashMap<String, String> messageInfo = new HashMap<>(); | ||||
110 | for (int columnIdx = 0; columnIdx < smsCursor.getColumnCount(); columnIdx++) { | ||||
111 | String colName = smsCursor.getColumnName(columnIdx); | ||||
112 | String body = smsCursor.getString(columnIdx); | ||||
113 | messageInfo.put(colName, body); | ||||
114 | } | ||||
115 | toReturn.add(new Message(messageInfo)); | ||||
116 | } while (smsCursor.moveToNext()); | ||||
117 | } else { | ||||
118 | // No SMSes available? | ||||
119 | } | ||||
120 | | ||||
121 | if (smsCursor != null) { | ||||
122 | smsCursor.close(); | ||||
123 | } | ||||
124 | | ||||
125 | return toReturn; | ||||
126 | } | ||||
127 | | ||||
128 | /** | ||||
129 | * Get the last message from each conversation. Can use those thread_ids to look up more | ||||
130 | * messages in those conversations | ||||
131 | * | ||||
132 | * @param context android.content.Context running the request | ||||
133 | * @return Mapping of thread_id to the first message in each thread | ||||
134 | */ | ||||
135 | public static Map<ThreadID, Message> getConversations(Context context) { | ||||
136 | HashMap<ThreadID, Message> toReturn = new HashMap<>(); | ||||
137 | | ||||
138 | Uri conversationUri = getConversationUri(); | ||||
139 | | ||||
140 | Cursor conversationsCursor = context.getContentResolver().query( | ||||
141 | conversationUri, | ||||
142 | Message.smsColumns, | ||||
143 | null, | ||||
144 | null, | ||||
145 | null); | ||||
146 | | ||||
147 | if (conversationsCursor != null && conversationsCursor.moveToFirst()) { | ||||
148 | int threadColumn = conversationsCursor.getColumnIndexOrThrow(ThreadID.lookupColumn); | ||||
149 | do { | ||||
150 | int thread = conversationsCursor.getInt(threadColumn); | ||||
151 | | ||||
152 | HashMap<String, String> messageInfo = new HashMap<>(); | ||||
153 | for (int columnIdx = 0; columnIdx < conversationsCursor.getColumnCount(); columnIdx++) { | ||||
154 | String colName = conversationsCursor.getColumnName(columnIdx); | ||||
155 | String body = conversationsCursor.getString(columnIdx); | ||||
156 | messageInfo.put(colName, body); | ||||
157 | } | ||||
158 | toReturn.put(new ThreadID(thread), new Message(messageInfo)); | ||||
159 | } while (conversationsCursor.moveToNext()); | ||||
160 | } else { | ||||
161 | // No conversations available? | ||||
162 | } | ||||
163 | | ||||
164 | if (conversationsCursor != null) { | ||||
165 | conversationsCursor.close(); | ||||
166 | } | ||||
167 | | ||||
168 | return toReturn; | ||||
169 | } | ||||
170 | | ||||
171 | /** | ||||
172 | * Represent an ID used to uniquely identify a message thread | ||||
173 | */ | ||||
174 | public static class ThreadID { | ||||
175 | Integer threadID; | ||||
176 | static final String lookupColumn = Telephony.Sms.THREAD_ID; | ||||
177 | | ||||
178 | public ThreadID(Integer threadID) { | ||||
179 | this.threadID = threadID; | ||||
180 | } | ||||
181 | | ||||
182 | public String toString() { | ||||
183 | return this.threadID.toString(); | ||||
184 | } | ||||
185 | | ||||
186 | @Override | ||||
187 | public int hashCode() { | ||||
188 | return this.threadID.hashCode(); | ||||
189 | } | ||||
190 | | ||||
191 | @Override | ||||
192 | public boolean equals(Object other) { | ||||
193 | if (other.getClass().isAssignableFrom(ThreadID.class)) { | ||||
194 | return ((ThreadID) other).threadID.equals(this.threadID); | ||||
195 | } | ||||
196 | | ||||
197 | return false; | ||||
198 | } | ||||
199 | } | ||||
200 | | ||||
201 | /** | ||||
202 | * Represent a message and all of its interesting data columns | ||||
203 | */ | ||||
204 | public static class Message { | ||||
205 | | ||||
206 | public final String m_address; | ||||
207 | public final String m_body; | ||||
208 | public final long m_date; | ||||
209 | public final int m_type; | ||||
210 | public final int m_read; | ||||
211 | public final int m_threadID; | ||||
212 | public final int m_uID; | ||||
213 | | ||||
214 | /** | ||||
215 | * Named constants which are used to construct a Message | ||||
216 | * See: https://developer.android.com/reference/android/provider/Telephony.TextBasedSmsColumns.html for full documentation | ||||
217 | */ | ||||
218 | public static final String ADDRESS = Telephony.Sms.ADDRESS; // Contact information (phone number or otherwise) of the remote | ||||
219 | public static final String BODY = Telephony.Sms.BODY; // Body of the message | ||||
220 | public static final String DATE = Telephony.Sms.DATE; // Date (Unix epoch millis) associated with the message | ||||
221 | public static final String TYPE = Telephony.Sms.TYPE; // Compare with Telephony.TextBasedSmsColumns.MESSAGE_TYPE_* | ||||
222 | public static final String READ = Telephony.Sms.READ; // Whether we have received a read report for this message (int) | ||||
223 | public static final String THREAD_ID = ThreadID.lookupColumn; // Magic number which binds (message) threads | ||||
224 | public static final String U_ID = Telephony.Sms._ID; // Something which uniquely identifies this message | ||||
225 | | ||||
226 | /** | ||||
227 | * Define the columns which are to be extracted from the Android SMS database | ||||
228 | */ | ||||
229 | public static final String[] smsColumns = new String[]{ | ||||
230 | Message.ADDRESS, | ||||
231 | Message.BODY, | ||||
232 | Message.DATE, | ||||
233 | Message.TYPE, | ||||
234 | Message.READ, | ||||
235 | Message.THREAD_ID, | ||||
236 | Message.U_ID, | ||||
237 | }; | ||||
238 | | ||||
239 | public Message(final HashMap<String, String> messageInfo) { | ||||
240 | m_address = messageInfo.get(Message.ADDRESS); | ||||
241 | m_body = messageInfo.get(Message.BODY); | ||||
242 | m_date = Long.parseLong(messageInfo.get(Message.DATE)); | ||||
243 | if (messageInfo.get(Message.TYPE) == null) | ||||
244 | { | ||||
245 | // To be honest, I have no idea why this happens. The docs say the TYPE field is mandatory. | ||||
246 | // Just stick some junk in here and hope we can figure it out later. | ||||
247 | // Quick investigation suggests that these are multi-target MMSes | ||||
248 | m_type = -1; | ||||
249 | } else { | ||||
250 | m_type = Integer.parseInt(messageInfo.get(Message.TYPE)); | ||||
251 | } | ||||
252 | m_read = Integer.parseInt(messageInfo.get(Message.READ)); | ||||
253 | m_threadID = Integer.parseInt(messageInfo.get(Message.THREAD_ID)); | ||||
254 | m_uID = Integer.parseInt(messageInfo.get(Message.U_ID)); | ||||
255 | } | ||||
256 | | ||||
257 | public JSONObject toJSONObject() throws JSONException { | ||||
258 | JSONObject json = new JSONObject(); | ||||
259 | | ||||
260 | json.put(Message.ADDRESS, m_address); | ||||
261 | json.put(Message.BODY, m_body); | ||||
262 | json.put(Message.DATE, m_date); | ||||
263 | json.put(Message.TYPE, m_type); | ||||
264 | json.put(Message.READ, m_read); | ||||
265 | json.put(Message.THREAD_ID, m_threadID); | ||||
266 | json.put(Message.U_ID, m_uID); | ||||
267 | | ||||
268 | return json; | ||||
269 | } | ||||
270 | | ||||
271 | @Override | ||||
272 | public String toString() { | ||||
273 | return this.m_body; | ||||
274 | } | ||||
275 | } | ||||
276 | } | ||||
277 | |