1 /* 2 * Copyright (C) 2011 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.settings; 18 19 import android.app.ActivityManager; 20 import android.app.ActivityManagerNative; 21 import android.app.AlertDialog.Builder; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.os.Handler; 25 import android.os.RemoteException; 26 import android.text.format.DateUtils; 27 import android.util.AttributeSet; 28 import android.util.Log; 29 import android.view.View; 30 import android.widget.CheckedTextView; 31 import android.widget.RadioButton; 32 import android.widget.TextView; 33 import android.widget.Toast; 34 35 import com.android.internal.logging.MetricsLogger; 36 import com.android.internal.logging.MetricsProto.MetricsEvent; 37 38 public class BugreportPreference extends CustomDialogPreference { 39 40 private static final String TAG = "BugreportPreference"; 41 private static final int BUGREPORT_DELAY_SECONDS = 3; 42 43 private CheckedTextView mInteractiveTitle; 44 private TextView mInteractiveSummary; 45 private CheckedTextView mFullTitle; 46 private TextView mFullSummary; 47 48 public BugreportPreference(Context context, AttributeSet attrs) { 49 super(context, attrs); 50 } 51 52 @Override 53 protected void onPrepareDialogBuilder(Builder builder, DialogInterface.OnClickListener listener) { 54 super.onPrepareDialogBuilder(builder, listener); 55 56 final View dialogView = View.inflate(getContext(), R.layout.bugreport_options_dialog, null); 57 mInteractiveTitle = (CheckedTextView) dialogView.findViewById(R.id.bugreport_option_interactive_title); 58 mInteractiveSummary = (TextView) dialogView.findViewById(R.id.bugreport_option_interactive_summary); 59 mFullTitle = (CheckedTextView) dialogView.findViewById(R.id.bugreport_option_full_title); 60 mFullSummary = (TextView) dialogView.findViewById(R.id.bugreport_option_full_summary); 61 final View.OnClickListener l = new View.OnClickListener() { 62 63 @Override 64 public void onClick(View v) { 65 if (v == mFullTitle || v == mFullSummary) { 66 mInteractiveTitle.setChecked(false); 67 mFullTitle.setChecked(true); 68 } 69 if (v == mInteractiveTitle || v == mInteractiveSummary) { 70 mInteractiveTitle.setChecked(true); 71 mFullTitle.setChecked(false); 72 } 73 } 74 }; 75 mInteractiveTitle.setOnClickListener(l); 76 mFullTitle.setOnClickListener(l); 77 mInteractiveSummary.setOnClickListener(l); 78 mFullSummary.setOnClickListener(l); 79 80 builder.setPositiveButton(com.android.internal.R.string.report, listener); 81 builder.setView(dialogView); 82 } 83 84 @Override 85 protected void onClick(DialogInterface dialog, int which) { 86 if (which == DialogInterface.BUTTON_POSITIVE) { 87 88 final Context context = getContext(); 89 if (mFullTitle.isChecked()) { 90 Log.v(TAG, "Taking full bugreport right away"); 91 MetricsLogger.action(context, MetricsEvent.ACTION_BUGREPORT_FROM_SETTINGS_FULL); 92 takeBugreport(ActivityManager.BUGREPORT_OPTION_FULL); 93 } else { 94 Log.v(TAG, "Taking interactive bugreport in " + BUGREPORT_DELAY_SECONDS + "s"); 95 MetricsLogger.action(context, 96 MetricsEvent.ACTION_BUGREPORT_FROM_SETTINGS_INTERACTIVE); 97 // Add a little delay before executing, to give the user a chance to close 98 // the Settings activity before it takes a screenshot. 99 final String msg = context.getResources() 100 .getQuantityString(com.android.internal.R.plurals.bugreport_countdown, 101 BUGREPORT_DELAY_SECONDS, BUGREPORT_DELAY_SECONDS); 102 Log.v(TAG, msg); 103 Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); 104 new Handler().postDelayed(new Runnable() { 105 @Override 106 public void run() { 107 takeBugreport(ActivityManager.BUGREPORT_OPTION_INTERACTIVE); 108 } 109 }, BUGREPORT_DELAY_SECONDS * DateUtils.SECOND_IN_MILLIS); 110 } 111 } 112 } 113 114 private void takeBugreport(int bugreportType) { 115 try { 116 ActivityManagerNative.getDefault().requestBugReport(bugreportType); 117 } catch (RemoteException e) { 118 Log.e(TAG, "error taking bugreport (bugreportType=" + bugreportType + ")", e); 119 } 120 } 121 } 122