Home | History | Annotate | Download | only in old
      1 /*
      2  * Copyright (C) 2014 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.tv.settings.dialog.old;
     18 
     19 import android.animation.ObjectAnimator;
     20 import android.app.Activity;
     21 import android.content.Context;
     22 import android.graphics.Bitmap;
     23 import android.graphics.Color;
     24 import android.graphics.drawable.Drawable;
     25 import android.net.Uri;
     26 import android.os.Bundle;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import android.view.ViewGroup.LayoutParams;
     31 import android.view.accessibility.AccessibilityManager;
     32 import android.widget.ImageView;
     33 import android.widget.RelativeLayout;
     34 import android.widget.TextView;
     35 
     36 import com.android.tv.settings.R;
     37 import com.android.tv.settings.widget.FrameLayoutWithShadows;
     38 
     39 /**
     40  * This class exists to make extending both v4 fragments and regular fragments easy
     41  */
     42 public class BaseContentFragment {
     43 
     44     private static final String EXTRA_TITLE = "title";
     45     private static final String EXTRA_BREADCRUMB = "breadcrumb";
     46     private static final String EXTRA_DESCRIPTION = "description";
     47     private static final String EXTRA_ICON_RESOURCE_ID = "iconResourceId";
     48     private static final String EXTRA_ICON_URI = "iconUri";
     49     private static final String EXTRA_ICON_BITMAP = "iconBitmap";
     50     private static final String EXTRA_ICON_BACKGROUND = "iconBackground";
     51 
     52     public static Bundle buildArgs(
     53             String title, String breadcrumb, String description, int iconResourceId,
     54             int backgroundColor) {
     55         return buildArgs(title, breadcrumb, description, iconResourceId, null, null,
     56                 backgroundColor);
     57     }
     58 
     59     public static Bundle buildArgs(String title, String breadcrumb, String description, Uri iconUri,
     60             int backgroundColor) {
     61         return buildArgs(title, breadcrumb, description, 0, iconUri, null, backgroundColor);
     62     }
     63 
     64     public static Bundle buildArgs(String title, String breadcrumb, String description,
     65             Bitmap iconBitmap) {
     66         return buildArgs(title, breadcrumb, description, 0, null, iconBitmap, Color.TRANSPARENT);
     67     }
     68 
     69     private static Bundle buildArgs(
     70             String title, String breadcrumb, String description, int iconResourceId,
     71             Uri iconUri, Bitmap iconBitmap, int iconBackgroundColor) {
     72         Bundle args = new Bundle();
     73         args.putString(EXTRA_TITLE, title);
     74         args.putString(EXTRA_BREADCRUMB, breadcrumb);
     75         args.putString(EXTRA_DESCRIPTION, description);
     76         args.putInt(EXTRA_ICON_RESOURCE_ID, iconResourceId);
     77         args.putParcelable(EXTRA_ICON_URI, iconUri);
     78         args.putParcelable(EXTRA_ICON_BITMAP, iconBitmap);
     79         args.putInt(EXTRA_ICON_BACKGROUND, iconBackgroundColor);
     80         return args;
     81     }
     82 
     83     private final LiteFragment mFragment;
     84     private Activity mActivity;
     85     private String mTitle;
     86     private String mBreadcrumb;
     87     private String mDescription;
     88     private int mIconResourceId;
     89     private Bitmap mIconBitmap;
     90     private int mIconBackgroundColor;
     91     private AccessibilityManager mAccessManager;
     92 
     93     public BaseContentFragment(LiteFragment fragment) {
     94         mFragment = fragment;
     95     }
     96 
     97     public void onCreate(Bundle savedInstanceState) {
     98         Bundle state = (savedInstanceState != null) ? savedInstanceState : mFragment.getArguments();
     99         if (mTitle == null) {
    100             mTitle = state.getString(EXTRA_TITLE);
    101         }
    102         if (mBreadcrumb == null) {
    103             mBreadcrumb = state.getString(EXTRA_BREADCRUMB);
    104         }
    105         if (mDescription == null) {
    106             mDescription = state.getString(EXTRA_DESCRIPTION);
    107         }
    108         if (mIconResourceId == 0) {
    109             mIconResourceId = state.getInt(EXTRA_ICON_RESOURCE_ID, 0);
    110         }
    111         if (mIconBitmap == null) {
    112             mIconBitmap = state.getParcelable(EXTRA_ICON_BITMAP);
    113         }
    114         if (mIconBackgroundColor == Color.TRANSPARENT) {
    115             mIconBackgroundColor = state.getInt(EXTRA_ICON_BACKGROUND, Color.TRANSPARENT);
    116         }
    117     }
    118 
    119     public void onSaveInstanceState(Bundle outState) {
    120         outState.putString(EXTRA_TITLE, mTitle);
    121         outState.putString(EXTRA_BREADCRUMB, mBreadcrumb);
    122         outState.putString(EXTRA_DESCRIPTION, mDescription);
    123         outState.putInt(EXTRA_ICON_RESOURCE_ID, mIconResourceId);
    124         outState.putParcelable(EXTRA_ICON_BITMAP, mIconBitmap);
    125         outState.putInt(EXTRA_ICON_BACKGROUND, mIconBackgroundColor);
    126     }
    127 
    128     /**
    129      * Pass activity from ContentFragment to BaseContentFragment when it is
    130      * attached.
    131      */
    132     public void onAttach(Activity activity) {
    133         mActivity = activity;
    134     }
    135 
    136     /**
    137      * Rest BaseContentFragment mActivity to null when ContentFragment is
    138      * detached.
    139      */
    140     public void onDetach() {
    141         mActivity = null;
    142     }
    143 
    144     public View onCreateView(
    145             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    146         final View view = inflater.inflate(R.layout.content_fragment, container, false);
    147         setText(view, R.id.title, mTitle);
    148         setText(view, R.id.breadcrumb, mBreadcrumb);
    149         setText(view, R.id.description, mDescription);
    150         int iconResourceId = getIconResourceId();
    151         final ImageView iconImageView = (ImageView) view.findViewById(R.id.icon);
    152         int iconBackground = getIconBackgroundColor();
    153         if (iconBackground != Color.TRANSPARENT) {
    154             iconImageView.setBackgroundColor(iconBackground);
    155         }
    156 
    157         if (iconResourceId != 0) {
    158             iconImageView.setImageResource(iconResourceId);
    159             addShadow(iconImageView, view);
    160             updateViewSize(iconImageView);
    161         } else {
    162             Bitmap iconBitmap = getIconBitmap();
    163             if (iconBitmap != null) {
    164                 iconImageView.setImageBitmap(iconBitmap);
    165                 addShadow(iconImageView, view);
    166                 updateViewSize(iconImageView);
    167             } else {
    168                 iconImageView.setVisibility(View.GONE);
    169             }
    170         }
    171 
    172         return view;
    173     }
    174 
    175     public ImageView getIcon() {
    176         if (mFragment.getView() == null) return null;
    177         return (ImageView) mFragment.getView().findViewById(R.id.icon);
    178     }
    179 
    180     public TextView getTitle() {
    181         if (mFragment.getView() == null) return null;
    182         return (TextView) mFragment.getView().findViewById(R.id.title);
    183     }
    184 
    185     public int getIconResourceId() {
    186         return mIconResourceId;
    187     }
    188 
    189     public Bitmap getIconBitmap() {
    190         return mIconBitmap;
    191     }
    192 
    193     public int getIconBackgroundColor() {
    194         return mIconBackgroundColor;
    195     }
    196 
    197     public RelativeLayout getRoot() {
    198         return (RelativeLayout) mFragment.getView();
    199     }
    200 
    201     public TextView getBreadCrumb() {
    202         if (mFragment.getView() == null) return null;
    203         return (TextView) mFragment.getView().findViewById(R.id.breadcrumb);
    204     }
    205 
    206     public TextView getDescription() {
    207         if (mFragment.getView() == null) return null;
    208         return (TextView) mFragment.getView().findViewById(R.id.description);
    209     }
    210 
    211     public void setTextToExtra(View parent, int textViewResourceId,
    212             String extraLabel) {
    213         String text = mFragment.getArguments().getString(extraLabel, null);
    214         setText(parent, textViewResourceId, text);
    215     }
    216 
    217     public void setTextToExtra(int textViewResourceId, String extraLabel) {
    218         if (mFragment.getView() == null) return;
    219         setTextToExtra(mFragment.getView(), textViewResourceId, extraLabel);
    220     }
    221 
    222     public void setText(View parent, int textViewResourceId, String text) {
    223         TextView textView = (TextView) parent.findViewById(textViewResourceId);
    224         if (textView != null && text != null) {
    225             textView.setText(text);
    226 
    227             // Enable focusable title and description if accessibility is enabled.
    228             if (mActivity != null) {
    229                 if (mAccessManager == null) {
    230                     mAccessManager = (AccessibilityManager) mActivity
    231                             .getSystemService(Context.ACCESSIBILITY_SERVICE);
    232                 }
    233                 if (mAccessManager.isEnabled()) {
    234                     textView.setFocusable(true);
    235                     textView.setFocusableInTouchMode(true);
    236                 }
    237             }
    238         }
    239     }
    240 
    241     public void setText(int textViewResourceId, String text) {
    242         if (mFragment.getView() == null) return;
    243         setText(mFragment.getView(), textViewResourceId, text);
    244     }
    245 
    246     public void setTitleText(String text) {
    247         mTitle = text;
    248         if (mFragment.getView() == null) return;
    249         setText(mFragment.getView(), R.id.title, text);
    250     }
    251 
    252     public void setBreadCrumbText(String text) {
    253         mBreadcrumb = text;
    254         if (mFragment.getView() == null) return;
    255         setText(mFragment.getView(), R.id.breadcrumb, text);
    256     }
    257 
    258     public void setDescriptionText(String text) {
    259         mDescription = text;
    260         if (mFragment.getView() == null) return;
    261         setText(mFragment.getView(), R.id.description, text);
    262     }
    263 
    264     /**
    265      * Unlike {@link #setIcon(int)}, this will only work if called
    266      * after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
    267      * @param iconDrawable
    268      */
    269     public void setIcon(Drawable iconDrawable) {
    270         if (mFragment.getView() == null) return;
    271 
    272         final ImageView iconImageView = (ImageView) mFragment.getView().findViewById(R.id.icon);
    273         if (iconImageView != null) {
    274             if (iconDrawable != null) {
    275                 iconImageView.setImageDrawable(iconDrawable);
    276                 iconImageView.setVisibility(View.VISIBLE);
    277                 updateViewSize(iconImageView);
    278             }
    279         }
    280     }
    281 
    282     public void setIcon(int iconResourceId) {
    283         mIconResourceId = iconResourceId;
    284         if (mFragment.getView() == null) return;
    285 
    286         final ImageView iconImageView = (ImageView) mFragment.getView().findViewById(R.id.icon);
    287         if (iconImageView != null) {
    288             if (iconResourceId != 0) {
    289                 iconImageView.setImageResource(iconResourceId);
    290                 iconImageView.setVisibility(View.VISIBLE);
    291                 updateViewSize(iconImageView);
    292             }
    293         }
    294     }
    295 
    296     private void updateViewSize(ImageView iconView) {
    297         int intrinsicWidth = iconView.getDrawable().getIntrinsicWidth();
    298         LayoutParams lp = iconView.getLayoutParams();
    299         if (intrinsicWidth > 0) {
    300             lp.height = lp.width * iconView.getDrawable().getIntrinsicHeight()
    301                     / intrinsicWidth;
    302         } else {
    303             // If no intrinsic width, then just mke this a square.
    304             lp.height = lp.width;
    305         }
    306     }
    307 
    308     private void addShadow(ImageView icon, View view) {
    309         FrameLayoutWithShadows shadowLayout = (FrameLayoutWithShadows)
    310                 view.findViewById(R.id.shadow_layout);
    311         shadowLayout.addShadowView(icon);
    312     }
    313 
    314     private void fadeIn(View v) {
    315         v.setAlpha(0f);
    316         ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(v, "alpha", 1f);
    317         alphaAnimator.setDuration(mActivity.getResources().getInteger(
    318                 android.R.integer.config_mediumAnimTime));
    319         alphaAnimator.start();
    320     }
    321 }
    322