ClipboardManager.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2007 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 android.text;
18
19import android.content.Context;
20import android.os.RemoteException;
21import android.os.Handler;
22import android.os.IBinder;
23import android.os.ServiceManager;
24import android.util.Log;
25
26/**
27 * Interface to the clipboard service, for placing and retrieving text in
28 * the global clipboard.
29 *
30 * <p>
31 * You do not instantiate this class directly; instead, retrieve it through
32 * {@link android.content.Context#getSystemService}.
33 *
34 * @see android.content.Context#getSystemService
35 */
36public class ClipboardManager {
37    private static IClipboard sService;
38
39    private Context mContext;
40
41    static private IClipboard getService() {
42        if (sService != null) {
43            return sService;
44        }
45        IBinder b = ServiceManager.getService("clipboard");
46        sService = IClipboard.Stub.asInterface(b);
47        return sService;
48    }
49
50    /** {@hide} */
51    public ClipboardManager(Context context, Handler handler) {
52        mContext = context;
53    }
54
55    /**
56     * Returns the text on the clipboard.  It will eventually be possible
57     * to store types other than text too, in which case this will return
58     * null if the type cannot be coerced to text.
59     */
60    public CharSequence getText() {
61        try {
62            return getService().getClipboardText();
63        } catch (RemoteException e) {
64            return null;
65        }
66    }
67
68    /**
69     * Sets the contents of the clipboard to the specified text.
70     */
71    public void setText(CharSequence text) {
72        try {
73            getService().setClipboardText(text);
74        } catch (RemoteException e) {
75        }
76    }
77
78    /**
79     * Returns true if the clipboard contains text; false otherwise.
80     */
81    public boolean hasText() {
82        try {
83            return getService().hasClipboardText();
84        } catch (RemoteException e) {
85            return false;
86        }
87    }
88}
89