• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.widget;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.net.Uri;
22 import android.view.Gravity;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.widget.ImageView;
26 import android.widget.TextView;
27 import android.widget.Toast;
28 
29 import com.android.tv.settings.R;
30 import com.android.tv.settings.widget.BitmapDownloader.BitmapCallback;
31 
32 /**
33  * Implementation of the SettingsToast notification.
34  */
35 public class SettingsToast extends Toast {
36 
37     protected final Context mContext;
38     protected final TextView mTextView;
39     protected final ImageView mIconView;
40     protected BitmapCallback mBitmapCallBack;
41 
42     /**
43      * Constructs a SettingsToast message with a text message.
44      *
45      * @param context  The context to use.  Usually your {@link android.app.Application}
46      *                 or {@link android.app.Activity} object.
47      * @param text     The text to show.  Can be formatted text.
48      */
SettingsToast(Context context, CharSequence text)49     public SettingsToast(Context context, CharSequence text) {
50         super(context);
51 
52         mContext = context;
53 
54         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
55                 Context.LAYOUT_INFLATER_SERVICE);
56         View layout = inflater.inflate(R.layout.toast_notification, null);
57 
58         mTextView = (TextView) layout.findViewById(R.id.text);
59         if (mTextView != null) {
60             mTextView.setText(text);
61         }
62 
63         mIconView = (ImageView) layout.findViewById(R.id.icon);
64 
65         setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
66         setView(layout);
67     }
68 
69     /**
70      * Constructs a SettingsToast message with a text message and an icon.
71      *
72      * @param context The context to use. Usually your
73      *            {@link android.app.Application} or
74      *            {@link android.app.Activity} object.
75      * @param text The text to show. Can be formatted text.
76      * @param iconUri URI String identifying the Icon to be used in this
77      *            notification.
78      */
SettingsToast(Context context, CharSequence text, String iconUri)79     public SettingsToast(Context context, CharSequence text, String iconUri) {
80         this(context, text);
81 
82         if (mIconView != null && iconUri != null) {
83             mIconView.setVisibility(View.INVISIBLE);
84 
85             BitmapDownloader bitmapDownloader = BitmapDownloader.getInstance(mContext);
86             mBitmapCallBack = new BitmapCallback() {
87                     @Override
88                 public void onBitmapRetrieved(Bitmap bitmap) {
89                     mIconView.setImageBitmap(bitmap);
90                     mIconView.setVisibility(View.VISIBLE);
91                 }
92             };
93 
94             bitmapDownloader.getBitmap(new BitmapWorkerOptions.Builder(mContext).resource(
95                     Uri.parse(iconUri)).width(mIconView.getLayoutParams().width)
96                     .height(mIconView.getLayoutParams().height).build(), mBitmapCallBack);
97         }
98     }
99 
100     /**
101      * Constructs a SettingsToast message with a text message and a Bitmap icon.
102      *
103      * @param context The context to use. Usually your
104      *            {@link android.app.Application} or
105      *            {@link android.app.Activity} object.
106      * @param text The text to show. Can be formatted text.
107      * @param iconBitmap Bitmap Icon to be used in this toast notification.
108      */
SettingsToast(Context context, CharSequence text, Bitmap iconBitmap)109     public SettingsToast(Context context, CharSequence text, Bitmap iconBitmap) {
110         this(context, text);
111 
112         if (mIconView != null && iconBitmap != null) {
113             mIconView.setImageBitmap(iconBitmap);
114             mIconView.setVisibility(View.VISIBLE);
115         }
116     }
117 
118     @Override
finalize()119     public void finalize() throws Throwable {
120         if (mBitmapCallBack != null) {
121             BitmapDownloader bitmapDownloader = BitmapDownloader.getInstance(mContext);
122             bitmapDownloader.cancelDownload(mBitmapCallBack);
123         }
124         super.finalize();
125     }
126 }
127