Blame client/Android/Studio/freeRDPCore/src/main/java/com/freerdp/freerdpcore/utils/BookmarkArrayAdapter.java

Packit Service fa4841
/*
Packit Service fa4841
   ArrayAdapter for bookmark lists
Packit Service fa4841
Packit Service fa4841
   Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
Packit Service fa4841
Packit Service bb5c11
   This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
Packit Service bb5c11
   If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit Service fa4841
*/
Packit Service fa4841
Packit Service fa4841
package com.freerdp.freerdpcore.utils;
Packit Service fa4841
Packit Service fa4841
import android.content.Context;
Packit Service fa4841
import android.content.Intent;
Packit Service fa4841
import android.os.Bundle;
Packit Service fa4841
import android.view.LayoutInflater;
Packit Service fa4841
import android.view.View;
Packit Service fa4841
import android.view.View.OnClickListener;
Packit Service fa4841
import android.view.ViewGroup;
Packit Service fa4841
import android.widget.ArrayAdapter;
Packit Service fa4841
import android.widget.ImageView;
Packit Service fa4841
import android.widget.TextView;
Packit Service fa4841
Packit Service fa4841
import com.freerdp.freerdpcore.R;
Packit Service fa4841
import com.freerdp.freerdpcore.domain.BookmarkBase;
Packit Service fa4841
import com.freerdp.freerdpcore.domain.ConnectionReference;
Packit Service fa4841
import com.freerdp.freerdpcore.domain.ManualBookmark;
Packit Service fa4841
import com.freerdp.freerdpcore.domain.PlaceholderBookmark;
Packit Service fa4841
import com.freerdp.freerdpcore.presentation.BookmarkActivity;
Packit Service fa4841
Packit Service fa4841
import java.util.List;
Packit Service fa4841
Packit Service bb5c11
public class BookmarkArrayAdapter extends ArrayAdapter<BookmarkBase> {
Packit Service bb5c11
Packit Service bb5c11
    public BookmarkArrayAdapter(Context context, int textViewResourceId, List<BookmarkBase> objects) {
Packit Service bb5c11
        super(context, textViewResourceId, objects);
Packit Service bb5c11
    }
Packit Service bb5c11
Packit Service bb5c11
    @Override
Packit Service bb5c11
    public View getView(int position, View convertView, ViewGroup parent) {
Packit Service bb5c11
        View curView = convertView;
Packit Service bb5c11
        if (curView == null) {
Packit Service bb5c11
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Packit Service bb5c11
            curView = vi.inflate(R.layout.bookmark_list_item, null);
Packit Service bb5c11
        }
Packit Service bb5c11
Packit Service bb5c11
        BookmarkBase bookmark = getItem(position);
Packit Service bb5c11
        TextView label = (TextView) curView.findViewById(R.id.bookmark_text1);
Packit Service bb5c11
        TextView hostname = (TextView) curView.findViewById(R.id.bookmark_text2);
Packit Service bb5c11
        ImageView star_icon = (ImageView) curView.findViewById(R.id.bookmark_icon2);
Packit Service bb5c11
        assert label != null;
Packit Service bb5c11
        assert hostname != null;
Packit Service bb5c11
Packit Service bb5c11
        label.setText(bookmark.getLabel());
Packit Service bb5c11
        star_icon.setVisibility(View.VISIBLE);
Packit Service bb5c11
Packit Service bb5c11
        String refStr;
Packit Service bb5c11
        if (bookmark.getType() == BookmarkBase.TYPE_MANUAL) {
Packit Service bb5c11
            hostname.setText(bookmark.<ManualBookmark>get().getHostname());
Packit Service bb5c11
            refStr = ConnectionReference.getManualBookmarkReference(bookmark.getId());
Packit Service bb5c11
            star_icon.setImageResource(R.drawable.icon_star_on);
Packit Service bb5c11
        } else if (bookmark.getType() == BookmarkBase.TYPE_QUICKCONNECT) {
Packit Service bb5c11
            // just set an empty hostname (with a blank) - the hostname is already displayed in the label
Packit Service bb5c11
            // and in case we just set it to "" the textview will shrunk
Packit Service bb5c11
            hostname.setText(" ");
Packit Service bb5c11
            refStr = ConnectionReference.getHostnameReference(bookmark.getLabel());
Packit Service bb5c11
            star_icon.setImageResource(R.drawable.icon_star_off);
Packit Service bb5c11
        } else if (bookmark.getType() == BookmarkBase.TYPE_PLACEHOLDER) {
Packit Service bb5c11
            hostname.setText(" ");
Packit Service bb5c11
            refStr = ConnectionReference.getPlaceholderReference(bookmark.<PlaceholderBookmark>get().getName());
Packit Service bb5c11
            star_icon.setVisibility(View.GONE);
Packit Service bb5c11
        } else {
Packit Service bb5c11
            // unknown bookmark type...
Packit Service bb5c11
            refStr = "";
Packit Service bb5c11
            assert false;
Packit Service bb5c11
        }
Packit Service bb5c11
Packit Service bb5c11
        star_icon.setOnClickListener(new OnClickListener() {
Packit Service bb5c11
            @Override
Packit Service bb5c11
            public void onClick(View v) {
Packit Service bb5c11
                // start bookmark editor
Packit Service bb5c11
                Bundle bundle = new Bundle();
Packit Service bb5c11
                String refStr = v.getTag().toString();
Packit Service bb5c11
                bundle.putString(BookmarkActivity.PARAM_CONNECTION_REFERENCE, refStr);
Packit Service bb5c11
Packit Service bb5c11
                Intent bookmarkIntent = new Intent(getContext(), BookmarkActivity.class);
Packit Service bb5c11
                bookmarkIntent.putExtras(bundle);
Packit Service bb5c11
                getContext().startActivity(bookmarkIntent);
Packit Service bb5c11
            }
Packit Service bb5c11
        });
Packit Service bb5c11
Packit Service bb5c11
        curView.setTag(refStr);
Packit Service bb5c11
        star_icon.setTag(refStr);
Packit Service bb5c11
Packit Service bb5c11
        return curView;
Packit Service bb5c11
    }
Packit Service bb5c11
Packit Service bb5c11
    public void addItems(List<BookmarkBase> newItems) {
Packit Service bb5c11
        for (BookmarkBase item : newItems)
Packit Service bb5c11
            add(item);
Packit Service bb5c11
    }
Packit Service bb5c11
Packit Service bb5c11
    public void replaceItems(List<BookmarkBase> newItems) {
Packit Service bb5c11
        clear();
Packit Service bb5c11
        for (BookmarkBase item : newItems)
Packit Service bb5c11
            add(item);
Packit Service bb5c11
    }
Packit Service bb5c11
Packit Service bb5c11
    public void remove(long bookmarkId) {
Packit Service bb5c11
        for (int i = 0; i < getCount(); i++) {
Packit Service bb5c11
            BookmarkBase bm = getItem(i);
Packit Service bb5c11
            if (bm.getId() == bookmarkId) {
Packit Service bb5c11
                remove(bm);
Packit Service bb5c11
                return;
Packit Service bb5c11
            }
Packit Service bb5c11
        }
Packit Service bb5c11
    }
Packit Service fa4841
}