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 @ 134

Revision 134, 5.0 KB (checked in by dmyung, 10 months ago)

added some secret sections for FormReview? for debug data injection

but for real stuff, added a settings file that's generated on startup for the first time, and is persisted in the settings directory to set a preference for ACK replies for parse and no parse events.

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.json.JSONException;
27import org.json.JSONObject;
28import org.rapidandroid.ApplicationGlobals;
29import org.rapidandroid.content.translation.MessageTranslator;
30import org.rapidandroid.content.translation.ModelTranslator;
31import org.rapidandroid.content.translation.ParsedDataTranslator;
32import org.rapidsms.java.core.model.Form;
33import org.rapidsms.java.core.model.Monitor;
34import org.rapidsms.java.core.parser.IParseResult;
35import org.rapidsms.java.core.parser.service.ParsingService;
36
37import android.content.BroadcastReceiver;
38import android.content.Context;
39import android.content.Intent;
40import android.util.Log;
41
42/**
43 * Second level broadcast receiver. The idea is upon a successful SMS message
44 * save, a separate receiver will be triggered to handle the actual parsing and
45 * processing of the message.
46 *
47 * @author Daniel Myung [email protected]
48 * @created Jan 12, 2009
49 *
50 */
51public class SmsParseReceiver extends BroadcastReceiver {
52
53        private static String[] prefixes = null;
54        private static Form[] forms = null;
55       
56       
57        private static boolean globalsLoaded = false;
58        private static boolean mReplyParse = true;
59        private static boolean mReplyFail = true;
60       
61        private static String mReplyParseText = "";
62        private static String mReplyFailText = "";
63       
64       
65        public static void initGlobals(Context context) {
66                if(!globalsLoaded) {
67                        JSONObject globals = ApplicationGlobals.loadSettingsFromFile(context);
68                        try {
69                                mReplyParse = globals.getBoolean(ApplicationGlobals.KEY_PARSE_REPLY);
70                                mReplyFail = globals.getBoolean(ApplicationGlobals.KEY_FAILED_REPLY);
71                                mReplyParseText = globals.getString(ApplicationGlobals.KEY_PARSE_REPLY_TEXT);
72                                mReplyFailText = globals.getString(ApplicationGlobals.KEY_FAILED_REPLY_TEXT);
73                                globalsLoaded = true;
74                        } catch (JSONException e) {
75                                // TODO Auto-generated catch block
76                                e.printStackTrace();
77                        }
78                }
79        }
80
81        // private Context mContext = null;
82
83        public synchronized static void initFormCache() {
84                forms = ModelTranslator.getAllForms();
85                prefixes = new String[forms.length];
86                for (int i = 0; i < forms.length; i++) {
87                        prefixes[i] = forms[i].getPrefix();
88                }
89        }
90
91        private Form determineForm(String message) {
92                int len = prefixes.length;
93                for (int i = 0; i < len; i++) {
94                        String prefix = prefixes[i];
95                        if (message.toLowerCase().trim().startsWith(prefix + " ")) {
96                                return forms[i];
97                        }
98                }
99                return null;
100        }
101
102        /**
103         * Upon message receipt, determine the form in question, then call the
104         * corresponding parsing logic.
105         */
106        @Override
107        public void onReceive(Context context, Intent intent) {
108                initGlobals(context);
109       
110                if (prefixes == null) {
111                        initFormCache(); // profiler shows us that this is being called
112                                                                // frequently on new messages.
113                }
114                // TODO Auto-generated method stub
115                String body = intent.getStringExtra("body");
116
117                if (body.startsWith("[email protected] /  / ")) {
118                        body = body.replace("[email protected] /  / ", "");
119                        Log.d("SmsParseReceiver", "Debug, snipping out the email address");
120                }
121
122                int msgid = intent.getIntExtra("msgid", 0);
123
124                Form form = determineForm(body);
125                if (form == null) {
126                       
127                       
128                        if (mReplyFail) {
129                                Intent broadcast = new Intent("org.rapidandroid.intents.SMS_REPLY");
130                                broadcast.putExtra(SmsReplyReceiver.KEY_DESTINATION_PHONE, intent.getStringExtra("from"));
131                                broadcast.putExtra(SmsReplyReceiver.KEY_MESSAGE, mReplyFailText);
132                                context.sendBroadcast(broadcast);
133                        }
134                        return;
135                } else {
136                        Monitor mon = MessageTranslator.GetMonitorAndInsertIfNew(context, intent.getStringExtra("from"));
137                        // if(mon.getReplyPreference()) {
138                        if (mReplyParse) {
139                                // for debug purposes, we'll just ack every time.
140                                Intent broadcast = new Intent("org.rapidandroid.intents.SMS_REPLY");
141                                broadcast.putExtra(SmsReplyReceiver.KEY_DESTINATION_PHONE, intent.getStringExtra("from"));
142                                broadcast.putExtra(SmsReplyReceiver.KEY_MESSAGE, mReplyParseText);
143                                context.sendBroadcast(broadcast);
144                        }
145                        Vector<IParseResult> results = ParsingService.ParseMessage(form, body);
146                        ParsedDataTranslator.InsertFormData(context, form, msgid, results);
147                }
148        }
149}
Note: See TracBrowser for help on using the browser.