mobile/android/base/RemoteTabsContainer.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/RemoteTabsContainer.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko;
     1.9 +
    1.10 +import org.mozilla.gecko.fxa.FirefoxAccounts;
    1.11 +import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
    1.12 +import org.mozilla.gecko.util.ThreadUtils;
    1.13 +import org.mozilla.gecko.widget.GeckoSwipeRefreshLayout;
    1.14 +
    1.15 +import android.accounts.Account;
    1.16 +import android.content.Context;
    1.17 +import android.util.AttributeSet;
    1.18 +import android.view.View;
    1.19 +import android.view.ViewGroup;
    1.20 +
    1.21 +/**
    1.22 + * Provides a container to wrap the list of synced tabs and provide swipe-to-refresh support. The
    1.23 + * only child view should be an instance of {@link RemoteTabsList}.
    1.24 + */
    1.25 +public class RemoteTabsContainer extends GeckoSwipeRefreshLayout
    1.26 +                                 implements TabsPanel.PanelView {
    1.27 +    private static final String[] STAGES_TO_SYNC_ON_REFRESH = new String[] { "tabs" };
    1.28 +
    1.29 +    private final Context context;
    1.30 +    private final RemoteTabsSyncObserver syncListener;
    1.31 +    private RemoteTabsList list;
    1.32 +
    1.33 +    public RemoteTabsContainer(Context context, AttributeSet attrs) {
    1.34 +        super(context, attrs);
    1.35 +        this.context = context;
    1.36 +        this.syncListener = new RemoteTabsSyncObserver();
    1.37 +
    1.38 +        setOnRefreshListener(new RemoteTabsRefreshListener());
    1.39 +    }
    1.40 +
    1.41 +    @Override
    1.42 +    public void addView(View child, int index, ViewGroup.LayoutParams params) {
    1.43 +        super.addView(child, index, params);
    1.44 +
    1.45 +        list = (RemoteTabsList) child;
    1.46 +
    1.47 +        // Must be called after the child view has been added.
    1.48 +        setColorScheme(R.color.swipe_refresh_orange1, R.color.swipe_refresh_orange2,
    1.49 +                       R.color.swipe_refresh_orange3, R.color.swipe_refresh_orange4);
    1.50 +    }
    1.51 +
    1.52 +
    1.53 +    @Override
    1.54 +    public boolean canChildScrollUp() {
    1.55 +        // We are not supporting swipe-to-refresh for old sync. This disables the swipe gesture if
    1.56 +        // no FxA are detected.
    1.57 +        if (FirefoxAccounts.firefoxAccountsExist(getContext())) {
    1.58 +            return super.canChildScrollUp();
    1.59 +        } else {
    1.60 +            return true;
    1.61 +        }
    1.62 +    }
    1.63 +
    1.64 +    @Override
    1.65 +    public ViewGroup getLayout() {
    1.66 +        return this;
    1.67 +    }
    1.68 +
    1.69 +    @Override
    1.70 +    public void setTabsPanel(TabsPanel panel) {
    1.71 +        list.setTabsPanel(panel);
    1.72 +    }
    1.73 +
    1.74 +    @Override
    1.75 +    public void show() {
    1.76 +        setVisibility(VISIBLE);
    1.77 +        TabsAccessor.getTabs(context, list);
    1.78 +        FirefoxAccounts.addSyncStatusListener(syncListener);
    1.79 +    }
    1.80 +
    1.81 +    @Override
    1.82 +    public void hide() {
    1.83 +        setVisibility(GONE);
    1.84 +        FirefoxAccounts.removeSyncStatusListener(syncListener);
    1.85 +    }
    1.86 +
    1.87 +    @Override
    1.88 +    public boolean shouldExpand() {
    1.89 +        return true;
    1.90 +    }
    1.91 +
    1.92 +    private class RemoteTabsRefreshListener implements OnRefreshListener {
    1.93 +        @Override
    1.94 +        public void onRefresh() {
    1.95 +            if (FirefoxAccounts.firefoxAccountsExist(getContext())) {
    1.96 +                final Account account = FirefoxAccounts.getFirefoxAccount(getContext());
    1.97 +                FirefoxAccounts.requestSync(account, FirefoxAccounts.FORCE, STAGES_TO_SYNC_ON_REFRESH, null);
    1.98 +            }
    1.99 +        }
   1.100 +    }
   1.101 +
   1.102 +    private class RemoteTabsSyncObserver implements FirefoxAccounts.SyncStatusListener {
   1.103 +        @Override
   1.104 +        public Context getContext() {
   1.105 +            return RemoteTabsContainer.this.getContext();
   1.106 +        }
   1.107 +
   1.108 +        @Override
   1.109 +        public Account getAccount() {
   1.110 +            return FirefoxAccounts.getFirefoxAccount(getContext());
   1.111 +        }
   1.112 +
   1.113 +        public void onSyncFinished() {
   1.114 +            ThreadUtils.postToUiThread(new Runnable() {
   1.115 +                @Override
   1.116 +                public void run() {
   1.117 +                    TabsAccessor.getTabs(context, list);
   1.118 +                    setRefreshing(false);
   1.119 +                }
   1.120 +            });
   1.121 +        }
   1.122 +
   1.123 +        public void onSyncStarted() {}
   1.124 +    }
   1.125 +}

mercurial