blob: 4e4fc0cbeb581b8c71e7b4415576c4c75bc2a99c [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 Hackborn9f531192010-08-04 17:48:03 -070019import android.content.ClippedData;
20import android.content.IClipboard;
21import android.content.IOnPrimaryClipChangedListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
Dianne Hackborn9f531192010-08-04 17:48:03 -070023import android.os.RemoteCallbackList;
24import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
26/**
27 * Implementation of the clipboard for copy and paste.
28 */
29public class ClipboardService extends IClipboard.Stub {
Dianne Hackborn9f531192010-08-04 17:48:03 -070030 private ClippedData mPrimaryClip;
31 private final RemoteCallbackList<IOnPrimaryClipChangedListener> mPrimaryClipListeners
32 = new RemoteCallbackList<IOnPrimaryClipChangedListener>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34 /**
35 * Instantiates the clipboard.
36 */
37 public ClipboardService(Context context) { }
38
Dianne Hackborn9f531192010-08-04 17:48:03 -070039 public void setPrimaryClip(ClippedData clip) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 synchronized (this) {
Dianne Hackborn9f531192010-08-04 17:48:03 -070041 if (clip != null && clip.getItemCount() <= 0) {
42 throw new IllegalArgumentException("No items");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 }
Dianne Hackborn9f531192010-08-04 17:48:03 -070044 mPrimaryClip = clip;
45 final int n = mPrimaryClipListeners.beginBroadcast();
46 for (int i = 0; i < n; i++) {
47 try {
48 mPrimaryClipListeners.getBroadcastItem(i).dispatchPrimaryClipChanged();
49 } catch (RemoteException e) {
50
51 // The RemoteCallbackList will take care of removing
52 // the dead object for us.
53 }
54 }
55 mPrimaryClipListeners.finishBroadcast();
56 }
57 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
Dianne Hackborn9f531192010-08-04 17:48:03 -070059 public ClippedData getPrimaryClip() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 synchronized (this) {
Dianne Hackborn9f531192010-08-04 17:48:03 -070061 return mPrimaryClip;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 }
63 }
64
Dianne Hackborn9f531192010-08-04 17:48:03 -070065 public boolean hasPrimaryClip() {
66 synchronized (this) {
67 return mPrimaryClip != null;
68 }
69 }
70
71 public void addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) {
72 synchronized (this) {
73 mPrimaryClipListeners.register(listener);
74 }
75 }
76
77 public void removePrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) {
78 synchronized (this) {
79 mPrimaryClipListeners.unregister(listener);
80 }
81 }
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 public boolean hasClipboardText() {
84 synchronized (this) {
Dianne Hackborn9f531192010-08-04 17:48:03 -070085 if (mPrimaryClip != null) {
86 CharSequence text = mPrimaryClip.getItem(0).getText();
87 return text != null && text.length() > 0;
88 }
89 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 }
91 }
92}