Backed out 5 changesets (bug 872388) for Android 2.2 bustage on a CLOSED TREE.

Backed out changeset f2fbb5c6cfc7 (bug 872388)
Backed out changeset 416f9cdde0f7 (bug 872388)
Backed out changeset 684ba082942b (bug 872388)
Backed out changeset 7acb6c63fdf4 (bug 872388)
Backed out changeset 2076903c21fa (bug 872388)
This commit is contained in:
Ryan VanderMeulen 2013-05-30 22:11:26 -04:00
parent a7e0d442e8
commit 3fb03ee4f9
16 changed files with 114 additions and 557 deletions

View File

@ -15,7 +15,9 @@ import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.util.UiAsyncTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
@ -553,6 +555,62 @@ public class AwesomeBar extends GeckoActivity
mContextMenuSubject = tab.getSubject(menu, view, menuInfo);
}
private abstract class EditBookmarkTextWatcher implements TextWatcher {
protected AlertDialog mDialog;
protected EditBookmarkTextWatcher mPairedTextWatcher;
protected boolean mEnabled = true;
public EditBookmarkTextWatcher(AlertDialog aDialog) {
mDialog = aDialog;
}
public void setPairedTextWatcher(EditBookmarkTextWatcher aTextWatcher) {
mPairedTextWatcher = aTextWatcher;
}
public boolean isEnabled() {
return mEnabled;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Disable if the we're disabled or paired partner is disabled
boolean enabled = mEnabled && (mPairedTextWatcher == null || mPairedTextWatcher.isEnabled());
mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled);
}
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
}
private class LocationTextWatcher extends EditBookmarkTextWatcher implements TextWatcher {
public LocationTextWatcher(AlertDialog aDialog) {
super(aDialog);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Disable if the location is empty
mEnabled = (s.toString().trim().length() > 0);
super.onTextChanged(s, start, before, count);
}
}
private class KeywordTextWatcher extends EditBookmarkTextWatcher implements TextWatcher {
public KeywordTextWatcher(AlertDialog aDialog) {
super(aDialog);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Disable if the keyword contains spaces
mEnabled = (s.toString().trim().indexOf(' ') == -1);
super.onTextChanged(s, start, before, count);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (mContextMenuSubject == null)
@ -595,7 +653,60 @@ public class AwesomeBar extends GeckoActivity
break;
}
case R.id.edit_bookmark: {
new EditBookmarkDialog(this).show(id, title, url, keyword);
AlertDialog.Builder editPrompt = new AlertDialog.Builder(this);
final View editView = getLayoutInflater().inflate(R.layout.bookmark_edit, null);
editPrompt.setTitle(R.string.bookmark_edit_title);
editPrompt.setView(editView);
final EditText nameText = ((EditText) editView.findViewById(R.id.edit_bookmark_name));
final EditText locationText = ((EditText) editView.findViewById(R.id.edit_bookmark_location));
final EditText keywordText = ((EditText) editView.findViewById(R.id.edit_bookmark_keyword));
nameText.setText(title);
locationText.setText(url);
keywordText.setText(keyword);
editPrompt.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
(new UiAsyncTask<Void, Void, Void>(ThreadUtils.getBackgroundHandler()) {
@Override
public Void doInBackground(Void... params) {
String newUrl = locationText.getText().toString().trim();
String newKeyword = keywordText.getText().toString().trim();
BrowserDB.updateBookmark(getContentResolver(), id, newUrl, nameText.getText().toString(), newKeyword);
return null;
}
@Override
public void onPostExecute(Void result) {
Toast.makeText(AwesomeBar.this, R.string.bookmark_updated, Toast.LENGTH_SHORT).show();
}
}).execute();
}
});
editPrompt.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// do nothing
}
});
final AlertDialog dialog = editPrompt.create();
// Create our TextWatchers
LocationTextWatcher locationTextWatcher = new LocationTextWatcher(dialog);
KeywordTextWatcher keywordTextWatcher = new KeywordTextWatcher(dialog);
// Cross reference the TextWatchers
locationTextWatcher.setPairedTextWatcher(keywordTextWatcher);
keywordTextWatcher.setPairedTextWatcher(locationTextWatcher);
// Add the TextWatcher Listeners
locationText.addTextChangedListener(locationTextWatcher);
keywordText.addTextChangedListener(keywordTextWatcher);
dialog.show();
break;
}
case R.id.remove_bookmark: {

View File

@ -21,7 +21,6 @@ import org.mozilla.gecko.util.HardwareUtils;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.util.UiAsyncTask;
import org.mozilla.gecko.widget.AboutHome;
import org.mozilla.gecko.widget.ButtonToast;
import org.json.JSONArray;
import org.json.JSONException;
@ -33,7 +32,6 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.PointF;
@ -88,8 +86,6 @@ abstract public class BrowserApp extends GeckoApp
private static final int READER_ADD_FAILED = 1;
private static final int READER_ADD_DUPLICATE = 2;
private static final String ADD_SHORTCUT_TOAST = "add_shortcut_toast";
private static final String STATE_DYNAMIC_TOOLBAR_ENABLED = "dynamic_toolbar";
public static BrowserToolbar mBrowserToolbar;
@ -109,7 +105,7 @@ abstract public class BrowserApp extends GeckoApp
}
private Vector<MenuItemInfo> mAddonMenuItemsCache;
private ButtonToast mToast;
private PropertyAnimator mMainLayoutAnimator;
private static final Interpolator sTabsInterpolator = new Interpolator() {
@ -366,15 +362,6 @@ abstract public class BrowserApp extends GeckoApp
RelativeLayout actionBar = (RelativeLayout) findViewById(R.id.browser_toolbar);
mToast = new ButtonToast(findViewById(R.id.toast), new ButtonToast.ToastListener() {
@Override
public void onButtonClicked(CharSequence token) {
if (ADD_SHORTCUT_TOAST.equals(token)) {
showBookmarkDialog();
}
}
});
((GeckoApp.MainLayout) mMainLayout).setTouchEventInterceptor(new HideTabsTouchListener());
((GeckoApp.MainLayout) mMainLayout).setMotionEventInterceptor(new MotionEventInterceptor() {
@Override
@ -481,42 +468,6 @@ abstract public class BrowserApp extends GeckoApp
});
}
private void showBookmarkDialog() {
final Tab tab = Tabs.getInstance().getSelectedTab();
final Prompt ps = new Prompt(this, new Prompt.PromptCallback() {
@Override
public void onPromptFinished(String result) {
int itemId = -1;
try {
itemId = new JSONObject(result).getInt("button");
} catch(Exception ex) {
Log.e(LOGTAG, "Exception reading bookmark prompt result", ex);
}
if (tab == null)
return;
if (itemId == 0) {
new EditBookmarkDialog(BrowserApp.this).show(tab.getURL());
} else if (itemId == 1) {
String url = tab.getURL();
String title = tab.getDisplayTitle();
Bitmap favicon = tab.getFavicon();
if (url != null && title != null) {
GeckoAppShell.createShortcut(title, url, url, favicon == null ? null : favicon, "");
}
}
}
});
final Prompt.PromptListItem[] items = new Prompt.PromptListItem[2];
Resources res = getResources();
items[0] = new Prompt.PromptListItem(res.getString(R.string.contextmenu_edit_bookmark));
items[1] = new Prompt.PromptListItem(res.getString(R.string.contextmenu_add_to_launcher));
ps.show("", "", items, false);
}
private void setDynamicToolbarEnabled(boolean enabled) {
if (enabled) {
if (mLayerView != null) {
@ -1600,11 +1551,7 @@ abstract public class BrowserApp extends GeckoApp
item.setIcon(R.drawable.ic_menu_bookmark_add);
} else {
tab.addBookmark();
mToast.show(false,
getResources().getString(R.string.bookmark_added),
getResources().getString(R.string.bookmark_options),
0,
ADD_SHORTCUT_TOAST);
Toast.makeText(this, R.string.bookmark_added, Toast.LENGTH_SHORT).show();
item.setIcon(R.drawable.ic_menu_bookmark_remove);
}
}

View File

@ -1,237 +0,0 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* 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/. */
package org.mozilla.gecko;
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.util.UiAsyncTask;
import android.content.Context;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
/**
* A dialog that allows editing a bookmarks url, title, or keywords
* <p>
* Invoked by calling one of the {@link org.mozilla.gecko.EditBookmarkDialog.show}
* methods.
*/
public class EditBookmarkDialog {
private static final String LOGTAG = "GeckoEditBookmarkDialog";
private Context mContext;
public EditBookmarkDialog(Context context) {
mContext = context;
}
/**
* A private struct to make it easier to pass bookmark data across threads
*/
private class Bookmark {
int id;
String title;
String url;
String keyword;
public Bookmark(int aId, String aTitle, String aUrl, String aKeyword) {
id = aId;
title = aTitle;
url = aUrl;
keyword = aKeyword;
}
}
/**
* This text watcher to enable or disable the OK button if the dialog contains
* valid information. This class is overridden to do data checking diffferent fields.
* By itself, it always enables the button.
*
* Callers can also assing a paired partner to the TextWatcher, and callers will check
* that both are enabled before enabling the ok button.
*/
private class EditBookmarkTextWatcher implements TextWatcher {
// A stored reference to the dialog containing the text field being watched
protected AlertDialog mDialog;
// A stored text watcher to do the real verification of a field
protected EditBookmarkTextWatcher mPairedTextWatcher;
// Whether or not the ok button should be enabled.
protected boolean mEnabled = true;
public EditBookmarkTextWatcher(AlertDialog aDialog) {
mDialog = aDialog;
}
public void setPairedTextWatcher(EditBookmarkTextWatcher aTextWatcher) {
mPairedTextWatcher = aTextWatcher;
}
public boolean isEnabled() {
return mEnabled;
}
// Textwatcher interface
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Disable if the we're disabled or the paired partner is disabled
boolean enabled = mEnabled && (mPairedTextWatcher == null || mPairedTextWatcher.isEnabled());
mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled);
}
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
}
/**
* A version of the EditBookmarkTextWatcher for the url field of the dialog.
* Only checks if the field is empty or not.
*/
private class LocationTextWatcher extends EditBookmarkTextWatcher {
public LocationTextWatcher(AlertDialog aDialog) {
super(aDialog);
}
// Disables the ok button if the location field is empty.
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mEnabled = (s.toString().trim().length() > 0);
super.onTextChanged(s, start, before, count);
}
}
/**
* A version of the EditBookmarkTextWatcher for the keyword field of the dialog.
* Checks if the field has any (non leading or trailing) spaces.
*/
private class KeywordTextWatcher extends EditBookmarkTextWatcher {
public KeywordTextWatcher(AlertDialog aDialog) {
super(aDialog);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Disable if the keyword contains spaces
mEnabled = (s.toString().trim().indexOf(' ') == -1);
super.onTextChanged(s, start, before, count);
}
}
/**
* Show the Edit bookmark dialog for a particular url. If the url is bookmarked multiple times
* this will just edit the first instance it finds.
*
* @param url The url of the bookmark to edit. The dialog will look up other information like the id,
* current title, or keywords associated with this url. If the url isn't bookmarked, the
* dialog will fail silently. If the url is bookmarked multiple times, this will only show
* information about the first it finds.
*/
public void show(final String url) {
(new UiAsyncTask<Void, Void, Bookmark>(ThreadUtils.getBackgroundHandler()) {
@Override
public Bookmark doInBackground(Void... params) {
Cursor cursor = BrowserDB.getBookmarkForUrl(mContext.getContentResolver(), url);
if (cursor == null) {
return null;
}
cursor.moveToFirst();
Bookmark bookmark = new Bookmark(cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID)),
cursor.getString(cursor.getColumnIndexOrThrow(Bookmarks.TITLE)),
cursor.getString(cursor.getColumnIndexOrThrow(Bookmarks.URL)),
cursor.getString(cursor.getColumnIndexOrThrow(Bookmarks.KEYWORD)));
cursor.close();
return bookmark;
}
@Override
public void onPostExecute(Bookmark bookmark) {
if (bookmark == null)
return;
show(bookmark.id, bookmark.title, bookmark.url, bookmark.keyword);
}
}).execute();
}
/**
* Show the Edit bookmark dialog for a set of data. This will show the dialog whether
* a bookmark with this url exists or not, but the results will NOT be saved if the id
* is not a valid bookmark id.
*
* @param id The id of the bookmark to change. If there is no bookmark with this ID, the dialog
* will fail silently.
* @param title The initial title to show in the dialog
* @param url The initial url to show in the dialog
* @param keyword The initial keyword to show in the dialog
*/
public void show(final int id, final String title, final String url, final String keyword) {
AlertDialog.Builder editPrompt = new AlertDialog.Builder(mContext);
final View editView = LayoutInflater.from(mContext).inflate(R.layout.bookmark_edit, null);
editPrompt.setTitle(R.string.bookmark_edit_title);
editPrompt.setView(editView);
final EditText nameText = ((EditText) editView.findViewById(R.id.edit_bookmark_name));
final EditText locationText = ((EditText) editView.findViewById(R.id.edit_bookmark_location));
final EditText keywordText = ((EditText) editView.findViewById(R.id.edit_bookmark_keyword));
nameText.setText(title);
locationText.setText(url);
keywordText.setText(keyword);
editPrompt.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
(new UiAsyncTask<Void, Void, Void>(ThreadUtils.getBackgroundHandler()) {
@Override
public Void doInBackground(Void... params) {
String newUrl = locationText.getText().toString().trim();
String newKeyword = keywordText.getText().toString().trim();
BrowserDB.updateBookmark(mContext.getContentResolver(), id, newUrl, nameText.getText().toString(), newKeyword);
return null;
}
@Override
public void onPostExecute(Void result) {
Toast.makeText(mContext, R.string.bookmark_updated, Toast.LENGTH_SHORT).show();
}
}).execute();
}
});
editPrompt.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// do nothing
}
});
final AlertDialog dialog = editPrompt.create();
// Create our TextWatchers
LocationTextWatcher locationTextWatcher = new LocationTextWatcher(dialog);
KeywordTextWatcher keywordTextWatcher = new KeywordTextWatcher(dialog);
// Cross reference the TextWatchers
locationTextWatcher.setPairedTextWatcher(keywordTextWatcher);
keywordTextWatcher.setPairedTextWatcher(locationTextWatcher);
// Add the TextWatcher Listeners
locationText.addTextChangedListener(locationTextWatcher);
keywordText.addTextChangedListener(keywordTextWatcher);
dialog.show();
}
}

View File

@ -82,7 +82,6 @@ FENNEC_JAVA_FILES = \
Distribution.java \
DoorHanger.java \
DoorHangerPopup.java \
EditBookmarkDialog.java \
Favicons.java \
FilePickerResultHandler.java \
FilePickerResultHandlerSync.java \
@ -229,7 +228,6 @@ FENNEC_JAVA_FILES = \
widget/AboutHomeView.java \
widget/AboutHomeSection.java \
widget/AddonsSection.java \
widget/ButtonToast.java \
widget/DateTimePicker.java \
widget/Divider.java \
widget/FaviconView.java \
@ -636,11 +634,6 @@ RES_DRAWABLE_MDPI = \
res/drawable-mdpi/tab_thumbnail_shadow.png \
res/drawable-mdpi/tabs_count.png \
res/drawable-mdpi/tabs_count_foreground.png \
res/drawable-mdpi/toast.9.png \
res/drawable-mdpi/toast_button_focused.9.png \
res/drawable-mdpi/toast_button_focused.9.png \
res/drawable-mdpi/toast_button_pressed.9.png \
res/drawable-mdpi/toast_divider.9.png \
res/drawable-mdpi/address_bar_url_default.9.png \
res/drawable-mdpi/address_bar_url_default_pb.9.png \
res/drawable-mdpi/address_bar_url_pressed.9.png \
@ -1067,7 +1060,6 @@ MOZ_ANDROID_DRAWABLES += \
mobile/android/base/resources/drawable/tab_thumbnail.xml \
mobile/android/base/resources/drawable/tabs_panel_indicator.xml \
mobile/android/base/resources/drawable/textbox_bg.xml \
mobile/android/base/resources/drawable/toast_button.xml \
mobile/android/base/resources/drawable/webapp_titlebar_bg.xml \
$(NULL)

View File

@ -112,8 +112,6 @@ public class BrowserDB {
public void unpinAllSites(ContentResolver cr);
public Cursor getPinnedSites(ContentResolver cr, int limit);
public Cursor getBookmarkForUrl(ContentResolver cr, String url);
}
static {
@ -291,10 +289,6 @@ public class BrowserDB {
return sDb.getPinnedSites(cr, limit);
}
public static Cursor getBookmarkForUrl(ContentResolver cr, String url) {
return sDb.getBookmarkForUrl(cr, url);
}
public static class PinnedSite {
public String title = "";
public String url = "";

View File

@ -1222,23 +1222,4 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
return (count > 0);
}
public Cursor getBookmarkForUrl(ContentResolver cr, String url) {
Cursor c = cr.query(bookmarksUriWithLimit(1),
new String[] { Bookmarks._ID,
Bookmarks.URL,
Bookmarks.TITLE,
Bookmarks.KEYWORD },
Bookmarks.URL + " = ?",
new String[] { url },
null);
if (c == null) {
return c;
} else if (c.getCount() == 0) {
c.close();
c = null;
}
return c;
}
}

View File

@ -42,7 +42,6 @@
<!ENTITY bookmark_added "Bookmark added">
<!ENTITY bookmark_removed "Bookmark removed">
<!ENTITY bookmark_updated "Bookmark updated">
<!ENTITY bookmark_options "Options">
<!ENTITY history_today_section "Today">
<!ENTITY history_yesterday_section "Yesterday">

Binary file not shown.

Before

Width:  |  Height:  |  Size: 839 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 311 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 311 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 963 B

View File

@ -1,20 +0,0 @@
<!--
Copyright 2012 Roman Nurik
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/toast_button_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/toast_button_focused" android:state_focused="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>

View File

@ -40,15 +40,4 @@
android:layout_height="@dimen/browser_toolbar_height"/>
</view>
<LinearLayout android:id="@+id/toast"
style="@style/Toast">
<TextView android:id="@+id/toast_message"
style="@style/ToastMessage" />
<Button android:id="@+id/toast_button"
style="@style/ToastButton" />
</LinearLayout>
</RelativeLayout>

View File

@ -447,52 +447,4 @@
<item name="android:ellipsize">middle</item>
</style>
<style name="Toast">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_alignParentBottom">true</item>
<item name="android:layout_gravity">bottom</item>
<item name="android:layout_marginLeft">8dp</item>
<item name="android:layout_marginRight">8dp</item>
<item name="android:layout_marginBottom">64dp</item>
<item name="android:layout_marginTop">0dp</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">@drawable/toast</item>
<item name="android:clickable">true</item>
<item name="android:showDividers">middle</item>
<item name="android:divider">@drawable/toast_divider</item>
<item name="android:dividerPadding">16dp</item>
<item name="android:paddingTop">0dp</item>
<item name="android:paddingBottom">0dp</item>
<item name="android:paddingLeft">0dp</item>
<item name="android:paddingRight">0dp</item>
</style>
<style name="ToastMessage">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:textColor">#fff</item>
<item name="android:textAppearance">?android:textAppearanceSmall</item>
<item name="android:paddingTop">0dp</item>
<item name="android:paddingBottom">0dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">0dp</item>
<item name="android:layout_marginTop">0dp</item>
<item name="android:layout_marginBottom">0dp</item>
<item name="android:layout_marginLeft">8dp</item>
<item name="android:layout_marginRight">0dp</item>
</style>
<style name="ToastButton">
<item name="android:background">@drawable/toast_button</item>
<item name="android:textAppearance">?android:textAppearanceSmall</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#fff</item>
<item name="android:layout_gravity">center_vertical</item>
</style>
</resources>

View File

@ -57,7 +57,6 @@
<string name="bookmark_added">&bookmark_added;</string>
<string name="bookmark_removed">&bookmark_removed;</string>
<string name="bookmark_updated">&bookmark_updated;</string>
<string name="bookmark_options">&bookmark_options;</string>
<string name="history_today_section">&history_today_section;</string>
<string name="history_yesterday_section">&history_yesterday_section;</string>

View File

@ -1,150 +0,0 @@
/*
* Copyright 2012 Roman Nurik
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mozilla.gecko.widget;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.widget.Button;
import android.widget.TextView;
import org.mozilla.gecko.R;
public class ButtonToast {
private final static int TOAST_DURATION = 5000;
private View mView;
private TextView mMessageView;
private Button mButton;
private ViewPropertyAnimator mBarAnimator;
private Handler mHideHandler = new Handler();
private ToastListener mListener;
// State objects
private CharSequence mToken;
private CharSequence mButtonMessage;
private int mButtonIcon;
private CharSequence mMessage;
public interface ToastListener {
void onButtonClicked(CharSequence token);
}
public ButtonToast(View view, ToastListener listener) {
mView = view;
mBarAnimator = mView.animate();
mListener = listener;
mMessageView = (TextView) mView.findViewById(R.id.toast_message);
mButton = (Button) mView.findViewById(R.id.toast_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hide(false);
mListener.onButtonClicked(mToken);
}
});
hide(true);
}
public void show(boolean immediate, CharSequence message,
CharSequence buttonMessage, int buttonIcon,
CharSequence token) {
mToken = token;
mMessage = message;
mButtonMessage = buttonMessage;
mMessageView.setText(mMessage);
mButton.setText(buttonMessage);
mButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, buttonIcon, 0);
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, TOAST_DURATION);
mView.setVisibility(View.VISIBLE);
if (immediate) {
mView.setAlpha(1);
} else {
mBarAnimator.cancel();
mBarAnimator
.alpha(1)
.setDuration(
mView.getResources()
.getInteger(android.R.integer.config_longAnimTime))
.setListener(null);
}
}
public void hide(boolean immediate) {
mHideHandler.removeCallbacks(mHideRunnable);
if (immediate) {
mView.setVisibility(View.GONE);
mView.setAlpha(0);
mMessage = null;
mToken = null;
} else {
mBarAnimator.cancel();
mBarAnimator
.alpha(0)
.setDuration(mView.getResources()
.getInteger(android.R.integer.config_shortAnimTime))
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mView.setVisibility(View.GONE);
mMessage = null;
mToken = null;
}
});
}
}
public void onSaveInstanceState(Bundle outState) {
outState.putCharSequence("toast_message", mMessage);
outState.putCharSequence("toast_button_message", mButtonMessage);
outState.putInt("toast_button_drawable", mButtonIcon);
outState.putCharSequence("toast_token", mToken);
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState != null) {
mMessage = savedInstanceState.getCharSequence("toast_message");
mButtonMessage = savedInstanceState.getCharSequence("toast_buttonmessage");
mButtonIcon = savedInstanceState.getInt("toast_button_drawable");
mToken = savedInstanceState.getCharSequence("toast_token");
if (mToken != null || !TextUtils.isEmpty(mMessage)) {
show(true, mMessage, mButtonMessage, mButtonIcon, mToken);
}
}
}
private Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
hide(false);
}
};
}