Update to follow clipboard APIs.
Change-Id: I905a499f2697bea218b6d4c3f8ec339fbe52c916
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
@@ -349,9 +350,16 @@ public class NotePadProvider extends ContentProvider implements PipeDataWriter<C
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown URI " + uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//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.
|
||||
*/
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user