From b3d3200781cbb909a2c2439c36a1da7438946a3a Mon Sep 17 00:00:00 2001 From: Sriram Ramasubramanian Date: Mon, 19 Mar 2012 16:15:05 -0700 Subject: [PATCH] Bug 735741: Factor out UI in about:home [r=mfinkle] --HG-- rename : mobile/android/base/resources/layout/abouthome_content.xml => mobile/android/base/resources/layout/abouthome_content.xml.in --- mobile/android/base/AboutHomeContent.java | 109 +++++++---------- mobile/android/base/AboutHomeSection.java | 103 ++++++++++++++++ mobile/android/base/Makefile.in | 16 ++- ...e_content.xml => abouthome_content.xml.in} | 114 ++++-------------- .../resources/layout/abouthome_section.xml | 37 ++++++ .../android/base/resources/values/attrs.xml | 10 ++ 6 files changed, 229 insertions(+), 160 deletions(-) create mode 100644 mobile/android/base/AboutHomeSection.java rename mobile/android/base/resources/layout/{abouthome_content.xml => abouthome_content.xml.in} (55%) create mode 100644 mobile/android/base/resources/layout/abouthome_section.xml create mode 100644 mobile/android/base/resources/values/attrs.xml diff --git a/mobile/android/base/AboutHomeContent.java b/mobile/android/base/AboutHomeContent.java index d762fcaec858..1bae5ff5a215 100644 --- a/mobile/android/base/AboutHomeContent.java +++ b/mobile/android/base/AboutHomeContent.java @@ -85,7 +85,6 @@ import android.widget.ArrayAdapter; import android.widget.FrameLayout; import android.widget.GridView; import android.widget.ImageView; -import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.ScrollView; @@ -124,9 +123,9 @@ public class AboutHomeContent extends ScrollView protected SimpleCursorAdapter mTopSitesAdapter; protected GridView mTopSitesGrid; - protected LinearLayout mAddonsLayout; - protected LinearLayout mLastTabsLayout; - protected LinearLayout mRemoteTabsLayout; + protected AboutHomeSection mAddons; + protected AboutHomeSection mLastTabs; + protected AboutHomeSection mRemoteTabs; private View.OnClickListener mRemoteTabClickListener; @@ -180,9 +179,9 @@ public class AboutHomeContent extends ScrollView } }); - mAddonsLayout = (LinearLayout) findViewById(R.id.recommended_addons); - mLastTabsLayout = (LinearLayout) findViewById(R.id.last_tabs); - mRemoteTabsLayout = (LinearLayout) findViewById(R.id.remote_tabs); + mAddons = (AboutHomeSection) findViewById(R.id.recommended_addons); + mLastTabs = (AboutHomeSection) findViewById(R.id.last_tabs); + mRemoteTabs = (AboutHomeSection) findViewById(R.id.remote_tabs); TextView allTopSitesText = (TextView) findViewById(R.id.all_top_sites_text); allTopSitesText.setOnClickListener(new View.OnClickListener() { @@ -191,16 +190,14 @@ public class AboutHomeContent extends ScrollView } }); - TextView allAddonsText = (TextView) findViewById(R.id.all_addons_text); - allAddonsText.setOnClickListener(new View.OnClickListener() { + mAddons.setOnMoreTextClickListener(new View.OnClickListener() { public void onClick(View v) { if (mUriLoadCallback != null) mUriLoadCallback.callback("https://addons.mozilla.org/android"); } }); - TextView allRemoteTabsText = (TextView) findViewById(R.id.all_remote_tabs_text); - allRemoteTabsText.setOnClickListener(new View.OnClickListener() { + mRemoteTabs.setOnMoreTextClickListener(new View.OnClickListener() { public void onClick(View v) { Context context = v.getContext(); context.startActivity(new Intent(context, RemoteTabs.class)); @@ -256,17 +253,10 @@ public class AboutHomeContent extends ScrollView } void setLastTabsVisibility(boolean visible) { - int visibility = visible ? View.VISIBLE : View.GONE; - findViewById(R.id.last_tabs_title).setVisibility(visibility); - findViewById(R.id.last_tabs).setVisibility(visibility); - findViewById(R.id.last_tabs_open_all).setVisibility(visibility); - } - - private void setAddonsVisibility(boolean visible) { - int visibility = visible ? View.VISIBLE : View.GONE; - findViewById(R.id.recommended_addons_title).setVisibility(visibility); - findViewById(R.id.recommended_addons).setVisibility(visibility); - findViewById(R.id.all_addons_text).setVisibility(visibility); + if (visible) + mLastTabs.show(); + else + mLastTabs.hide(); } private void setTopSitesVisibility(boolean visible, boolean hasTopSites) { @@ -280,14 +270,6 @@ public class AboutHomeContent extends ScrollView findViewById(R.id.no_top_sites_text).setVisibility(visibilityWithoutTopSites); } - private void setRemoteTabsVisibility(boolean visible) { - int visibility = visible ? View.VISIBLE : View.GONE; - findViewById(R.id.remote_tabs_title).setVisibility(visibility); - findViewById(R.id.remote_tabs_client).setVisibility(visibility); - findViewById(R.id.remote_tabs).setVisibility(visibility); - findViewById(R.id.all_remote_tabs_text).setVisibility(visibility); - } - private void setSyncVisibility(boolean visible) { int visibility = visible ? View.VISIBLE : View.GONE; findViewById(R.id.sync_box_container).setVisibility(visibility); @@ -518,14 +500,14 @@ public class AboutHomeContent extends ScrollView public void run() { try { if (array == null || array.length() == 0) { - setAddonsVisibility(false); + mAddons.hide(); return; } for (int i = 0; i < array.length(); i++) { JSONObject jsonobj = array.getJSONObject(i); - final View row = mInflater.inflate(R.layout.abouthome_addon_row, mAddonsLayout, false); + final View row = mInflater.inflate(R.layout.abouthome_addon_row, mAddons.getItemsContainer(), false); ((TextView) row.findViewById(R.id.addon_title)).setText(jsonobj.getString("name")); ((TextView) row.findViewById(R.id.addon_version)).setText(jsonobj.getString("version")); @@ -551,10 +533,10 @@ public class AboutHomeContent extends ScrollView } }); - mAddonsLayout.addView(row); + mAddons.addItem(row); } - setAddonsVisibility(true); + mAddons.show(); } catch (JSONException e) { Log.i(LOGTAG, "error reading json file", e); } @@ -611,7 +593,7 @@ public class AboutHomeContent extends ScrollView GeckoApp.mAppContext.mMainHandler.post(new Runnable() { public void run() { - View container = mInflater.inflate(R.layout.abouthome_last_tabs_row, mLastTabsLayout, false); + View container = mInflater.inflate(R.layout.abouthome_last_tabs_row, mLastTabs.getItemsContainer(), false); ((TextView) container.findViewById(R.id.last_tab_title)).setText(title); ((TextView) container.findViewById(R.id.last_tab_url)).setText(url); if (favicon != null) @@ -623,39 +605,38 @@ public class AboutHomeContent extends ScrollView } }); - mLastTabsLayout.addView(container); + mLastTabs.addItem(container); } }); } - int numLastTabs = lastTabUrlsList.size(); - if (numLastTabs > 0) { - GeckoApp.mAppContext.mMainHandler.post(new Runnable() { - public void run() { - findViewById(R.id.last_tabs_title).setVisibility(View.VISIBLE); + final int numLastTabs = lastTabUrlsList.size(); + GeckoApp.mAppContext.mMainHandler.post(new Runnable() { + public void run() { + if (numLastTabs > 1) { + mLastTabs.showMoreText(); + mLastTabs.setOnMoreTextClickListener(new View.OnClickListener() { + public void onClick(View v) { + for (String url : lastTabUrlsList) + GeckoApp.mAppContext.loadUrlInTab(url); + } + }); + mLastTabs.show(); + } else if (numLastTabs == 1) { + mLastTabs.hideMoreText(); + mLastTabs.show(); } - }); - - if (numLastTabs > 1) { - GeckoApp.mAppContext.mMainHandler.post(new Runnable() { - public void run() { - LinkTextView openAll = (LinkTextView) findViewById(R.id.last_tabs_open_all); - openAll.setVisibility(View.VISIBLE); - openAll.setOnClickListener(new LinkTextView.OnClickListener() { - public void onClick(View v) { - for (String url : lastTabUrlsList) - GeckoApp.mAppContext.loadUrlInTab(url); - } - }); - } - }); } - } + }); } private void loadRemoteTabs(final Activity activity) { if (!isSyncSetup()) { - setRemoteTabsVisibility(false); + GeckoApp.mAppContext.mMainHandler.post(new Runnable() { + public void run() { + mRemoteTabs.hide(); + } + }); return; } @@ -666,11 +647,11 @@ public class AboutHomeContent extends ScrollView public void onQueryTabsComplete(List tabsList) { ArrayList tabs = new ArrayList (tabsList); if (tabs == null || tabs.size() == 0) { - setRemoteTabsVisibility(false); + mRemoteTabs.hide(); return; } - mRemoteTabsLayout.removeAllViews(); + mRemoteTabs.clear(); String client = null; @@ -680,15 +661,15 @@ public class AboutHomeContent extends ScrollView else if (!TextUtils.equals(client, tab.name)) break; - final TextView row = (TextView) mInflater.inflate(R.layout.abouthome_remote_tab_row, mRemoteTabsLayout, false); + 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); - mRemoteTabsLayout.addView(row); + mRemoteTabs.addItem(row); row.setOnClickListener(mRemoteTabClickListener); } - ((TextView) findViewById(R.id.remote_tabs_client)).setText(client); - setRemoteTabsVisibility(true); + mRemoteTabs.setSubtitle(client); + mRemoteTabs.show(); } public static class TopSitesGridView extends GridView { diff --git a/mobile/android/base/AboutHomeSection.java b/mobile/android/base/AboutHomeSection.java new file mode 100644 index 000000000000..e21263913d03 --- /dev/null +++ b/mobile/android/base/AboutHomeSection.java @@ -0,0 +1,103 @@ +/* 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 android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.LinearLayout.LayoutParams; +import android.widget.TextView; +import android.text.TextUtils; + +public class AboutHomeSection extends LinearLayout { + private static final String LOGTAG = "GeckoAboutHomeSection"; + + private TextView mTitle; + private TextView mSubtitle; + private LinearLayout mItemsContainer; + private LinkTextView mMoreText; + + public AboutHomeSection(Context context, AttributeSet attrs) { + super(context, attrs); + + setOrientation(VERTICAL); + + LayoutInflater.from(context).inflate(R.layout.abouthome_section, this); + + mTitle = (TextView) this.findViewById(R.id.title); + mSubtitle = (TextView) this.findViewById(R.id.subtitle); + mItemsContainer = (LinearLayout) this.findViewById(R.id.items_container); + mMoreText = (LinkTextView) this.findViewById(R.id.more_text); + + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AboutHomeSection); + setTitle(a.getText(R.styleable.AboutHomeSection_title)); + setSubtitle(a.getText(R.styleable.AboutHomeSection_subtitle)); + setMoreText(a.getText(R.styleable.AboutHomeSection_more_text)); + a.recycle(); + } + + public LinearLayout getItemsContainer() { + return mItemsContainer; + } + + public void setTitle(CharSequence title) { + if (!TextUtils.isEmpty(title)) { + mTitle.setText(title); + mTitle.setVisibility(View.VISIBLE); + } else { + mTitle.setVisibility(View.GONE); + } + } + + public void setSubtitle(CharSequence subtitle) { + if (!TextUtils.isEmpty(subtitle)) { + mSubtitle.setText(subtitle); + mSubtitle.setVisibility(View.VISIBLE); + } else { + mSubtitle.setVisibility(View.GONE); + } + } + + public void setMoreText(CharSequence moreText) { + if (!TextUtils.isEmpty(moreText)) { + mMoreText.setText(moreText); + mMoreText.setVisibility(View.VISIBLE); + } else { + mMoreText.setVisibility(View.GONE); + } + } + + public void setOnMoreTextClickListener(View.OnClickListener listener) { + mMoreText.setOnClickListener(listener); + } + + public void addItem(View item) { + mItemsContainer.addView(item); + } + + public void clear() { + mItemsContainer.removeAllViews(); + } + + public void show() { + setVisibility(View.VISIBLE); + } + + public void hide() { + setVisibility(View.GONE); + } + + public void showMoreText() { + mMoreText.setVisibility(View.VISIBLE); + } + + public void hideMoreText() { + mMoreText.setVisibility(View.GONE); + } +} diff --git a/mobile/android/base/Makefile.in b/mobile/android/base/Makefile.in index 5d0bc4259fba..90f506afbff1 100644 --- a/mobile/android/base/Makefile.in +++ b/mobile/android/base/Makefile.in @@ -62,6 +62,7 @@ SYNC_PP_RES_XML=res/xml/sync_syncadapter.xml res/xml/sync_options.xml FENNEC_JAVA_FILES = \ AboutHomeContent.java \ + AboutHomeSection.java \ AlertNotification.java \ AwesomeBar.java \ AwesomeBarTabs.java \ @@ -169,6 +170,10 @@ FENNEC_PP_JAVA_FILES = \ SmsManager.java \ $(NULL) +FENNEC_PP_XML_FILES = \ + res/layout/abouthome_content.xml \ + $(NULL) + ifneq (,$(findstring -march=armv7,$(OS_CFLAGS))) MIN_CPU_VERSION=7 @@ -203,6 +208,7 @@ GARBAGE += \ gecko.ap_ \ res/values/strings.xml \ R.java \ + $(FENNEC_PP_XML_FILES) \ $(SYNC_PP_RES_XML) \ package-name.txt \ Manifest.java \ @@ -263,11 +269,11 @@ RES_LAYOUT = \ res/layout/tabs_tray.xml \ res/layout/list_item_header.xml \ res/layout/select_dialog_list.xml \ - res/layout/abouthome_content.xml \ - res/layout/abouthome_topsite_item.xml \ res/layout/abouthome_addon_row.xml \ res/layout/abouthome_last_tabs_row.xml \ + res/layout/abouthome_section.xml \ res/layout/abouthome_remote_tab_row.xml \ + res/layout/abouthome_topsite_item.xml \ $(NULL) RES_LAYOUT_V11 = \ @@ -280,6 +286,7 @@ RES_LAYOUT_LAND_V14 = \ RES_VALUES = \ $(SYNC_RES_VALUES) \ + res/values/attrs.xml \ res/values/arrays.xml \ res/values/colors.xml \ res/values/styles.xml \ @@ -624,7 +631,10 @@ classes.dex: $(FENNEC_JAVA_FILES) $(FENNEC_PP_JAVA_FILES) $(SYNC_JAVA_FILES) $(S $(JAVAC) $(JAVAC_FLAGS) -Xlint:all,-deprecation,-fallthrough -d classes -classpath classes $(addprefix $(srcdir)/,$(FENNEC_JAVA_FILES)) $(FENNEC_PP_JAVA_FILES) $(addprefix $(srcdir)/,$(SYNC_JAVA_FILES)) $(SYNC_PP_JAVA_FILES) R.java $(DX) --dex --output=$@ classes -PP_RES_XML=$(SYNC_PP_RES_XML) +PP_RES_XML= \ + $(SYNC_PP_RES_XML) \ + $(FENNEC_PP_XML_FILES) \ + $(NULL) # This is kinda awful; if any of the source files change, we remake them all. $(PP_RES_XML): $(patsubst res/%,$(srcdir)/resources/%.in,$(PP_RES_XML)) diff --git a/mobile/android/base/resources/layout/abouthome_content.xml b/mobile/android/base/resources/layout/abouthome_content.xml.in similarity index 55% rename from mobile/android/base/resources/layout/abouthome_content.xml rename to mobile/android/base/resources/layout/abouthome_content.xml.in index 37f73e405f18..99f180922842 100644 --- a/mobile/android/base/resources/layout/abouthome_content.xml +++ b/mobile/android/base/resources/layout/abouthome_content.xml.in @@ -1,5 +1,7 @@ +#filter substitution - + - + - + - - - - - - - - - - - - - - - + diff --git a/mobile/android/base/resources/layout/abouthome_section.xml b/mobile/android/base/resources/layout/abouthome_section.xml new file mode 100644 index 000000000000..c54565ce0387 --- /dev/null +++ b/mobile/android/base/resources/layout/abouthome_section.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + diff --git a/mobile/android/base/resources/values/attrs.xml b/mobile/android/base/resources/values/attrs.xml new file mode 100644 index 000000000000..48f86fa337e7 --- /dev/null +++ b/mobile/android/base/resources/values/attrs.xml @@ -0,0 +1,10 @@ + + + + + + + + + +