FreeRDP
BookmarkArrayAdapter.java
1 /*
2  ArrayAdapter for bookmark lists
3 
4  Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5 
6  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7  If a copy of the MPL was not distributed with this file, You can obtain one at
8  http://mozilla.org/MPL/2.0/.
9 */
10 
11 package com.freerdp.freerdpcore.utils;
12 
13 import android.content.Context;
14 import android.content.Intent;
15 import android.os.Bundle;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.view.View.OnClickListener;
19 import android.view.ViewGroup;
20 import android.widget.ArrayAdapter;
21 import android.widget.ImageView;
22 import android.widget.TextView;
23 
24 import com.freerdp.freerdpcore.R;
25 import com.freerdp.freerdpcore.domain.BookmarkBase;
26 import com.freerdp.freerdpcore.domain.ConnectionReference;
27 import com.freerdp.freerdpcore.domain.ManualBookmark;
28 import com.freerdp.freerdpcore.domain.PlaceholderBookmark;
29 import com.freerdp.freerdpcore.presentation.BookmarkActivity;
30 
31 import java.util.List;
32 
33 public class BookmarkArrayAdapter extends ArrayAdapter<BookmarkBase>
34 {
35 
36  public BookmarkArrayAdapter(Context context, int textViewResourceId, List<BookmarkBase> objects)
37  {
38  super(context, textViewResourceId, objects);
39  }
40 
41  @Override public View getView(int position, View convertView, ViewGroup parent)
42  {
43  View curView = convertView;
44  if (curView == null)
45  {
46  LayoutInflater vi =
47  (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
48  curView = vi.inflate(R.layout.bookmark_list_item, null);
49  }
50 
51  BookmarkBase bookmark = getItem(position);
52  TextView label = (TextView)curView.findViewById(R.id.bookmark_text1);
53  TextView hostname = (TextView)curView.findViewById(R.id.bookmark_text2);
54  ImageView star_icon = (ImageView)curView.findViewById(R.id.bookmark_icon2);
55  assert label != null;
56  assert hostname != null;
57 
58  label.setText(bookmark.getLabel());
59  star_icon.setVisibility(View.VISIBLE);
60 
61  String refStr;
62  if (bookmark.getType() == BookmarkBase.TYPE_MANUAL)
63  {
64  hostname.setText(bookmark.<ManualBookmark>get().getHostname());
65  refStr = ConnectionReference.getManualBookmarkReference(bookmark.getId());
66  star_icon.setImageResource(R.drawable.icon_star_on);
67  }
68  else if (bookmark.getType() == BookmarkBase.TYPE_QUICKCONNECT)
69  {
70  // just set an empty hostname (with a blank) - the hostname is already displayed in the
71  // label and in case we just set it to "" the textview will shrunk
72  hostname.setText(" ");
73  refStr = ConnectionReference.getHostnameReference(bookmark.getLabel());
74  star_icon.setImageResource(R.drawable.icon_star_off);
75  }
76  else if (bookmark.getType() == BookmarkBase.TYPE_PLACEHOLDER)
77  {
78  hostname.setText(" ");
79  refStr = ConnectionReference.getPlaceholderReference(
80  bookmark.<PlaceholderBookmark>get().getName());
81  star_icon.setVisibility(View.GONE);
82  }
83  else
84  {
85  // unknown bookmark type...
86  refStr = "";
87  assert false;
88  }
89 
90  star_icon.setOnClickListener(new OnClickListener() {
91  @Override public void onClick(View v)
92  {
93  // start bookmark editor
94  Bundle bundle = new Bundle();
95  String refStr = v.getTag().toString();
96  bundle.putString(BookmarkActivity.PARAM_CONNECTION_REFERENCE, refStr);
97 
98  Intent bookmarkIntent = new Intent(getContext(), BookmarkActivity.class);
99  bookmarkIntent.putExtras(bundle);
100  getContext().startActivity(bookmarkIntent);
101  }
102  });
103 
104  curView.setTag(refStr);
105  star_icon.setTag(refStr);
106 
107  return curView;
108  }
109 
110  public void addItems(List<BookmarkBase> newItems)
111  {
112  for (BookmarkBase item : newItems)
113  add(item);
114  }
115 
116  public void replaceItems(List<BookmarkBase> newItems)
117  {
118  clear();
119  for (BookmarkBase item : newItems)
120  add(item);
121  }
122 
123  public void remove(long bookmarkId)
124  {
125  for (int i = 0; i < getCount(); i++)
126  {
127  BookmarkBase bm = getItem(i);
128  if (bm.getId() == bookmarkId)
129  {
130  remove(bm);
131  return;
132  }
133  }
134  }
135 }