Debian Codegraph @

core/java/android/content/ClipboardManager.java

raw symbols SCIP
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.Manifest;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.RequiresPermission;
23 import android.annotation.SystemApi;
24 import android.annotation.SystemService;
25 import android.annotation.TestApi;
26 import android.compat.annotation.UnsupportedAppUsage;
27 import android.os.Handler;
28 import android.os.RemoteException;
29 import android.os.ServiceManager;
30 import android.os.ServiceManager.ServiceNotFoundException;
31
32 import java.util.ArrayList;
33 import java.util.Objects;
34
35 /**
36 * Interface to the clipboard service, for placing and retrieving text in
37 * the global clipboard.
38 *
39 * <p>
40 * The ClipboardManager API itself is very simple: it consists of methods
41 * to atomically get and set the current primary clipboard data. That data
42 * is expressed as a {@link ClipData} object, which defines the protocol
43 * for data exchange between applications.
44 *
45 * <div class="special reference">
46 * <h3>Developer Guides</h3>
47 * <p>For more information about using the clipboard framework, read the
48 * <a href="{@docRoot}guide/topics/clipboard/copy-paste.html">Copy and Paste</a>
49 * developer guide.</p>
50 * </div>
51 */
52 @SystemService(Context.CLIPBOARD_SERVICE)
53 public class ClipboardManager extends android.text.ClipboardManager {
54
55 /**
56 * DeviceConfig property, within the clipboard namespace, that determines whether notifications
57 * are shown when an app accesses clipboard. This may be overridden by a user-controlled
58 * setting.
59 *
60 * @hide
61 */
62 public static final String DEVICE_CONFIG_SHOW_ACCESS_NOTIFICATIONS =
63 "show_access_notifications";
64
65 /**
66 * Default value for the DeviceConfig property that determines whether notifications are shown
67 * when an app accesses clipboard.
68 *
69 * @hide
70 */
71 public static final boolean DEVICE_CONFIG_DEFAULT_SHOW_ACCESS_NOTIFICATIONS = true;
72
73 private final Context mContext;
74 private final Handler mHandler;
75 private final IClipboard mService;
76
77 private final ArrayList<OnPrimaryClipChangedListener> mPrimaryClipChangedListeners
78 = new ArrayList<OnPrimaryClipChangedListener>();
79
80 private final IOnPrimaryClipChangedListener.Stub mPrimaryClipChangedServiceListener
81 = new IOnPrimaryClipChangedListener.Stub() {
82 @Override
83 public void dispatchPrimaryClipChanged() {
84 mHandler.post(() -> {
85 reportPrimaryClipChanged();
86 });
87 }
88 };
89
90 /**
91 * Defines a listener callback that is invoked when the primary clip on the clipboard changes.
92 * Objects that want to register a listener call
93 * {@link android.content.ClipboardManager#addPrimaryClipChangedListener(OnPrimaryClipChangedListener)
94 * addPrimaryClipChangedListener()} with an
95 * object that implements OnPrimaryClipChangedListener.
96 *
97 */
98 public interface OnPrimaryClipChangedListener {
99
100 /**
101 * Callback that is invoked by {@link android.content.ClipboardManager} when the primary
102 * clip changes.
103 *
104 * <p>This is called when the result of {@link ClipDescription#getClassificationStatus()}
105 * changes, as well as when new clip data is set. So in cases where text classification is
106 * performed, this callback may be invoked multiple times for the same clip.
107 */
108 void onPrimaryClipChanged();
109 }
110
111 /** {@hide} */
112 @UnsupportedAppUsage
113 public ClipboardManager(Context context, Handler handler) throws ServiceNotFoundException {
114 mContext = context;
115 mHandler = handler;
116 mService = IClipboard.Stub.asInterface(
117 ServiceManager.getServiceOrThrow(Context.CLIPBOARD_SERVICE));
118 }
119
120 /**
121 * Sets the current primary clip on the clipboard. This is the clip that
122 * is involved in normal cut and paste operations.
123 *
124 * @param clip The clipped data item to set.
125 * @see #getPrimaryClip()
126 * @see #clearPrimaryClip()
127 */
128 public void setPrimaryClip(@NonNull ClipData clip) {
129 try {
130 Objects.requireNonNull(clip);
131 clip.prepareToLeaveProcess(true);
132 mService.setPrimaryClip(
133 clip,
134 mContext.getOpPackageName(),
135 mContext.getAttributionTag(),
136 mContext.getUserId());
137 } catch (RemoteException e) {
138 throw e.rethrowFromSystemServer();
139 }
140 }
141
142 /**
143 * Sets the current primary clip on the clipboard, attributed to the specified {@code
144 * sourcePackage}. The primary clip is the clip that is involved in normal cut and paste
145 * operations.
146 *
147 * @param clip The clipped data item to set.
148 * @param sourcePackage The package name of the app that is the source of the clip data.
149 * @throws IllegalArgumentException if the clip is null or contains no items.
150 *
151 * @hide
152 */
153 @SystemApi
154 @RequiresPermission(Manifest.permission.SET_CLIP_SOURCE)
155 public void setPrimaryClipAsPackage(@NonNull ClipData clip, @NonNull String sourcePackage) {
156 try {
157 Objects.requireNonNull(clip);
158 Objects.requireNonNull(sourcePackage);
159 clip.prepareToLeaveProcess(true);
160 mService.setPrimaryClipAsPackage(
161 clip,
162 mContext.getOpPackageName(),
163 mContext.getAttributionTag(),
164 mContext.getUserId(),
165 sourcePackage);
166 } catch (RemoteException e) {
167 throw e.rethrowFromSystemServer();
168 }
169 }
170
171 /**
172 * Clears any current primary clip on the clipboard.
173 *
174 * @see #setPrimaryClip(ClipData)
175 */
176 public void clearPrimaryClip() {
177 try {
178 mService.clearPrimaryClip(
179 mContext.getOpPackageName(),
180 mContext.getAttributionTag(),
181 mContext.getUserId());
182 } catch (RemoteException e) {
183 throw e.rethrowFromSystemServer();
184 }
185 }
186
187 /**
188 * Returns the current primary clip on the clipboard.
189 *
190 * <em>If the application is not the default IME or does not have input focus this return
191 * {@code null}.</em>
192 *
193 * @see #setPrimaryClip(ClipData)
194 */
195 public @Nullable ClipData getPrimaryClip() {
196 try {
197 return mService.getPrimaryClip(
198 mContext.getOpPackageName(),
199 mContext.getAttributionTag(),
200 mContext.getUserId());
201 } catch (RemoteException e) {
202 throw e.rethrowFromSystemServer();
203 }
204 }
205
206 /**
207 * Returns a description of the current primary clip on the clipboard but not a copy of its
208 * data.
209 *
210 * <p><em>If the application is not the default IME or does not have input focus this return
211 * {@code null}.</em>
212 *
213 * @see #setPrimaryClip(ClipData)
214 */
215 public @Nullable ClipDescription getPrimaryClipDescription() {
216 try {
217 return mService.getPrimaryClipDescription(
218 mContext.getOpPackageName(),
219 mContext.getAttributionTag(),
220 mContext.getUserId());
221 } catch (RemoteException e) {
222 throw e.rethrowFromSystemServer();
223 }
224 }
225
226 /**
227 * Returns true if there is currently a primary clip on the clipboard.
228 *
229 * <em>If the application is not the default IME or the does not have input focus this will
230 * return {@code false}.</em>
231 */
232 public boolean hasPrimaryClip() {
233 try {
234 return mService.hasPrimaryClip(
235 mContext.getOpPackageName(),
236 mContext.getAttributionTag(),
237 mContext.getUserId());
238 } catch (RemoteException e) {
239 throw e.rethrowFromSystemServer();
240 }
241 }
242
243 public void addPrimaryClipChangedListener(OnPrimaryClipChangedListener what) {
244 synchronized (mPrimaryClipChangedListeners) {
245 if (mPrimaryClipChangedListeners.isEmpty()) {
246 try {
247 mService.addPrimaryClipChangedListener(
248 mPrimaryClipChangedServiceListener,
249 mContext.getOpPackageName(),
250 mContext.getAttributionTag(),
251 mContext.getUserId());
252 } catch (RemoteException e) {
253 throw e.rethrowFromSystemServer();
254 }
255 }
256 mPrimaryClipChangedListeners.add(what);
257 }
258 }
259
260 public void removePrimaryClipChangedListener(OnPrimaryClipChangedListener what) {
261 synchronized (mPrimaryClipChangedListeners) {
262 mPrimaryClipChangedListeners.remove(what);
263 if (mPrimaryClipChangedListeners.isEmpty()) {
264 try {
265 mService.removePrimaryClipChangedListener(
266 mPrimaryClipChangedServiceListener,
267 mContext.getOpPackageName(),
268 mContext.getAttributionTag(),
269 mContext.getUserId());
270 } catch (RemoteException e) {
271 throw e.rethrowFromSystemServer();
272 }
273 }
274 }
275 }
276
277 /**
278 * @deprecated Use {@link #getPrimaryClip()} instead. This retrieves
279 * the primary clip and tries to coerce it to a string.
280 */
281 @Deprecated
282 public CharSequence getText() {
283 ClipData clip = getPrimaryClip();
284 if (clip != null && clip.getItemCount() > 0) {
285 return clip.getItemAt(0).coerceToText(mContext);
286 }
287 return null;
288 }
289
290 /**
291 * @deprecated Use {@link #setPrimaryClip(ClipData)} instead. This
292 * creates a ClippedItem holding the given text and sets it as the
293 * primary clip. It has no label or icon.
294 */
295 @Deprecated
296 public void setText(CharSequence text) {
297 setPrimaryClip(ClipData.newPlainText(null, text));
298 }
299
300 /**
301 * @deprecated Use {@link #hasPrimaryClip()} instead.
302 */
303 @Deprecated
304 public boolean hasText() {
305 try {
306 return mService.hasClipboardText(
307 mContext.getOpPackageName(),
308 mContext.getAttributionTag(),
309 mContext.getUserId());
310 } catch (RemoteException e) {
311 throw e.rethrowFromSystemServer();
312 }
313 }
314
315 /**
316 * Returns the package name of the source of the current primary clip, or null if there is no
317 * primary clip or if a source is not available.
318 *
319 * @hide
320 */
321 @TestApi
322 @Nullable
323 @RequiresPermission(Manifest.permission.SET_CLIP_SOURCE)
324 public String getPrimaryClipSource() {
325 try {
326 return mService.getPrimaryClipSource(
327 mContext.getOpPackageName(),
328 mContext.getAttributionTag(),
329 mContext.getUserId());
330 } catch (RemoteException e) {
331 throw e.rethrowFromSystemServer();
332 }
333 }
334
335 @UnsupportedAppUsage
336 void reportPrimaryClipChanged() {
337 Object[] listeners;
338
339 synchronized (mPrimaryClipChangedListeners) {
340 final int N = mPrimaryClipChangedListeners.size();
341 if (N <= 0) {
342 return;
343 }
344 listeners = mPrimaryClipChangedListeners.toArray();
345 }
346
347 for (int i=0; i<listeners.length; i++) {
348 ((OnPrimaryClipChangedListener)listeners[i]).onPrimaryClipChanged();
349 }
350 }
351 }