Example usage for android.graphics.drawable BitmapDrawable setBounds

List of usage examples for android.graphics.drawable BitmapDrawable setBounds

Introduction

In this page you can find the example usage for android.graphics.drawable BitmapDrawable setBounds.

Prototype

public void setBounds(int left, int top, int right, int bottom) 

Source Link

Document

Specify a bounding rectangle for the Drawable.

Usage

From source file:ch.dbrgn.android.simplerepost.activities.RepostActivity.java

/**
 * Add the watermark from the specified resource file onto the
 * specified image.//from   w w  w .  ja  v  a2s.co  m
 */
private Bitmap addWatermark(String filename, int watermarkResourceFile) {
    // Read background into drawable
    BitmapDrawable background = null;
    try {
        final InputStream is = openFileInput(filename);
        background = new BitmapDrawable(getResources(), is);
        is.close();
    } catch (FileNotFoundException e) {
        ToastHelper.showShortToast(this, "Could not find downloaded image on filesystem");
        Log.e(LOG_TAG, "IOException: " + e.toString());
        return null;
    } catch (IOException e) {
        Log.w(LOG_TAG, "Could not close InputStream");
    }

    // Read watermark into Drawable
    final InputStream is = getResources().openRawResource(watermarkResourceFile);
    final BitmapDrawable watermark = new BitmapDrawable(getResources(), is);
    try {
        is.close();
    } catch (IOException e) {
        Log.w(LOG_TAG, "Could not close InputStream");
    }

    // Get dimensions
    int w = background.getBitmap().getWidth();
    int h = background.getBitmap().getHeight();

    // Create canvas for final output
    Bitmap watermarked = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(watermarked);

    // Write background onto canvas
    background.setBounds(0, 0, w, h);
    background.draw(canvas);
    background.getBitmap().recycle();

    // Write watermark onto canvas
    watermark.setBounds(0, 0, w, h);
    watermark.draw(canvas);
    watermark.getBitmap().recycle();

    return watermarked;
}

From source file:org.zirco.ui.activities.MainActivity.java

/**
 * Get a Drawable of the current favicon, with its size normalized relative to current screen density.
 * @return The normalized favicon.//  w  w w . ja  va 2  s  .co  m
 */
private BitmapDrawable getNormalizedFavicon() {

    BitmapDrawable favIcon = new BitmapDrawable(getResources(), mCurrentWebView.getFavicon());

    if (mCurrentWebView.getFavicon() != null) {
        int imageButtonSize = ApplicationUtils.getImageButtonSize(this);
        int favIconSize = ApplicationUtils.getFaviconSize(this);

        Bitmap bm = Bitmap.createBitmap(imageButtonSize, imageButtonSize, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bm);

        favIcon.setBounds((imageButtonSize / 2) - (favIconSize / 2), (imageButtonSize / 2) - (favIconSize / 2),
                (imageButtonSize / 2) + (favIconSize / 2), (imageButtonSize / 2) + (favIconSize / 2));
        favIcon.draw(canvas);

        favIcon = new BitmapDrawable(getResources(), bm);
    }

    return favIcon;
}

From source file:com.android.launcher2.AsyncTaskCallback.java

private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId, int cellHSpan,
        int cellVSpan, int maxWidth, int maxHeight) {
    // Load the preview image if possible
    String packageName = provider.getPackageName();
    if (maxWidth < 0)
        maxWidth = Integer.MAX_VALUE;
    if (maxHeight < 0)
        maxHeight = Integer.MAX_VALUE;

    Drawable drawable = null;//from  w w w  .  j  a va2s .  c  o  m
    if (previewImage != 0) {
        drawable = mPackageManager.getDrawable(packageName, previewImage, null);
        if (drawable == null) {
            Log.w(TAG, "Can't load widget preview drawable 0x" + Integer.toHexString(previewImage)
                    + " for provider: " + provider);
        }
    }

    int bitmapWidth;
    int bitmapHeight;
    Bitmap defaultPreview = null;
    boolean widgetPreviewExists = (drawable != null);
    if (widgetPreviewExists) {
        bitmapWidth = drawable.getIntrinsicWidth();
        bitmapHeight = drawable.getIntrinsicHeight();
    } else {
        // Generate a preview image if we couldn't load one
        if (cellHSpan < 1)
            cellHSpan = 1;
        if (cellVSpan < 1)
            cellVSpan = 1;

        BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
                .getDrawable(R.drawable.widget_preview_tile);
        final int previewDrawableWidth = previewDrawable.getIntrinsicWidth();
        final int previewDrawableHeight = previewDrawable.getIntrinsicHeight();
        bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
        bitmapHeight = previewDrawableHeight * cellVSpan;

        defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
        final Canvas c = mCachedAppWidgetPreviewCanvas.get();
        c.setBitmap(defaultPreview);
        previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
        previewDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        previewDrawable.draw(c);
        c.setBitmap(null);

        // Draw the icon in the top left corner
        int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
        int smallestSide = Math.min(bitmapWidth, bitmapHeight);
        float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);

        try {
            Drawable icon = null;
            int hoffset = (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
            int yoffset = (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
            if (iconId > 0)
                icon = mIconCache.getFullResIcon(packageName, iconId);
            if (icon != null) {
                renderDrawableToBitmap(icon, defaultPreview, hoffset, yoffset, (int) (mAppIconSize * iconScale),
                        (int) (mAppIconSize * iconScale));
            }
        } catch (Resources.NotFoundException e) {
        }
    }

    // Scale to fit width only - let the widget preview be clipped in the
    // vertical dimension
    float scale = 1f;
    if (bitmapWidth > maxWidth) {
        scale = maxWidth / (float) bitmapWidth;
    }
    if (scale != 1f) {
        bitmapWidth = (int) (scale * bitmapWidth);
        bitmapHeight = (int) (scale * bitmapHeight);
    }

    Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);

    // Draw the scaled preview into the final bitmap
    if (widgetPreviewExists) {
        renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
    } else {
        final Canvas c = mCachedAppWidgetPreviewCanvas.get();
        final Rect src = mCachedAppWidgetPreviewSrcRect.get();
        final Rect dest = mCachedAppWidgetPreviewDestRect.get();
        c.setBitmap(preview);
        src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
        dest.set(0, 0, preview.getWidth(), preview.getHeight());

        Paint p = mCachedAppWidgetPreviewPaint.get();
        if (p == null) {
            p = new Paint();
            p.setFilterBitmap(true);
            mCachedAppWidgetPreviewPaint.set(p);
        }
        c.drawBitmap(defaultPreview, src, dest, p);
        c.setBitmap(null);
    }
    return preview;
}

From source file:org.tvbrowser.tvbrowser.TvBrowser.java

private void sortChannels() {
    ContentResolver cr = getContentResolver();

    StringBuilder where = new StringBuilder(TvBrowserContentProvider.CHANNEL_KEY_SELECTION);
    where.append("=1");

    LinearLayout main = (LinearLayout) getLayoutInflater().inflate(R.layout.channel_sort_list,
            getParentViewGroup(), false);

    Button sortAlphabetically = (Button) main.findViewById(R.id.channel_sort_alpabetically);

    final DynamicListView channelSort = (DynamicListView) main.findViewById(R.id.channel_sort);

    String[] projection = { TvBrowserContentProvider.KEY_ID, TvBrowserContentProvider.CHANNEL_KEY_NAME,
            TvBrowserContentProvider.CHANNEL_KEY_ORDER_NUMBER, TvBrowserContentProvider.CHANNEL_KEY_SELECTION,
            TvBrowserContentProvider.CHANNEL_KEY_LOGO };

    Cursor channels = cr.query(TvBrowserContentProvider.CONTENT_URI_CHANNELS, projection, where.toString(),
            null, TvBrowserContentProvider.CHANNEL_KEY_ORDER_NUMBER);

    final ArrayList<SortInterface> channelSource = new ArrayList<SortInterface>();

    if (channels.moveToFirst()) {
        do {/*from   w  w w . j  a va 2  s  .co m*/
            int key = channels.getInt(0);
            String name = channels.getString(1);

            int order = 0;

            if (!channels.isNull(channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_ORDER_NUMBER))) {
                order = channels
                        .getInt(channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_ORDER_NUMBER));
            }

            Bitmap channelLogo = UiUtils.createBitmapFromByteArray(
                    channels.getBlob(channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_LOGO)));

            if (channelLogo != null) {
                BitmapDrawable l = new BitmapDrawable(getResources(), channelLogo);

                ColorDrawable background = new ColorDrawable(SettingConstants.LOGO_BACKGROUND_COLOR);
                background.setBounds(0, 0, channelLogo.getWidth() + 2, channelLogo.getHeight() + 2);

                LayerDrawable logoDrawable = new LayerDrawable(new Drawable[] { background, l });
                logoDrawable.setBounds(background.getBounds());

                l.setBounds(2, 2, channelLogo.getWidth(), channelLogo.getHeight());

                channelLogo = UiUtils.drawableToBitmap(logoDrawable);
            }

            channelSource.add(new ChannelSort(key, name, order, channelLogo));
        } while (channels.moveToNext());

        channels.close();

        final Comparator<SortInterface> sortComparator = new Comparator<SortInterface>() {
            @Override
            public int compare(SortInterface lhs, SortInterface rhs) {
                if (lhs.getSortNumber() < rhs.getSortNumber()) {
                    return -1;
                } else if (lhs.getSortNumber() > rhs.getSortNumber()) {
                    return 1;
                }

                return 0;
            }
        };

        Collections.sort(channelSource, sortComparator);

        // create default logo for channels without logo
        final Bitmap defaultLogo = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        final StableArrayAdapter<SortInterface> aa = new StableArrayAdapter<SortInterface>(TvBrowser.this,
                R.layout.channel_sort_row, channelSource) {
            public View getView(int position, View convertView, ViewGroup parent) {
                ChannelSort value = (ChannelSort) getItem(position);
                ViewHolder holder = null;

                if (convertView == null) {
                    LayoutInflater mInflater = (LayoutInflater) getContext()
                            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

                    holder = new ViewHolder();

                    convertView = mInflater.inflate(R.layout.channel_sort_row, getParentViewGroup(), false);

                    holder.mTextView = (TextView) convertView.findViewById(R.id.row_of_channel_sort_text);
                    holder.mSortNumber = (TextView) convertView.findViewById(R.id.row_of_channel_sort_number);
                    holder.mLogo = (ImageView) convertView.findViewById(R.id.row_of_channel_sort_icon);

                    convertView.setTag(holder);

                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                holder.mTextView.setText(value.getName());

                String sortNumber = String.valueOf(value.getSortNumber());

                if (value.getSortNumber() == 0) {
                    sortNumber = "-";
                }

                sortNumber += ".";

                holder.mSortNumber.setText(sortNumber);

                Bitmap logo = value.getLogo();

                if (logo != null) {
                    holder.mLogo.setImageBitmap(logo);
                } else {
                    holder.mLogo.setImageBitmap(defaultLogo);
                }

                return convertView;
            }
        };
        channelSort.setAdapter(aa);
        channelSort.setArrayList(channelSource);
        channelSort.setSortDropListener(new SortDropListener() {
            @Override
            public void dropped(int originalPosition, int position) {
                int startIndex = originalPosition;
                int endIndex = position;

                int droppedPos = position;

                if (originalPosition > position) {
                    startIndex = position;
                    endIndex = originalPosition;
                }

                int previousNumber = 0;

                if (startIndex > 0) {
                    previousNumber = aa.getItem(startIndex - 1).getSortNumber();
                }

                int firstVisible = channelSort.getFirstVisiblePosition();

                for (int i = startIndex; i <= endIndex; i++) {
                    if (i == droppedPos || aa.getItem(i).getSortNumber() != 0) {
                        aa.getItem(i).setSortNumber(++previousNumber);

                        if (i >= firstVisible) {
                            View line = channelSort.getChildAt(i - firstVisible);

                            if (line != null) {
                                ((TextView) line.findViewById(R.id.row_of_channel_sort_number))
                                        .setText(String.valueOf(previousNumber) + ".");
                            }
                        }
                    }
                }

            }
        });

        channelSort.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> adapterView, final View view, final int position,
                    long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(TvBrowser.this);

                LinearLayout numberSelection = (LinearLayout) getLayoutInflater()
                        .inflate(R.layout.sort_number_selection, getParentViewGroup(), false);

                mSelectionNumberChanged = false;

                final NumberPicker number = (NumberPicker) numberSelection.findViewById(R.id.sort_picker);
                number.setMinValue(1);
                number.setMaxValue(channelSource.size());
                number.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
                number.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                    @Override
                    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                        mSelectionNumberChanged = true;
                    }
                });

                final EditText numberAlternative = (EditText) numberSelection
                        .findViewById(R.id.sort_entered_number);

                builder.setView(numberSelection);

                final ChannelSort selection = (ChannelSort) channelSource.get(position);

                TextView name = (TextView) numberSelection.findViewById(R.id.sort_picker_channel_name);
                name.setText(selection.getName());

                if (selection.getSortNumber() > 0) {
                    if (selection.getSortNumber() < channelSource.size() + 1) {
                        number.setValue(selection.getSortNumber());
                    } else {
                        numberAlternative.setText(String.valueOf(selection.getSortNumber()));
                    }
                }

                builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String test = numberAlternative.getText().toString().trim();

                        if (test.length() == 0 || mSelectionNumberChanged) {
                            selection.setSortNumber(number.getValue());
                        } else {
                            try {
                                selection.setSortNumber(Integer.parseInt(test));
                            } catch (NumberFormatException e1) {
                            }
                        }

                        Collections.sort(channelSource, sortComparator);
                        aa.notifyDataSetChanged();
                    }
                });

                builder.setNegativeButton(android.R.string.cancel, null);

                builder.show();
            }
        });

        sortAlphabetically.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Collections.sort(channelSource, new Comparator<SortInterface>() {
                    @Override
                    public int compare(SortInterface lhs, SortInterface rhs) {
                        return lhs.getName().compareToIgnoreCase(rhs.getName());
                    }
                });

                for (int i = 0; i < channelSource.size(); i++) {
                    channelSource.get(i).setSortNumber(i + 1);
                }

                aa.notifyDataSetChanged();
            }
        });

        AlertDialog.Builder builder = new AlertDialog.Builder(TvBrowser.this);

        builder.setTitle(R.string.action_sort_channels);
        builder.setView(main);

        builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                boolean somethingChanged = false;

                for (SortInterface selection : channelSource) {
                    if (((ChannelSort) selection).wasChanged()) {
                        somethingChanged = true;

                        ContentValues values = new ContentValues();
                        values.put(TvBrowserContentProvider.CHANNEL_KEY_ORDER_NUMBER,
                                selection.getSortNumber());

                        getContentResolver().update(
                                ContentUris.withAppendedId(TvBrowserContentProvider.CONTENT_URI_CHANNELS,
                                        ((ChannelSort) selection).getKey()),
                                values, null, null);
                    }
                }

                if (somethingChanged) {
                    updateProgramListChannelBar();
                }
            }
        });
        builder.setNegativeButton(android.R.string.cancel, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builder.show();
    }
}

From source file:org.tvbrowser.tvbrowser.TvBrowser.java

private void showChannelSelectionInternal(final String selection, final String title, final String help,
        final boolean delete) {
    String[] projection = {/*from  ww  w .j  av a  2  s  .c o m*/
            TvBrowserContentProvider.CHANNEL_TABLE + "." + TvBrowserContentProvider.KEY_ID + " AS "
                    + TvBrowserContentProvider.KEY_ID,
            TvBrowserContentProvider.GROUP_KEY_DATA_SERVICE_ID, TvBrowserContentProvider.CHANNEL_KEY_NAME,
            TvBrowserContentProvider.CHANNEL_KEY_SELECTION, TvBrowserContentProvider.CHANNEL_KEY_CATEGORY,
            TvBrowserContentProvider.CHANNEL_KEY_LOGO, TvBrowserContentProvider.CHANNEL_KEY_ALL_COUNTRIES };

    ContentResolver cr = getContentResolver();
    Cursor channels = cr.query(TvBrowserContentProvider.CONTENT_URI_CHANNELS_WITH_GROUP, projection, selection,
            null, TvBrowserContentProvider.CHANNEL_KEY_NAME);
    channels.moveToPosition(-1);

    // populate array list with all available channels
    final ArrayListWrapper channelSelectionList = new ArrayListWrapper();
    ArrayList<Country> countryList = new ArrayList<Country>();

    int channelIdColumn = channels.getColumnIndex(TvBrowserContentProvider.KEY_ID);
    int categoryColumn = channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_CATEGORY);
    int logoColumn = channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_LOGO);
    int dataServiceColumn = channels.getColumnIndex(TvBrowserContentProvider.GROUP_KEY_DATA_SERVICE_ID);
    int nameColumn = channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_NAME);
    int countyColumn = channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_ALL_COUNTRIES);
    int selectionColumn = channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_SELECTION);
    ;
    while (channels.moveToNext()) {
        int channelID = channels.getInt(channelIdColumn);
        int category = channels.getInt(categoryColumn);
        byte[] logo = channels.getBlob(logoColumn);
        String dataService = channels.getString(dataServiceColumn);
        String name = channels.getString(nameColumn);
        String countries = channels.getString(countyColumn);
        boolean isSelected = channels.getInt(selectionColumn) == 1 && !delete;

        if (countries.contains("$")) {
            String[] values = countries.split("\\$");

            for (String country : values) {
                Country test = new Country(new Locale(country, country));

                if (!countryList.contains(test) && test.mLocale.getDisplayCountry().trim().length() > 0) {
                    countryList.add(test);
                }
            }
        } else {
            Country test = new Country(new Locale(countries, countries));

            if (!countryList.contains(test) && test.mLocale.getDisplayCountry().trim().length() > 0) {
                countryList.add(test);
            }
        }

        Bitmap channelLogo = UiUtils.createBitmapFromByteArray(logo);

        if (channelLogo != null) {
            BitmapDrawable l = new BitmapDrawable(getResources(), channelLogo);

            ColorDrawable background = new ColorDrawable(SettingConstants.LOGO_BACKGROUND_COLOR);
            background.setBounds(0, 0, channelLogo.getWidth() + 2, channelLogo.getHeight() + 2);

            LayerDrawable logoDrawable = new LayerDrawable(new Drawable[] { background, l });
            logoDrawable.setBounds(background.getBounds());

            l.setBounds(2, 2, channelLogo.getWidth(), channelLogo.getHeight());

            channelLogo = UiUtils.drawableToBitmap(logoDrawable);
        }

        channelSelectionList.add(new ChannelSelection(channelID, name, category, countries, channelLogo,
                isSelected, SettingConstants.EPG_DONATE_KEY.equals(dataService)));
    }

    // sort countries for filtering
    Collections.sort(countryList, new Comparator<Country>() {
        @Override
        public int compare(Country lhs, Country rhs) {
            return lhs.toString().compareToIgnoreCase(rhs.toString());
        }
    });

    countryList.add(0, new Country(null));

    channels.close();

    // create filter for filtering of category and country
    final ChannelFilter filter = new ChannelFilter(SettingConstants.TV_CATEGORY, null);

    // create default logo for channels without logo
    final Bitmap defaultLogo = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

    final Set<String> firstDeletedChannels = PrefUtils.getStringSetValue(R.string.PREF_FIRST_DELETED_CHANNELS,
            new HashSet<String>());
    final Set<String> keptDeletedChannels = PrefUtils.getStringSetValue(R.string.PREF_KEPT_DELETED_CHANNELS,
            new HashSet<String>());

    final int firstDeletedColor = getResources().getColor(R.color.pref_first_deleted_channels);
    final int keptDeletedColor = getResources().getColor(R.color.pref_kept_deleted_channels);

    // Custom array adapter for channel selection
    final ArrayAdapter<ChannelSelection> channelSelectionAdapter = new ArrayAdapter<ChannelSelection>(
            TvBrowser.this, R.layout.channel_row, channelSelectionList) {
        public View getView(int position, View convertView, ViewGroup parent) {
            ChannelSelection value = getItem(position);
            ViewHolder holder = null;

            if (convertView == null) {
                LayoutInflater mInflater = (LayoutInflater) getContext()
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

                holder = new ViewHolder();

                convertView = mInflater.inflate(R.layout.channel_row, getParentViewGroup(), false);

                holder.mTextView = (TextView) convertView.findViewById(R.id.row_of_channel_text);
                holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.row_of_channel_selection);
                holder.mLogo = (ImageView) convertView.findViewById(R.id.row_of_channel_icon);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            SpannableStringBuilder nameBuilder = new SpannableStringBuilder(value.toString());

            String channelID = String.valueOf(value.getChannelID());

            if (keptDeletedChannels.contains(channelID)) {
                nameBuilder.setSpan(new ForegroundColorSpan(keptDeletedColor), 0, value.toString().length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else if (firstDeletedChannels.contains(channelID)) {
                nameBuilder.setSpan(new ForegroundColorSpan(firstDeletedColor), 0, value.toString().length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (value.isEpgDonateChannel()) {
                nameBuilder.append("\n(EPGdonate)");
                nameBuilder.setSpan(new RelativeSizeSpan(0.65f), value.toString().length(),
                        nameBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            holder.mTextView.setText(nameBuilder);
            holder.mCheckBox.setChecked(value.isSelected());

            Bitmap logo = value.getLogo();

            if (logo != null) {
                holder.mLogo.setImageBitmap(logo);
            } else {
                holder.mLogo.setImageBitmap(defaultLogo);
            }

            return convertView;
        }
    };

    // inflate channel selection view
    View channelSelectionView = getLayoutInflater().inflate(R.layout.dialog_channel_selection_list,
            getParentViewGroup(), false);
    channelSelectionView.findViewById(R.id.channel_selection_selection_buttons).setVisibility(View.GONE);
    channelSelectionView.findViewById(R.id.channel_selection_input_id_name).setVisibility(View.GONE);

    TextView infoView = (TextView) channelSelectionView.findViewById(R.id.channel_selection_label_id_name);

    if (help != null) {
        infoView.setText(help);
        infoView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                getResources().getDimension(R.dimen.epg_donate_info_font_size));
    } else {
        infoView.setVisibility(View.GONE);
    }

    // get spinner for country filtering and create array adapter with all available countries
    Spinner country = (Spinner) channelSelectionView.findViewById(R.id.channel_country_value);

    final ArrayAdapter<Country> countryListAdapter = new ArrayAdapter<Country>(this,
            android.R.layout.simple_spinner_item, countryList);
    countryListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    country.setAdapter(countryListAdapter);

    // add item selection listener to react of user setting filter for country
    country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Country country = countryListAdapter.getItem(position);

            filter.mCountry = country.getCountry();
            channelSelectionList.setFilter(filter);
            channelSelectionAdapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    // get spinner for category selection and add listener to react to user category selection
    Spinner category = (Spinner) channelSelectionView.findViewById(R.id.channel_category_value);
    category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
            case 1:
                filter.mCategory = SettingConstants.TV_CATEGORY;
                break;
            case 2:
                filter.mCategory = SettingConstants.RADIO_CATEGORY;
                break;
            case 3:
                filter.mCategory = SettingConstants.CINEMA_CATEGORY;
                break;

            default:
                filter.mCategory = SettingConstants.NO_CATEGORY;
                break;
            }

            channelSelectionList.setFilter(filter);
            channelSelectionAdapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    if (delete) {
        channelSelectionView.findViewById(R.id.channel_country_label).setVisibility(View.GONE);
        channelSelectionView.findViewById(R.id.channel_category_label).setVisibility(View.GONE);

        country.setVisibility(View.GONE);
        category.setVisibility(View.GONE);
    }

    // get the list view of the layout and add adapter with available channels
    ListView list = (ListView) channelSelectionView.findViewById(R.id.channel_selection_list);
    list.setAdapter(channelSelectionAdapter);

    // add listener to react to user selection of channels
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CheckBox check = (CheckBox) view.findViewById(R.id.row_of_channel_selection);

            if (check != null) {
                check.setChecked(!check.isChecked());
                channelSelectionAdapter.getItem(position).setSelected(check.isChecked());
            }
        }
    });

    // show dialog only if channels are available
    if (!channelSelectionList.isEmpty()) {
        AlertDialog.Builder builder = new AlertDialog.Builder(TvBrowser.this);

        if (title == null) {
            builder.setTitle(R.string.select_channels);
        } else {
            builder.setTitle(title);
        }

        builder.setView(channelSelectionView);

        builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                boolean somethingSelected = false;
                boolean somethingChanged = false;

                Iterator<ChannelSelection> it = channelSelectionList.superIterator();

                StringBuilder deleteWhere = new StringBuilder();
                HashSet<String> keep = new HashSet<String>();

                while (it.hasNext()) {
                    ChannelSelection sel = it.next();

                    if (sel.isSelected() && !sel.wasSelected()) {
                        somethingChanged = somethingSelected = true;

                        if (delete) {
                            if (deleteWhere.length() > 0) {
                                deleteWhere.append(", ");
                            }

                            deleteWhere.append(sel.getChannelID());
                        } else {
                            ContentValues values = new ContentValues();

                            values.put(TvBrowserContentProvider.CHANNEL_KEY_SELECTION, 1);

                            getContentResolver().update(
                                    ContentUris.withAppendedId(TvBrowserContentProvider.CONTENT_URI_CHANNELS,
                                            sel.getChannelID()),
                                    values, null, null);
                        }
                    } else if (!sel.isSelected() && sel.wasSelected()) {
                        somethingChanged = true;

                        ContentValues values = new ContentValues();

                        values.put(TvBrowserContentProvider.CHANNEL_KEY_SELECTION, 0);

                        getContentResolver().update(
                                ContentUris.withAppendedId(TvBrowserContentProvider.CONTENT_URI_CHANNELS,
                                        sel.getChannelID()),
                                values, null, null);

                        getContentResolver().delete(TvBrowserContentProvider.CONTENT_URI_DATA_VERSION,
                                TvBrowserContentProvider.CHANNEL_KEY_CHANNEL_ID + "=" + sel.getChannelID(),
                                null);
                        getContentResolver().delete(TvBrowserContentProvider.CONTENT_URI_DATA,
                                TvBrowserContentProvider.CHANNEL_KEY_CHANNEL_ID + "=" + sel.getChannelID(),
                                null);
                    } else if (delete && !sel.isSelected()) {
                        keep.add(String.valueOf(sel.getChannelID()));
                    }
                }

                if (delete) {
                    if (deleteWhere.length() > 0) {
                        deleteWhere.insert(0, TvBrowserContentProvider.KEY_ID + " IN ( ");
                        deleteWhere.append(" ) ");

                        Log.d("info2", "DELETE WHERE FOR REMOVED CHANNELS " + deleteWhere.toString());

                        int count = getContentResolver().delete(TvBrowserContentProvider.CONTENT_URI_CHANNELS,
                                deleteWhere.toString(), null);

                        Log.d("info2", "REMOVED CHANNELS COUNT " + count);
                    }

                    Editor edit = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this).edit();
                    edit.putStringSet(getString(R.string.PREF_KEPT_DELETED_CHANNELS), keep);
                    edit.commit();
                }

                // if something was changed we need to update channel list bar in program list and the complete program table
                if (somethingChanged) {
                    SettingConstants.initializeLogoMap(TvBrowser.this, true);
                    updateProgramListChannelBar();
                }

                // if something was selected we need to download new data
                if (somethingSelected && !delete) {
                    checkTermsAccepted();
                }
            }
        });

        builder.setNegativeButton(android.R.string.cancel, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (delete) {
                    HashSet<String> keep = new HashSet<String>();
                    Iterator<ChannelSelection> it = channelSelectionList.superIterator();

                    while (it.hasNext()) {
                        ChannelSelection sel = it.next();

                        keep.add(String.valueOf(sel.getChannelID()));
                    }

                    Editor edit = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this).edit();
                    edit.putStringSet(getString(R.string.PREF_KEPT_DELETED_CHANNELS), keep);
                    edit.commit();
                }
            }
        });

        builder.show();
    }

    selectingChannels = false;
}