Bug 778247: mAppContext cleanup: AboutHomeContent is associated with only one activity. [r=mbrubeck]

--HG--
extra : rebase_source : 60aa53488d18f395e4e4b6d0023e357ffcb23ee4
This commit is contained in:
Sriram Ramasubramanian 2012-07-27 23:31:42 -07:00
parent 387b831795
commit c3b75f7817
2 changed files with 59 additions and 52 deletions

View File

@ -75,6 +75,8 @@ public class AboutHomeContent extends ScrollView
public static final EnumSet<UpdateFlags> ALL = EnumSet.allOf(UpdateFlags.class); public static final EnumSet<UpdateFlags> ALL = EnumSet.allOf(UpdateFlags.class);
} }
private Context mContext;
private BrowserApp mActivity;
private Cursor mCursor; private Cursor mCursor;
UriLoadCallback mUriLoadCallback = null; UriLoadCallback mUriLoadCallback = null;
private LayoutInflater mInflater; private LayoutInflater mInflater;
@ -98,34 +100,25 @@ public class AboutHomeContent extends ScrollView
public AboutHomeContent(Context context) { public AboutHomeContent(Context context) {
super(context); super(context);
mContext = context;
} }
public AboutHomeContent(Context context, AttributeSet attrs) { public AboutHomeContent(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
mContext = context;
mActivity = (BrowserApp) context;
} }
public void init() { public void init() {
final Context context = getContext(); mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.abouthome_content, this); mInflater.inflate(R.layout.abouthome_content, this);
mAccountManager = AccountManager.get(context); mAccountManager = AccountManager.get(mContext);
// The listener will run on the background thread (see 2nd argument) // The listener will run on the background thread (see 2nd argument)
mAccountManager.addOnAccountsUpdatedListener(mAccountListener = new OnAccountsUpdateListener() { mAccountManager.addOnAccountsUpdatedListener(mAccountListener = new OnAccountsUpdateListener() {
public void onAccountsUpdated(Account[] accounts) { public void onAccountsUpdated(Account[] accounts) {
final GeckoApp.StartupMode startupMode = GeckoApp.mAppContext.getStartupMode(); updateLayoutForSync();
final boolean syncIsSetup = SyncAccounts.syncAccountsExist(context);
GeckoApp.mAppContext.mMainHandler.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(startupMode, syncIsSetup);
}
});
} }
}, GeckoAppShell.getHandler(), false); }, GeckoAppShell.getHandler(), false);
@ -149,7 +142,7 @@ public class AboutHomeContent extends ScrollView
TextView allTopSitesText = (TextView) findViewById(R.id.all_top_sites_text); TextView allTopSitesText = (TextView) findViewById(R.id.all_top_sites_text);
allTopSitesText.setOnClickListener(new View.OnClickListener() { allTopSitesText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
GeckoApp.mAppContext.showAwesomebar(AwesomeBar.Target.CURRENT_TAB); mActivity.showAwesomebar(AwesomeBar.Target.CURRENT_TAB);
} }
}); });
@ -162,7 +155,7 @@ public class AboutHomeContent extends ScrollView
mRemoteTabs.setOnMoreTextClickListener(new View.OnClickListener() { mRemoteTabs.setOnMoreTextClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
GeckoApp.mAppContext.showRemoteTabs(); mActivity.showRemoteTabs();
} }
}); });
@ -273,6 +266,21 @@ public class AboutHomeContent extends ScrollView
updateSyncLayout(isFirstRun, hasTopSites); updateSyncLayout(isFirstRun, hasTopSites);
} }
private void updateLayoutForSync() {
final GeckoApp.StartupMode startupMode = mActivity.getStartupMode();
final boolean syncIsSetup = SyncAccounts.syncAccountsExist(mContext);
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(startupMode, syncIsSetup);
}
});
}
private int getNumberOfTopSites() { private int getNumberOfTopSites() {
Configuration config = getContext().getResources().getConfiguration(); Configuration config = getContext().getResources().getConfiguration();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) if (config.orientation == Configuration.ORIENTATION_LANDSCAPE)
@ -281,25 +289,25 @@ public class AboutHomeContent extends ScrollView
return NUMBER_OF_TOP_SITES_PORTRAIT; return NUMBER_OF_TOP_SITES_PORTRAIT;
} }
private void loadTopSites(final Activity activity) { private void loadTopSites() {
// Ensure we initialize GeckoApp's startup mode in // Ensure we initialize GeckoApp's startup mode in
// background thread before we use it when updating // background thread before we use it when updating
// the top sites section layout in main thread. // the top sites section layout in main thread.
final GeckoApp.StartupMode startupMode = GeckoApp.mAppContext.getStartupMode(); final GeckoApp.StartupMode startupMode = mActivity.getStartupMode();
// The SyncAccounts.syncAccountsExist method should not be called on // The SyncAccounts.syncAccountsExist method should not be called on
// UI thread as it touches disk to access a sqlite DB. // UI thread as it touches disk to access a sqlite DB.
final boolean syncIsSetup = SyncAccounts.syncAccountsExist(activity); final boolean syncIsSetup = SyncAccounts.syncAccountsExist(mActivity);
final ContentResolver resolver = GeckoApp.mAppContext.getContentResolver(); final ContentResolver resolver = mActivity.getContentResolver();
final Cursor oldCursor = mCursor; final Cursor oldCursor = mCursor;
// Swap in the new cursor. // Swap in the new cursor.
mCursor = BrowserDB.getTopSites(resolver, NUMBER_OF_TOP_SITES_PORTRAIT);; mCursor = BrowserDB.getTopSites(resolver, NUMBER_OF_TOP_SITES_PORTRAIT);;
GeckoApp.mAppContext.mMainHandler.post(new Runnable() { post(new Runnable() {
public void run() { public void run() {
if (mTopSitesAdapter == null) { if (mTopSitesAdapter == null) {
mTopSitesAdapter = new TopSitesCursorAdapter(activity, mTopSitesAdapter = new TopSitesCursorAdapter(mActivity,
R.layout.abouthome_topsite_item, R.layout.abouthome_topsite_item,
mCursor, mCursor,
new String[] { URLColumns.TITLE, new String[] { URLColumns.TITLE,
@ -321,20 +329,20 @@ public class AboutHomeContent extends ScrollView
}); });
} }
void update(final Activity activity, final EnumSet<UpdateFlags> flags) { void update(final EnumSet<UpdateFlags> flags) {
GeckoAppShell.getHandler().post(new Runnable() { GeckoAppShell.getHandler().post(new Runnable() {
public void run() { public void run() {
if (flags.contains(UpdateFlags.TOP_SITES)) if (flags.contains(UpdateFlags.TOP_SITES))
loadTopSites(activity); loadTopSites();
if (flags.contains(UpdateFlags.PREVIOUS_TABS)) if (flags.contains(UpdateFlags.PREVIOUS_TABS))
readLastTabs(activity); readLastTabs();
if (flags.contains(UpdateFlags.RECOMMENDED_ADDONS)) if (flags.contains(UpdateFlags.RECOMMENDED_ADDONS))
readRecommendedAddons(activity); readRecommendedAddons();
if (flags.contains(UpdateFlags.REMOTE_TABS)) if (flags.contains(UpdateFlags.REMOTE_TABS))
loadRemoteTabs(activity); loadRemoteTabs();
} }
}); });
} }
@ -343,8 +351,8 @@ public class AboutHomeContent extends ScrollView
mUriLoadCallback = uriLoadCallback; mUriLoadCallback = uriLoadCallback;
} }
public void onActivityContentChanged(Activity activity) { public void onActivityContentChanged() {
update(activity, EnumSet.of(UpdateFlags.TOP_SITES)); update(EnumSet.of(UpdateFlags.TOP_SITES));
} }
@Override @Override
@ -373,12 +381,12 @@ public class AboutHomeContent extends ScrollView
mOnInterceptTouchListener = listener; mOnInterceptTouchListener = listener;
} }
private String readFromZipFile(Activity activity, String filename) { private String readFromZipFile(String filename) {
ZipFile zip = null; ZipFile zip = null;
String str = null; String str = null;
try { try {
InputStream fileStream = null; InputStream fileStream = null;
File applicationPackage = new File(activity.getApplication().getPackageResourcePath()); File applicationPackage = new File(mActivity.getApplication().getPackageResourcePath());
zip = new ZipFile(applicationPackage); zip = new ZipFile(applicationPackage);
if (zip == null) if (zip == null)
return null; return null;
@ -444,14 +452,14 @@ public class AboutHomeContent extends ScrollView
return pageUrl; return pageUrl;
} }
private void readRecommendedAddons(final Activity activity) { private void readRecommendedAddons() {
final String addonsFilename = "recommended-addons.json"; final String addonsFilename = "recommended-addons.json";
String jsonString; String jsonString;
try { try {
jsonString = GeckoApp.mAppContext.getProfile().readFile(addonsFilename); jsonString = mActivity.getProfile().readFile(addonsFilename);
} catch (IOException ioe) { } catch (IOException ioe) {
Log.i(LOGTAG, "filestream is null"); Log.i(LOGTAG, "filestream is null");
jsonString = readFromZipFile(activity, addonsFilename); jsonString = readFromZipFile(addonsFilename);
} }
JSONArray addonsArray = null; JSONArray addonsArray = null;
@ -464,7 +472,7 @@ public class AboutHomeContent extends ScrollView
} }
final JSONArray array = addonsArray; final JSONArray array = addonsArray;
GeckoApp.mAppContext.mMainHandler.post(new Runnable() { post(new Runnable() {
public void run() { public void run() {
try { try {
if (array == null || array.length() == 0) { if (array == null || array.length() == 0) {
@ -490,7 +498,7 @@ public class AboutHomeContent extends ScrollView
} }
}); });
Favicons favicons = GeckoApp.mAppContext.getFavicons(); Favicons favicons = mActivity.getFavicons();
favicons.loadFavicon(pageUrl, iconUrl, favicons.loadFavicon(pageUrl, iconUrl,
new Favicons.OnFaviconLoadedListener() { new Favicons.OnFaviconLoadedListener() {
public void onFaviconLoaded(String url, Drawable favicon) { public void onFaviconLoaded(String url, Drawable favicon) {
@ -512,8 +520,8 @@ public class AboutHomeContent extends ScrollView
}); });
} }
private void readLastTabs(final Activity activity) { private void readLastTabs() {
String jsonString = GeckoApp.mAppContext.getProfile().readSessionFile(GeckoApp.sIsGeckoReady); String jsonString = mActivity.getProfile().readSessionFile(GeckoApp.sIsGeckoReady);
if (jsonString == null) { if (jsonString == null) {
// no previous session data // no previous session data
return; return;
@ -555,11 +563,11 @@ public class AboutHomeContent extends ScrollView
if (url.startsWith("about:")) if (url.startsWith("about:"))
continue; continue;
ContentResolver resolver = GeckoApp.mAppContext.getContentResolver(); ContentResolver resolver = mActivity.getContentResolver();
final BitmapDrawable favicon = BrowserDB.getFaviconForUrl(resolver, url); final BitmapDrawable favicon = BrowserDB.getFaviconForUrl(resolver, url);
lastTabUrlsList.add(url); lastTabUrlsList.add(url);
GeckoApp.mAppContext.mMainHandler.post(new Runnable() { post(new Runnable() {
public void run() { public void run() {
View container = mInflater.inflate(R.layout.abouthome_last_tabs_row, mLastTabs.getItemsContainer(), 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_title)).setText(title);
@ -569,7 +577,7 @@ public class AboutHomeContent extends ScrollView
container.setOnClickListener(new View.OnClickListener() { container.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
GeckoApp.mAppContext.loadUrlInTab(url); mActivity.loadUrlInTab(url);
} }
}); });
@ -579,14 +587,14 @@ public class AboutHomeContent extends ScrollView
} }
final int numLastTabs = lastTabUrlsList.size(); final int numLastTabs = lastTabUrlsList.size();
GeckoApp.mAppContext.mMainHandler.post(new Runnable() { post(new Runnable() {
public void run() { public void run() {
if (numLastTabs > 1) { if (numLastTabs > 1) {
mLastTabs.showMoreText(); mLastTabs.showMoreText();
mLastTabs.setOnMoreTextClickListener(new View.OnClickListener() { mLastTabs.setOnMoreTextClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
for (String url : lastTabUrlsList) for (String url : lastTabUrlsList)
GeckoApp.mAppContext.loadUrlInTab(url); mActivity.loadUrlInTab(url);
} }
}); });
mLastTabs.show(); mLastTabs.show();
@ -598,9 +606,9 @@ public class AboutHomeContent extends ScrollView
}); });
} }
private void loadRemoteTabs(final Activity activity) { private void loadRemoteTabs() {
if (!SyncAccounts.syncAccountsExist(activity)) { if (!SyncAccounts.syncAccountsExist(mActivity)) {
GeckoApp.mAppContext.mMainHandler.post(new Runnable() { post(new Runnable() {
public void run() { public void run() {
mRemoteTabs.hide(); mRemoteTabs.hide();
} }

View File

@ -206,7 +206,7 @@ abstract public class BrowserApp extends GeckoApp
public void onContentChanged() { public void onContentChanged() {
super.onContentChanged(); super.onContentChanged();
if (mAboutHomeContent != null) if (mAboutHomeContent != null)
mAboutHomeContent.onActivityContentChanged(this); mAboutHomeContent.onActivityContentChanged();
} }
@Override @Override
@ -506,7 +506,7 @@ abstract public class BrowserApp extends GeckoApp
if (mAboutHomeContent == null) if (mAboutHomeContent == null)
return; return;
mAboutHomeContent.update(this, EnumSet.of(AboutHomeContent.UpdateFlags.TOP_SITES)); mAboutHomeContent.update(EnumSet.of(AboutHomeContent.UpdateFlags.TOP_SITES));
} }
public void showAboutHome() { public void showAboutHome() {
@ -531,7 +531,7 @@ abstract public class BrowserApp extends GeckoApp
if (mAboutHomeContent == null) { if (mAboutHomeContent == null) {
mAboutHomeContent = (AboutHomeContent) findViewById(R.id.abouthome_content); mAboutHomeContent = (AboutHomeContent) findViewById(R.id.abouthome_content);
mAboutHomeContent.init(); mAboutHomeContent.init();
mAboutHomeContent.update(mAppContext, AboutHomeContent.UpdateFlags.ALL); mAboutHomeContent.update(AboutHomeContent.UpdateFlags.ALL);
mAboutHomeContent.setUriLoadCallback(new AboutHomeContent.UriLoadCallback() { mAboutHomeContent.setUriLoadCallback(new AboutHomeContent.UriLoadCallback() {
public void callback(String url) { public void callback(String url) {
mBrowserToolbar.setProgressVisibility(true); mBrowserToolbar.setProgressVisibility(true);
@ -540,8 +540,7 @@ abstract public class BrowserApp extends GeckoApp
}); });
mAboutHomeContent.setOnInterceptTouchListener(new ContentTouchListener()); mAboutHomeContent.setOnInterceptTouchListener(new ContentTouchListener());
} else { } else {
mAboutHomeContent.update(mAppContext, mAboutHomeContent.update(EnumSet.of(AboutHomeContent.UpdateFlags.TOP_SITES,
EnumSet.of(AboutHomeContent.UpdateFlags.TOP_SITES,
AboutHomeContent.UpdateFlags.REMOTE_TABS)); AboutHomeContent.UpdateFlags.REMOTE_TABS));
} }