1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/widget/DoorHanger.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,348 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.widget; 1.10 + 1.11 +import org.mozilla.gecko.R; 1.12 +import org.mozilla.gecko.Tabs; 1.13 +import org.mozilla.gecko.prompts.PromptInput; 1.14 + 1.15 +import org.json.JSONArray; 1.16 +import org.json.JSONException; 1.17 +import org.json.JSONObject; 1.18 + 1.19 +import android.content.Context; 1.20 +import android.content.res.Resources; 1.21 +import android.graphics.Rect; 1.22 +import android.os.Build; 1.23 +import android.text.SpannableString; 1.24 +import android.text.TextUtils; 1.25 +import android.text.method.LinkMovementMethod; 1.26 +import android.text.style.ForegroundColorSpan; 1.27 +import android.text.style.URLSpan; 1.28 +import android.util.Log; 1.29 +import android.view.LayoutInflater; 1.30 +import android.view.View; 1.31 +import android.view.ViewGroup; 1.32 +import android.widget.Button; 1.33 +import android.widget.CheckBox; 1.34 +import android.widget.ImageView; 1.35 +import android.widget.LinearLayout; 1.36 +import android.widget.Spinner; 1.37 +import android.widget.SpinnerAdapter; 1.38 +import android.widget.TextView; 1.39 + 1.40 +import java.util.ArrayList; 1.41 +import java.util.List; 1.42 + 1.43 +public class DoorHanger extends LinearLayout { 1.44 + private static final String LOGTAG = "GeckoDoorHanger"; 1.45 + 1.46 + private static int sInputPadding = -1; 1.47 + private static int sSpinnerTextColor = -1; 1.48 + private static int sSpinnerTextSize = -1; 1.49 + 1.50 + private static LayoutParams sButtonParams; 1.51 + static { 1.52 + sButtonParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f); 1.53 + } 1.54 + 1.55 + private final TextView mTextView; 1.56 + private final ImageView mIcon; 1.57 + private final LinearLayout mChoicesLayout; 1.58 + 1.59 + // Divider between doorhangers. 1.60 + private final View mDivider; 1.61 + 1.62 + // The tab associated with this notification. 1.63 + private final int mTabId; 1.64 + 1.65 + // Value used to identify the notification. 1.66 + private final String mValue; 1.67 + 1.68 + private Resources mResources; 1.69 + 1.70 + private List<PromptInput> mInputs; 1.71 + private CheckBox mCheckBox; 1.72 + 1.73 + private int mPersistence = 0; 1.74 + private boolean mPersistWhileVisible = false; 1.75 + private long mTimeout = 0; 1.76 + 1.77 + // Color used for dividers above and between buttons. 1.78 + private int mDividerColor; 1.79 + 1.80 + public static enum Theme { 1.81 + LIGHT, 1.82 + DARK 1.83 + } 1.84 + 1.85 + public interface OnButtonClickListener { 1.86 + public void onButtonClick(DoorHanger dh, String tag); 1.87 + } 1.88 + 1.89 + public DoorHanger(Context context, Theme theme) { 1.90 + this(context, 0, null, theme); 1.91 + } 1.92 + 1.93 + public DoorHanger(Context context, int tabId, String value) { 1.94 + this(context, tabId, value, Theme.LIGHT); 1.95 + } 1.96 + 1.97 + private DoorHanger(Context context, int tabId, String value, Theme theme) { 1.98 + super(context); 1.99 + 1.100 + mTabId = tabId; 1.101 + mValue = value; 1.102 + mResources = getResources(); 1.103 + 1.104 + if (sInputPadding == -1) { 1.105 + sInputPadding = mResources.getDimensionPixelSize(R.dimen.doorhanger_padding); 1.106 + } 1.107 + if (sSpinnerTextColor == -1) { 1.108 + sSpinnerTextColor = mResources.getColor(R.color.text_color_primary_disable_only); 1.109 + } 1.110 + if (sSpinnerTextSize == -1) { 1.111 + sSpinnerTextSize = mResources.getDimensionPixelSize(R.dimen.doorhanger_spinner_textsize); 1.112 + } 1.113 + 1.114 + setOrientation(VERTICAL); 1.115 + 1.116 + LayoutInflater.from(context).inflate(R.layout.doorhanger, this); 1.117 + mTextView = (TextView) findViewById(R.id.doorhanger_title); 1.118 + mIcon = (ImageView) findViewById(R.id.doorhanger_icon); 1.119 + mChoicesLayout = (LinearLayout) findViewById(R.id.doorhanger_choices); 1.120 + mDivider = findViewById(R.id.divider_doorhanger); 1.121 + 1.122 + setTheme(theme); 1.123 + } 1.124 + 1.125 + private void setTheme(Theme theme) { 1.126 + if (theme == Theme.LIGHT) { 1.127 + // The default styles declared in doorhanger.xml are light-themed, so we just 1.128 + // need to set the divider color that we'll use in addButton. 1.129 + mDividerColor = mResources.getColor(R.color.doorhanger_divider_light); 1.130 + 1.131 + } else if (theme == Theme.DARK) { 1.132 + mDividerColor = mResources.getColor(R.color.doorhanger_divider_dark); 1.133 + 1.134 + // Set a dark background, and use a smaller text size for dark-themed DoorHangers. 1.135 + setBackgroundColor(mResources.getColor(R.color.doorhanger_background_dark)); 1.136 + mTextView.setTextSize(mResources.getDimension(R.dimen.doorhanger_textsize_small)); 1.137 + } 1.138 + } 1.139 + 1.140 + public int getTabId() { 1.141 + return mTabId; 1.142 + } 1.143 + 1.144 + public String getValue() { 1.145 + return mValue; 1.146 + } 1.147 + 1.148 + public List<PromptInput> getInputs() { 1.149 + return mInputs; 1.150 + } 1.151 + 1.152 + public CheckBox getCheckBox() { 1.153 + return mCheckBox; 1.154 + } 1.155 + 1.156 + public void showDivider() { 1.157 + mDivider.setVisibility(View.VISIBLE); 1.158 + } 1.159 + 1.160 + public void hideDivider() { 1.161 + mDivider.setVisibility(View.GONE); 1.162 + } 1.163 + 1.164 + public void setMessage(String message) { 1.165 + mTextView.setText(message); 1.166 + } 1.167 + 1.168 + public void setIcon(int resId) { 1.169 + mIcon.setImageResource(resId); 1.170 + mIcon.setVisibility(View.VISIBLE); 1.171 + } 1.172 + 1.173 + public void addLink(String label, String url, String delimiter) { 1.174 + String title = mTextView.getText().toString(); 1.175 + SpannableString titleWithLink = new SpannableString(title + delimiter + label); 1.176 + URLSpan linkSpan = new URLSpan(url) { 1.177 + @Override 1.178 + public void onClick(View view) { 1.179 + Tabs.getInstance().loadUrlInTab(getURL()); 1.180 + } 1.181 + }; 1.182 + 1.183 + // Prevent text outside the link from flashing when clicked. 1.184 + ForegroundColorSpan colorSpan = new ForegroundColorSpan(mTextView.getCurrentTextColor()); 1.185 + titleWithLink.setSpan(colorSpan, 0, title.length(), 0); 1.186 + 1.187 + titleWithLink.setSpan(linkSpan, title.length() + 1, titleWithLink.length(), 0); 1.188 + mTextView.setText(titleWithLink); 1.189 + mTextView.setMovementMethod(LinkMovementMethod.getInstance()); 1.190 + } 1.191 + 1.192 + public void addButton(final String text, final String tag, final OnButtonClickListener listener) { 1.193 + final Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.doorhanger_button, null); 1.194 + button.setText(text); 1.195 + button.setTag(tag); 1.196 + 1.197 + button.setOnClickListener(new Button.OnClickListener() { 1.198 + @Override 1.199 + public void onClick(View v) { 1.200 + listener.onButtonClick(DoorHanger.this, tag); 1.201 + } 1.202 + }); 1.203 + 1.204 + if (mChoicesLayout.getChildCount() == 0) { 1.205 + // If this is the first button we're adding, make the choices layout visible. 1.206 + mChoicesLayout.setVisibility(View.VISIBLE); 1.207 + // Make the divider above the buttons visible. 1.208 + View divider = findViewById(R.id.divider_choices); 1.209 + divider.setVisibility(View.VISIBLE); 1.210 + divider.setBackgroundColor(mDividerColor); 1.211 + } else { 1.212 + // Add a vertical divider between additional buttons. 1.213 + Divider divider = new Divider(getContext(), null); 1.214 + divider.setOrientation(Divider.Orientation.VERTICAL); 1.215 + divider.setBackgroundColor(mDividerColor); 1.216 + mChoicesLayout.addView(divider); 1.217 + } 1.218 + 1.219 + mChoicesLayout.addView(button, sButtonParams); 1.220 + } 1.221 + 1.222 + public void setOptions(final JSONObject options) { 1.223 + final int persistence = options.optInt("persistence"); 1.224 + if (persistence > 0) { 1.225 + mPersistence = persistence; 1.226 + } 1.227 + 1.228 + mPersistWhileVisible = options.optBoolean("persistWhileVisible"); 1.229 + 1.230 + final long timeout = options.optLong("timeout"); 1.231 + if (timeout > 0) { 1.232 + mTimeout = timeout; 1.233 + } 1.234 + 1.235 + final JSONObject link = options.optJSONObject("link"); 1.236 + if (link != null) { 1.237 + try { 1.238 + final String linkLabel = link.getString("label"); 1.239 + final String linkUrl = link.getString("url"); 1.240 + addLink(linkLabel, linkUrl, " "); 1.241 + } catch (JSONException e) { } 1.242 + } 1.243 + 1.244 + final JSONArray inputs = options.optJSONArray("inputs"); 1.245 + if (inputs != null) { 1.246 + mInputs = new ArrayList<PromptInput>(); 1.247 + 1.248 + final ViewGroup group = (ViewGroup) findViewById(R.id.doorhanger_inputs); 1.249 + group.setVisibility(VISIBLE); 1.250 + 1.251 + for (int i = 0; i < inputs.length(); i++) { 1.252 + try { 1.253 + PromptInput input = PromptInput.getInput(inputs.getJSONObject(i)); 1.254 + mInputs.add(input); 1.255 + 1.256 + View v = input.getView(getContext()); 1.257 + styleInput(input, v); 1.258 + group.addView(v); 1.259 + } catch(JSONException ex) { } 1.260 + } 1.261 + } 1.262 + 1.263 + final String checkBoxText = options.optString("checkbox"); 1.264 + if (!TextUtils.isEmpty(checkBoxText)) { 1.265 + mCheckBox = (CheckBox) findViewById(R.id.doorhanger_checkbox); 1.266 + mCheckBox.setText(checkBoxText); 1.267 + mCheckBox.setVisibility(VISIBLE); 1.268 + } 1.269 + } 1.270 + 1.271 + private void styleInput(PromptInput input, View view) { 1.272 + if (input instanceof PromptInput.MenulistInput) { 1.273 + styleSpinner(input, view); 1.274 + } else { 1.275 + // add some top and bottom padding to separate inputs 1.276 + view.setPadding(0, sInputPadding, 1.277 + 0, sInputPadding); 1.278 + } 1.279 + } 1.280 + 1.281 + private void styleSpinner(PromptInput input, View view) { 1.282 + PromptInput.MenulistInput spinInput = (PromptInput.MenulistInput) input; 1.283 + 1.284 + /* Spinners have some intrinsic padding. To force the spinner's text to line up with 1.285 + * the doorhanger text, we have to take that padding into account. 1.286 + * 1.287 + * |-----A-------| <-- Normal doorhanger message 1.288 + * |-B-|---C+D---| <-- (optional) Spinner Label 1.289 + * |-B-|-C-|--D--| <-- Spinner 1.290 + * 1.291 + * A - Desired padding (sInputPadding) 1.292 + * B - Final padding applied to input element (sInputPadding - rect.left - textPadding). 1.293 + * C - Spinner background drawable padding (rect.left). 1.294 + * D - Spinner inner TextView padding (textPadding). 1.295 + */ 1.296 + 1.297 + // First get the padding of the selected view inside the spinner. Since the spinner 1.298 + // hasn't been shown yet, we get this view directly from the adapter. 1.299 + Spinner spinner = spinInput.spinner; 1.300 + SpinnerAdapter adapter = spinner.getAdapter(); 1.301 + View dropView = adapter.getView(0, null, spinner); 1.302 + int textPadding = 0; 1.303 + if (dropView != null) { 1.304 + textPadding = dropView.getPaddingLeft(); 1.305 + } 1.306 + 1.307 + // Then get the intrinsic padding built into the background image of the spinner. 1.308 + Rect rect = new Rect(); 1.309 + spinner.getBackground().getPadding(rect); 1.310 + 1.311 + // Set the difference in padding to the spinner view to align it with doorhanger text. 1.312 + view.setPadding(sInputPadding - rect.left - textPadding, 0, rect.right, sInputPadding); 1.313 + 1.314 + if (spinInput.textView != null) { 1.315 + spinInput.textView.setTextColor(sSpinnerTextColor); 1.316 + spinInput.textView.setTextSize(sSpinnerTextSize); 1.317 + 1.318 + // If this spinner has a label, offset it to also be aligned with the doorhanger text. 1.319 + spinInput.textView.setPadding(rect.left + textPadding, 0, 0, 0); 1.320 + } 1.321 + } 1.322 + 1.323 + 1.324 + /* 1.325 + * Checks with persistence and timeout options to see if it's okay to remove a doorhanger. 1.326 + * 1.327 + * @param isShowing Whether or not this doorhanger is currently visible to the user. 1.328 + * (e.g. the DoorHanger view might be VISIBLE, but its parent could be hidden) 1.329 + */ 1.330 + public boolean shouldRemove(boolean isShowing) { 1.331 + if (mPersistWhileVisible && isShowing) { 1.332 + // We still want to decrement mPersistence, even if the popup is showing 1.333 + if (mPersistence != 0) 1.334 + mPersistence--; 1.335 + return false; 1.336 + } 1.337 + 1.338 + // If persistence is set to -1, the doorhanger will never be 1.339 + // automatically removed. 1.340 + if (mPersistence != 0) { 1.341 + mPersistence--; 1.342 + return false; 1.343 + } 1.344 + 1.345 + if (System.currentTimeMillis() <= mTimeout) { 1.346 + return false; 1.347 + } 1.348 + 1.349 + return true; 1.350 + } 1.351 +}