1 /** 2 * Copyright (c) 2010, 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.example.android.apis.content; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.content.ClipboardManager; 23 import android.content.ClipData; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.res.Resources; 27 import android.net.Uri; 28 import android.os.Bundle; 29 import android.text.method.LinkMovementMethod; 30 import android.view.View; 31 import android.widget.AdapterView; 32 import android.widget.ArrayAdapter; 33 import android.widget.Spinner; 34 import android.widget.TextView; 35 import android.widget.AdapterView.OnItemSelectedListener; 36 37 public class ClipboardSample extends Activity { 38 ClipboardManager mClipboard; 39 40 Spinner mSpinner; 41 TextView mMimeTypes; 42 TextView mDataText; 43 44 CharSequence mStyledText; 45 String mPlainText; 46 String mHtmlText; 47 String mHtmlPlainText; 48 49 ClipboardManager.OnPrimaryClipChangedListener mPrimaryChangeListener 50 = new ClipboardManager.OnPrimaryClipChangedListener() { 51 public void onPrimaryClipChanged() { 52 updateClipData(true); 53 } 54 }; 55 56 @Override 57 protected void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 60 mClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 61 62 // See res/any/layout/resources.xml for this view layout definition. 63 setContentView(R.layout.clipboard); 64 65 TextView tv; 66 67 mStyledText = getText(R.string.styled_text); 68 tv = (TextView)findViewById(R.id.styled_text); 69 tv.setText(mStyledText); 70 71 mPlainText = mStyledText.toString(); 72 tv = (TextView)findViewById(R.id.plain_text); 73 tv.setText(mPlainText); 74 75 mHtmlText = "<b>Link:</b> <a href=\"http://www.android.com\">Android</a>"; 76 mHtmlPlainText = "Link: http://www.android.com"; 77 tv = (TextView)findViewById(R.id.html_text); 78 tv.setText(mHtmlText); 79 80 mSpinner = (Spinner) findViewById(R.id.clip_type); 81 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( 82 this, R.array.clip_data_types, android.R.layout.simple_spinner_item); 83 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 84 mSpinner.setAdapter(adapter); 85 mSpinner.setOnItemSelectedListener( 86 new OnItemSelectedListener() { 87 public void onItemSelected( 88 AdapterView<?> parent, View view, int position, long id) { 89 updateClipData(false); 90 } 91 public void onNothingSelected(AdapterView<?> parent) { 92 } 93 }); 94 95 mMimeTypes = (TextView)findViewById(R.id.clip_mime_types); 96 mDataText = (TextView)findViewById(R.id.clip_text); 97 98 mClipboard.addPrimaryClipChangedListener(mPrimaryChangeListener); 99 updateClipData(true); 100 } 101 102 @Override 103 protected void onDestroy() { 104 super.onDestroy(); 105 mClipboard.removePrimaryClipChangedListener(mPrimaryChangeListener); 106 } 107 108 public void pasteStyledText(View button) { 109 mClipboard.setPrimaryClip(ClipData.newPlainText("Styled Text", mStyledText)); 110 } 111 112 public void pastePlainText(View button) { 113 mClipboard.setPrimaryClip(ClipData.newPlainText("Styled Text", mPlainText)); 114 } 115 116 public void pasteHtmlText(View button) { 117 mClipboard.setPrimaryClip(ClipData.newHtmlText("HTML Text", mHtmlPlainText, mHtmlText)); 118 } 119 120 public void pasteIntent(View button) { 121 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.android.com/")); 122 mClipboard.setPrimaryClip(ClipData.newIntent("VIEW intent", intent)); 123 } 124 125 public void pasteUri(View button) { 126 mClipboard.setPrimaryClip(ClipData.newRawUri("URI", Uri.parse("http://www.android.com/"))); 127 } 128 129 void updateClipData(boolean updateType) { 130 ClipData clip = mClipboard.getPrimaryClip(); 131 String[] mimeTypes = clip != null ? clip.getDescription().filterMimeTypes("*/*") : null; 132 if (mimeTypes != null) { 133 mMimeTypes.setText(""); 134 for (int i=0; i<mimeTypes.length; i++) { 135 if (i > 0) { 136 mMimeTypes.append("\n"); 137 } 138 mMimeTypes.append(mimeTypes[i]); 139 } 140 } else { 141 mMimeTypes.setText("NULL"); 142 } 143 144 if (updateType) { 145 if (clip != null) { 146 ClipData.Item item = clip.getItemAt(0); 147 if (item.getHtmlText() != null) { 148 mSpinner.setSelection(2); 149 } else if (item.getText() != null) { 150 mSpinner.setSelection(1); 151 } else if (item.getIntent() != null) { 152 mSpinner.setSelection(3); 153 } else if (item.getUri() != null) { 154 mSpinner.setSelection(4); 155 } else { 156 mSpinner.setSelection(0); 157 } 158 } else { 159 mSpinner.setSelection(0); 160 } 161 } 162 163 if (clip != null) { 164 ClipData.Item item = clip.getItemAt(0); 165 switch (mSpinner.getSelectedItemPosition()) { 166 case 0: 167 mDataText.setText("(No data)"); 168 break; 169 case 1: 170 mDataText.setText(item.getText()); 171 break; 172 case 2: 173 mDataText.setText(item.getHtmlText()); 174 break; 175 case 3: 176 mDataText.setText(item.getIntent().toUri(0)); 177 break; 178 case 4: 179 mDataText.setText(item.getUri().toString()); 180 break; 181 case 5: 182 mDataText.setText(item.coerceToText(this)); 183 break; 184 case 6: 185 mDataText.setText(item.coerceToStyledText(this)); 186 break; 187 case 7: 188 mDataText.setText(item.coerceToHtmlText(this)); 189 break; 190 default: 191 mDataText.setText("Unknown option: " + mSpinner.getSelectedItemPosition()); 192 break; 193 } 194 } else { 195 mDataText.setText("(NULL clip)"); 196 } 197 mDataText.setMovementMethod(LinkMovementMethod.getInstance()); 198 } 199 } 200