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.BrowserWebView;
     27 import com.android.browser.WebViewProperties;
     28 
     29 public class InvertedContrastPreview extends WebViewPreview {
     30 
     31     static final String IMG_ROOT = "content://com.android.browser.home/res/raw/";
     32     static final String[] THUMBS = new String[] {
     33         "thumb_google",
     34         "thumb_amazon",
     35         "thumb_cnn",
     36         "thumb_espn",
     37         "", // break
     38         "thumb_bbc",
     39         "thumb_nytimes",
     40         "thumb_weatherchannel",
     41         "thumb_picasa",
     42     };
     43 
     44     String mHtml;
     45 
     46     public InvertedContrastPreview(
     47             Context context, AttributeSet attrs, int defStyle) {
     48         super(context, attrs, defStyle);
     49     }
     50 
     51     public InvertedContrastPreview(Context context, AttributeSet attrs) {
     52         super(context, attrs);
     53     }
     54 
     55     public InvertedContrastPreview(Context context) {
     56         super(context);
     57     }
     58 
     59     @Override
     60     protected void init(Context context) {
     61         super.init(context);
     62         StringBuilder builder = new StringBuilder("<html><body style=\"width: 1000px\">");
     63         for (String thumb : THUMBS) {
     64             if (TextUtils.isEmpty(thumb)) {
     65                 builder.append("<br />");
     66                 continue;
     67             }
     68             builder.append("<img src=\"");
     69             builder.append(IMG_ROOT);
     70             builder.append(thumb);
     71             builder.append("\" />&nbsp;");
     72         }
     73         builder.append("</body></html>");
     74         mHtml = builder.toString();
     75     }
     76 
     77     @Override
     78     protected void updatePreview(boolean forceReload) {
     79         if (mWebView == null || !BrowserWebView.isClassic()) return;
     80 
     81         WebSettingsClassic ws = WebViewClassic.fromWebView(mWebView).getSettings();
     82         BrowserSettings bs = BrowserSettings.getInstance();
     83         ws.setProperty(WebViewProperties.gfxInvertedScreen,
     84                 bs.useInvertedRendering() ? "true" : "false");
     85         ws.setProperty(WebViewProperties.gfxInvertedScreenContrast,
     86                 Float.toString(bs.getInvertedContrast()));
     87         if (forceReload) {
     88             mWebView.loadData(mHtml, "text/html", null);
     89         }
     90     }
     91 
     92 }
     93