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