diff --git a/mobile/android/base/AboutHomeContent.java b/mobile/android/base/AboutHomeContent.java index 56ce60751f57..5898b4d4b24c 100644 --- a/mobile/android/base/AboutHomeContent.java +++ b/mobile/android/base/AboutHomeContent.java @@ -75,6 +75,8 @@ public class AboutHomeContent extends ScrollView public static final EnumSet ALL = EnumSet.allOf(UpdateFlags.class); } + private Context mContext; + private BrowserApp mActivity; private Cursor mCursor; UriLoadCallback mUriLoadCallback = null; private LayoutInflater mInflater; @@ -98,34 +100,25 @@ public class AboutHomeContent extends ScrollView public AboutHomeContent(Context context) { super(context); + mContext = context; } public AboutHomeContent(Context context, AttributeSet attrs) { super(context, attrs); + mContext = context; + mActivity = (BrowserApp) context; } public void init() { - final Context context = getContext(); - mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 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) mAccountManager.addOnAccountsUpdatedListener(mAccountListener = new OnAccountsUpdateListener() { public void onAccountsUpdated(Account[] accounts) { - final GeckoApp.StartupMode startupMode = GeckoApp.mAppContext.getStartupMode(); - 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); - } - }); + updateLayoutForSync(); } }, GeckoAppShell.getHandler(), false); @@ -149,7 +142,7 @@ public class AboutHomeContent extends ScrollView TextView allTopSitesText = (TextView) findViewById(R.id.all_top_sites_text); allTopSitesText.setOnClickListener(new View.OnClickListener() { 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() { public void onClick(View v) { - GeckoApp.mAppContext.showRemoteTabs(); + mActivity.showRemoteTabs(); } }); @@ -273,6 +266,21 @@ public class AboutHomeContent extends ScrollView 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() { Configuration config = getContext().getResources().getConfiguration(); if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) @@ -281,25 +289,25 @@ public class AboutHomeContent extends ScrollView return NUMBER_OF_TOP_SITES_PORTRAIT; } - private void loadTopSites(final Activity activity) { + private void loadTopSites() { // Ensure we initialize GeckoApp's startup mode in // background thread before we use it when updating // 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 // 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; // Swap in the new cursor. mCursor = BrowserDB.getTopSites(resolver, NUMBER_OF_TOP_SITES_PORTRAIT);; - GeckoApp.mAppContext.mMainHandler.post(new Runnable() { + post(new Runnable() { public void run() { if (mTopSitesAdapter == null) { - mTopSitesAdapter = new TopSitesCursorAdapter(activity, + mTopSitesAdapter = new TopSitesCursorAdapter(mActivity, R.layout.abouthome_topsite_item, mCursor, new String[] { URLColumns.TITLE, @@ -321,20 +329,20 @@ public class AboutHomeContent extends ScrollView }); } - void update(final Activity activity, final EnumSet flags) { + void update(final EnumSet flags) { GeckoAppShell.getHandler().post(new Runnable() { public void run() { if (flags.contains(UpdateFlags.TOP_SITES)) - loadTopSites(activity); + loadTopSites(); if (flags.contains(UpdateFlags.PREVIOUS_TABS)) - readLastTabs(activity); + readLastTabs(); if (flags.contains(UpdateFlags.RECOMMENDED_ADDONS)) - readRecommendedAddons(activity); + readRecommendedAddons(); if (flags.contains(UpdateFlags.REMOTE_TABS)) - loadRemoteTabs(activity); + loadRemoteTabs(); } }); } @@ -343,8 +351,8 @@ public class AboutHomeContent extends ScrollView mUriLoadCallback = uriLoadCallback; } - public void onActivityContentChanged(Activity activity) { - update(activity, EnumSet.of(UpdateFlags.TOP_SITES)); + public void onActivityContentChanged() { + update(EnumSet.of(UpdateFlags.TOP_SITES)); } @Override @@ -373,12 +381,12 @@ public class AboutHomeContent extends ScrollView mOnInterceptTouchListener = listener; } - private String readFromZipFile(Activity activity, String filename) { + private String readFromZipFile(String filename) { ZipFile zip = null; String str = null; try { InputStream fileStream = null; - File applicationPackage = new File(activity.getApplication().getPackageResourcePath()); + File applicationPackage = new File(mActivity.getApplication().getPackageResourcePath()); zip = new ZipFile(applicationPackage); if (zip == null) return null; @@ -444,14 +452,14 @@ public class AboutHomeContent extends ScrollView return pageUrl; } - private void readRecommendedAddons(final Activity activity) { + private void readRecommendedAddons() { final String addonsFilename = "recommended-addons.json"; String jsonString; try { - jsonString = GeckoApp.mAppContext.getProfile().readFile(addonsFilename); + jsonString = mActivity.getProfile().readFile(addonsFilename); } catch (IOException ioe) { Log.i(LOGTAG, "filestream is null"); - jsonString = readFromZipFile(activity, addonsFilename); + jsonString = readFromZipFile(addonsFilename); } JSONArray addonsArray = null; @@ -464,7 +472,7 @@ public class AboutHomeContent extends ScrollView } final JSONArray array = addonsArray; - GeckoApp.mAppContext.mMainHandler.post(new Runnable() { + post(new Runnable() { public void run() { try { 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, new Favicons.OnFaviconLoadedListener() { public void onFaviconLoaded(String url, Drawable favicon) { @@ -512,8 +520,8 @@ public class AboutHomeContent extends ScrollView }); } - private void readLastTabs(final Activity activity) { - String jsonString = GeckoApp.mAppContext.getProfile().readSessionFile(GeckoApp.sIsGeckoReady); + private void readLastTabs() { + String jsonString = mActivity.getProfile().readSessionFile(GeckoApp.sIsGeckoReady); if (jsonString == null) { // no previous session data return; @@ -555,11 +563,11 @@ public class AboutHomeContent extends ScrollView if (url.startsWith("about:")) continue; - ContentResolver resolver = GeckoApp.mAppContext.getContentResolver(); + ContentResolver resolver = mActivity.getContentResolver(); final BitmapDrawable favicon = BrowserDB.getFaviconForUrl(resolver, url); lastTabUrlsList.add(url); - GeckoApp.mAppContext.mMainHandler.post(new Runnable() { + 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(title); @@ -569,7 +577,7 @@ public class AboutHomeContent extends ScrollView container.setOnClickListener(new View.OnClickListener() { 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(); - GeckoApp.mAppContext.mMainHandler.post(new Runnable() { + 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); + mActivity.loadUrlInTab(url); } }); mLastTabs.show(); @@ -598,9 +606,9 @@ public class AboutHomeContent extends ScrollView }); } - private void loadRemoteTabs(final Activity activity) { - if (!SyncAccounts.syncAccountsExist(activity)) { - GeckoApp.mAppContext.mMainHandler.post(new Runnable() { + private void loadRemoteTabs() { + if (!SyncAccounts.syncAccountsExist(mActivity)) { + post(new Runnable() { public void run() { mRemoteTabs.hide(); } diff --git a/mobile/android/base/BrowserApp.java b/mobile/android/base/BrowserApp.java index e3a6d4eccb81..a19b75d7d1bf 100644 --- a/mobile/android/base/BrowserApp.java +++ b/mobile/android/base/BrowserApp.java @@ -206,7 +206,7 @@ abstract public class BrowserApp extends GeckoApp public void onContentChanged() { super.onContentChanged(); if (mAboutHomeContent != null) - mAboutHomeContent.onActivityContentChanged(this); + mAboutHomeContent.onActivityContentChanged(); } @Override @@ -506,7 +506,7 @@ abstract public class BrowserApp extends GeckoApp if (mAboutHomeContent == null) return; - mAboutHomeContent.update(this, EnumSet.of(AboutHomeContent.UpdateFlags.TOP_SITES)); + mAboutHomeContent.update(EnumSet.of(AboutHomeContent.UpdateFlags.TOP_SITES)); } public void showAboutHome() { @@ -531,7 +531,7 @@ abstract public class BrowserApp extends GeckoApp if (mAboutHomeContent == null) { mAboutHomeContent = (AboutHomeContent) findViewById(R.id.abouthome_content); mAboutHomeContent.init(); - mAboutHomeContent.update(mAppContext, AboutHomeContent.UpdateFlags.ALL); + mAboutHomeContent.update(AboutHomeContent.UpdateFlags.ALL); mAboutHomeContent.setUriLoadCallback(new AboutHomeContent.UriLoadCallback() { public void callback(String url) { mBrowserToolbar.setProgressVisibility(true); @@ -540,8 +540,7 @@ abstract public class BrowserApp extends GeckoApp }); mAboutHomeContent.setOnInterceptTouchListener(new ContentTouchListener()); } else { - mAboutHomeContent.update(mAppContext, - EnumSet.of(AboutHomeContent.UpdateFlags.TOP_SITES, + mAboutHomeContent.update(EnumSet.of(AboutHomeContent.UpdateFlags.TOP_SITES, AboutHomeContent.UpdateFlags.REMOTE_TABS)); }