Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.fxa.FirefoxAccounts; |
michael@0 | 8 | import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; |
michael@0 | 9 | import org.mozilla.gecko.util.ThreadUtils; |
michael@0 | 10 | import org.mozilla.gecko.widget.GeckoSwipeRefreshLayout; |
michael@0 | 11 | |
michael@0 | 12 | import android.accounts.Account; |
michael@0 | 13 | import android.content.Context; |
michael@0 | 14 | import android.util.AttributeSet; |
michael@0 | 15 | import android.view.View; |
michael@0 | 16 | import android.view.ViewGroup; |
michael@0 | 17 | |
michael@0 | 18 | /** |
michael@0 | 19 | * Provides a container to wrap the list of synced tabs and provide swipe-to-refresh support. The |
michael@0 | 20 | * only child view should be an instance of {@link RemoteTabsList}. |
michael@0 | 21 | */ |
michael@0 | 22 | public class RemoteTabsContainer extends GeckoSwipeRefreshLayout |
michael@0 | 23 | implements TabsPanel.PanelView { |
michael@0 | 24 | private static final String[] STAGES_TO_SYNC_ON_REFRESH = new String[] { "tabs" }; |
michael@0 | 25 | |
michael@0 | 26 | private final Context context; |
michael@0 | 27 | private final RemoteTabsSyncObserver syncListener; |
michael@0 | 28 | private RemoteTabsList list; |
michael@0 | 29 | |
michael@0 | 30 | public RemoteTabsContainer(Context context, AttributeSet attrs) { |
michael@0 | 31 | super(context, attrs); |
michael@0 | 32 | this.context = context; |
michael@0 | 33 | this.syncListener = new RemoteTabsSyncObserver(); |
michael@0 | 34 | |
michael@0 | 35 | setOnRefreshListener(new RemoteTabsRefreshListener()); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | @Override |
michael@0 | 39 | public void addView(View child, int index, ViewGroup.LayoutParams params) { |
michael@0 | 40 | super.addView(child, index, params); |
michael@0 | 41 | |
michael@0 | 42 | list = (RemoteTabsList) child; |
michael@0 | 43 | |
michael@0 | 44 | // Must be called after the child view has been added. |
michael@0 | 45 | setColorScheme(R.color.swipe_refresh_orange1, R.color.swipe_refresh_orange2, |
michael@0 | 46 | R.color.swipe_refresh_orange3, R.color.swipe_refresh_orange4); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | |
michael@0 | 50 | @Override |
michael@0 | 51 | public boolean canChildScrollUp() { |
michael@0 | 52 | // We are not supporting swipe-to-refresh for old sync. This disables the swipe gesture if |
michael@0 | 53 | // no FxA are detected. |
michael@0 | 54 | if (FirefoxAccounts.firefoxAccountsExist(getContext())) { |
michael@0 | 55 | return super.canChildScrollUp(); |
michael@0 | 56 | } else { |
michael@0 | 57 | return true; |
michael@0 | 58 | } |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | @Override |
michael@0 | 62 | public ViewGroup getLayout() { |
michael@0 | 63 | return this; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | @Override |
michael@0 | 67 | public void setTabsPanel(TabsPanel panel) { |
michael@0 | 68 | list.setTabsPanel(panel); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | @Override |
michael@0 | 72 | public void show() { |
michael@0 | 73 | setVisibility(VISIBLE); |
michael@0 | 74 | TabsAccessor.getTabs(context, list); |
michael@0 | 75 | FirefoxAccounts.addSyncStatusListener(syncListener); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | @Override |
michael@0 | 79 | public void hide() { |
michael@0 | 80 | setVisibility(GONE); |
michael@0 | 81 | FirefoxAccounts.removeSyncStatusListener(syncListener); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | @Override |
michael@0 | 85 | public boolean shouldExpand() { |
michael@0 | 86 | return true; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | private class RemoteTabsRefreshListener implements OnRefreshListener { |
michael@0 | 90 | @Override |
michael@0 | 91 | public void onRefresh() { |
michael@0 | 92 | if (FirefoxAccounts.firefoxAccountsExist(getContext())) { |
michael@0 | 93 | final Account account = FirefoxAccounts.getFirefoxAccount(getContext()); |
michael@0 | 94 | FirefoxAccounts.requestSync(account, FirefoxAccounts.FORCE, STAGES_TO_SYNC_ON_REFRESH, null); |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | private class RemoteTabsSyncObserver implements FirefoxAccounts.SyncStatusListener { |
michael@0 | 100 | @Override |
michael@0 | 101 | public Context getContext() { |
michael@0 | 102 | return RemoteTabsContainer.this.getContext(); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | @Override |
michael@0 | 106 | public Account getAccount() { |
michael@0 | 107 | return FirefoxAccounts.getFirefoxAccount(getContext()); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | public void onSyncFinished() { |
michael@0 | 111 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 112 | @Override |
michael@0 | 113 | public void run() { |
michael@0 | 114 | TabsAccessor.getTabs(context, list); |
michael@0 | 115 | setRefreshing(false); |
michael@0 | 116 | } |
michael@0 | 117 | }); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | public void onSyncStarted() {} |
michael@0 | 121 | } |
michael@0 | 122 | } |