FreeRDP
ClipboardManagerProxy.java
1 package com.freerdp.freerdpcore.utils;
2 
3 import android.annotation.TargetApi;
4 import android.content.ClipData;
5 import android.content.ClipboardManager;
6 import android.content.Context;
7 import android.os.Build.VERSION;
8 import android.os.Build.VERSION_CODES;
9 
10 public abstract class ClipboardManagerProxy
11 {
12 
13  public static ClipboardManagerProxy getClipboardManager(Context ctx)
14  {
15  if (VERSION.SDK_INT < VERSION_CODES.HONEYCOMB)
16  return new PreHCClipboardManager(ctx);
17  else
18  return new HCClipboardManager(ctx);
19  }
20 
21  public abstract void setClipboardData(String data);
22 
23  public abstract void addClipboardChangedListener(OnClipboardChangedListener listener);
24 
25  public abstract void removeClipboardboardChangedListener(OnClipboardChangedListener listener);
26 
27  public abstract void getPrimaryClipManually();
28 
29  public static interface OnClipboardChangedListener {
30  void onClipboardChanged(String data);
31  }
32 
33  private static class PreHCClipboardManager extends ClipboardManagerProxy
34  {
35 
36  public PreHCClipboardManager(Context ctx)
37  {
38  }
39 
40  @Override public void setClipboardData(String data)
41  {
42  }
43 
44  @Override public void addClipboardChangedListener(OnClipboardChangedListener listener)
45  {
46  }
47 
48  @Override
49  public void removeClipboardboardChangedListener(OnClipboardChangedListener listener)
50  {
51  }
52 
53  @Override public void getPrimaryClipManually()
54  {
55  }
56  }
57 
58  @TargetApi(11)
59  private static class HCClipboardManager
60  extends ClipboardManagerProxy implements ClipboardManager.OnPrimaryClipChangedListener
61  {
62  private ClipboardManager mClipboardManager;
63  private OnClipboardChangedListener mListener;
64 
65  public HCClipboardManager(Context ctx)
66  {
67  mClipboardManager = (ClipboardManager)ctx.getSystemService(Context.CLIPBOARD_SERVICE);
68  }
69 
70  @Override public void setClipboardData(String data)
71  {
72  mClipboardManager.setPrimaryClip(
73  ClipData.newPlainText("rdp-clipboard", data == null ? "" : data));
74  }
75 
76  @Override public void onPrimaryClipChanged()
77  {
78  ClipData clip = mClipboardManager.getPrimaryClip();
79  String data = null;
80 
81  if (clip != null && clip.getItemCount() > 0)
82  {
83  CharSequence cs = clip.getItemAt(0).getText();
84  if (cs != null)
85  data = cs.toString();
86  }
87  if (mListener != null)
88  {
89  mListener.onClipboardChanged(data);
90  }
91  }
92 
93  @Override public void addClipboardChangedListener(OnClipboardChangedListener listener)
94  {
95  mListener = listener;
96  mClipboardManager.addPrimaryClipChangedListener(this);
97  }
98 
99  @Override
100  public void removeClipboardboardChangedListener(OnClipboardChangedListener listener)
101  {
102  mListener = null;
103  mClipboardManager.removePrimaryClipChangedListener(this);
104  }
105 
106  @Override public void getPrimaryClipManually()
107  {
108  onPrimaryClipChanged();
109  }
110  }
111 }