The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 19 | import android.content.ClipData; |
| 20 | import android.content.ClipDescription; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 21 | import android.content.IClipboard; |
| 22 | import android.content.IOnPrimaryClipChangedListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 24 | import android.os.RemoteCallbackList; |
| 25 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Implementation of the clipboard for copy and paste. |
| 29 | */ |
| 30 | public class ClipboardService extends IClipboard.Stub { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 31 | private ClipData mPrimaryClip; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 32 | private final RemoteCallbackList<IOnPrimaryClipChangedListener> mPrimaryClipListeners |
| 33 | = new RemoteCallbackList<IOnPrimaryClipChangedListener>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Instantiates the clipboard. |
| 37 | */ |
| 38 | public ClipboardService(Context context) { } |
| 39 | |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 40 | public void setPrimaryClip(ClipData clip) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | synchronized (this) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 42 | if (clip != null && clip.getItemCount() <= 0) { |
| 43 | throw new IllegalArgumentException("No items"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | } |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 45 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 60 | public ClipData getPrimaryClip() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | synchronized (this) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 62 | return mPrimaryClip; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 66 | public ClipDescription getPrimaryClipDescription() { |
| 67 | synchronized (this) { |
| 68 | return new ClipDescription(mPrimaryClip); |
| 69 | } |
| 70 | } |
| 71 | |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 72 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | public boolean hasClipboardText() { |
| 91 | synchronized (this) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 92 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |