1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
|
/* -*- 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;
import org.mozilla.gecko.db.BrowserContract.Thumbnails;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
import org.mozilla.gecko.db.BrowserDB.PinnedSite;
import org.mozilla.gecko.db.BrowserDB.TopSitesCursorWrapper;
import org.mozilla.gecko.sync.setup.SyncAccounts;
import org.mozilla.gecko.util.ActivityResultHandler;
import org.mozilla.gecko.util.GeckoAsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.PathShape;
import android.graphics.Path;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class AboutHomeContent extends ScrollView
implements TabsAccessor.OnQueryTabsCompleteListener,
LightweightTheme.OnChangeListener {
private static final String LOGTAG = "GeckoAboutHome";
private static final int NUMBER_OF_REMOTE_TABS = 5;
private static int mNumberOfTopSites;
private static int mNumberOfCols;
private Map<String, Bitmap> mPendingThumbnails;
public static enum UnpinFlags {
REMOVE_PIN,
REMOVE_HISTORY
}
static enum UpdateFlags {
TOP_SITES,
PREVIOUS_TABS,
RECOMMENDED_ADDONS,
REMOTE_TABS;
public static final EnumSet<UpdateFlags> ALL = EnumSet.allOf(UpdateFlags.class);
}
private Context mContext;
private BrowserApp mActivity;
UriLoadCallback mUriLoadCallback = null;
VoidCallback mLoadCompleteCallback = null;
private LayoutInflater mInflater;
private ContentObserver mTabsContentObserver = null;
protected TopSitesCursorAdapter mTopSitesAdapter;
protected TopSitesGridView mTopSitesGrid;
private AboutHomePromoBox mPromoBox;
protected AboutHomeSection mAddons;
protected AboutHomeSection mLastTabs;
protected AboutHomeSection mRemoteTabs;
private View.OnClickListener mRemoteTabClickListener;
private static Rect sIconBounds;
private static TextAppearanceSpan sSubTitleSpan;
private static Drawable sPinDrawable = null;
public interface UriLoadCallback {
public void callback(String uriSpec);
}
public interface VoidCallback {
public void callback();
}
public AboutHomeContent(Context context) {
super(context);
mContext = context;
mActivity = (BrowserApp) context;
}
public AboutHomeContent(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mActivity = (BrowserApp) context;
}
public void init() {
int iconSize = mContext.getResources().getDimensionPixelSize(R.dimen.abouthome_addon_icon_size);
sIconBounds = new Rect(0, 0, iconSize, iconSize);
sSubTitleSpan = new TextAppearanceSpan(mContext, R.style.AboutHome_TextAppearance_SubTitle);
inflate();
// Reload the mobile homepage on inbound tab syncs
// Because the tabs URI is coarse grained, this updates the
// remote tabs component on *every* tab change
// The observer will run on the background thread (see constructor argument)
mTabsContentObserver = new ContentObserver(GeckoAppShell.getHandler()) {
public void onChange(boolean selfChange) {
update(EnumSet.of(AboutHomeContent.UpdateFlags.REMOTE_TABS));
}
};
mActivity.getContentResolver().registerContentObserver(BrowserContract.Tabs.CONTENT_URI,
false, mTabsContentObserver);
mRemoteTabClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int flags = Tabs.LOADURL_NEW_TAB;
if (Tabs.getInstance().getSelectedTab().isPrivate())
flags |= Tabs.LOADURL_PRIVATE;
Tabs.getInstance().loadUrl((String) v.getTag(), flags);
}
};
}
private void inflate() {
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.abouthome_content, this);
mTopSitesGrid = (TopSitesGridView)findViewById(R.id.top_sites_grid);
mTopSitesGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TopSitesViewHolder holder = (TopSitesViewHolder) v.getTag();
String spec = holder.getUrl();
// If we don't have a url, this must be an empty row. Show the edit dialog box
if (TextUtils.isEmpty(spec)) {
editSite(spec, position);
return;
}
if (mUriLoadCallback != null)
mUriLoadCallback.callback(spec);
}
});
mTopSitesGrid.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
mTopSitesGrid.setSelectedPosition(info.position);
MenuInflater inflater = mActivity.getMenuInflater();
inflater.inflate(R.menu.abouthome_topsites_contextmenu, menu);
// If nothing is pinned at all, hide both clear items
// We can assume that the adapter count and view count are the same in this case because our grid view
// force all items to be visible all the time
View view = mTopSitesGrid.getChildAt(info.position);
TopSitesViewHolder holder = (TopSitesViewHolder) view.getTag();
if (TextUtils.isEmpty(holder.getUrl())) {
menu.findItem(R.id.abouthome_topsites_pin).setVisible(false);
menu.findItem(R.id.abouthome_topsites_unpin).setVisible(false);
menu.findItem(R.id.abouthome_topsites_remove).setVisible(false);
} else if (holder.isPinned()) {
menu.findItem(R.id.abouthome_topsites_pin).setVisible(false);
} else {
menu.findItem(R.id.abouthome_topsites_unpin).setVisible(false);
}
}
});
mPromoBox = (AboutHomePromoBox) findViewById(R.id.promo_box);
mAddons = (AboutHomeSection) findViewById(R.id.recommended_addons);
mLastTabs = (AboutHomeSection) findViewById(R.id.last_tabs);
mRemoteTabs = (AboutHomeSection) findViewById(R.id.remote_tabs);
mAddons.setOnMoreTextClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mUriLoadCallback != null)
mUriLoadCallback.callback("https://addons.mozilla.org/android");
}
});
mRemoteTabs.setOnMoreTextClickListener(new View.OnClickListener() {
public void onClick(View v) {
mActivity.showRemoteTabs();
}
});
setTopSitesConstants();
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
mActivity.getLightweightTheme().addListener(this);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mActivity.getLightweightTheme().removeListener(this);
}
public void onDestroy() {
if (mTopSitesAdapter != null) {
Cursor cursor = mTopSitesAdapter.getCursor();
if (cursor != null && !cursor.isClosed())
cursor.close();
}
if (mTabsContentObserver != null) {
mActivity.getContentResolver().unregisterContentObserver(mTabsContentObserver);
mTabsContentObserver = null;
}
}
void setLastTabsVisibility(boolean visible) {
if (visible)
mLastTabs.show();
else
mLastTabs.hide();
}
private void setTopSitesVisibility(boolean hasTopSites) {
int visibility = hasTopSites ? View.VISIBLE : View.GONE;
findViewById(R.id.top_sites_title).setVisibility(visibility);
findViewById(R.id.top_sites_grid).setVisibility(visibility);
}
private void updateLayout() {
boolean hasTopSites = mTopSitesAdapter.getCount() > 0;
setTopSitesVisibility(hasTopSites);
mPromoBox.showRandomPromo();
}
private void updateLayoutForSync() {
final GeckoApp.StartupMode startupMode = mActivity.getStartupMode();
post(new Runnable() {
public void run() {
// The listener might run before the UI is initially updated.
// In this case, we should simply wait for the initial setup
// to happen.
if (mTopSitesAdapter != null)
updateLayout();
}
});
}
private void loadTopSites() {
final ContentResolver resolver = mActivity.getContentResolver();
Cursor old = null;
if (mTopSitesAdapter != null) {
old = mTopSitesAdapter.getCursor();
}
// Swap in the new cursor.
final Cursor oldCursor = old;
final Cursor newCursor = BrowserDB.getTopSites(resolver, mNumberOfTopSites);
post(new Runnable() {
public void run() {
if (mTopSitesAdapter == null) {
mTopSitesAdapter = new TopSitesCursorAdapter(mActivity,
R.layout.abouthome_topsite_item,
newCursor,
new String[] { URLColumns.TITLE },
new int[] { R.id.title });
mTopSitesGrid.setAdapter(mTopSitesAdapter);
} else {
mTopSitesAdapter.changeCursor(newCursor);
}
if (mTopSitesAdapter.getCount() > 0)
loadTopSitesThumbnails(resolver);
updateLayout();
// Free the old Cursor in the right thread now.
if (oldCursor != null && !oldCursor.isClosed())
oldCursor.close();
// Even if AboutHome isn't necessarily entirely loaded if we
// get here, for phones this is the part the user initially sees,
// so it's the one we will care about for now.
if (mLoadCompleteCallback != null)
mLoadCompleteCallback.callback();
}
});
}
private List<String> getTopSitesUrls() {
List<String> urls = new ArrayList<String>();
Cursor c = mTopSitesAdapter.getCursor();
if (c == null || !c.moveToFirst())
return urls;
do {
final String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
urls.add(url);
} while (c.moveToNext());
return urls;
}
private void displayThumbnail(View view, Bitmap thumbnail) {
ImageView thumbnailView = (ImageView) view.findViewById(R.id.thumbnail);
if (thumbnail == null) {
thumbnailView.setScaleType(ImageView.ScaleType.FIT_CENTER);
thumbnailView.setImageResource(R.drawable.abouthome_thumbnail_bg);
} else {
try {
thumbnailView.setScaleType(ImageView.ScaleType.CENTER_CROP);
thumbnailView.setImageBitmap(thumbnail);
} catch (OutOfMemoryError oom) {
Log.e(LOGTAG, "Unable to load thumbnail bitmap", oom);
thumbnailView.setScaleType(ImageView.ScaleType.FIT_CENTER);
thumbnailView.setImageResource(R.drawable.abouthome_thumbnail_bg);
}
}
}
private void updateTopSitesThumbnails(Map<String, Bitmap> thumbnails) {
for (int i = 0; i < mTopSitesAdapter.getCount(); i++) {
final View view = mTopSitesGrid.getChildAt(i);
// The grid view might get temporarily out of sync with the
// adapter refreshes (e.g. on device rotation)
if (view == null)
continue;
TopSitesViewHolder holder = (TopSitesViewHolder)view.getTag();
final String url = holder.getUrl();
if (TextUtils.isEmpty(url)) {
holder.thumbnailView.setScaleType(ImageView.ScaleType.FIT_CENTER);
holder.thumbnailView.setImageResource(R.drawable.abouthome_thumbnail_add);
} else {
displayThumbnail(view, thumbnails.get(url));
}
}
mTopSitesGrid.invalidate();
}
public Map<String, Bitmap> getThumbnailsFromCursor(Cursor c) {
Map<String, Bitmap> thumbnails = new HashMap<String, Bitmap>();
try {
if (c == null || !c.moveToFirst())
return thumbnails;
do {
final String url = c.getString(c.getColumnIndexOrThrow(Thumbnails.URL));
final byte[] b = c.getBlob(c.getColumnIndexOrThrow(Thumbnails.DATA));
if (b == null)
continue;
Bitmap thumbnail = BitmapFactory.decodeByteArray(b, 0, b.length);
if (thumbnail == null)
continue;
thumbnails.put(url, thumbnail);
} while (c.moveToNext());
} finally {
if (c != null)
c.close();
}
return thumbnails;
}
private void loadTopSitesThumbnails(final ContentResolver cr) {
final List<String> urls = getTopSitesUrls();
if (urls.size() == 0)
return;
(new GeckoAsyncTask<Void, Void, Map<String, Bitmap> >(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
@Override
public Map<String, Bitmap> doInBackground(Void... params) {
return getThumbnailsFromCursor(BrowserDB.getThumbnailsForUrls(cr, urls));
}
@Override
public void onPostExecute(Map<String, Bitmap> thumbnails) {
// If we're waiting for a layout to happen, the GridView may be
// stale, so store the pending thumbnails here. They will be
// shown on the next layout pass.
if (isLayoutRequested()) {
mPendingThumbnails = thumbnails;
} else {
updateTopSitesThumbnails(thumbnails);
}
}
}).execute();
}
void update(final EnumSet<UpdateFlags> flags) {
GeckoAppShell.getHandler().post(new Runnable() {
public void run() {
if (flags.contains(UpdateFlags.TOP_SITES))
loadTopSites();
if (flags.contains(UpdateFlags.PREVIOUS_TABS))
readLastTabs();
if (flags.contains(UpdateFlags.RECOMMENDED_ADDONS))
readRecommendedAddons();
if (flags.contains(UpdateFlags.REMOTE_TABS))
loadRemoteTabs();
}
});
}
public void setUriLoadCallback(UriLoadCallback uriLoadCallback) {
mUriLoadCallback = uriLoadCallback;
}
public void setLoadCompleteCallback(VoidCallback callback) {
mLoadCompleteCallback = callback;
}
public void onActivityContentChanged() {
update(EnumSet.of(UpdateFlags.TOP_SITES));
}
private void setTopSitesConstants() {
mNumberOfTopSites = getResources().getInteger(R.integer.number_of_top_sites);
mNumberOfCols = getResources().getInteger(R.integer.number_of_top_sites_cols);
}
/**
* Reinflates and updates all components of this view.
*/
public void refresh() {
if (mTopSitesAdapter != null)
mTopSitesAdapter.notifyDataSetChanged();
removeAllViews(); // We must remove the currently inflated view to allow for reinflation.
inflate();
mTopSitesGrid.setAdapter(mTopSitesAdapter); // mTopSitesGrid is a new instance (from loadTopSites()).
update(AboutHomeContent.UpdateFlags.ALL); // Refresh all elements.
}
private String readFromZipFile(String filename) {
ZipFile zip = null;
String str = null;
try {
InputStream fileStream = null;
File applicationPackage = new File(mActivity.getApplication().getPackageResourcePath());
zip = new ZipFile(applicationPackage);
if (zip == null)
return null;
ZipEntry fileEntry = zip.getEntry(filename);
if (fileEntry == null)
return null;
fileStream = zip.getInputStream(fileEntry);
str = readStringFromStream(fileStream);
} catch (IOException ioe) {
Log.e(LOGTAG, "error reading zip file: " + filename, ioe);
} finally {
try {
if (zip != null)
zip.close();
} catch (IOException ioe) {
// catch this here because we can continue even if the
// close failed
Log.e(LOGTAG, "error closing zip filestream", ioe);
}
}
return str;
}
private String readStringFromStream(InputStream fileStream) {
String str = null;
try {
byte[] buf = new byte[32768];
StringBuffer jsonString = new StringBuffer();
int read = 0;
while ((read = fileStream.read(buf, 0, 32768)) != -1)
jsonString.append(new String(buf, 0, read));
str = jsonString.toString();
} catch (IOException ioe) {
Log.i(LOGTAG, "error reading filestream", ioe);
} finally {
try {
if (fileStream != null)
fileStream.close();
} catch (IOException ioe) {
// catch this here because we can continue even if the
// close failed
Log.e(LOGTAG, "error closing filestream", ioe);
}
}
return str;
}
private String getPageUrlFromIconUrl(String iconUrl) {
// Addon icon URLs come with a query argument that is usually
// used for expiration purposes. We want the "page URL" here to be
// stable enough to avoid unnecessary duplicate records of the
// same addon.
String pageUrl = iconUrl;
try {
URL urlForIcon = new URL(iconUrl);
URL urlForPage = new URL(urlForIcon.getProtocol(), urlForIcon.getAuthority(), urlForIcon.getPath());
pageUrl = urlForPage.toString();
} catch (MalformedURLException e) {
// Defaults to pageUrl = iconUrl in case of error
}
return pageUrl;
}
private void readRecommendedAddons() {
final String addonsFilename = "recommended-addons.json";
String jsonString;
try {
jsonString = mActivity.getProfile().readFile(addonsFilename);
} catch (IOException ioe) {
Log.i(LOGTAG, "filestream is null");
jsonString = readFromZipFile(addonsFilename);
}
JSONArray addonsArray = null;
if (jsonString != null) {
try {
addonsArray = new JSONObject(jsonString).getJSONArray("addons");
} catch (JSONException e) {
Log.i(LOGTAG, "error reading json file", e);
}
}
final JSONArray array = addonsArray;
post(new Runnable() {
public void run() {
try {
if (array == null || array.length() == 0) {
mAddons.hide();
return;
}
for (int i = 0; i < array.length(); i++) {
JSONObject jsonobj = array.getJSONObject(i);
String name = jsonobj.getString("name");
String version = jsonobj.getString("version");
String text = name + " " + version;
SpannableString spannable = new SpannableString(text);
spannable.setSpan(sSubTitleSpan, name.length() + 1, text.length(), 0);
final TextView row = (TextView) mInflater.inflate(R.layout.abouthome_addon_row, mAddons.getItemsContainer(), false);
row.setText(spannable, TextView.BufferType.SPANNABLE);
Drawable drawable = mContext.getResources().getDrawable(R.drawable.ic_addons_empty);
drawable.setBounds(sIconBounds);
row.setCompoundDrawables(drawable, null, null, null);
String iconUrl = jsonobj.getString("iconURL");
String pageUrl = getPageUrlFromIconUrl(iconUrl);
final String homepageUrl = jsonobj.getString("homepageURL");
row.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mUriLoadCallback != null)
mUriLoadCallback.callback(homepageUrl);
}
});
Favicons favicons = Favicons.getInstance();
favicons.loadFavicon(pageUrl, iconUrl, true,
new Favicons.OnFaviconLoadedListener() {
public void onFaviconLoaded(String url, Bitmap favicon) {
if (favicon != null) {
Drawable drawable = new BitmapDrawable(favicon);
drawable.setBounds(sIconBounds);
row.setCompoundDrawables(drawable, null, null, null);
}
}
});
mAddons.addItem(row);
}
mAddons.show();
} catch (JSONException e) {
Log.i(LOGTAG, "error reading json file", e);
}
}
});
}
private void readLastTabs() {
String jsonString = mActivity.getProfile().readSessionFile(true);
if (jsonString == null) {
// no previous session data
return;
}
final ArrayList<String> lastTabUrlsList = new ArrayList<String>();
new SessionParser() {
@Override
public void onTabRead(final SessionTab tab) {
final String url = tab.getSelectedUrl();
// don't show last tabs for about:home
if (url.equals("about:home")) {
return;
}
ContentResolver resolver = mActivity.getContentResolver();
final Bitmap favicon = BrowserDB.getFaviconForUrl(resolver, url);
lastTabUrlsList.add(url);
AboutHomeContent.this.post(new Runnable() {
public void run() {
View container = mInflater.inflate(R.layout.abouthome_last_tabs_row, mLastTabs.getItemsContainer(), false);
((TextView) container.findViewById(R.id.last_tab_title)).setText(tab.getSelectedTitle());
((TextView) container.findViewById(R.id.last_tab_url)).setText(tab.getSelectedUrl());
if (favicon != null) {
((ImageView) container.findViewById(R.id.last_tab_favicon)).setImageBitmap(favicon);
}
container.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int flags = Tabs.LOADURL_NEW_TAB;
if (Tabs.getInstance().getSelectedTab().isPrivate())
flags |= Tabs.LOADURL_PRIVATE;
Tabs.getInstance().loadUrl(url, flags);
}
});
mLastTabs.addItem(container);
}
});
}
}.parse(jsonString);
final int numLastTabs = lastTabUrlsList.size();
if (numLastTabs >= 1) {
post(new Runnable() {
public void run() {
if (numLastTabs > 1) {
mLastTabs.showMoreText();
mLastTabs.setOnMoreTextClickListener(new View.OnClickListener() {
public void onClick(View v) {
int flags = Tabs.LOADURL_NEW_TAB;
if (Tabs.getInstance().getSelectedTab().isPrivate())
flags |= Tabs.LOADURL_PRIVATE;
for (String url : lastTabUrlsList) {
Tabs.getInstance().loadUrl(url, flags);
}
}
});
} else if (numLastTabs == 1) {
mLastTabs.hideMoreText();
}
mLastTabs.show();
}
});
}
}
private void loadRemoteTabs() {
if (!SyncAccounts.syncAccountsExist(mActivity)) {
post(new Runnable() {
public void run() {
mRemoteTabs.hide();
}
});
return;
}
TabsAccessor.getTabs(getContext(), NUMBER_OF_REMOTE_TABS, this);
}
@Override
public void onQueryTabsComplete(List<TabsAccessor.RemoteTab> tabsList) {
ArrayList<TabsAccessor.RemoteTab> tabs = new ArrayList<TabsAccessor.RemoteTab> (tabsList);
if (tabs == null || tabs.size() == 0) {
mRemoteTabs.hide();
return;
}
mRemoteTabs.clear();
String client = null;
for (TabsAccessor.RemoteTab tab : tabs) {
if (client == null)
client = tab.name;
else if (!TextUtils.equals(client, tab.name))
break;
final TextView row = (TextView) mInflater.inflate(R.layout.abouthome_remote_tab_row, mRemoteTabs.getItemsContainer(), false);
row.setText(TextUtils.isEmpty(tab.title) ? tab.url : tab.title);
row.setTag(tab.url);
mRemoteTabs.addItem(row);
row.setOnClickListener(mRemoteTabClickListener);
}
mRemoteTabs.setSubtitle(client);
mRemoteTabs.show();
}
@Override
public void onLightweightThemeChanged() {
LightweightThemeDrawable drawable = mActivity.getLightweightTheme().getColorDrawable(this);
if (drawable == null)
return;
drawable.setAlpha(255, 0);
setBackgroundDrawable(drawable);
boolean isLight = mActivity.getLightweightTheme().isLightTheme();
if (mAddons != null) {
mAddons.setTheme(isLight);
mLastTabs.setTheme(isLight);
mRemoteTabs.setTheme(isLight);
((GeckoImageView) findViewById(R.id.abouthome_logo)).setTheme(isLight);
((GeckoTextView) findViewById(R.id.top_sites_title)).setTheme(isLight);
}
}
@Override
public void onLightweightThemeReset() {
setBackgroundColor(getContext().getResources().getColor(R.color.background_normal));
if (mAddons != null) {
mAddons.resetTheme();
mLastTabs.resetTheme();
mRemoteTabs.resetTheme();
((GeckoImageView) findViewById(R.id.abouthome_logo)).resetTheme();
((GeckoTextView) findViewById(R.id.top_sites_title)).resetTheme();
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (mPendingThumbnails != null) {
updateTopSitesThumbnails(mPendingThumbnails);
mPendingThumbnails = null;
}
onLightweightThemeChanged();
}
public static class TopSitesGridView extends GridView {
int mSelected = -1;
public TopSitesGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public int getColumnWidth() {
return getColumnWidth(getWidth());
}
public int getColumnWidth(int width) {
// super.getColumnWidth() doesn't always return the correct value.
return (width - getPaddingLeft() - getPaddingRight()) / mNumberOfCols;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredWidth = View.MeasureSpec.getSize(widthMeasureSpec);
int numRows;
SimpleCursorAdapter adapter = (SimpleCursorAdapter) getAdapter();
int nSites = Integer.MAX_VALUE;
if (adapter != null) {
Cursor c = adapter.getCursor();
if (c != null)
nSites = c.getCount();
}
nSites = Math.min(nSites, mNumberOfTopSites);
numRows = (int) Math.round((double) nSites / mNumberOfCols);
setNumColumns(mNumberOfCols);
// Just using getWidth() will use incorrect values during onMeasure when rotating the device
// Instead we pass in the measuredWidth, which is correct
int w = getColumnWidth(measuredWidth);
ThumbnailHelper.getInstance().setThumbnailWidth(w);
heightMeasureSpec = MeasureSpec.makeMeasureSpec((int)(w*ThumbnailHelper.THUMBNAIL_ASPECT_RATIO*numRows) + getPaddingTop() + getPaddingBottom(),
MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void setSelectedPosition(int position) {
mSelected = position;
}
public int getSelectedPosition() {
return mSelected;
}
}
private class TopSitesViewHolder {
public TextView titleView = null;
public ImageView thumbnailView = null;
public ImageView pinnedView = null;
private String mTitle = null;
private String mUrl = null;
private boolean mIsPinned = false;
public TopSitesViewHolder(View v) {
titleView = (TextView) v.findViewById(R.id.title);
thumbnailView = (ImageView) v.findViewById(R.id.thumbnail);
pinnedView = (ImageView) v.findViewById(R.id.pinned);
}
public void setTitle(String title) {
if (mTitle != null && mTitle.equals(title))
return;
mTitle = title;
updateTitleView();
}
public String getTitle() {
return (!TextUtils.isEmpty(mTitle) ? mTitle : mUrl);
}
public void setUrl(String url) {
if (mUrl != null && mUrl.equals(url))
return;
mUrl = url;
updateTitleView();
}
public String getUrl() {
return mUrl;
}
public void updateTitleView() {
String title = getTitle();
if (!TextUtils.isEmpty(title)) {
titleView.setText(title);
titleView.setVisibility(View.VISIBLE);
} else {
titleView.setVisibility(View.INVISIBLE);
}
}
private Drawable getPinDrawable() {
if (sPinDrawable == null) {
int size = mContext.getResources().getDimensionPixelSize(R.dimen.abouthome_topsite_pinsize);
// Draw a little triangle in the upper right corner
Path path = new Path();
path.moveTo(0, 0);
path.lineTo(size, 0);
path.lineTo(size, size);
path.close();
sPinDrawable = new ShapeDrawable(new PathShape(path, size, size));
Paint p = ((ShapeDrawable) sPinDrawable).getPaint();
p.setColor(mContext.getResources().getColor(R.color.abouthome_topsite_pin));
}
return sPinDrawable;
}
public void setPinned(boolean aPinned) {
mIsPinned = aPinned;
pinnedView.setBackgroundDrawable(aPinned ? getPinDrawable() : null);
}
public boolean isPinned() {
return mIsPinned;
}
}
public class TopSitesCursorAdapter extends SimpleCursorAdapter {
public TopSitesCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public int getCount() {
return Math.min(super.getCount(), mNumberOfTopSites);
}
@Override
protected void onContentChanged () {
// Don't do anything. We don't want to regenerate every time
// our history database is updated.
return;
}
private View buildView(String url, String title, boolean pinned, View convertView) {
TopSitesViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.abouthome_topsite_item, null);
viewHolder = new TopSitesViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (TopSitesViewHolder) convertView.getTag();
}
viewHolder.setTitle(title);
viewHolder.setUrl(url);
viewHolder.setPinned(pinned);
// Force the view to fit inside this slot in the grid
convertView.setLayoutParams(new AbsListView.LayoutParams(mTopSitesGrid.getColumnWidth(),
Math.round(mTopSitesGrid.getColumnWidth()*ThumbnailHelper.THUMBNAIL_ASPECT_RATIO)));
return convertView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String url = "";
String title = "";
boolean pinned = false;
Cursor c = getCursor();
c.moveToPosition(position);
if (!c.isAfterLast()) {
url = c.getString(c.getColumnIndex(URLColumns.URL));
title = c.getString(c.getColumnIndex(URLColumns.TITLE));
pinned = ((TopSitesCursorWrapper)c).isPinned();
}
return buildView(url, title, pinned, convertView);
}
}
private void clearThumbnailsWithUrl(final String url) {
for (int i = 0; i < mTopSitesAdapter.getCount(); i++) {
final View view = mTopSitesGrid.getChildAt(i);
final TopSitesViewHolder holder = (TopSitesViewHolder) view.getTag();
if (holder.getUrl().equals(url)) {
clearThumbnail(holder);
}
}
}
private void clearThumbnail(TopSitesViewHolder holder) {
holder.setTitle("");
holder.setUrl("");
holder.thumbnailView.setScaleType(ImageView.ScaleType.FIT_CENTER);
holder.thumbnailView.setImageResource(R.drawable.abouthome_thumbnail_add);
holder.setPinned(false);
}
public void unpinSite(final UnpinFlags flags) {
final int position = mTopSitesGrid.getSelectedPosition();
final View v = mTopSitesGrid.getChildAt(position);
final TopSitesViewHolder holder = (TopSitesViewHolder) v.getTag();
final String url = holder.getUrl();
// Quickly update the view so that there isn't as much lag between the request and response
clearThumbnail(holder);
(new GeckoAsyncTask<Void, Void, Void>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
@Override
public Void doInBackground(Void... params) {
final ContentResolver resolver = mActivity.getContentResolver();
BrowserDB.unpinSite(resolver, position);
if (flags == UnpinFlags.REMOVE_HISTORY) {
BrowserDB.removeHistoryEntry(resolver, url);
}
return null;
}
}).execute();
}
public void pinSite() {
final int position = mTopSitesGrid.getSelectedPosition();
View v = mTopSitesGrid.getChildAt(position);
final TopSitesViewHolder holder = (TopSitesViewHolder) v.getTag();
holder.setPinned(true);
// update the database on a background thread
(new GeckoAsyncTask<Void, Void, Void>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
@Override
public Void doInBackground(Void... params) {
final ContentResolver resolver = mActivity.getContentResolver();
BrowserDB.pinSite(resolver, holder.getUrl(), holder.getTitle(), position);
return null;
}
}).execute();
}
public void editSite() {
int position = mTopSitesGrid.getSelectedPosition();
View v = mTopSitesGrid.getChildAt(position);
TopSitesViewHolder holder = (TopSitesViewHolder) v.getTag();
editSite(holder.getUrl(), position);
}
// Edit the site at position. Provide a url to start editing with
public void editSite(String url, final int position) {
Intent intent = new Intent(mContext, AwesomeBar.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra(AwesomeBar.TARGET_KEY, AwesomeBar.Target.PICK_SITE.toString());
if (url != null && !TextUtils.isEmpty(url)) {
intent.putExtra(AwesomeBar.CURRENT_URL_KEY, url);
}
int requestCode = GeckoAppShell.sActivityHelper.makeRequestCode(new ActivityResultHandler() {
public void onActivityResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_CANCELED || data == null)
return;
final View v = mTopSitesGrid.getChildAt(position);
final TopSitesViewHolder holder = (TopSitesViewHolder) v.getTag();
final String title = data.getStringExtra(AwesomeBar.TITLE_KEY);
final String url = data.getStringExtra(AwesomeBar.URL_KEY);
clearThumbnailsWithUrl(url);
holder.setUrl(url);
holder.setTitle(title);
holder.setPinned(true);
// update the database on a background thread
(new GeckoAsyncTask<Void, Void, Bitmap>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
@Override
public Bitmap doInBackground(Void... params) {
final ContentResolver resolver = mActivity.getContentResolver();
BrowserDB.pinSite(resolver, holder.getUrl(), holder.getTitle(), position);
List<String> urls = new ArrayList<String>();
urls.add(holder.getUrl());
Cursor c = BrowserDB.getThumbnailsForUrls(resolver, urls);
if (c == null || !c.moveToFirst()) {
return null;
}
final byte[] b = c.getBlob(c.getColumnIndexOrThrow(Thumbnails.DATA));
Bitmap bitmap = null;
if (b != null) {
bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
}
c.close();
return bitmap;
}
@Override
public void onPostExecute(Bitmap b) {
displayThumbnail(v, b);
}
}).execute();
}
});
mActivity.startActivityForResult(intent, requestCode);
}
}
|