Home | History | Annotate | Download | only in preferences
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.browser.preferences;
     18 
     19 import android.content.Context;
     20 import android.text.TextUtils;
     21 import android.util.AttributeSet;
     22 import android.webkit.WebSettingsClassic;
     23 import android.webkit.WebViewClassic;
     24 
     25 import com.android.browser.BrowserSettings;
     26 import com.android.browser.WebViewProperties;
     27 
     28 public class InvertedContrastPreview extends WebViewPreview {
     29 
     30     static final String IMG_ROOT = "content://com.android.browser.home/res/raw/";
     31     static final String[] THUMBS = new String[] {
     32         "thumb_google",
     33         "thumb_amazon",
     34         "thumb_cnn",
     35         "thumb_espn",
     36         "", // break
     37         "thumb_bbc",
     38         "thumb_nytimes",
     39         "thumb_weatherchannel",
     40         "thumb_picasa",
     41     };
     42 
     43     String mHtml;
     44 
     45     public InvertedContrastPreview(
     46             Context context, AttributeSet attrs, int defStyle) {
     47         super(context, attrs, defStyle);
     48     }
     49 
     50     public InvertedContrastPreview(Context context, AttributeSet attrs) {
     51         super(context, attrs);
     52     }
     53 
     54     public InvertedContrastPreview(Context context) {
     55         super(context);
     56     }
     57 
     58     @Override
     59     protected void init(Context context) {
     60         super.init(context);
     61         StringBuilder builder = new StringBuilder("<html><body style=\"width: 1000px\">");
     62         for (String thumb : THUMBS) {
     63             if (TextUtils.isEmpty(thumb)) {
     64                 builder.append("<br />");
     65                 continue;
     66             }
     67             builder.append("<img src=\"");
     68             builder.append(IMG_ROOT);
     69             builder.append(thumb);
     70             builder.append("\" />&nbsp;");
     71         }
     72         builder.append("</body></html>");
     73         mHtml = builder.toString();
     74     }
     75 
     76     @Override
     77     protected void updatePreview(boolean forceReload) {
     78         if (mWebView == null) return;
     79 
     80         WebSettingsClassic ws = WebViewClassic.fromWebView(mWebView).getSettings();
     81         BrowserSettings bs = BrowserSettings.getInstance();
     82         ws.setProperty(WebViewProperties.gfxInvertedScreen,
     83                 bs.useInvertedRendering() ? "true" : "false");
     84         ws.setProperty(WebViewProperties.gfxInvertedScreenContrast,
     85                 Float.toString(bs.getInvertedContrast()));
     86         if (forceReload) {
     87             mWebView.loadData(mHtml, "text/html", null);
     88         }
     89     }
     90 
     91 }
     92