• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
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.badlogic.gdx.backends.android;
18 
19 import android.content.ClipData;
20 
21 import android.content.Context;
22 import com.badlogic.gdx.utils.Clipboard;
23 
24 public class AndroidClipboard implements Clipboard {
25 
26 	private android.text.ClipboardManager clipboard;
27 	private android.content.ClipboardManager honeycombClipboard;
28 
AndroidClipboard(Context context)29 	public AndroidClipboard (Context context) {
30 		if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
31 			clipboard = (android.text.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
32 		} else {
33 			honeycombClipboard = (android.content.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
34 		}
35 	}
36 
37 	@Override
getContents()38 	public String getContents () {
39 		if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
40 			if (clipboard.getText() == null) return null;
41 			return clipboard.getText().toString();
42 		} else {
43 			ClipData clip = honeycombClipboard.getPrimaryClip();
44 			if (clip == null) return null;
45 			CharSequence text = clip.getItemAt(0).getText();
46 			if (text == null) return null;
47 			return text.toString();
48 		}
49 	}
50 
51 	@Override
setContents(final String contents)52 	public void setContents (final String contents) {
53 		if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
54 			clipboard.setText(contents);
55 		} else {
56 			ClipData data = ClipData.newPlainText(contents, contents);
57 			honeycombClipboard.setPrimaryClip(data);
58 		}
59 	}
60 }
61