Bug 1182514 - Add Lightweight theme for restricted profiles. r=margaret, a=lizzard
authorSebastian Kaspari <s.kaspari@gmail.com>
Fri, 07 Aug 2015 13:23:18 +0200
changeset 288929 9ca02c1d8027122bfd29927f4b51636b8ad875e8
parent 288928 62156e0a139a3a912151c5904c5b3ee10ad0913c
child 288930 c47645f91f51afbfc50416a08ba625c5d4b4cf54
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret, lizzard
bugs1182514
milestone42.0a2
Bug 1182514 - Add Lightweight theme for restricted profiles. r=margaret, a=lizzard
mobile/android/app/assets/parental_controls_theme.png
mobile/android/base/lwt/LightweightTheme.java
mobile/android/chrome/content/browser.js
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..64d76469ebdfb0cc8090c9c5b00c3a70bfe7b39a
GIT binary patch
literal 2915
zc%17D@N?(olHy`uVBq!ia0y~y;NAgbpWt8vk`)GPoPYvao-U3d6?5L+a%4Q9AaG#A
z?GyzGLu1R73+{^+F#TGS|DTxw0jBTWj?7Is_J9w;b7(mzi{Le|G+QGn<zy>I;tBD*
zL*l7O++jrUJPdXq*)buZfE~e8IEZ3l)2M}`7LHmtYT>AbqZW=@IBMajg`*bI%t8wl
VZRVRMHzI98JWp3Ymvv4FO#pwr6WRa(
--- a/mobile/android/base/lwt/LightweightTheme.java
+++ b/mobile/android/base/lwt/LightweightTheme.java
@@ -1,31 +1,34 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.lwt;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 
 import org.json.JSONObject;
 import org.mozilla.gecko.AppConstants.Versions;
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoSharedPrefs;
 import org.mozilla.gecko.gfx.BitmapUtils;
 import org.mozilla.gecko.util.GeckoEventListener;
 import org.mozilla.gecko.util.WindowUtils;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.util.ThreadUtils.AssertBehavior;
 
 import android.app.Application;
 import android.content.SharedPreferences;
 import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Rect;
 import android.graphics.Shader;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.text.TextUtils;
@@ -35,16 +38,18 @@ import android.view.View;
 import android.view.ViewParent;
 
 public class LightweightTheme implements GeckoEventListener {
     private static final String LOGTAG = "GeckoLightweightTheme";
 
     private static final String PREFS_URL = "lightweightTheme.headerURL";
     private static final String PREFS_COLOR = "lightweightTheme.color";
 
+    private static final String ASSETS_PREFIX = "resource://android/assets/";
+
     private final Application mApplication;
 
     private Bitmap mBitmap;
     private int mColor;
     private boolean mIsLight;
 
     public static interface OnChangeListener {
         // The View should change its background/text color. 
@@ -114,18 +119,41 @@ public class LightweightTheme implements
             }
 
             String croppedURL = mHeaderURL;
             int mark = croppedURL.indexOf('?');
             if (mark != -1) {
                 croppedURL = croppedURL.substring(0, mark);
             }
 
-            // Get the image and convert it to a bitmap.
-            final Bitmap bitmap = BitmapUtils.decodeUrl(croppedURL);
+            if (croppedURL.startsWith(ASSETS_PREFIX)) {
+                onBitmapLoaded(loadFromAssets(croppedURL));
+            } else {
+                onBitmapLoaded(BitmapUtils.decodeUrl(croppedURL));
+            }
+        }
+
+        private Bitmap loadFromAssets(String url) {
+            InputStream stream = null;
+
+            try {
+                stream = mApplication.getAssets().open(url.substring(ASSETS_PREFIX.length()));
+                return BitmapFactory.decodeStream(stream);
+            } catch (IOException e) {
+                return null;
+            } finally {
+                if (stream != null) {
+                    try {
+                        stream.close();
+                    } catch (IOException e) {}
+                }
+            }
+        }
+
+        private void onBitmapLoaded(final Bitmap bitmap) {
             ThreadUtils.postToUiThread(new Runnable() {
                 @Override
                 public void run() {
                     setLightweightTheme(bitmap, mColor);
                 }
             });
         }
     }
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -3093,16 +3093,20 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 var LightWeightThemeWebInstaller = {
   init: function sh_init() {
     let temp = {};
     Cu.import("resource://gre/modules/LightweightThemeConsumer.jsm", temp);
     let theme = new temp.LightweightThemeConsumer(document);
     BrowserApp.deck.addEventListener("InstallBrowserTheme", this, false, true);
     BrowserApp.deck.addEventListener("PreviewBrowserTheme", this, false, true);
     BrowserApp.deck.addEventListener("ResetBrowserThemePreview", this, false, true);
+
+    if (ParentalControls.parentalControlsEnabled && !this._manager.currentTheme) {
+      this._installParentalControlsTheme();
+    }
   },
 
   handleEvent: function (event) {
     switch (event.type) {
       case "InstallBrowserTheme":
       case "PreviewBrowserTheme":
       case "ResetBrowserThemePreview":
         // ignore requests from background tabs
@@ -3129,16 +3133,28 @@ var LightWeightThemeWebInstaller = {
 
   get _manager () {
     let temp = {};
     Cu.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
     delete this._manager;
     return this._manager = temp.LightweightThemeManager;
   },
 
+  _installParentalControlsTheme: function() {
+    let mgr = this._manager;
+    let parentalControlsTheme = {
+      "headerURL": "resource://android/assets/parental_controls_theme.png",
+      "name": "Parental Controls Theme",
+      "id": "parental-controls-theme@mozilla.org"
+    };
+
+    mgr.addBuiltInTheme(parentalControlsTheme);
+    mgr.themeChanged(parentalControlsTheme);
+  },
+
   _installRequest: function (event) {
     let node = event.target;
     let data = this._getThemeFromNode(node);
     if (!data)
       return;
 
     if (this._isAllowed(node)) {
       this._install(data);