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