Home | History | Annotate | Download | only in debug
      1 /*
      2  * Copyright (C) 2015 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.messaging.ui.debug;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.app.DialogFragment;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.res.Resources;
     25 import android.net.Uri;
     26 import android.os.Bundle;
     27 import android.os.Environment;
     28 import android.telephony.SmsMessage;
     29 import android.view.LayoutInflater;
     30 import android.view.View;
     31 import android.view.View.OnClickListener;
     32 import android.view.ViewGroup;
     33 import android.widget.ArrayAdapter;
     34 import android.widget.ListView;
     35 import android.widget.TextView;
     36 
     37 import com.android.messaging.R;
     38 import com.android.messaging.datamodel.action.ReceiveMmsMessageAction;
     39 import com.android.messaging.datamodel.data.ParticipantData;
     40 import com.android.messaging.receiver.SmsReceiver;
     41 import com.android.messaging.sms.MmsUtils;
     42 import com.android.messaging.util.DebugUtils;
     43 import com.android.messaging.util.LogUtil;
     44 
     45 /**
     46  * Class that displays UI for choosing SMS/MMS dump files for debugging
     47  */
     48 public class DebugSmsMmsFromDumpFileDialogFragment extends DialogFragment {
     49     public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
     50     public static final String KEY_DUMP_FILES = "dump_files";
     51     public static final String KEY_ACTION = "action";
     52 
     53     public static final String ACTION_LOAD = "load";
     54     public static final String ACTION_EMAIL = "email";
     55 
     56     private String[] mDumpFiles;
     57     private String mAction;
     58 
     59     public static DebugSmsMmsFromDumpFileDialogFragment newInstance(final String[] dumpFiles,
     60             final String action) {
     61         final DebugSmsMmsFromDumpFileDialogFragment frag =
     62                 new DebugSmsMmsFromDumpFileDialogFragment();
     63         final Bundle args = new Bundle();
     64         args.putSerializable(KEY_DUMP_FILES, dumpFiles);
     65         args.putString(KEY_ACTION, action);
     66         frag.setArguments(args);
     67         return frag;
     68     }
     69 
     70     @Override
     71     public Dialog onCreateDialog(final Bundle savedInstanceState) {
     72         final Bundle args = getArguments();
     73         mDumpFiles = (String[]) args.getSerializable(KEY_DUMP_FILES);
     74         mAction = args.getString(KEY_ACTION);
     75 
     76         final LayoutInflater inflater = getActivity().getLayoutInflater();
     77         final View layout = inflater.inflate(
     78                 R.layout.debug_sms_mms_from_dump_file_dialog, null/*root*/);
     79         final ListView list = (ListView) layout.findViewById(R.id.dump_file_list);
     80         list.setAdapter(new DumpFileListAdapter(getActivity(), mDumpFiles));
     81         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     82         final Resources resources = getResources();
     83         if (ACTION_LOAD.equals(mAction)) {
     84             builder.setTitle(resources.getString(
     85                     R.string.load_sms_mms_from_dump_file_dialog_title));
     86         } else if (ACTION_EMAIL.equals(mAction)) {
     87             builder.setTitle(resources.getString(
     88                     R.string.email_sms_mms_from_dump_file_dialog_title));
     89         }
     90         builder.setView(layout);
     91         return builder.create();
     92     }
     93 
     94     private class DumpFileListAdapter extends ArrayAdapter<String> {
     95         public DumpFileListAdapter(final Context context, final String[] dumpFiles) {
     96             super(context, R.layout.sms_mms_dump_file_list_item, dumpFiles);
     97         }
     98 
     99         @Override
    100         public View getView(final int position, final View view, final ViewGroup parent) {
    101             TextView actionItemView;
    102             if (view == null || !(view instanceof TextView)) {
    103                 final LayoutInflater inflater = LayoutInflater.from(getContext());
    104                 actionItemView = (TextView) inflater.inflate(
    105                         R.layout.sms_mms_dump_file_list_item, parent, false);
    106             } else {
    107                 actionItemView = (TextView) view;
    108             }
    109 
    110             final String file = getItem(position);
    111             actionItemView.setText(file);
    112             actionItemView.setOnClickListener(new OnClickListener() {
    113                 @Override
    114                 public void onClick(final View view) {
    115                     dismiss();
    116                     if (ACTION_LOAD.equals(mAction)) {
    117                         receiveFromDumpFile(file);
    118                     } else if (ACTION_EMAIL.equals(mAction)) {
    119                         emailDumpFile(file);
    120                     }
    121                 }
    122             });
    123             return actionItemView;
    124         }
    125     }
    126 
    127     /**
    128      * Load MMS/SMS from the dump file
    129      */
    130     private void receiveFromDumpFile(final String dumpFileName) {
    131         if (dumpFileName.startsWith(MmsUtils.SMS_DUMP_PREFIX)) {
    132             final SmsMessage[] messages = DebugUtils.retreiveSmsFromDumpFile(dumpFileName);
    133             if (messages != null) {
    134                 SmsReceiver.deliverSmsMessages(getActivity(), ParticipantData.DEFAULT_SELF_SUB_ID,
    135                         0, messages);
    136             } else {
    137                 LogUtil.e(LogUtil.BUGLE_TAG,
    138                         "receiveFromDumpFile: invalid sms dump file " + dumpFileName);
    139             }
    140         } else if (dumpFileName.startsWith(MmsUtils.MMS_DUMP_PREFIX)) {
    141             final byte[] data = MmsUtils.createDebugNotificationInd(dumpFileName);
    142             if (data != null) {
    143                 final ReceiveMmsMessageAction action = new ReceiveMmsMessageAction(
    144                         ParticipantData.DEFAULT_SELF_SUB_ID, data);
    145                 action.start();
    146             } else {
    147                 LogUtil.e(LogUtil.BUGLE_TAG,
    148                         "receiveFromDumpFile: invalid mms dump file " + dumpFileName);
    149             }
    150         } else {
    151             LogUtil.e(LogUtil.BUGLE_TAG,
    152                     "receiveFromDumpFile: invalid dump file name " + dumpFileName);
    153         }
    154     }
    155 
    156     /**
    157      * Launch email app to send the dump file
    158      */
    159     private void emailDumpFile(final String file) {
    160         final Resources resources = getResources();
    161         final String fileLocation = "file://"
    162                 + Environment.getExternalStorageDirectory() + "/" + file;
    163         final Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    164         sharingIntent.setType(APPLICATION_OCTET_STREAM);
    165         sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileLocation));
    166         sharingIntent.putExtra(Intent.EXTRA_SUBJECT,
    167                 resources.getString(R.string.email_sms_mms_dump_file_subject));
    168         getActivity().startActivity(Intent.createChooser(sharingIntent,
    169                 resources.getString(R.string.email_sms_mms_dump_file_chooser_title)));
    170     }
    171 }
    172