Update to follow clipboard APIs.
Change-Id: I905a499f2697bea218b6d4c3f8ec339fbe52c916
This commit is contained in:
@@ -88,6 +88,14 @@
|
|||||||
android:prompt="@string/clip_type_prompt"
|
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
|
<EditText
|
||||||
android:id="@+id/clip_text"
|
android:id="@+id/clip_text"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|||||||
@@ -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;
|
package com.example.android.apis.content;
|
||||||
|
|
||||||
import com.example.android.apis.R;
|
import com.example.android.apis.R;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ClipboardManager;
|
import android.content.ClipboardManager;
|
||||||
import android.content.ClippedData;
|
import android.content.ClipData;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
@@ -22,6 +38,7 @@ public class ClipboardSample extends Activity {
|
|||||||
ClipboardManager mClipboard;
|
ClipboardManager mClipboard;
|
||||||
|
|
||||||
Spinner mSpinner;
|
Spinner mSpinner;
|
||||||
|
TextView mMimeTypes;
|
||||||
EditText mEditText;
|
EditText mEditText;
|
||||||
|
|
||||||
CharSequence mStyledText;
|
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);
|
mEditText = (EditText)findViewById(R.id.clip_text);
|
||||||
|
|
||||||
mClipboard.addPrimaryClipChangedListener(mPrimaryChangeListener);
|
mClipboard.addPrimaryClipChangedListener(mPrimaryChangeListener);
|
||||||
@@ -81,28 +99,33 @@ public class ClipboardSample extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void pasteStyledText(View button) {
|
public void pasteStyledText(View button) {
|
||||||
mClipboard.setPrimaryClip(new ClippedData("Styled Text", null,
|
mClipboard.setPrimaryClip(ClipData.newPlainText("Styled Text", null, mStyledText));
|
||||||
new ClippedData.Item(mStyledText)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pastePlainText(View button) {
|
public void pastePlainText(View button) {
|
||||||
mClipboard.setPrimaryClip(new ClippedData("Plain Text", null,
|
mClipboard.setPrimaryClip(ClipData.newPlainText("Styled Text", null, mPlainText));
|
||||||
new ClippedData.Item(mPlainText)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pasteIntent(View button) {
|
public void pasteIntent(View button) {
|
||||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.android.com/"));
|
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.android.com/"));
|
||||||
mClipboard.setPrimaryClip(new ClippedData("View intent", null,
|
mClipboard.setPrimaryClip(ClipData.newIntent("VIEW intent", null, intent));
|
||||||
new ClippedData.Item(intent)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pasteUri(View button) {
|
public void pasteUri(View button) {
|
||||||
mClipboard.setPrimaryClip(new ClippedData("View intent", null,
|
mClipboard.setPrimaryClip(ClipData.newRawUri("URI", null,
|
||||||
new ClippedData.Item(Uri.parse("http://www.android.com/"))));
|
Uri.parse("http://www.android.com/")));
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateClipData() {
|
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) {
|
if (clip == null) {
|
||||||
mSpinner.setSelection(0);
|
mSpinner.setSelection(0);
|
||||||
mEditText.setText("");
|
mEditText.setText("");
|
||||||
|
|||||||
16
samples/NotePad/Android.mk
Normal file
16
samples/NotePad/Android.mk
Normal 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))
|
||||||
@@ -20,7 +20,7 @@ import com.example.android.notepad.NotePad;
|
|||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ClipboardManager;
|
import android.content.ClipboardManager;
|
||||||
import android.content.ClippedData;
|
import android.content.ClipData;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
@@ -510,14 +510,14 @@ public class NoteEditor extends Activity {
|
|||||||
ContentResolver cr = getContentResolver();
|
ContentResolver cr = getContentResolver();
|
||||||
|
|
||||||
// Gets the clipboard data from the clipboard
|
// Gets the clipboard data from the clipboard
|
||||||
ClippedData clip = clipboard.getPrimaryClip();
|
ClipData clip = clipboard.getPrimaryClip();
|
||||||
if (clip != null) {
|
if (clip != null) {
|
||||||
|
|
||||||
String text=null;
|
String text=null;
|
||||||
String title=null;
|
String title=null;
|
||||||
|
|
||||||
// Gets the first item from the clipboard data
|
// 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
|
// Tries to get the item's contents as a URI pointing to a note
|
||||||
Uri uri = item.getUri();
|
Uri uri = item.getUri();
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package com.example.android.notepad;
|
|||||||
|
|
||||||
import com.example.android.notepad.NotePad;
|
import com.example.android.notepad.NotePad;
|
||||||
|
|
||||||
|
import android.content.ClipDescription;
|
||||||
import android.content.ContentProvider;
|
import android.content.ContentProvider;
|
||||||
import android.content.ContentUris;
|
import android.content.ContentUris;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
@@ -349,9 +350,16 @@ public class NotePadProvider extends ContentProvider implements PipeDataWriter<C
|
|||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unknown URI " + uri);
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//BEGIN_INCLUDE(stream)
|
//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.
|
* 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.
|
* 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
|
// If the pattern is for note IDs and the MIME filter is text/plain, then return
|
||||||
// text/plain
|
// text/plain
|
||||||
case NOTE_ID:
|
case NOTE_ID:
|
||||||
if (compareMimeTypes("text/plain", mimeTypeFilter)) {
|
return NOTE_STREAM_TYPES.filterMimeTypes(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;
|
|
||||||
|
|
||||||
// If the URI pattern doesn't match any permitted patterns, throws an exception.
|
// If the URI pattern doesn't match any permitted patterns, throws an exception.
|
||||||
default:
|
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
|
* 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,
|
* {@link android.content.ContentProvider#openPipeHelper(Uri, String, Bundle, Object,
|
||||||
* PipeDataWriter)} to start another thread in which to convert the data into a stream.
|
* 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 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
|
* @param mimeTypeFilter A String containing a MIME type. This method tries to get a stream of
|
||||||
* data with this MIME type.
|
* data with this MIME type.
|
||||||
* @param opts The access mode for the data stream. This may be "r" for read-only, "w" for write
|
* @param opts Additional options supplied by the caller. Can be interpreted as
|
||||||
* with overwrite, "wa" for write with append, "rw" for read and write access for existing data,
|
* desired by the content provider.
|
||||||
* and "rwt" that truncates the existing file if the new data is smaller.
|
|
||||||
* @return AssetFileDescriptor A handle to the file.
|
* @return AssetFileDescriptor A handle to the file.
|
||||||
* @throws FileNotFoundException if there is no file associated with the incoming URI.
|
* @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.app.ListActivity;
|
||||||
import android.content.ClipboardManager;
|
import android.content.ClipboardManager;
|
||||||
import android.content.ClippedData;
|
import android.content.ClipData;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.ContentUris;
|
import android.content.ContentUris;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -485,7 +485,6 @@ public class NotesList extends ListActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//BEGIN_INCLUDE(copy)
|
//BEGIN_INCLUDE(copy)
|
||||||
|
|
||||||
// Copies the selected note to the clipboard
|
// Copies the selected note to the clipboard
|
||||||
case MENU_ITEM_COPY: {
|
case MENU_ITEM_COPY: {
|
||||||
|
|
||||||
@@ -497,12 +496,11 @@ public class NotesList extends ListActivity {
|
|||||||
Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);
|
Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);
|
||||||
|
|
||||||
// Copies the notes URI to the clipboard. In effect, this copies the note itself
|
// Copies the notes URI to the clipboard. In effect, this copies the note itself
|
||||||
clipboard.setPrimaryClip(
|
clipboard.setPrimaryClip(ClipData.newUri( // new clipboard item holding a URI
|
||||||
new ClippedData( // creates a new clipboard item
|
getContentResolver(), // resolver to retrieve URI info
|
||||||
null, // no visible label
|
"Note", // label for the clip
|
||||||
null, // no visible icon
|
null, // icon for the clip
|
||||||
new ClippedData.Item(noteUri) // A clipboard Item that is a URI.
|
noteUri) // the URI
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Returns to the caller and skips further processing.
|
// Returns to the caller and skips further processing.
|
||||||
|
|||||||
Reference in New Issue
Block a user