OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.tab; | 5 package org.chromium.chrome.browser.tab; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.support.annotation.StringRes; | 8 import android.support.annotation.IntDef; |
9 import android.text.SpannableString; | |
10 import android.text.TextUtils; | |
9 import android.text.method.LinkMovementMethod; | 11 import android.text.method.LinkMovementMethod; |
12 import android.text.style.BulletSpan; | |
10 import android.view.LayoutInflater; | 13 import android.view.LayoutInflater; |
11 import android.view.View; | 14 import android.view.View; |
12 import android.view.View.OnClickListener; | 15 import android.view.View.OnClickListener; |
13 import android.widget.Button; | 16 import android.widget.Button; |
14 import android.widget.TextView; | 17 import android.widget.TextView; |
15 | 18 |
19 import org.chromium.base.metrics.RecordHistogram; | |
16 import org.chromium.chrome.R; | 20 import org.chromium.chrome.R; |
17 import org.chromium.ui.text.NoUnderlineClickableSpan; | 21 import org.chromium.ui.text.NoUnderlineClickableSpan; |
18 import org.chromium.ui.text.SpanApplier; | 22 import org.chromium.ui.text.SpanApplier; |
19 import org.chromium.ui.text.SpanApplier.SpanInfo; | 23 import org.chromium.ui.text.SpanApplier.SpanInfo; |
20 | 24 |
25 import java.lang.annotation.Retention; | |
26 import java.lang.annotation.RetentionPolicy; | |
27 | |
21 /** | 28 /** |
22 * A factory class for creating the "Sad Tab" view, which is shown in place of a crashed renderer. | 29 * A factory class for creating the "Sad Tab" view, which is shown in place of a crashed renderer. |
23 */ | 30 */ |
24 public class SadTabViewFactory { | 31 public class SadTabViewFactory { |
32 // This IntDef backs an UMA histogram, so it should be treated as append-onl y. | |
33 // A native counterpart exists in sad_tab.cc. | |
34 @IntDef({SAD_TAB_EVENT_DISPLAYED, SAD_TAB_EVENT_BUTTON_CLICKED, | |
35 SAD_TAB_EVENT_HELP_LINK_CLICKED}) | |
36 @Retention(RetentionPolicy.SOURCE) | |
37 public @interface SadTabEvent {} | |
38 public static final int SAD_TAB_EVENT_DISPLAYED = 0; | |
39 public static final int SAD_TAB_EVENT_BUTTON_CLICKED = 1; | |
40 public static final int SAD_TAB_EVENT_HELP_LINK_CLICKED = 2; | |
41 private static final int MAX_SAD_TAB_EVENT = 3; | |
42 | |
43 private static final int BULLET_GAP_WIDTH = 100; | |
25 | 44 |
26 /** | 45 /** |
27 * @param context Context of the resulting Sad Tab view. | 46 * @param context Context of the resulting Sad Tab view. |
28 * @param suggestionAction Action to be executed when user clicks "try these suggestions". | 47 * @param suggestionAction Action to be executed when user clicks "try these suggestions". |
29 * @param reloadButtonAction Action to be executed when Reload button is pre ssed. | 48 * @param reloadButtonAction Action to be executed when Reload button is pre ssed. |
30 * (e.g., refreshing the page) | 49 * (e.g., refreshing the page) |
31 * @param buttonTextId The string resource for the button text label. | 50 * @param showSendFeedbackView Whether to show the "send feedback" version o f the Sad Tab view. |
32 * @return A "Sad Tab" view instance which is used in place of a crashed ren derer. | 51 * @return A "Sad Tab" view instance which is used in place of a crashed ren derer. |
33 */ | 52 */ |
34 public static View createSadTabView( | 53 public static View createSadTabView(Context context, final OnClickListener s uggestionAction, |
35 Context context, final OnClickListener suggestionAction, | 54 OnClickListener reloadButtonAction, boolean showSendFeedbackView) { |
36 OnClickListener reloadButtonAction, @StringRes int buttonTextId) { | |
37 // Inflate Sad tab and initialize. | 55 // Inflate Sad tab and initialize. |
38 LayoutInflater inflater = (LayoutInflater) context.getSystemService( | 56 LayoutInflater inflater = (LayoutInflater) context.getSystemService( |
39 Context.LAYOUT_INFLATER_SERVICE); | 57 Context.LAYOUT_INFLATER_SERVICE); |
40 View sadTabView = inflater.inflate(R.layout.sad_tab, null); | 58 View sadTabView = inflater.inflate(R.layout.sad_tab, null); |
41 | 59 |
60 TextView titleText = (TextView) sadTabView.findViewById(R.id.sad_tab_tit le); | |
61 int titleTextId = | |
62 showSendFeedbackView ? R.string.sad_tab_reload_title : R.string. sad_tab_title; | |
63 titleText.setText(titleTextId); | |
64 | |
65 if (showSendFeedbackView) intializeSuggestionsViews(context, sadTabView) ; | |
66 | |
42 TextView messageText = (TextView) sadTabView.findViewById(R.id.sad_tab_m essage); | 67 TextView messageText = (TextView) sadTabView.findViewById(R.id.sad_tab_m essage); |
43 messageText.setText(getHelpMessage(context, suggestionAction)); | 68 messageText.setText(getHelpMessage(context, suggestionAction, showSendFe edbackView)); |
44 messageText.setMovementMethod(LinkMovementMethod.getInstance()); | 69 messageText.setMovementMethod(LinkMovementMethod.getInstance()); |
45 | 70 |
46 Button reloadButton = (Button) sadTabView.findViewById(R.id.sad_tab_relo ad_button); | 71 Button reloadButton = (Button) sadTabView.findViewById(R.id.sad_tab_relo ad_button); |
72 int buttonTextId = showSendFeedbackView ? R.string.sad_tab_send_feedback _label | |
73 : R.string.sad_tab_reload_label; | |
47 reloadButton.setText(buttonTextId); | 74 reloadButton.setText(buttonTextId); |
48 reloadButton.setOnClickListener(reloadButtonAction); | 75 reloadButton.setOnClickListener(reloadButtonAction); |
49 | 76 |
50 return sadTabView; | 77 return sadTabView; |
51 } | 78 } |
52 | 79 |
53 /** | 80 /** |
54 * Construct and return help message to be displayed on R.id.sad_tab_message . | 81 * Construct and return help message to be displayed on R.id.sad_tab_message . |
55 * @param context Context of the resulting Sad Tab view. This is needed to l oad the strings. | 82 * @param context Context of the resulting Sad Tab view. This is needed to l oad the strings. |
56 * @param suggestionAction Action to be executed when user clicks "try these suggestions". | 83 * @param suggestionAction Action to be executed when user clicks "try these suggestions" |
84 * or "learn more". | |
57 * @return Help message to be displayed on R.id.sad_tab_message. | 85 * @return Help message to be displayed on R.id.sad_tab_message. |
58 */ | 86 */ |
59 private static CharSequence getHelpMessage( | 87 private static CharSequence getHelpMessage( |
60 Context context, final OnClickListener suggestionAction) { | 88 Context context, final OnClickListener suggestionAction, boolean sho wSendFeedback) { |
61 String helpMessage = context.getString(R.string.sad_tab_message) | 89 NoUnderlineClickableSpan linkSpan = new NoUnderlineClickableSpan() { |
62 + "\n\n" + context.getString(R.string.sad_tab_suggestions); | |
63 NoUnderlineClickableSpan span = new NoUnderlineClickableSpan() { | |
64 @Override | 90 @Override |
65 public void onClick(View view) { | 91 public void onClick(View view) { |
66 suggestionAction.onClick(view); | 92 suggestionAction.onClick(view); |
67 } | 93 } |
68 }; | 94 }; |
69 return SpanApplier.applySpans(helpMessage, new SpanInfo("<link>", "</lin k>", span)); | 95 |
96 if (showSendFeedback) { | |
97 SpannableString learnMoreLink = | |
98 new SpannableString(context.getString(R.string.sad_tab_reloa d_learn_more)); | |
99 learnMoreLink.setSpan(linkSpan, 0, learnMoreLink.length(), 0); | |
100 return learnMoreLink; | |
101 } else { | |
102 String helpMessage = context.getString(R.string.sad_tab_message) + " \n\n" | |
103 + context.getString(R.string.sad_tab_suggestions); | |
104 return SpanApplier.applySpans(helpMessage, new SpanInfo("<link>", "< /link>", linkSpan)); | |
105 } | |
106 } | |
107 | |
108 /** | |
109 * Initializes the TextViews that display tips for handling repeated crashes . | |
110 * @param context Context of the resulting Sad Tab view. | |
Theresa
2017/05/09 21:01:36
nit: This comment could be better.
| |
111 * @param sadTabView The parent Sad Tab view that contains the TextViews. | |
112 */ | |
113 private static void intializeSuggestionsViews(Context context, View sadTabVi ew) { | |
114 TextView suggestionsTitle = | |
115 (TextView) sadTabView.findViewById(R.id.sad_tab_suggestions_titl e); | |
116 suggestionsTitle.setVisibility(View.VISIBLE); | |
117 suggestionsTitle.setText(R.string.sad_tab_reload_try); | |
118 | |
119 SpannableString bullet1 = | |
120 new SpannableString(context.getString(R.string.sad_tab_reload_cl ose_notabs)); | |
121 bullet1.setSpan(new BulletSpan(BULLET_GAP_WIDTH), 0, bullet1.length(), 0 ); | |
122 | |
123 SpannableString bullet2 = | |
124 new SpannableString(context.getString(R.string.sad_tab_reload_in cognito)); | |
125 bullet2.setSpan(new BulletSpan(BULLET_GAP_WIDTH), 0, bullet2.length(), 0 ); | |
126 | |
127 SpannableString bullet3 = | |
128 new SpannableString(context.getString(R.string.sad_tab_reload_re start_browser)); | |
129 bullet3.setSpan(new BulletSpan(BULLET_GAP_WIDTH), 0, bullet3.length(), 0 ); | |
130 | |
131 SpannableString bullet4 = | |
132 new SpannableString(context.getString(R.string.sad_tab_reload_re start_device)); | |
133 bullet4.setSpan(new BulletSpan(BULLET_GAP_WIDTH), 0, bullet4.length(), 0 ); | |
134 | |
135 TextView suggestions = (TextView) sadTabView.findViewById(R.id.sad_tab_s uggestions); | |
136 suggestions.setVisibility(View.VISIBLE); | |
137 suggestions.setText( | |
138 TextUtils.concat(bullet1, "\n", bullet2, "\n", bullet3, "\n", bu llet4, "\n")); | |
139 } | |
140 | |
141 /** | |
142 * Records enumerated histograms for {@link SadTabEvent}. | |
143 * @param sendFeedbackView Whether the event is for the "send feedback" vers ion of the Sad Tab. | |
144 * @param event The {@link SadTabEvent} to record. | |
145 */ | |
146 public static void recordEvent(boolean sendFeedbackView, @SadTabEvent int ev ent) { | |
147 if (sendFeedbackView) { | |
148 RecordHistogram.recordEnumeratedHistogram( | |
149 "Tabs.SadTab.Feedback.Event", event, MAX_SAD_TAB_EVENT); | |
150 } else { | |
151 RecordHistogram.recordEnumeratedHistogram( | |
152 "Tabs.SadTab.Reload.Event", event, MAX_SAD_TAB_EVENT); | |
153 } | |
70 } | 154 } |
71 } | 155 } |
OLD | NEW |