Home | History | Annotate | Download | only in mms
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.mms;
     18 
     19 import com.android.mms.data.Contact;
     20 import com.android.mms.data.Conversation;
     21 import com.android.mms.data.RecipientIdCache;
     22 
     23 import android.app.Activity;
     24 import android.app.AlertDialog;
     25 import android.content.Context;
     26 import android.content.DialogInterface;
     27 import android.content.DialogInterface.OnClickListener;
     28 import android.util.Log;
     29 
     30 public class LogTag {
     31     public static final String TAG = "Mms";
     32 
     33     public static final String TRANSACTION = "Mms:transaction";
     34     public static final String APP = "Mms:app";
     35     public static final String THREAD_CACHE = "Mms:threadcache";
     36     public static final boolean VERBOSE = false;
     37     public static final boolean SEVERE_WARNING = true;                  // Leave this true
     38     private static final boolean SHOW_SEVERE_WARNING_DIALOG = false;    // Set to false before ship
     39     public static final boolean DEBUG_SEND = false;    // Set to false before ship
     40     public static final boolean DEBUG_DUMP = false;    // Set to false before ship
     41     public static final boolean ALLOW_DUMP_IN_LOGS = false;  // Set to false before ship
     42 
     43     private static String prettyArray(String[] array) {
     44         if (array.length == 0) {
     45             return "[]";
     46         }
     47 
     48         StringBuilder sb = new StringBuilder("[");
     49         int len = array.length-1;
     50         for (int i = 0; i < len; i++) {
     51             sb.append(array[i]);
     52             sb.append(", ");
     53         }
     54         sb.append(array[len]);
     55         sb.append("]");
     56 
     57         return sb.toString();
     58     }
     59 
     60     private static String logFormat(String format, Object... args) {
     61         for (int i = 0; i < args.length; i++) {
     62             if (args[i] instanceof String[]) {
     63                 args[i] = prettyArray((String[])args[i]);
     64             }
     65         }
     66         String s = String.format(format, args);
     67         s = "[" + Thread.currentThread().getId() + "] " + s;
     68         return s;
     69     }
     70 
     71     public static void debug(String format, Object... args) {
     72         Log.d(TAG, logFormat(format, args));
     73     }
     74 
     75     public static void warn(String format, Object... args) {
     76         Log.w(TAG, logFormat(format, args));
     77     }
     78 
     79     public static void error(String format, Object... args) {
     80         Log.e(TAG, logFormat(format, args));
     81     }
     82 
     83     public static void dumpInternalTables(final Context context) {
     84         if (!ALLOW_DUMP_IN_LOGS) {
     85             return;
     86         }
     87         new Thread(new Runnable() {
     88             public void run() {
     89                 RecipientIdCache.canonicalTableDump();
     90                 RecipientIdCache.dump();
     91                 Conversation.dumpThreadsTable(context);
     92                 Conversation.dump();
     93                 Conversation.dumpSmsTable(context);
     94                 Contact.dump();
     95             }
     96         }).start();
     97     }
     98 
     99     public static void warnPossibleRecipientMismatch(final String msg, final Activity activity) {
    100         Log.e(TAG, "WARNING!!!! " + msg, new RuntimeException());
    101 
    102         if (SHOW_SEVERE_WARNING_DIALOG) {
    103             dumpInternalTables(activity);
    104             activity.runOnUiThread(new Runnable() {
    105                 public void run() {
    106                     new AlertDialog.Builder(activity)
    107                         .setIcon(android.R.drawable.ic_dialog_alert)
    108                         .setTitle(R.string.error_state)
    109                         .setMessage(msg + "\n\n" + activity.getString(R.string.error_state_text))
    110                         .setPositiveButton(R.string.yes, new OnClickListener() {
    111                             public void onClick(DialogInterface dialog, int which) {
    112                                 dialog.dismiss();
    113                             }
    114                         })
    115                         .show();
    116                 }
    117             });
    118         }
    119     }
    120 
    121 }
    122