michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.mozilla.gecko.fxa.FirefoxAccounts; michael@0: import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; michael@0: import org.mozilla.gecko.util.ThreadUtils; michael@0: import org.mozilla.gecko.widget.GeckoSwipeRefreshLayout; michael@0: michael@0: import android.accounts.Account; michael@0: import android.content.Context; michael@0: import android.util.AttributeSet; michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: michael@0: /** michael@0: * Provides a container to wrap the list of synced tabs and provide swipe-to-refresh support. The michael@0: * only child view should be an instance of {@link RemoteTabsList}. michael@0: */ michael@0: public class RemoteTabsContainer extends GeckoSwipeRefreshLayout michael@0: implements TabsPanel.PanelView { michael@0: private static final String[] STAGES_TO_SYNC_ON_REFRESH = new String[] { "tabs" }; michael@0: michael@0: private final Context context; michael@0: private final RemoteTabsSyncObserver syncListener; michael@0: private RemoteTabsList list; michael@0: michael@0: public RemoteTabsContainer(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: this.context = context; michael@0: this.syncListener = new RemoteTabsSyncObserver(); michael@0: michael@0: setOnRefreshListener(new RemoteTabsRefreshListener()); michael@0: } michael@0: michael@0: @Override michael@0: public void addView(View child, int index, ViewGroup.LayoutParams params) { michael@0: super.addView(child, index, params); michael@0: michael@0: list = (RemoteTabsList) child; michael@0: michael@0: // Must be called after the child view has been added. michael@0: setColorScheme(R.color.swipe_refresh_orange1, R.color.swipe_refresh_orange2, michael@0: R.color.swipe_refresh_orange3, R.color.swipe_refresh_orange4); michael@0: } michael@0: michael@0: michael@0: @Override michael@0: public boolean canChildScrollUp() { michael@0: // We are not supporting swipe-to-refresh for old sync. This disables the swipe gesture if michael@0: // no FxA are detected. michael@0: if (FirefoxAccounts.firefoxAccountsExist(getContext())) { michael@0: return super.canChildScrollUp(); michael@0: } else { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public ViewGroup getLayout() { michael@0: return this; michael@0: } michael@0: michael@0: @Override michael@0: public void setTabsPanel(TabsPanel panel) { michael@0: list.setTabsPanel(panel); michael@0: } michael@0: michael@0: @Override michael@0: public void show() { michael@0: setVisibility(VISIBLE); michael@0: TabsAccessor.getTabs(context, list); michael@0: FirefoxAccounts.addSyncStatusListener(syncListener); michael@0: } michael@0: michael@0: @Override michael@0: public void hide() { michael@0: setVisibility(GONE); michael@0: FirefoxAccounts.removeSyncStatusListener(syncListener); michael@0: } michael@0: michael@0: @Override michael@0: public boolean shouldExpand() { michael@0: return true; michael@0: } michael@0: michael@0: private class RemoteTabsRefreshListener implements OnRefreshListener { michael@0: @Override michael@0: public void onRefresh() { michael@0: if (FirefoxAccounts.firefoxAccountsExist(getContext())) { michael@0: final Account account = FirefoxAccounts.getFirefoxAccount(getContext()); michael@0: FirefoxAccounts.requestSync(account, FirefoxAccounts.FORCE, STAGES_TO_SYNC_ON_REFRESH, null); michael@0: } michael@0: } michael@0: } michael@0: michael@0: private class RemoteTabsSyncObserver implements FirefoxAccounts.SyncStatusListener { michael@0: @Override michael@0: public Context getContext() { michael@0: return RemoteTabsContainer.this.getContext(); michael@0: } michael@0: michael@0: @Override michael@0: public Account getAccount() { michael@0: return FirefoxAccounts.getFirefoxAccount(getContext()); michael@0: } michael@0: michael@0: public void onSyncFinished() { michael@0: ThreadUtils.postToUiThread(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: TabsAccessor.getTabs(context, list); michael@0: setRefreshing(false); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: public void onSyncStarted() {} michael@0: } michael@0: }