mobile/android/base/RemoteTabsContainer.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial