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