Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2010, 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 | |
| 17 | package android.content; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.Message; |
| 21 | import android.os.RemoteException; |
| 22 | import android.os.Handler; |
| 23 | import android.os.IBinder; |
| 24 | import android.os.ServiceManager; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.ArrayList; |
| 27 | |
| 28 | /** |
| 29 | * Interface to the clipboard service, for placing and retrieving text in |
| 30 | * the global clipboard. |
| 31 | * |
| 32 | * <p> |
| 33 | * You do not instantiate this class directly; instead, retrieve it through |
| 34 | * {@link android.content.Context#getSystemService}. |
| 35 | * |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 36 | * <p> |
| 37 | * The ClipboardManager API itself is very simple: it consists of methods |
| 38 | * to atomically get and set the current primary clipboard data. That data |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 39 | * is expressed as a {@link ClipData} object, which defines the protocol |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 40 | * for data exchange between applications. |
| 41 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 42 | * <div class="special reference"> |
| 43 | * <h3>Developer Guides</h3> |
| 44 | * <p>For more information about using the clipboard framework, read the |
| 45 | * <a href="{@docRoot}guide/topics/clipboard/copy-paste.html">Copy and Paste</a> |
| 46 | * developer guide.</p> |
| 47 | * </div> |
| 48 | * |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 49 | * @see android.content.Context#getSystemService |
| 50 | */ |
| 51 | public class ClipboardManager extends android.text.ClipboardManager { |
| 52 | private final static Object sStaticLock = new Object(); |
| 53 | private static IClipboard sService; |
| 54 | |
| 55 | private final Context mContext; |
| 56 | |
| 57 | private final ArrayList<OnPrimaryClipChangedListener> mPrimaryClipChangedListeners |
| 58 | = new ArrayList<OnPrimaryClipChangedListener>(); |
| 59 | |
| 60 | private final IOnPrimaryClipChangedListener.Stub mPrimaryClipChangedServiceListener |
| 61 | = new IOnPrimaryClipChangedListener.Stub() { |
| 62 | public void dispatchPrimaryClipChanged() { |
| 63 | mHandler.sendEmptyMessage(MSG_REPORT_PRIMARY_CLIP_CHANGED); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | static final int MSG_REPORT_PRIMARY_CLIP_CHANGED = 1; |
| 68 | |
| 69 | private final Handler mHandler = new Handler() { |
| 70 | @Override |
| 71 | public void handleMessage(Message msg) { |
| 72 | switch (msg.what) { |
| 73 | case MSG_REPORT_PRIMARY_CLIP_CHANGED: |
| 74 | reportPrimaryClipChanged(); |
| 75 | } |
| 76 | } |
| 77 | }; |
| 78 | |
Joe Malin | 413e9b3 | 2011-12-15 15:42:25 -0800 | [diff] [blame] | 79 | /** |
| 80 | * Defines a listener callback that is invoked when the primary clip on the clipboard changes. |
| 81 | * Objects that want to register a listener call |
| 82 | * {@link android.content.ClipboardManager#addPrimaryClipChangedListener(OnPrimaryClipChangedListener) |
| 83 | * addPrimaryClipChangedListener()} with an |
| 84 | * object that implements OnPrimaryClipChangedListener. |
| 85 | * |
| 86 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 87 | public interface OnPrimaryClipChangedListener { |
Joe Malin | 413e9b3 | 2011-12-15 15:42:25 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Callback that is invoked by {@link android.content.ClipboardManager} when the primary |
| 91 | * clip changes. |
| 92 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 93 | void onPrimaryClipChanged(); |
| 94 | } |
| 95 | |
| 96 | static private IClipboard getService() { |
| 97 | synchronized (sStaticLock) { |
| 98 | if (sService != null) { |
| 99 | return sService; |
| 100 | } |
| 101 | IBinder b = ServiceManager.getService("clipboard"); |
| 102 | sService = IClipboard.Stub.asInterface(b); |
| 103 | return sService; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** {@hide} */ |
| 108 | public ClipboardManager(Context context, Handler handler) { |
| 109 | mContext = context; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Sets the current primary clip on the clipboard. This is the clip that |
| 114 | * is involved in normal cut and paste operations. |
| 115 | * |
| 116 | * @param clip The clipped data item to set. |
| 117 | */ |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 118 | public void setPrimaryClip(ClipData clip) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 119 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 120 | if (clip != null) { |
| 121 | clip.prepareToLeaveProcess(); |
| 122 | } |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 123 | getService().setPrimaryClip(clip, mContext.getOpPackageName()); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 124 | } catch (RemoteException e) { |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns the current primary clip on the clipboard. |
| 130 | */ |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 131 | public ClipData getPrimaryClip() { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 132 | try { |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 133 | return getService().getPrimaryClip(mContext.getOpPackageName()); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 134 | } catch (RemoteException e) { |
| 135 | return null; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 140 | * Returns a description of the current primary clip on the clipboard |
| 141 | * but not a copy of its data. |
| 142 | */ |
| 143 | public ClipDescription getPrimaryClipDescription() { |
| 144 | try { |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 145 | return getService().getPrimaryClipDescription(mContext.getOpPackageName()); |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 146 | } catch (RemoteException e) { |
| 147 | return null; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 152 | * Returns true if there is currently a primary clip on the clipboard. |
| 153 | */ |
| 154 | public boolean hasPrimaryClip() { |
| 155 | try { |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 156 | return getService().hasPrimaryClip(mContext.getOpPackageName()); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 157 | } catch (RemoteException e) { |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | public void addPrimaryClipChangedListener(OnPrimaryClipChangedListener what) { |
| 163 | synchronized (mPrimaryClipChangedListeners) { |
| 164 | if (mPrimaryClipChangedListeners.size() == 0) { |
| 165 | try { |
| 166 | getService().addPrimaryClipChangedListener( |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 167 | mPrimaryClipChangedServiceListener, mContext.getOpPackageName()); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 168 | } catch (RemoteException e) { |
| 169 | } |
| 170 | } |
| 171 | mPrimaryClipChangedListeners.add(what); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | public void removePrimaryClipChangedListener(OnPrimaryClipChangedListener what) { |
| 176 | synchronized (mPrimaryClipChangedListeners) { |
| 177 | mPrimaryClipChangedListeners.remove(what); |
| 178 | if (mPrimaryClipChangedListeners.size() == 0) { |
| 179 | try { |
| 180 | getService().removePrimaryClipChangedListener( |
| 181 | mPrimaryClipChangedServiceListener); |
| 182 | } catch (RemoteException e) { |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @deprecated Use {@link #getPrimaryClip()} instead. This retrieves |
| 190 | * the primary clip and tries to coerce it to a string. |
| 191 | */ |
| 192 | public CharSequence getText() { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 193 | ClipData clip = getPrimaryClip(); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 194 | if (clip != null && clip.getItemCount() > 0) { |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 195 | return clip.getItemAt(0).coerceToText(mContext); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 196 | } |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | /** |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 201 | * @deprecated Use {@link #setPrimaryClip(ClipData)} instead. This |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 202 | * creates a ClippedItem holding the given text and sets it as the |
| 203 | * primary clip. It has no label or icon. |
| 204 | */ |
| 205 | public void setText(CharSequence text) { |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 206 | setPrimaryClip(ClipData.newPlainText(null, text)); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 210 | * @deprecated Use {@link #hasPrimaryClip()} instead. |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 211 | */ |
| 212 | public boolean hasText() { |
| 213 | try { |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 214 | return getService().hasClipboardText(mContext.getOpPackageName()); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 215 | } catch (RemoteException e) { |
| 216 | return false; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void reportPrimaryClipChanged() { |
| 221 | Object[] listeners; |
| 222 | |
| 223 | synchronized (mPrimaryClipChangedListeners) { |
| 224 | final int N = mPrimaryClipChangedListeners.size(); |
| 225 | if (N <= 0) { |
| 226 | return; |
| 227 | } |
| 228 | listeners = mPrimaryClipChangedListeners.toArray(); |
| 229 | } |
| 230 | |
| 231 | for (int i=0; i<listeners.length; i++) { |
| 232 | ((OnPrimaryClipChangedListener)listeners[i]).onPrimaryClipChanged(); |
| 233 | } |
| 234 | } |
| 235 | } |