Best Android App InstaSquare Lite for full-size publication of photos in Instagram, Facebook, Twitter. Run the android application on PC. You can through the android emulator Droid4X.

root/trunk/rapidandroid/org.rapidandroid/src/org/rapidandroid/receiver/SmsParseReceiver.java @ 133

Revision 133, 4.1 KB (checked in by dmyung, 10 months ago)

applying GPL license to source

Line 
1/*
2 *    rapidandroid - SMS gateway for the android platform
3 *    Copyright (C) 2009 Dimagi Inc., UNICEF
4 *
5 *   This program is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19/**
20 *
21 */
22package org.rapidandroid.receiver;
23
24import java.util.Vector;
25
26import org.rapidandroid.content.translation.MessageTranslator;
27import org.rapidandroid.content.translation.ModelTranslator;
28import org.rapidandroid.content.translation.ParsedDataTranslator;
29
30import org.rapidsms.java.core.model.Form;
31import org.rapidsms.java.core.model.Monitor;
32import org.rapidsms.java.core.parser.IParseResult;
33import org.rapidsms.java.core.parser.service.ParsingService;
34
35import android.content.BroadcastReceiver;
36import android.content.Context;
37import android.content.Intent;
38import android.util.Log;
39
40/**
41 * Second level broadcast receiver. The idea is upon a successful SMS message
42 * save, a separate receiver will be triggered to handle the actual parsing and
43 * processing of the message.
44 *
45 * @author Daniel Myung [email protected]
46 * @created Jan 12, 2009
47 *
48 */
49public class SmsParseReceiver extends BroadcastReceiver {
50
51        private static String[] prefixes = null;
52        private static Form[] forms = null;
53
54        // private Context mContext = null;
55
56        public synchronized static void initFormCache() {
57                forms = ModelTranslator.getAllForms();
58                prefixes = new String[forms.length];
59                for (int i = 0; i < forms.length; i++) {
60                        prefixes[i] = forms[i].getPrefix();
61                }
62        }
63
64        private Form determineForm(String message) {
65                int len = prefixes.length;
66                for (int i = 0; i < len; i++) {
67                        String prefix = prefixes[i];
68                        if (message.toLowerCase().trim().startsWith(prefix + " ")) {
69                                return forms[i];
70                        }
71                }
72                return null;
73        }
74
75        /**
76         * Upon message receipt, determine the form in question, then call the
77         * corresponding parsing logic.
78         */
79        @Override
80        public void onReceive(Context context, Intent intent) {
81                // if (mContext == null) {
82                // mContext = context;
83                // }
84                if (prefixes == null) {
85                        initFormCache(); // profiler shows us that this is being called
86                                                                // frequently on new messages.
87                }
88                // TODO Auto-generated method stub
89                String body = intent.getStringExtra("body");
90
91                if (body.startsWith("[email protected] /  / ")) {
92                        body = body.replace("[email protected] /  / ", "");
93                        Log.d("SmsParseReceiver", "Debug, snipping out the email address");
94                }
95
96                int msgid = intent.getIntExtra("msgid", 0);
97
98                Form form = determineForm(body);
99                if (form == null) {
100                        Intent broadcast = new Intent("org.rapidandroid.intents.SMS_REPLY");
101                        broadcast.putExtra(SmsReplyReceiver.KEY_DESTINATION_PHONE, intent.getStringExtra("from"));
102                        broadcast.putExtra(SmsReplyReceiver.KEY_MESSAGE, "Your message could not be parsed");
103                        context.sendBroadcast(broadcast);
104                        return;
105                } else {
106                        Monitor mon = MessageTranslator.GetMonitorAndInsertIfNew(context, intent.getStringExtra("from"));
107                        // if(mon.getReplyPreference()) {
108                        // if(true) {
109                        // //for debug purposes, we'll just ack every time.
110                        // Intent broadcast = new
111                        // Intent("org.rapidandroid.intents.SMS_REPLY");
112                        // broadcast.putExtra(SmsReplyReceiver.KEY_DESTINATION_PHONE,
113                        // intent.getStringExtra("from"));
114                        // broadcast.putExtra(SmsReplyReceiver.KEY_MESSAGE,
115                        // "Message parse successful, thank you!");
116                        // context.sendBroadcast(broadcast);
117                        // }
118
119                        Vector<IParseResult> results = ParsingService.ParseMessage(form, body);
120                        ParsedDataTranslator.InsertFormData(context, form, msgid, results);
121
122                }
123        }
124}
Note: See TracBrowser for help on using the browser.