OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.blimp.toolbar; | |
6 | |
7 import android.content.Context; | |
8 import android.graphics.Bitmap; | |
9 import android.text.TextUtils; | |
10 import android.util.AttributeSet; | |
11 import android.view.View; | |
12 import android.widget.ImageButton; | |
13 import android.widget.LinearLayout; | |
14 import android.widget.ProgressBar; | |
15 | |
16 import org.chromium.base.annotations.CalledByNative; | |
17 import org.chromium.base.annotations.JNINamespace; | |
18 import org.chromium.blimp.R; | |
19 import org.chromium.blimp.session.BlimpClientSession; | |
20 | |
21 /** | |
22 * A {@link View} that visually represents the Blimp toolbar, which lets users i
ssue navigation | |
23 * commands and displays relevant navigation UI. | |
24 */ | |
25 @JNINamespace("blimp::client") | |
26 public class Toolbar extends LinearLayout implements UrlBar.UrlBarObserver, View
.OnClickListener { | |
27 /** | |
28 * Delegate for the Toolbar. | |
29 */ | |
30 public interface ToolbarDelegate { | |
31 /** | |
32 * Resets the metrics. Used for displaying per navigation metrics. | |
33 */ | |
34 public void resetDebugStats(); | |
35 } | |
36 | |
37 private static final String TAG = "Toolbar"; | |
38 | |
39 private long mNativeToolbarPtr; | |
40 | |
41 private Context mContext; | |
42 private UrlBar mUrlBar; | |
43 private ToolbarMenu mToolbarMenu; | |
44 private ImageButton mReloadButton; | |
45 private ImageButton mMenuButton; | |
46 private ProgressBar mProgressBar; | |
47 private BlimpClientSession mBlimpClientSession; | |
48 private ToolbarDelegate mDelegate; | |
49 | |
50 /** | |
51 * A URL to load when this object is initialized. This handles the case whe
re there is a URL | |
52 * to load before native is ready to receive any URL. | |
53 * */ | |
54 private String mUrlToLoad; | |
55 | |
56 /** | |
57 * Builds a new {@link Toolbar}. | |
58 * @param context A {@link Context} instance. | |
59 * @param attrs An {@link AttributeSet} instance. | |
60 */ | |
61 public Toolbar(Context context, AttributeSet attrs) { | |
62 super(context, attrs); | |
63 mContext = context; | |
64 } | |
65 | |
66 /** | |
67 * @return the mToolbarMenu | |
68 */ | |
69 public ToolbarMenu getToolbarMenu() { | |
70 return mToolbarMenu; | |
71 } | |
72 | |
73 /** | |
74 * To be called when the native library is loaded so that this class can ini
tialize its native | |
75 * components. | |
76 * @param blimpClientSession The {@link BlimpClientSession} that contains th
e content-lite | |
77 * features required by the native components of t
he Toolbar. | |
78 * delegate The delegate for the Toolbar. | |
79 */ | |
80 public void initialize(BlimpClientSession blimpClientSession, ToolbarDelegat
e delegate) { | |
81 assert mNativeToolbarPtr == 0; | |
82 | |
83 mDelegate = delegate; | |
84 | |
85 mBlimpClientSession = blimpClientSession; | |
86 mNativeToolbarPtr = nativeInit(mBlimpClientSession); | |
87 sendUrlTextInternal(mUrlToLoad); | |
88 | |
89 mToolbarMenu = new ToolbarMenu(mContext, this); | |
90 mBlimpClientSession.addObserver(mToolbarMenu); | |
91 } | |
92 | |
93 /** | |
94 * To be called when this class should be torn down. This {@link View} shou
ld not be used after | |
95 * this. | |
96 */ | |
97 public void destroy() { | |
98 mBlimpClientSession.removeObserver(mToolbarMenu); | |
99 if (mNativeToolbarPtr != 0) { | |
100 nativeDestroy(mNativeToolbarPtr); | |
101 mNativeToolbarPtr = 0; | |
102 } | |
103 } | |
104 | |
105 /** | |
106 * Loads {@code text} as if it had been typed by the user. Useful for speci
fically loading | |
107 * startup URLs or testing. | |
108 * @param text The URL or text to load. | |
109 */ | |
110 public void loadUrl(String text) { | |
111 mUrlBar.setText(text); | |
112 mDelegate.resetDebugStats(); | |
113 sendUrlTextInternal(text); | |
114 } | |
115 | |
116 /** | |
117 * Returns the URL from the URL bar. | |
118 * @return Current URL | |
119 */ | |
120 public String getUrl() { | |
121 return mUrlBar.getText().toString(); | |
122 } | |
123 | |
124 /** | |
125 * To be called when the user triggers a back navigation action. | |
126 * @return Whether or not the back event was consumed. | |
127 */ | |
128 public boolean onBackPressed() { | |
129 if (mNativeToolbarPtr == 0) return false; | |
130 mDelegate.resetDebugStats(); | |
131 return nativeOnBackPressed(mNativeToolbarPtr); | |
132 } | |
133 | |
134 /** | |
135 * To be called when the user triggers a forward navigation action. | |
136 */ | |
137 public void onForwardPressed() { | |
138 if (mNativeToolbarPtr == 0) return; | |
139 mDelegate.resetDebugStats(); | |
140 nativeOnForwardPressed(mNativeToolbarPtr); | |
141 } | |
142 | |
143 // View overrides. | |
144 @Override | |
145 protected void onFinishInflate() { | |
146 super.onFinishInflate(); | |
147 | |
148 mUrlBar = (UrlBar) findViewById(R.id.toolbar_url_bar); | |
149 mUrlBar.addUrlBarObserver(this); | |
150 | |
151 mReloadButton = (ImageButton) findViewById(R.id.toolbar_reload_btn); | |
152 mReloadButton.setOnClickListener(this); | |
153 | |
154 mMenuButton = (ImageButton) findViewById(R.id.menu_button); | |
155 mMenuButton.setOnClickListener(new View.OnClickListener() { | |
156 @Override | |
157 public void onClick(View v) { | |
158 mToolbarMenu.showMenu(v); | |
159 } | |
160 }); | |
161 | |
162 mProgressBar = (ProgressBar) findViewById(R.id.page_load_progress); | |
163 mProgressBar.setVisibility(View.GONE); | |
164 } | |
165 | |
166 // UrlBar.UrlBarObserver interface. | |
167 @Override | |
168 public void onNewTextEntered(String text) { | |
169 sendUrlTextInternal(text); | |
170 } | |
171 | |
172 // View.OnClickListener interface. | |
173 @Override | |
174 public void onClick(View view) { | |
175 if (mNativeToolbarPtr == 0) return; | |
176 if (view == mReloadButton) nativeOnReloadPressed(mNativeToolbarPtr); | |
177 } | |
178 | |
179 private void sendUrlTextInternal(String text) { | |
180 mUrlToLoad = null; | |
181 if (TextUtils.isEmpty(text)) return; | |
182 | |
183 if (mNativeToolbarPtr == 0) { | |
184 mUrlToLoad = text; | |
185 return; | |
186 } | |
187 | |
188 nativeOnUrlTextEntered(mNativeToolbarPtr, text); | |
189 | |
190 // When triggering a navigation to a new URL, show the progress bar. | |
191 // TODO(khushalsagar): We need more signals to hide the bar when the loa
d might have failed. | |
192 // For instance, in the case of a wrong URL right now or if there is no
network connection. | |
193 updateProgressBar(true); | |
194 } | |
195 | |
196 private void updateProgressBar(boolean show) { | |
197 if (show) { | |
198 mProgressBar.setVisibility(View.VISIBLE); | |
199 } else { | |
200 mProgressBar.setVisibility(View.GONE); | |
201 } | |
202 } | |
203 | |
204 // Methods that are called by native via JNI. | |
205 @CalledByNative | |
206 private void onEngineSentUrl(String url) { | |
207 if (url != null) mUrlBar.setText(url); | |
208 mDelegate.resetDebugStats(); | |
209 } | |
210 | |
211 @CalledByNative | |
212 private void onEngineSentFavicon(Bitmap favicon) { | |
213 // TODO(dtrainor): Add a UI for the favicon. | |
214 } | |
215 | |
216 @CalledByNative | |
217 private void onEngineSentTitle(String title) { | |
218 // TODO(dtrainor): Add a UI for the title. | |
219 } | |
220 | |
221 @CalledByNative | |
222 private void onEngineSentLoading(boolean loading) { | |
223 // TODO(dtrainor): Add a UI for the loading state. | |
224 } | |
225 | |
226 @CalledByNative | |
227 private void onEngineSentPageLoadStatusUpdate(boolean completed) { | |
228 boolean show = !completed; | |
229 updateProgressBar(show); | |
230 } | |
231 | |
232 private native long nativeInit(BlimpClientSession blimpClientSession); | |
233 private native void nativeDestroy(long nativeToolbar); | |
234 private native void nativeOnUrlTextEntered(long nativeToolbar, String text); | |
235 private native void nativeOnReloadPressed(long nativeToolbar); | |
236 private native void nativeOnForwardPressed(long nativeToolbar); | |
237 private native boolean nativeOnBackPressed(long nativeToolbar); | |
238 } | |
OLD | NEW |