blob: 308c9c0c8c23580252fa2b96a67c7eb9d44d6ee2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
17package com.android.server;
18
Dianne Hackborn1040dc42010-08-26 22:11:06 -070019import android.content.ClipData;
20import android.content.ClipDescription;
Dianne Hackborn9f531192010-08-04 17:48:03 -070021import android.content.IClipboard;
22import android.content.IOnPrimaryClipChangedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Dianne Hackborn9f531192010-08-04 17:48:03 -070024import android.os.RemoteCallbackList;
25import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27/**
28 * Implementation of the clipboard for copy and paste.
29 */
30public class ClipboardService extends IClipboard.Stub {
Dianne Hackborn1040dc42010-08-26 22:11:06 -070031 private ClipData mPrimaryClip;
Dianne Hackborn9f531192010-08-04 17:48:03 -070032 private final RemoteCallbackList<IOnPrimaryClipChangedListener> mPrimaryClipListeners
33 = new RemoteCallbackList<IOnPrimaryClipChangedListener>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35 /**
36 * Instantiates the clipboard.
37 */
38 public ClipboardService(Context context) { }
39
Dianne Hackborn1040dc42010-08-26 22:11:06 -070040 public void setPrimaryClip(ClipData clip) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 synchronized (this) {
Dianne Hackborn9f531192010-08-04 17:48:03 -070042 if (clip != null && clip.getItemCount() <= 0) {
43 throw new IllegalArgumentException("No items");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 }
Dianne Hackborn9f531192010-08-04 17:48:03 -070045 mPrimaryClip = clip;
46 final int n = mPrimaryClipListeners.beginBroadcast();
47 for (int i = 0; i < n; i++) {
48 try {
49 mPrimaryClipListeners.getBroadcastItem(i).dispatchPrimaryClipChanged();
50 } catch (RemoteException e) {
51
52 // The RemoteCallbackList will take care of removing
53 // the dead object for us.
54 }
55 }
56 mPrimaryClipListeners.finishBroadcast();
57 }
58 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
Dianne Hackborn1040dc42010-08-26 22:11:06 -070060 public ClipData getPrimaryClip() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 synchronized (this) {
Dianne Hackborn9f531192010-08-04 17:48:03 -070062 return mPrimaryClip;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 }
64 }
65
Dianne Hackborn1040dc42010-08-26 22:11:06 -070066 public ClipDescription getPrimaryClipDescription() {
67 synchronized (this) {
68 return new ClipDescription(mPrimaryClip);
69 }
70 }
71
Dianne Hackborn9f531192010-08-04 17:48:03 -070072 public boolean hasPrimaryClip() {
73 synchronized (this) {
74 return mPrimaryClip != null;
75 }
76 }
77
78 public void addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) {
79 synchronized (this) {
80 mPrimaryClipListeners.register(listener);
81 }
82 }
83
84 public void removePrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) {
85 synchronized (this) {
86 mPrimaryClipListeners.unregister(listener);
87 }
88 }
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 public boolean hasClipboardText() {
91 synchronized (this) {
Dianne Hackborn9f531192010-08-04 17:48:03 -070092 if (mPrimaryClip != null) {
93 CharSequence text = mPrimaryClip.getItem(0).getText();
94 return text != null && text.length() > 0;
95 }
96 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
98 }
99}