mobile/android/base/widget/ThemedView.java.frag

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.widget;
     7 import org.mozilla.gecko.GeckoApplication;
     8 import org.mozilla.gecko.LightweightTheme;
     9 import org.mozilla.gecko.R;
    11 import android.content.Context;
    12 import android.content.res.TypedArray;
    13 import android.graphics.drawable.ColorDrawable;
    14 import android.util.AttributeSet;
    16 public class Themed@VIEW_NAME_SUFFIX@ extends @BASE_TYPE@
    17                                      implements LightweightTheme.OnChangeListener {
    18     private final LightweightTheme mTheme;
    20     private static final int[] STATE_PRIVATE_MODE = { R.attr.state_private };
    21     private static final int[] STATE_LIGHT = { R.attr.state_light };
    22     private static final int[] STATE_DARK = { R.attr.state_dark };
    24     protected static final int[] PRIVATE_PRESSED_STATE_SET = { R.attr.state_private, android.R.attr.state_pressed };
    25     protected static final int[] PRIVATE_FOCUSED_STATE_SET = { R.attr.state_private, android.R.attr.state_focused };
    26     protected static final int[] PRIVATE_STATE_SET = { R.attr.state_private };
    28     private boolean mIsPrivate = false;
    29     private boolean mIsLight = false;
    30     private boolean mIsDark = false;
    31     private boolean mAutoUpdateTheme = true;
    33     public Themed@VIEW_NAME_SUFFIX@(Context context, AttributeSet attrs) {
    34         super(context, attrs);
    35         mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
    37         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LightweightTheme);
    38         mAutoUpdateTheme = a.getBoolean(R.styleable.LightweightTheme_autoUpdateTheme, true);
    39         a.recycle();
    40     }
    42     @Override
    43     public void onAttachedToWindow() {
    44         super.onAttachedToWindow();
    46         if (mAutoUpdateTheme)
    47             mTheme.addListener(this);
    48     }
    50     @Override
    51     public void onDetachedFromWindow() {
    52         super.onDetachedFromWindow();
    54         if (mAutoUpdateTheme)
    55             mTheme.removeListener(this);
    56     }
    58     @Override
    59     public int[] onCreateDrawableState(int extraSpace) {
    60         final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
    62         if (mIsPrivate)
    63             mergeDrawableStates(drawableState, STATE_PRIVATE_MODE);
    64         else if (mIsLight)
    65             mergeDrawableStates(drawableState, STATE_LIGHT);
    66         else if (mIsDark)
    67             mergeDrawableStates(drawableState, STATE_DARK);
    69         return drawableState;
    70     }
    72     @Override
    73     public void onLightweightThemeChanged() {
    74         if (mAutoUpdateTheme && mTheme.isEnabled())
    75             setTheme(mTheme.isLightTheme());
    76     }
    78     @Override
    79     public void onLightweightThemeReset() {
    80         if (mAutoUpdateTheme)
    81             resetTheme();
    82     }
    84     @Override
    85     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    86         super.onLayout(changed, left, top, right, bottom);
    87         onLightweightThemeChanged();
    88     }
    90     public boolean isPrivateMode() {
    91         return mIsPrivate;
    92     }
    94     public void setPrivateMode(boolean isPrivate) {
    95         if (mIsPrivate != isPrivate) {
    96             mIsPrivate = isPrivate;
    97             refreshDrawableState();
    98         }
    99     }
   101     public void setTheme(boolean isLight) {
   102         // Set the theme only if it is different from existing theme.
   103         if ((isLight && mIsLight != isLight) ||
   104             (!isLight && mIsDark == isLight)) {
   105             if (isLight) {
   106                 mIsLight = true;
   107                 mIsDark = false;
   108             } else {
   109                 mIsLight = false;
   110                 mIsDark = true;
   111             }
   113             refreshDrawableState();
   114         }
   115     }
   117     public void resetTheme() {
   118         if (mIsLight || mIsDark) {
   119             mIsLight = false;
   120             mIsDark = false;
   121             refreshDrawableState();
   122         }
   123     }
   125     public void setAutoUpdateTheme(boolean autoUpdateTheme) {
   126         if (mAutoUpdateTheme != autoUpdateTheme) {
   127             mAutoUpdateTheme = autoUpdateTheme;
   129             if (mAutoUpdateTheme)
   130                 mTheme.addListener(this);
   131             else
   132                 mTheme.removeListener(this);
   133         }
   134     }
   136     public ColorDrawable getColorDrawable(int id) {
   137         return new ColorDrawable(getResources().getColor(id));
   138     }
   139 }

mercurial