2011-11-18 10:28:17 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
2013-05-15 10:41:25 -07:00
|
|
|
|
|
|
|
import org.mozilla.gecko.widget.Divider;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-08-04 16:34:44 -07:00
|
|
|
import org.json.JSONArray;
|
2012-07-27 17:53:54 -07:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2013-05-30 15:18:47 -07:00
|
|
|
import android.content.Context;
|
2013-05-30 15:18:47 -07:00
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.os.Build;
|
2012-03-06 15:08:55 -08:00
|
|
|
import android.text.SpannableString;
|
2013-06-21 20:36:35 -07:00
|
|
|
import android.text.TextUtils;
|
2012-03-06 15:08:55 -08:00
|
|
|
import android.text.method.LinkMovementMethod;
|
|
|
|
import android.text.style.ForegroundColorSpan;
|
|
|
|
import android.text.style.URLSpan;
|
2012-03-27 14:36:20 -07:00
|
|
|
import android.util.Log;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
2013-05-30 15:18:47 -07:00
|
|
|
import android.view.ViewGroup;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.widget.Button;
|
2012-03-27 14:36:20 -07:00
|
|
|
import android.widget.CheckBox;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.widget.LinearLayout;
|
2013-05-30 17:01:31 -07:00
|
|
|
import android.widget.Spinner;
|
2013-05-30 15:18:47 -07:00
|
|
|
import android.widget.SpinnerAdapter;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2013-05-30 15:18:47 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
public class DoorHanger extends LinearLayout {
|
2012-08-04 16:34:44 -07:00
|
|
|
private static final String LOGTAG = "GeckoDoorHanger";
|
2012-03-27 14:36:20 -07:00
|
|
|
|
2013-05-30 17:01:31 -07:00
|
|
|
private static int sInputPadding = -1;
|
|
|
|
private static int sSpinnerTextColor = -1;
|
|
|
|
private static int sSpinnerTextSize = -1;
|
2013-05-30 15:18:47 -07:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
private static LayoutParams sButtonParams;
|
|
|
|
static {
|
|
|
|
sButtonParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
private final TextView mTextView;
|
|
|
|
private final LinearLayout mChoicesLayout;
|
|
|
|
|
|
|
|
// Divider between doorhangers.
|
|
|
|
private final View mDivider;
|
|
|
|
|
|
|
|
// The tab associated with this notification.
|
2012-08-04 16:34:44 -07:00
|
|
|
private final int mTabId;
|
2013-06-21 20:36:35 -07:00
|
|
|
|
|
|
|
// Value used to identify the notification.
|
2012-08-04 16:34:44 -07:00
|
|
|
private final String mValue;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
private List<PromptInput> mInputs;
|
2012-03-27 14:36:20 -07:00
|
|
|
private CheckBox mCheckBox;
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
private int mPersistence = 0;
|
2012-07-05 16:12:17 -07:00
|
|
|
private boolean mPersistWhileVisible = false;
|
2011-11-18 10:28:17 -08:00
|
|
|
private long mTimeout = 0;
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
public interface OnButtonClickListener {
|
|
|
|
public void onButtonClick(DoorHanger dh, String tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
DoorHanger(Context context) {
|
|
|
|
this(context, 0, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
DoorHanger(Context context, int tabId, String value) {
|
2013-05-30 15:18:47 -07:00
|
|
|
super(context);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2013-05-30 15:18:47 -07:00
|
|
|
mTabId = tabId;
|
|
|
|
mValue = value;
|
2013-05-30 15:18:47 -07:00
|
|
|
|
2013-05-30 17:01:31 -07:00
|
|
|
if (sInputPadding == -1) {
|
2013-06-11 12:14:44 -07:00
|
|
|
sInputPadding = getResources().getDimensionPixelSize(R.dimen.doorhanger_padding);
|
2013-05-30 15:18:47 -07:00
|
|
|
}
|
2013-05-30 17:01:31 -07:00
|
|
|
if (sSpinnerTextColor == -1) {
|
|
|
|
sSpinnerTextColor = getResources().getColor(R.color.text_color_primary_disable_only);
|
2013-05-30 15:18:47 -07:00
|
|
|
}
|
2013-05-30 17:01:31 -07:00
|
|
|
if (sSpinnerTextSize == -1) {
|
|
|
|
sSpinnerTextSize = getResources().getDimensionPixelSize(R.dimen.doorhanger_spinner_textsize);
|
2013-05-30 15:18:47 -07:00
|
|
|
}
|
2013-06-21 20:36:35 -07:00
|
|
|
|
|
|
|
setOrientation(VERTICAL);
|
|
|
|
|
|
|
|
LayoutInflater.from(context).inflate(R.layout.doorhanger, this);
|
|
|
|
mTextView = (TextView) findViewById(R.id.doorhanger_title);
|
|
|
|
mChoicesLayout = (LinearLayout) findViewById(R.id.doorhanger_choices);
|
|
|
|
mDivider = findViewById(R.id.divider_doorhanger);
|
2012-08-04 16:34:44 -07:00
|
|
|
}
|
2013-06-21 20:36:35 -07:00
|
|
|
|
2012-08-04 16:34:44 -07:00
|
|
|
int getTabId() {
|
|
|
|
return mTabId;
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-08-04 16:34:44 -07:00
|
|
|
String getValue() {
|
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
List<PromptInput> getInputs() {
|
|
|
|
return mInputs;
|
2013-03-11 14:07:11 -07:00
|
|
|
}
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
CheckBox getCheckBox() {
|
|
|
|
return mCheckBox;
|
2013-03-11 14:07:11 -07:00
|
|
|
}
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
void showDivider() {
|
|
|
|
mDivider.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
void hideDivider() {
|
|
|
|
mDivider.setVisibility(View.GONE);
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
void setMessage(String message) {
|
2012-08-04 16:34:44 -07:00
|
|
|
mTextView.setText(message);
|
2013-06-21 20:36:35 -07:00
|
|
|
}
|
2012-08-04 16:34:44 -07:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
void addLink(String label, String url, String delimiter) {
|
|
|
|
String title = mTextView.getText().toString();
|
|
|
|
SpannableString titleWithLink = new SpannableString(title + delimiter + label);
|
|
|
|
URLSpan linkSpan = new URLSpan(url) {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
Tabs.getInstance().loadUrlInTab(getURL());
|
2012-08-04 16:34:44 -07:00
|
|
|
}
|
2013-06-21 20:36:35 -07:00
|
|
|
};
|
2012-08-04 16:34:44 -07:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
// Prevent text outside the link from flashing when clicked.
|
|
|
|
ForegroundColorSpan colorSpan = new ForegroundColorSpan(mTextView.getCurrentTextColor());
|
|
|
|
titleWithLink.setSpan(colorSpan, 0, title.length(), 0);
|
2012-09-24 13:34:27 -07:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
titleWithLink.setSpan(linkSpan, title.length() + 1, titleWithLink.length(), 0);
|
|
|
|
mTextView.setText(titleWithLink);
|
|
|
|
mTextView.setMovementMethod(LinkMovementMethod.getInstance());
|
2012-08-04 16:34:44 -07:00
|
|
|
}
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
void addButton(final String text, final String tag, final OnButtonClickListener listener) {
|
|
|
|
final Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.doorhanger_button, null);
|
|
|
|
button.setText(text);
|
|
|
|
button.setTag(tag);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
button.setOnClickListener(new Button.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
listener.onButtonClick(DoorHanger.this, tag);
|
|
|
|
}
|
|
|
|
});
|
2013-03-11 14:07:11 -07:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
if (mChoicesLayout.getChildCount() == 0) {
|
|
|
|
// If this is the first button we're adding, make the choices layout visible.
|
|
|
|
mChoicesLayout.setVisibility(View.VISIBLE);
|
|
|
|
findViewById(R.id.divider_choices).setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
// Add a divider for additional buttons.
|
2013-05-30 15:18:47 -07:00
|
|
|
Divider divider = new Divider(getContext(), null);
|
2013-03-11 14:07:11 -07:00
|
|
|
divider.setOrientation(Divider.Orientation.VERTICAL);
|
|
|
|
divider.setBackgroundColor(0xFFD1D5DA);
|
|
|
|
mChoicesLayout.addView(divider);
|
|
|
|
}
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
mChoicesLayout.addView(button, sButtonParams);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
void setOptions(final JSONObject options) {
|
|
|
|
final int persistence = options.optInt("persistence");
|
|
|
|
if (persistence > 0) {
|
|
|
|
mPersistence = persistence;
|
2012-03-27 14:36:20 -07:00
|
|
|
}
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
mPersistWhileVisible = options.optBoolean("persistWhileVisible");
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
final long timeout = options.optLong("timeout");
|
|
|
|
if (timeout > 0) {
|
|
|
|
mTimeout = timeout;
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
final JSONObject link = options.optJSONObject("link");
|
|
|
|
if (link != null) {
|
|
|
|
try {
|
|
|
|
final String linkLabel = link.getString("label");
|
|
|
|
final String linkUrl = link.getString("url");
|
|
|
|
addLink(linkLabel, linkUrl, " ");
|
|
|
|
} catch (JSONException e) { }
|
|
|
|
}
|
2012-03-27 14:36:20 -07:00
|
|
|
|
2013-05-30 15:18:47 -07:00
|
|
|
final JSONArray inputs = options.optJSONArray("inputs");
|
|
|
|
if (inputs != null) {
|
|
|
|
mInputs = new ArrayList<PromptInput>();
|
|
|
|
|
|
|
|
final ViewGroup group = (ViewGroup) findViewById(R.id.doorhanger_inputs);
|
|
|
|
group.setVisibility(VISIBLE);
|
|
|
|
|
|
|
|
for (int i = 0; i < inputs.length(); i++) {
|
|
|
|
try {
|
2013-05-30 15:18:47 -07:00
|
|
|
PromptInput input = PromptInput.getInput(inputs.getJSONObject(i));
|
|
|
|
mInputs.add(input);
|
|
|
|
|
|
|
|
View v = input.getView(getContext());
|
|
|
|
styleInput(input, v);
|
|
|
|
group.addView(v);
|
2013-05-30 15:18:47 -07:00
|
|
|
} catch(JSONException ex) { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
final String checkBoxText = options.optString("checkbox");
|
|
|
|
if (!TextUtils.isEmpty(checkBoxText)) {
|
2012-03-27 14:36:20 -07:00
|
|
|
mCheckBox = (CheckBox) findViewById(R.id.doorhanger_checkbox);
|
|
|
|
mCheckBox.setText(checkBoxText);
|
|
|
|
mCheckBox.setVisibility(VISIBLE);
|
2013-06-21 20:36:35 -07:00
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2013-05-30 15:18:47 -07:00
|
|
|
private void styleInput(PromptInput input, View view) {
|
|
|
|
if (input instanceof PromptInput.MenulistInput) {
|
|
|
|
styleSpinner(input, view);
|
|
|
|
} else {
|
|
|
|
// add some top and bottom padding to separate inputs
|
2013-05-30 17:01:31 -07:00
|
|
|
view.setPadding(0, sInputPadding,
|
|
|
|
0, sInputPadding);
|
2013-05-30 15:18:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void styleSpinner(PromptInput input, View view) {
|
|
|
|
PromptInput.MenulistInput spinInput = (PromptInput.MenulistInput) input;
|
2013-05-30 17:01:31 -07:00
|
|
|
|
|
|
|
/* Spinners have some intrinsic padding. To force the spinner's text to line up with
|
|
|
|
* the doorhanger text, we have to take that padding into account.
|
|
|
|
*
|
|
|
|
* |-----A-------| <-- Normal doorhanger message
|
|
|
|
* |-B-|---C+D---| <-- (optional) Spinner Label
|
|
|
|
* |-B-|-C-|--D--| <-- Spinner
|
|
|
|
*
|
|
|
|
* A - Desired padding (sInputPadding)
|
|
|
|
* B - Final padding applied to input element (sInputPadding - rect.left - textPadding).
|
|
|
|
* C - Spinner background drawable padding (rect.left).
|
|
|
|
* D - Spinner inner TextView padding (textPadding).
|
|
|
|
*/
|
2013-05-30 15:18:47 -07:00
|
|
|
|
|
|
|
// First get the padding of the selected view inside the spinner. Since the spinner
|
|
|
|
// hasn't been shown yet, we get this view directly from the adapter.
|
2013-05-30 17:01:31 -07:00
|
|
|
Spinner spinner = spinInput.spinner;
|
|
|
|
SpinnerAdapter adapter = spinner.getAdapter();
|
|
|
|
View dropView = adapter.getView(0, null, spinner);
|
|
|
|
int textPadding = 0;
|
2013-05-30 15:18:47 -07:00
|
|
|
if (dropView != null) {
|
2013-05-30 17:01:31 -07:00
|
|
|
textPadding = dropView.getPaddingLeft();
|
2013-05-30 15:18:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then get the intrinsic padding built into the background image of the spinner.
|
|
|
|
Rect rect = new Rect();
|
2013-05-30 17:01:31 -07:00
|
|
|
spinner.getBackground().getPadding(rect);
|
2013-05-30 15:18:47 -07:00
|
|
|
|
2013-05-30 17:01:31 -07:00
|
|
|
// Set the difference in padding to the spinner view to align it with doorhanger text.
|
|
|
|
view.setPadding(sInputPadding - rect.left - textPadding, 0, rect.right, sInputPadding);
|
2013-05-30 15:18:47 -07:00
|
|
|
|
|
|
|
if (spinInput.textView != null) {
|
2013-05-30 17:01:31 -07:00
|
|
|
spinInput.textView.setTextColor(sSpinnerTextColor);
|
|
|
|
spinInput.textView.setTextSize(sSpinnerTextSize);
|
2013-05-30 15:18:47 -07:00
|
|
|
|
|
|
|
// If this spinner has a label, offset it to also be aligned with the doorhanger text.
|
2013-05-30 17:01:31 -07:00
|
|
|
spinInput.textView.setPadding(rect.left + textPadding, 0, 0, 0);
|
2013-05-30 15:18:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 20:36:35 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks with persistence and timeout options to see if it's okay to remove a doorhanger.
|
|
|
|
*
|
|
|
|
* @param isShowing Whether or not this doorhanger is currently visible to the user.
|
|
|
|
* (e.g. the DoorHanger view might be VISIBLE, but its parent could be hidden)
|
|
|
|
*/
|
|
|
|
boolean shouldRemove(boolean isShowing) {
|
|
|
|
if (mPersistWhileVisible && isShowing) {
|
2012-07-05 16:12:17 -07:00
|
|
|
// We still want to decrement mPersistence, even if the popup is showing
|
|
|
|
if (mPersistence != 0)
|
|
|
|
mPersistence--;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
// If persistence is set to -1, the doorhanger will never be
|
|
|
|
// automatically removed.
|
|
|
|
if (mPersistence != 0) {
|
|
|
|
mPersistence--;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-13 11:08:39 -08:00
|
|
|
if (System.currentTimeMillis() <= mTimeout) {
|
2011-11-18 10:28:17 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|