|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko; |
|
7 |
|
8 import android.content.Intent; |
|
9 import android.os.Bundle; |
|
10 import android.util.Log; |
|
11 import android.view.View; |
|
12 import android.view.MenuItem; |
|
13 import android.widget.TextView; |
|
14 import android.widget.RelativeLayout; |
|
15 import android.content.Context; |
|
16 import android.content.SharedPreferences; |
|
17 import android.graphics.Bitmap; |
|
18 import android.graphics.Color; |
|
19 import android.graphics.drawable.Drawable; |
|
20 import android.graphics.drawable.GradientDrawable; |
|
21 import android.view.animation.AnimationUtils; |
|
22 import android.view.animation.Animation; |
|
23 import android.widget.ImageView; |
|
24 import android.view.Display; |
|
25 |
|
26 import java.io.File; |
|
27 import java.net.URI; |
|
28 |
|
29 public class WebappImpl extends GeckoApp { |
|
30 private static final String LOGTAG = "GeckoWebappImpl"; |
|
31 |
|
32 private URI mOrigin; |
|
33 private TextView mTitlebarText = null; |
|
34 private View mTitlebar = null; |
|
35 |
|
36 private View mSplashscreen; |
|
37 |
|
38 protected int getIndex() { return 0; } |
|
39 |
|
40 @Override |
|
41 public int getLayout() { return R.layout.web_app; } |
|
42 |
|
43 @Override |
|
44 public boolean hasTabsSideBar() { return false; } |
|
45 |
|
46 @Override |
|
47 public void onCreate(Bundle savedInstanceState) |
|
48 { |
|
49 super.onCreate(savedInstanceState); |
|
50 |
|
51 mSplashscreen = (RelativeLayout) findViewById(R.id.splashscreen); |
|
52 if (!GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) { |
|
53 overridePendingTransition(R.anim.grow_fade_in_center, android.R.anim.fade_out); |
|
54 showSplash(); |
|
55 } |
|
56 |
|
57 String action = getIntent().getAction(); |
|
58 Bundle extras = getIntent().getExtras(); |
|
59 String title = extras != null ? extras.getString(Intent.EXTRA_SHORTCUT_NAME) : null; |
|
60 setTitle(title != null ? title : "Web App"); |
|
61 |
|
62 mTitlebarText = (TextView)findViewById(R.id.webapp_title); |
|
63 mTitlebar = findViewById(R.id.webapp_titlebar); |
|
64 if (!action.startsWith(ACTION_WEBAPP_PREFIX)) { |
|
65 Log.e(LOGTAG, "Webapp launch, but intent action is " + action + "!"); |
|
66 return; |
|
67 } |
|
68 |
|
69 // Try to use the origin stored in the WebappAllocator first |
|
70 String origin = WebappAllocator.getInstance(this).getAppForIndex(getIndex()); |
|
71 try { |
|
72 mOrigin = new URI(origin); |
|
73 } catch (java.net.URISyntaxException ex) { |
|
74 // If we can't parse the this is an app protocol, just settle for not having an origin |
|
75 if (!origin.startsWith("app://")) { |
|
76 return; |
|
77 } |
|
78 |
|
79 // If that failed fall back to the origin stored in the shortcut |
|
80 Log.i(LOGTAG, "Webapp is not registered with allocator"); |
|
81 try { |
|
82 mOrigin = new URI(getIntent().getData().toString()); |
|
83 } catch (java.net.URISyntaxException ex2) { |
|
84 Log.e(LOGTAG, "Unable to parse intent url: ", ex); |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 @Override |
|
90 protected void loadStartupTab(String uri) { |
|
91 String action = getIntent().getAction(); |
|
92 if (GeckoApp.ACTION_WEBAPP_PREFIX.equals(action)) { |
|
93 // This action assumes the uri is not an installed Webapp. We will |
|
94 // use the WebappAllocator to register the uri with an Android |
|
95 // process so it can run chromeless. |
|
96 int index = WebappAllocator.getInstance(this).findAndAllocateIndex(uri, "App", (Bitmap) null); |
|
97 Intent appIntent = GeckoAppShell.getWebappIntent(index, uri); |
|
98 startActivity(appIntent); |
|
99 finish(); |
|
100 } |
|
101 } |
|
102 |
|
103 private void showSplash() { |
|
104 SharedPreferences prefs = getSharedPreferences("webapps", Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS); |
|
105 |
|
106 // get the favicon dominant color, stored when the app was installed |
|
107 int[] colors = new int[2]; |
|
108 int dominantColor = prefs.getInt(WebappAllocator.iconKey(getIndex()), -1); |
|
109 |
|
110 // now lighten it, to ensure that the icon stands out in the center |
|
111 float[] f = new float[3]; |
|
112 Color.colorToHSV(dominantColor, f); |
|
113 f[2] = Math.min(f[2]*2, 1.0f); |
|
114 colors[0] = Color.HSVToColor(255, f); |
|
115 |
|
116 // now generate a second, slightly darker version of the same color |
|
117 f[2] *= 0.75; |
|
118 colors[1] = Color.HSVToColor(255, f); |
|
119 |
|
120 // Draw the background gradient |
|
121 GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TL_BR, colors); |
|
122 gd.setGradientType(GradientDrawable.RADIAL_GRADIENT); |
|
123 Display display = getWindowManager().getDefaultDisplay(); |
|
124 gd.setGradientCenter(0.5f, 0.5f); |
|
125 gd.setGradientRadius(Math.max(display.getWidth()/2, display.getHeight()/2)); |
|
126 mSplashscreen.setBackgroundDrawable((Drawable)gd); |
|
127 |
|
128 // look for a logo.png in the profile dir and show it. If we can't find a logo show nothing |
|
129 File profile = getProfile().getDir(); |
|
130 File logoFile = new File(profile, "logo.png"); |
|
131 if (logoFile.exists()) { |
|
132 ImageView image = (ImageView)findViewById(R.id.splashscreen_icon); |
|
133 Drawable d = Drawable.createFromPath(logoFile.getPath()); |
|
134 image.setImageDrawable(d); |
|
135 |
|
136 Animation fadein = AnimationUtils.loadAnimation(this, R.anim.grow_fade_in_center); |
|
137 fadein.setStartOffset(500); |
|
138 fadein.setDuration(1000); |
|
139 image.startAnimation(fadein); |
|
140 } |
|
141 } |
|
142 |
|
143 @Override |
|
144 protected String getDefaultProfileName() { |
|
145 String action = getIntent().getAction(); |
|
146 if (!action.startsWith(ACTION_WEBAPP_PREFIX)) { |
|
147 Log.e(LOGTAG, "Webapp launch, but intent action is " + action + "!"); |
|
148 return null; |
|
149 } |
|
150 |
|
151 return "webapp" + action.substring(ACTION_WEBAPP_PREFIX.length()); |
|
152 } |
|
153 |
|
154 @Override |
|
155 protected boolean getSessionRestoreState(Bundle savedInstanceState) { |
|
156 // for now webapps never restore your session |
|
157 return false; |
|
158 } |
|
159 |
|
160 @Override |
|
161 public void onTabChanged(Tab tab, Tabs.TabEvents msg, Object data) { |
|
162 switch(msg) { |
|
163 case SELECTED: |
|
164 case LOCATION_CHANGE: |
|
165 if (Tabs.getInstance().isSelectedTab(tab)) { |
|
166 final String urlString = tab.getURL(); |
|
167 final URI uri; |
|
168 |
|
169 try { |
|
170 uri = new URI(urlString); |
|
171 } catch (java.net.URISyntaxException ex) { |
|
172 mTitlebarText.setText(urlString); |
|
173 |
|
174 // If we can't parse the url, and its an app protocol hide |
|
175 // the titlebar and return, otherwise show the titlebar |
|
176 // and the full url |
|
177 if (!urlString.startsWith("app://")) { |
|
178 mTitlebar.setVisibility(View.VISIBLE); |
|
179 } else { |
|
180 mTitlebar.setVisibility(View.GONE); |
|
181 } |
|
182 return; |
|
183 } |
|
184 |
|
185 if (mOrigin != null && mOrigin.getHost().equals(uri.getHost())) { |
|
186 mTitlebar.setVisibility(View.GONE); |
|
187 } else { |
|
188 mTitlebarText.setText(uri.getScheme() + "://" + uri.getHost()); |
|
189 mTitlebar.setVisibility(View.VISIBLE); |
|
190 } |
|
191 } |
|
192 break; |
|
193 case LOADED: |
|
194 if (mSplashscreen != null && mSplashscreen.getVisibility() == View.VISIBLE) { |
|
195 Animation fadeout = AnimationUtils.loadAnimation(this, android.R.anim.fade_out); |
|
196 fadeout.setAnimationListener(new Animation.AnimationListener() { |
|
197 @Override |
|
198 public void onAnimationEnd(Animation animation) { |
|
199 mSplashscreen.setVisibility(View.GONE); |
|
200 } |
|
201 @Override |
|
202 public void onAnimationRepeat(Animation animation) { } |
|
203 @Override |
|
204 public void onAnimationStart(Animation animation) { } |
|
205 }); |
|
206 mSplashscreen.startAnimation(fadeout); |
|
207 } |
|
208 break; |
|
209 case START: |
|
210 if (mSplashscreen != null && mSplashscreen.getVisibility() == View.VISIBLE) { |
|
211 View area = findViewById(R.id.splashscreen_progress); |
|
212 area.setVisibility(View.VISIBLE); |
|
213 Animation fadein = AnimationUtils.loadAnimation(this, android.R.anim.fade_in); |
|
214 fadein.setDuration(1000); |
|
215 area.startAnimation(fadein); |
|
216 } |
|
217 break; |
|
218 } |
|
219 super.onTabChanged(tab, msg, data); |
|
220 } |
|
221 }; |