Update to follow clipboard APIs.

Change-Id: I905a499f2697bea218b6d4c3f8ec339fbe52c916
This commit is contained in:
Dianne Hackborn
2010-08-26 22:14:57 -07:00
parent eaca8a46d1
commit 3e4a5c8a5e
6 changed files with 79 additions and 32 deletions

View File

@@ -88,6 +88,14 @@
android:prompt="@string/clip_type_prompt"
/>
<TextView
android:id="@+id/clip_mime_types"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:textStyle="normal"
/>
<EditText
android:id="@+id/clip_text"
android:layout_width="match_parent"

View File

@@ -1,10 +1,26 @@
/**
* Copyright (c) 2010, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.content;
import com.example.android.apis.R;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.ClippedData;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
@@ -22,6 +38,7 @@ public class ClipboardSample extends Activity {
ClipboardManager mClipboard;
Spinner mSpinner;
TextView mMimeTypes;
EditText mEditText;
CharSequence mStyledText;
@@ -68,6 +85,7 @@ public class ClipboardSample extends Activity {
}
});
mMimeTypes = (TextView)findViewById(R.id.clip_mime_types);
mEditText = (EditText)findViewById(R.id.clip_text);
mClipboard.addPrimaryClipChangedListener(mPrimaryChangeListener);
@@ -81,28 +99,33 @@ public class ClipboardSample extends Activity {
}
public void pasteStyledText(View button) {
mClipboard.setPrimaryClip(new ClippedData("Styled Text", null,
new ClippedData.Item(mStyledText)));
mClipboard.setPrimaryClip(ClipData.newPlainText("Styled Text", null, mStyledText));
}
public void pastePlainText(View button) {
mClipboard.setPrimaryClip(new ClippedData("Plain Text", null,
new ClippedData.Item(mPlainText)));
mClipboard.setPrimaryClip(ClipData.newPlainText("Styled Text", null, mPlainText));
}
public void pasteIntent(View button) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.android.com/"));
mClipboard.setPrimaryClip(new ClippedData("View intent", null,
new ClippedData.Item(intent)));
mClipboard.setPrimaryClip(ClipData.newIntent("VIEW intent", null, intent));
}
public void pasteUri(View button) {
mClipboard.setPrimaryClip(new ClippedData("View intent", null,
new ClippedData.Item(Uri.parse("http://www.android.com/"))));
mClipboard.setPrimaryClip(ClipData.newRawUri("URI", null,
Uri.parse("http://www.android.com/")));
}
void updateClipData() {
ClippedData clip = mClipboard.getPrimaryClip();
ClipData clip = mClipboard.getPrimaryClip();
String[] mimeTypes = clip != null ? clip.filterMimeTypes("*/*") : null;
mMimeTypes.setText("");
if (mimeTypes != null) {
for (int i=0; i<mimeTypes.length; i++) {
mMimeTypes.append(mimeTypes[i]);
mMimeTypes.append("\n");
}
}
if (clip == null) {
mSpinner.setSelection(0);
mEditText.setText("");

View File

@@ -0,0 +1,16 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := samples
# Only compile source java files in this apk.
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := NotePad
LOCAL_SDK_VERSION := current
include $(BUILD_PACKAGE)
# Use the following include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))

View File

@@ -20,7 +20,7 @@ import com.example.android.notepad.NotePad;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.ClippedData;
import android.content.ClipData;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues;
@@ -510,14 +510,14 @@ public class NoteEditor extends Activity {
ContentResolver cr = getContentResolver();
// Gets the clipboard data from the clipboard
ClippedData clip = clipboard.getPrimaryClip();
ClipData clip = clipboard.getPrimaryClip();
if (clip != null) {
String text=null;
String title=null;
// Gets the first item from the clipboard data
ClippedData.Item item = clip.getItem(0);
ClipData.Item item = clip.getItem(0);
// Tries to get the item's contents as a URI pointing to a note
Uri uri = item.getUri();

View File

@@ -18,6 +18,7 @@ package com.example.android.notepad;
import com.example.android.notepad.NotePad;
import android.content.ClipDescription;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
@@ -352,6 +353,13 @@ public class NotePadProvider extends ContentProvider implements PipeDataWriter<C
}
//BEGIN_INCLUDE(stream)
/**
* This describes the MIME types that are supported for opening a note
* URI as a stream.
*/
static ClipDescription NOTE_STREAM_TYPES = new ClipDescription(null,
new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN });
/**
* Returns the types of available data streams. URIs to specific notes are supported.
* The application can convert such a note to a plain text stream.
@@ -378,12 +386,7 @@ public class NotePadProvider extends ContentProvider implements PipeDataWriter<C
// If the pattern is for note IDs and the MIME filter is text/plain, then return
// text/plain
case NOTE_ID:
if (compareMimeTypes("text/plain", mimeTypeFilter)) {
return new String[] { "text/plain" };
}
// If the URI is for a note id, but the MIME filter isn't text/plain, return null.
return null;
return NOTE_STREAM_TYPES.filterMimeTypes(mimeTypeFilter);
// If the URI pattern doesn't match any permitted patterns, throws an exception.
default:
@@ -394,16 +397,15 @@ public class NotePadProvider extends ContentProvider implements PipeDataWriter<C
/**
* Returns a stream of data for each supported stream type. This method does a query on the
* incomding URI, then uses
* incoming URI, then uses
* {@link android.content.ContentProvider#openPipeHelper(Uri, String, Bundle, Object,
* PipeDataWriter)} to start another thread in which to convert the data into a stream.
*
* @param uri The URI pattern that points to the data stream
* @param mimeTypeFilter A String containing a MIME type. This method tries to get a stream of
* data with this MIME type.
* @param opts The access mode for the data stream. This may be "r" for read-only, "w" for write
* with overwrite, "wa" for write with append, "rw" for read and write access for existing data,
* and "rwt" that truncates the existing file if the new data is smaller.
* @param opts Additional options supplied by the caller. Can be interpreted as
* desired by the content provider.
* @return AssetFileDescriptor A handle to the file.
* @throws FileNotFoundException if there is no file associated with the incoming URI.
*/

View File

@@ -20,7 +20,7 @@ import com.example.android.notepad.NotePad;
import android.app.ListActivity;
import android.content.ClipboardManager;
import android.content.ClippedData;
import android.content.ClipData;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.Context;
@@ -485,7 +485,6 @@ public class NotesList extends ListActivity {
}
//BEGIN_INCLUDE(copy)
// Copies the selected note to the clipboard
case MENU_ITEM_COPY: {
@@ -497,12 +496,11 @@ public class NotesList extends ListActivity {
Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);
// Copies the notes URI to the clipboard. In effect, this copies the note itself
clipboard.setPrimaryClip(
new ClippedData( // creates a new clipboard item
null, // no visible label
null, // no visible icon
new ClippedData.Item(noteUri) // A clipboard Item that is a URI.
)
clipboard.setPrimaryClip(ClipData.newUri( // new clipboard item holding a URI
getContentResolver(), // resolver to retrieve URI info
"Note", // label for the clip
null, // icon for the clip
noteUri) // the URI
);
// Returns to the caller and skips further processing.