|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko; |
|
6 |
|
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; |
|
11 |
|
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; |
|
17 |
|
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" }; |
|
25 |
|
26 private final Context context; |
|
27 private final RemoteTabsSyncObserver syncListener; |
|
28 private RemoteTabsList list; |
|
29 |
|
30 public RemoteTabsContainer(Context context, AttributeSet attrs) { |
|
31 super(context, attrs); |
|
32 this.context = context; |
|
33 this.syncListener = new RemoteTabsSyncObserver(); |
|
34 |
|
35 setOnRefreshListener(new RemoteTabsRefreshListener()); |
|
36 } |
|
37 |
|
38 @Override |
|
39 public void addView(View child, int index, ViewGroup.LayoutParams params) { |
|
40 super.addView(child, index, params); |
|
41 |
|
42 list = (RemoteTabsList) child; |
|
43 |
|
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 } |
|
48 |
|
49 |
|
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 } |
|
60 |
|
61 @Override |
|
62 public ViewGroup getLayout() { |
|
63 return this; |
|
64 } |
|
65 |
|
66 @Override |
|
67 public void setTabsPanel(TabsPanel panel) { |
|
68 list.setTabsPanel(panel); |
|
69 } |
|
70 |
|
71 @Override |
|
72 public void show() { |
|
73 setVisibility(VISIBLE); |
|
74 TabsAccessor.getTabs(context, list); |
|
75 FirefoxAccounts.addSyncStatusListener(syncListener); |
|
76 } |
|
77 |
|
78 @Override |
|
79 public void hide() { |
|
80 setVisibility(GONE); |
|
81 FirefoxAccounts.removeSyncStatusListener(syncListener); |
|
82 } |
|
83 |
|
84 @Override |
|
85 public boolean shouldExpand() { |
|
86 return true; |
|
87 } |
|
88 |
|
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 } |
|
98 |
|
99 private class RemoteTabsSyncObserver implements FirefoxAccounts.SyncStatusListener { |
|
100 @Override |
|
101 public Context getContext() { |
|
102 return RemoteTabsContainer.this.getContext(); |
|
103 } |
|
104 |
|
105 @Override |
|
106 public Account getAccount() { |
|
107 return FirefoxAccounts.getFirefoxAccount(getContext()); |
|
108 } |
|
109 |
|
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 } |
|
119 |
|
120 public void onSyncStarted() {} |
|
121 } |
|
122 } |