summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/util/DebugUtils.java
blob: f2c1d6540cc6abb5e25c5a0ecef428ddae8743bd (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Environment;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.widget.ArrayAdapter;

import com.android.messaging.R;
import com.android.messaging.datamodel.SyncManager;
import com.android.messaging.datamodel.action.DumpDatabaseAction;
import com.android.messaging.datamodel.action.LogTelephonyDatabaseAction;
import com.android.messaging.sms.MmsUtils;
import com.android.messaging.ui.UIIntents;
import com.android.messaging.ui.debug.DebugSmsMmsFromDumpFileDialogFragment;
import com.google.common.io.ByteStreams;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.StreamCorruptedException;

public class DebugUtils {
    private static final String TAG = "bugle.util.DebugUtils";

    private static boolean sDebugNoise;
    private static boolean sDebugClassZeroSms;
    private static MediaPlayer [] sMediaPlayer;
    private static final Object sLock = new Object();

    public static final int DEBUG_SOUND_SERVER_REQUEST = 0;
    public static final int DEBUG_SOUND_DB_OP = 1;

    public static void maybePlayDebugNoise(final Context context, final int sound) {
        if (sDebugNoise) {
            synchronized (sLock) {
                try {
                    if (sMediaPlayer == null) {
                        sMediaPlayer = new MediaPlayer[2];
                        sMediaPlayer[DEBUG_SOUND_SERVER_REQUEST] =
                                MediaPlayer.create(context, R.raw.server_request_debug);
                        sMediaPlayer[DEBUG_SOUND_DB_OP] =
                                MediaPlayer.create(context, R.raw.db_op_debug);
                        sMediaPlayer[DEBUG_SOUND_DB_OP].setVolume(1.0F, 1.0F);
                        sMediaPlayer[DEBUG_SOUND_SERVER_REQUEST].setVolume(0.3F, 0.3F);
                    }
                    if (sMediaPlayer[sound] != null) {
                        sMediaPlayer[sound].start();
                    }
                } catch (final IllegalArgumentException e) {
                    LogUtil.e(TAG, "MediaPlayer exception", e);
                } catch (final SecurityException e) {
                    LogUtil.e(TAG, "MediaPlayer exception", e);
                } catch (final IllegalStateException e) {
                    LogUtil.e(TAG, "MediaPlayer exception", e);
                }
            }
        }
    }

    public static boolean isDebugEnabled() {
        return BugleGservices.get().getBoolean(BugleGservicesKeys.ENABLE_DEBUGGING_FEATURES,
                BugleGservicesKeys.ENABLE_DEBUGGING_FEATURES_DEFAULT);
    }

    public abstract static class DebugAction {
        String mTitle;
        public DebugAction(final String title) {
            mTitle = title;
        }

        @Override
        public String toString() {
            return mTitle;
        }

        public abstract void run();
    }

    public static void showDebugOptions(final Activity host) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(host);

        final ArrayAdapter<DebugAction> arrayAdapter = new ArrayAdapter<DebugAction>(
                host, android.R.layout.simple_list_item_1);

        arrayAdapter.add(new DebugAction("Dump Database") {
            @Override
            public void run() {
                DumpDatabaseAction.dumpDatabase();
            }
        });

        arrayAdapter.add(new DebugAction("Log Telephony Data") {
            @Override
            public void run() {
                LogTelephonyDatabaseAction.dumpDatabase();
            }
        });

        arrayAdapter.add(new DebugAction("Toggle Noise") {
            @Override
            public void run() {
                sDebugNoise = !sDebugNoise;
            }
        });

        arrayAdapter.add(new DebugAction("Force sync SMS") {
            @Override
            public void run() {
                final BuglePrefs prefs = BuglePrefs.getApplicationPrefs();
                prefs.putLong(BuglePrefsKeys.LAST_FULL_SYNC_TIME, -1);
                SyncManager.forceSync();
            }
        });

        arrayAdapter.add(new DebugAction("Sync SMS") {
            @Override
            public void run() {
                SyncManager.sync();
            }
        });

        arrayAdapter.add(new DebugAction("Load SMS/MMS from dump file") {
            @Override
            public void run() {
                new DebugSmsMmsDumpTask(host,
                        DebugSmsMmsFromDumpFileDialogFragment.ACTION_LOAD).executeOnThreadPool();
            }
        });

        arrayAdapter.add(new DebugAction("Email SMS/MMS dump file") {
            @Override
            public void run() {
                new DebugSmsMmsDumpTask(host,
                        DebugSmsMmsFromDumpFileDialogFragment.ACTION_EMAIL).executeOnThreadPool();
            }
        });

        arrayAdapter.add(new DebugAction("MMS Config...") {
            @Override
            public void run() {
                UIIntents.get().launchDebugMmsConfigActivity(host);
            }
        });

        arrayAdapter.add(new DebugAction(sDebugClassZeroSms ? "Turn off Class 0 sms test" :
                "Turn on Class Zero test") {
            @Override
            public void run() {
                sDebugClassZeroSms = !sDebugClassZeroSms;
            }
        });

        builder.setAdapter(arrayAdapter,
                new android.content.DialogInterface.OnClickListener() {
            @Override
            public void onClick(final DialogInterface arg0, final int pos) {
                arrayAdapter.getItem(pos).run();
            }
        });

        builder.create().show();
    }

    /**
     * Task to list all the dump files and perform an action on it
     */
    private static class DebugSmsMmsDumpTask extends SafeAsyncTask<Void, Void, String[]> {
        private final String mAction;
        private final Activity mHost;

        public DebugSmsMmsDumpTask(final Activity host, final String action) {
            mHost = host;
            mAction = action;
        }

        @Override
        protected void onPostExecute(final String[] result) {
            if (result == null || result.length < 1) {
                return;
            }
            final FragmentManager fragmentManager = mHost.getFragmentManager();
            final FragmentTransaction ft = fragmentManager.beginTransaction();
            final DebugSmsMmsFromDumpFileDialogFragment dialog =
                    DebugSmsMmsFromDumpFileDialogFragment.newInstance(result, mAction);
            dialog.show(fragmentManager, ""/*tag*/);
        }

        @Override
        protected String[] doInBackgroundTimed(final Void... params) {
            final File dir = DebugUtils.getDebugFilesDir();
            return dir.list(new FilenameFilter() {
                @Override
                public boolean accept(final File dir, final String filename) {
                    return filename != null
                            && ((mAction == DebugSmsMmsFromDumpFileDialogFragment.ACTION_EMAIL
                            && filename.equals(DumpDatabaseAction.DUMP_NAME))
                            || filename.startsWith(MmsUtils.MMS_DUMP_PREFIX)
                            || filename.startsWith(MmsUtils.SMS_DUMP_PREFIX));
                }
            });
        }
    }

    /**
     * Dump the received raw SMS data into a file on external storage
     *
     * @param id The ID to use as part of the dump file name
     * @param messages The raw SMS data
     */
    public static void dumpSms(final long id, final android.telephony.SmsMessage[] messages,
            final String format) {
        try {
            final String dumpFileName = MmsUtils.SMS_DUMP_PREFIX + Long.toString(id);
            final File dumpFile = DebugUtils.getDebugFile(dumpFileName, true);
            if (dumpFile != null) {
                final FileOutputStream fos = new FileOutputStream(dumpFile);
                final DataOutputStream dos = new DataOutputStream(fos);
                try {
                    final int chars = (TextUtils.isEmpty(format) ? 0 : format.length());
                    dos.writeInt(chars);
                    if (chars > 0) {
                        dos.writeUTF(format);
                    }
                    dos.writeInt(messages.length);
                    for (final android.telephony.SmsMessage message : messages) {
                        final byte[] pdu = message.getPdu();
                        dos.writeInt(pdu.length);
                        dos.write(pdu, 0, pdu.length);
                    }
                    dos.flush();
                } finally {
                    dos.close();
                    ensureReadable(dumpFile);
                }
            }
        } catch (final IOException e) {
            LogUtil.e(LogUtil.BUGLE_TAG, "dumpSms: " + e, e);
        }
    }

    /**
     * Load MMS/SMS from the dump file
     */
    public static SmsMessage[] retreiveSmsFromDumpFile(final String dumpFileName) {
        SmsMessage[] messages = null;
        final File inputFile = DebugUtils.getDebugFile(dumpFileName, false);
        if (inputFile != null) {
            FileInputStream fis = null;
            DataInputStream dis = null;
            try {
                fis = new FileInputStream(inputFile);
                dis = new DataInputStream(fis);

                // SMS dump
                final int chars = dis.readInt();
                if (chars > 0) {
                    final String format = dis.readUTF();
                }
                final int count = dis.readInt();
                final SmsMessage[] messagesTemp = new SmsMessage[count];
                for (int i = 0; i < count; i++) {
                    final int length = dis.readInt();
                    final byte[] pdu = new byte[length];
                    dis.read(pdu, 0, length);
                    messagesTemp[i] = SmsMessage.createFromPdu(pdu);
                }
                messages = messagesTemp;
            } catch (final FileNotFoundException e) {
                // Nothing to do
            } catch (final StreamCorruptedException e) {
                // Nothing to do
            } catch (final IOException e) {
                // Nothing to do
            } finally {
                if (dis != null) {
                    try {
                        dis.close();
                    } catch (final IOException e) {
                        // Nothing to do
                    }
                }
            }
        }
        return messages;
    }

    public static File getDebugFile(final String fileName, final boolean create) {
        final File dir = getDebugFilesDir();
        final File file = new File(dir, fileName);
        if (create && file.exists()) {
            file.delete();
        }
        return file;
    }

    public static File getDebugFilesDir() {
        final File dir = Environment.getExternalStorageDirectory();
        return dir;
    }

    /**
     * Load MMS/SMS from the dump file
     */
    public static byte[] receiveFromDumpFile(final String dumpFileName) {
        byte[] data = null;
        try {
            final File inputFile = getDebugFile(dumpFileName, false);
            if (inputFile != null) {
                final FileInputStream fis = new FileInputStream(inputFile);
                final BufferedInputStream bis = new BufferedInputStream(fis);
                try {
                    // dump file
                    data = ByteStreams.toByteArray(bis);
                    if (data == null || data.length < 1) {
                        LogUtil.e(LogUtil.BUGLE_TAG, "receiveFromDumpFile: empty data");
                    }
                } finally {
                    bis.close();
                }
            }
        } catch (final IOException e) {
            LogUtil.e(LogUtil.BUGLE_TAG, "receiveFromDumpFile: " + e, e);
        }
        return data;
    }

    public static void ensureReadable(final File file) {
        if (file.exists()){
            file.setReadable(true, false);
        }
    }

    /**
     * Logs the name of the method that is currently executing, e.g. "MyActivity.onCreate". This is
     * useful for surgically adding logs for tracing execution while debugging.
     * <p>
     * NOTE: This method retrieves the current thread's stack trace, which adds runtime overhead.
     * However, this method is only executed on eng builds if DEBUG logs are loggable.
     */
    public static void logCurrentMethod(String tag) {
        if (!LogUtil.isLoggable(tag, LogUtil.DEBUG)) {
            return;
        }
        StackTraceElement caller = getCaller(1);
        if (caller == null) {
            return;
        }
        String className = caller.getClassName();
        // Strip off the package name
        int lastDot = className.lastIndexOf('.');
        if (lastDot > -1) {
            className = className.substring(lastDot + 1);
        }
        LogUtil.d(tag, className + "." + caller.getMethodName());
    }

    /**
     * Returns info about the calling method. The {@code depth} parameter controls how far back to
     * go. For example, if foo() calls bar(), and bar() calls getCaller(0), it returns info about
     * bar(). If bar() instead called getCaller(1), it would return info about foo(). And so on.
     * <p>
     * NOTE: This method retrieves the current thread's stack trace, which adds runtime overhead.
     * It should only be used in production where necessary to gather context about an error or
     * unexpected event (e.g. the {@link Assert} class uses it).
     *
     * @return stack frame information for the caller (if found); otherwise {@code null}.
     */
    public static StackTraceElement getCaller(int depth) {
        // If the signature of this method is changed, proguard.flags must be updated!
        if (depth < 0) {
            throw new IllegalArgumentException("depth cannot be negative");
        }
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        if (trace == null || trace.length < (depth + 2)) {
            return null;
        }
        // The stack trace includes some methods we don't care about (e.g. this method).
        // Walk down until we find this method, and then back up to the caller we're looking for.
        for (int i = 0; i < trace.length - 1; i++) {
            String methodName = trace[i].getMethodName();
            if ("getCaller".equals(methodName)) {
                return trace[i + depth + 1];
            }
        }
        // Never found ourself in the stack?!
        return null;
    }

    /**
     * Returns a boolean indicating whether ClassZero debugging is enabled. If enabled, any received
     * sms is treated as if it were a class zero message and displayed by the ClassZeroActivity.
     */
    public static boolean debugClassZeroSmsEnabled() {
        return sDebugClassZeroSms;
    }
}