am 473bbd21: am 95240270: Instead of a raw AIDL interface, give DropBox a Java interface (android.os.DropBox); move the Binder interface behind the scenes. Make DropBoxEntry into DropBox.Entry. Make it possible to get a dropbox from an (Application)Context with the u

Merge commit '473bbd2140a2515a6a9a450ee955a790e0b6dcff'

* commit '473bbd2140a2515a6a9a450ee955a790e0b6dcff':
  Instead of a raw AIDL interface, give DropBox a Java
This commit is contained in:
Dan Egnor
2009-10-29 02:00:36 -07:00
committed by Android Git Automerger
11 changed files with 455 additions and 393 deletions

View File

@@ -70,6 +70,7 @@ import android.net.wifi.IWifiManager;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.DropBox;
import android.os.FileUtils;
import android.os.Handler;
import android.os.IBinder;
@@ -93,6 +94,8 @@ import android.view.inputmethod.InputMethodManager;
import android.accounts.AccountManager;
import android.accounts.IAccountManager;
import com.android.internal.os.IDropBoxService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -182,6 +185,7 @@ class ApplicationContext extends Context {
private ClipboardManager mClipboardManager = null;
private boolean mRestricted;
private AccountManager mAccountManager; // protected by mSync
private DropBox mDropBox = null;
private final Object mSync = new Object();
@@ -896,6 +900,8 @@ class ApplicationContext extends Context {
return getClipboardManager();
} else if (WALLPAPER_SERVICE.equals(name)) {
return getWallpaperManager();
} else if (DROPBOX_SERVICE.equals(name)) {
return getDropBox();
}
return null;
@@ -1045,7 +1051,7 @@ class ApplicationContext extends Context {
}
return mVibrator;
}
private AudioManager getAudioManager()
{
if (mAudioManager == null) {
@@ -1054,6 +1060,17 @@ class ApplicationContext extends Context {
return mAudioManager;
}
private DropBox getDropBox() {
synchronized (mSync) {
if (mDropBox == null) {
IBinder b = ServiceManager.getService(DROPBOX_SERVICE);
IDropBoxService service = IDropBoxService.Stub.asInterface(b);
mDropBox = new DropBox(service);
}
}
return mDropBox;
}
@Override
public int checkPermission(String permission, int pid, int uid) {
if (permission == null) {

View File

@@ -1309,7 +1309,7 @@ public abstract class Context {
* @see #getSystemService
*/
public static final String APPWIDGET_SERVICE = "appwidget";
/**
* Use with {@link #getSystemService} to retrieve an
* {@blink android.backup.IBackupManager IBackupManager} for communicating
@@ -1319,7 +1319,16 @@ public abstract class Context {
* @see #getSystemService
*/
public static final String BACKUP_SERVICE = "backup";
/**
* Use with {@link #getSystemService} to retrieve a
* {@blink android.os.DropBox DropBox} instance for recording
* diagnostic logs.
* @hide
* @see #getSystemService
*/
public static final String DROPBOX_SERVICE = "dropbox";
/**
* Determine whether the given permission is allowed for a particular
* process and user ID running in the system.

View File

@@ -16,4 +16,4 @@
package android.os;
parcelable DropBoxEntry;
parcelable DropBox.Entry;

View File

@@ -0,0 +1,276 @@
/*
* Copyright (C) 2009 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 android.os;
import android.util.Log;
import com.android.internal.os.IDropBoxService;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
/**
* Enqueues chunks of data (from various sources -- application crashes, kernel
* log records, etc.). The queue is size bounded and will drop old data if the
* enqueued data exceeds the maximum size. You can think of this as a
* persistent, system-wide, blob-oriented "logcat".
*
* <p>You can obtain an instance of this class by calling
* {@link android.content.Context#getSystemService}
* with {@link android.content.Context#DROPBOX_SERVICE}.
*
* <p>DropBox entries are not sent anywhere directly, but other system services
* and debugging tools may scan and upload entries for processing.
*
* {@pending}
*/
public class DropBox {
private static final String TAG = "DropBox";
private final IDropBoxService mService;
/** Flag value: Entry's content was deleted to save space. */
public static final int IS_EMPTY = 1;
/** Flag value: Content is human-readable UTF-8 text (can be combined with IS_GZIPPED). */
public static final int IS_TEXT = 2;
/** Flag value: Content can be decompressed with {@link GZIPOutputStream}. */
public static final int IS_GZIPPED = 4;
/**
* A single entry retrieved from the drop box.
* This may include a reference to a stream, so you must call
* {@link #close()} when you are done using it.
*/
public static class Entry implements Parcelable {
private final String mTag;
private final long mTimeMillis;
private final byte[] mData;
private final ParcelFileDescriptor mFileDescriptor;
private final int mFlags;
/** Create a new empty Entry with no contents. */
public Entry(String tag, long millis) {
this(tag, millis, (Object) null, IS_EMPTY);
}
/** Create a new Entry with plain text contents. */
public Entry(String tag, long millis, String text) {
this(tag, millis, (Object) text.getBytes(), IS_TEXT);
}
/**
* Create a new Entry with byte array contents.
* The data array must not be modified after creating this entry.
*/
public Entry(String tag, long millis, byte[] data, int flags) {
this(tag, millis, (Object) data, flags);
}
/**
* Create a new Entry with streaming data contents.
* Takes ownership of the ParcelFileDescriptor.
*/
public Entry(String tag, long millis, ParcelFileDescriptor data, int flags) {
this(tag, millis, (Object) data, flags);
}
/**
* Create a new Entry with the contents read from a file.
* The file will be read when the entry's contents are requested.
*/
public Entry(String tag, long millis, File data, int flags) throws IOException {
this(tag, millis, (Object) ParcelFileDescriptor.open(
data, ParcelFileDescriptor.MODE_READ_ONLY), flags);
}
/** Internal constructor for CREATOR.createFromParcel(). */
private Entry(String tag, long millis, Object value, int flags) {
if (tag == null) throw new NullPointerException();
if (((flags & IS_EMPTY) != 0) != (value == null)) throw new IllegalArgumentException();
mTag = tag;
mTimeMillis = millis;
mFlags = flags;
if (value == null) {
mData = null;
mFileDescriptor = null;
} else if (value instanceof byte[]) {
mData = (byte[]) value;
mFileDescriptor = null;
} else if (value instanceof ParcelFileDescriptor) {
mData = null;
mFileDescriptor = (ParcelFileDescriptor) value;
} else {
throw new IllegalArgumentException();
}
}
/** Close the input stream associated with this entry. */
public void close() {
try { if (mFileDescriptor != null) mFileDescriptor.close(); } catch (IOException e) { }
}
/** @return the tag originally attached to the entry. */
public String getTag() { return mTag; }
/** @return time when the entry was originally created. */
public long getTimeMillis() { return mTimeMillis; }
/** @return flags describing the content returned by @{link #getInputStream()}. */
public int getFlags() { return mFlags & ~IS_GZIPPED; } // getInputStream() decompresses.
/**
* @param maxBytes of string to return (will truncate at this length).
* @return the uncompressed text contents of the entry, null if the entry is not text.
*/
public String getText(int maxBytes) {
if ((mFlags & IS_TEXT) == 0) return null;
if (mData != null) return new String(mData, 0, Math.min(maxBytes, mData.length));
InputStream is = null;
try {
is = getInputStream();
byte[] buf = new byte[maxBytes];
return new String(buf, 0, Math.max(0, is.read(buf)));
} catch (IOException e) {
return null;
} finally {
try { if (is != null) is.close(); } catch (IOException e) {}
}
}
/** @return the uncompressed contents of the entry, or null if the contents were lost */
public InputStream getInputStream() throws IOException {
InputStream is;
if (mData != null) {
is = new ByteArrayInputStream(mData);
} else if (mFileDescriptor != null) {
is = new ParcelFileDescriptor.AutoCloseInputStream(mFileDescriptor);
} else {
return null;
}
return (mFlags & IS_GZIPPED) != 0 ? new GZIPInputStream(is) : is;
}
public static final Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator() {
public Entry[] newArray(int size) { return new Entry[size]; }
public Entry createFromParcel(Parcel in) {
return new Entry(
in.readString(), in.readLong(), in.readValue(null), in.readInt());
}
};
public int describeContents() {
return mFileDescriptor != null ? Parcelable.CONTENTS_FILE_DESCRIPTOR : 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeString(mTag);
out.writeLong(mTimeMillis);
if (mFileDescriptor != null) {
out.writeValue(mFileDescriptor);
} else {
out.writeValue(mData);
}
out.writeInt(mFlags);
}
}
/** {@hide} */
public DropBox(IDropBoxService service) { mService = service; }
/**
* Create a dummy instance for testing. All methods will fail unless
* overridden with an appropriate mock implementation. To obtain a
* functional instance, use {@link android.content.Context#getSystemService}.
*/
protected DropBox() { mService = null; }
/**
* Stores human-readable text. The data may be discarded eventually (or even
* immediately) if space is limited, or ignored entirely if the tag has been
* blocked (see {@link #isTagEnabled}).
*
* @param tag describing the type of entry being stored
* @param data value to store
*/
public void addText(String tag, String data) {
try { mService.add(new Entry(tag, 0, data)); } catch (RemoteException e) {}
}
/**
* Stores binary data, which may be ignored or discarded as with {@link #addText}.
*
* @param tag describing the type of entry being stored
* @param data value to store
* @param flags describing the data
*/
public void addData(String tag, byte[] data, int flags) {
if (data == null) throw new NullPointerException();
try { mService.add(new Entry(tag, 0, data, flags)); } catch (RemoteException e) {}
}
/**
* Stores data read from a file descriptor. The data may be ignored or
* discarded as with {@link #addText}. You must close your
* ParcelFileDescriptor object after calling this method!
*
* @param tag describing the type of entry being stored
* @param fd file descriptor to read from
* @param flags describing the data
*/
public void addFile(String tag, ParcelFileDescriptor fd, int flags) {
if (fd == null) throw new NullPointerException();
try { mService.add(new Entry(tag, 0, fd, flags)); } catch (RemoteException e) {}
}
/**
* Checks any blacklists (set in system settings) to see whether a certain
* tag is allowed. Entries with disabled tags will be dropped immediately,
* so you can save the work of actually constructing and sending the data.
*
* @param tag that would be used in {@link #addText} or {@link #addFile}
* @return whether events with that tag would be accepted
*/
public boolean isTagEnabled(String tag) {
try { return mService.isTagEnabled(tag); } catch (RemoteException e) { return false; }
}
/**
* Gets the next entry from the drop box *after* the specified time.
* Requires android.permission.READ_LOGS. You must always call
* {@link Entry#close()} on the return value!
*
* @param tag of entry to look for, null for all tags
* @param msec time of the last entry seen
* @return the next entry, or null if there are no more entries
*/
public Entry getNextEntry(String tag, long msec) {
try { return mService.getNextEntry(tag, msec); } catch (RemoteException e) { return null; }
}
// TODO: It may be useful to have some sort of notification mechanism
// when data is added to the dropbox, for demand-driven readers --
// for now readers need to poll the dropbox to find new data.
}

View File

@@ -1,163 +0,0 @@
/*
* Copyright (C) 2009 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 android.os;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.zip.GZIPInputStream;
/**
* A single entry retrieved from an {@link IDropBox} implementation.
* This may include a reference to a stream, so you must call
* {@link #close()} when you are done using it.
*
* {@pending}
*/
public class DropBoxEntry implements Parcelable {
private final String mTag;
private final long mTimeMillis;
private final String mText;
private final ParcelFileDescriptor mFileDescriptor;
private final int mFlags;
/** Flag value: Entry's content was deleted to save space. */
public static final int IS_EMPTY = 1;
/** Flag value: Content is human-readable UTF-8 text (possibly compressed). */
public static final int IS_TEXT = 2;
/** Flag value: Content can been decompressed with {@link GZIPOutputStream}. */
public static final int IS_GZIPPED = 4;
/** Create a new DropBoxEntry with the specified contents. */
public DropBoxEntry(String tag, long timeMillis, String text) {
if (tag == null || text == null) throw new NullPointerException();
mTag = tag;
mTimeMillis = timeMillis;
mText = text;
mFileDescriptor = null;
mFlags = IS_TEXT;
}
/** Create a new DropBoxEntry with the specified contents. */
public DropBoxEntry(String tag, long millis, File data, int flags) throws IOException {
if (tag == null) throw new NullPointerException();
if (((flags & IS_EMPTY) != 0) != (data == null)) throw new IllegalArgumentException();
mTag = tag;
mTimeMillis = millis;
mText = null;
mFlags = flags;
mFileDescriptor = data == null ? null :
ParcelFileDescriptor.open(data, ParcelFileDescriptor.MODE_READ_ONLY);
}
/** Internal constructor for CREATOR.createFromParcel(). */
private DropBoxEntry(String tag, long millis, Object value, int flags) {
if (tag == null) throw new NullPointerException();
if (((flags & IS_EMPTY) != 0) != (value == null)) throw new IllegalArgumentException();
mTag = tag;
mTimeMillis = millis;
mFlags = flags;
if (value == null) {
mText = null;
mFileDescriptor = null;
} else if (value instanceof String) {
if ((flags & IS_TEXT) == 0) throw new IllegalArgumentException();
mText = (String) value;
mFileDescriptor = null;
} else if (value instanceof ParcelFileDescriptor) {
mText = null;
mFileDescriptor = (ParcelFileDescriptor) value;
} else {
throw new IllegalArgumentException();
}
}
/** Close the input stream associated with this entry. */
public synchronized void close() {
try { if (mFileDescriptor != null) mFileDescriptor.close(); } catch (IOException e) { }
}
/** @return the tag originally attached to the entry. */
public String getTag() { return mTag; }
/** @return time when the entry was originally created. */
public long getTimeMillis() { return mTimeMillis; }
/** @return flags describing the content returned by @{link #getInputStream()}. */
public int getFlags() { return mFlags & ~IS_GZIPPED; } // getInputStream() decompresses.
/**
* @param maxLength of string to return (will truncate at this length).
* @return the uncompressed text contents of the entry, null if the entry is not text.
*/
public String getText(int maxLength) {
if (mText != null) return mText.substring(0, Math.min(maxLength, mText.length()));
if ((mFlags & IS_TEXT) == 0) return null;
try {
InputStream stream = getInputStream();
if (stream == null) return null;
char[] buf = new char[maxLength];
InputStreamReader reader = new InputStreamReader(stream);
return new String(buf, 0, Math.max(0, reader.read(buf)));
} catch (IOException e) {
return null;
}
}
/** @return the uncompressed contents of the entry, or null if the contents were lost */
public InputStream getInputStream() throws IOException {
if (mText != null) return new ByteArrayInputStream(mText.getBytes("UTF8"));
if (mFileDescriptor == null) return null;
InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(mFileDescriptor);
return (mFlags & IS_GZIPPED) != 0 ? new GZIPInputStream(is) : is;
}
public static final Parcelable.Creator<DropBoxEntry> CREATOR = new Parcelable.Creator() {
public DropBoxEntry[] newArray(int size) { return new DropBoxEntry[size]; }
public DropBoxEntry createFromParcel(Parcel in) {
return new DropBoxEntry(
in.readString(), in.readLong(), in.readValue(null), in.readInt());
}
};
public int describeContents() {
return mFileDescriptor != null ? Parcelable.CONTENTS_FILE_DESCRIPTOR : 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeString(mTag);
out.writeLong(mTimeMillis);
if (mFileDescriptor != null) {
out.writeValue(mFileDescriptor);
} else {
out.writeValue(mText);
}
out.writeInt(mFlags);
}
}

View File

@@ -1,92 +0,0 @@
/*
* Copyright (C) 2009 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 android.os;
import android.os.DropBoxEntry;
import android.os.ParcelFileDescriptor;
/**
* Enqueues chunks of data (from various sources -- application crashes, kernel
* log records, etc.). The queue is size bounded and will drop old data if the
* enqueued data exceeds the maximum size.
*
* <p>This interface is implemented by a system service you can access:
*
* <pre>IDropBox.Stub.asInterface(ServiceManager.getService("dropbox"));</pre>
*
* <p>Other system services and debugging tools may scan the drop box to upload
* entries for processing.
*
* {@pending}
*/
interface IDropBox {
/**
* Stores human-readable text. The data may be discarded eventually (or even
* immediately) if space is limited, or ignored entirely if the tag has been
* blocked (see {@link #isTagEnabled}).
*
* @param tag describing the type of entry being stored
* @param data value to store
*/
void addText(String tag, String data);
/**
* Stores binary data. The data may be ignored or discarded as with
* {@link #addText}.
*
* @param tag describing the type of entry being stored
* @param data value to store
* @param flags describing the data, defined in {@link DropBoxEntry}
*/
void addData(String tag, in byte[] data, int flags);
/**
* Stores data read from a file descriptor. The data may be ignored or
* discarded as with {@link #addText}. You must close your
* ParcelFileDescriptor object after calling this method!
*
* @param tag describing the type of entry being stored
* @param data file descriptor to read from
* @param flags describing the data, defined in {@link DropBoxEntry}
*/
void addFile(String tag, in ParcelFileDescriptor data, int flags);
/**
* Checks any blacklists (set in system settings) to see whether a certain
* tag is allowed. Entries with disabled tags will be dropped immediately,
* so you can save the work of actually constructing and sending the data.
*
* @param tag that would be used in {@link #addText} or {@link #addFile}
* @return whether events with that tag would be accepted
*/
boolean isTagEnabled(String tag);
/**
* Gets the next entry from the drop box *after* the specified time.
* Requires android.permission.READ_LOGS. You must always call
* {@link DropBoxEntry#close()} on the return value!
*
* @param tag of entry to look for, null for all tags
* @param millis time of the last entry seen
* @return the next entry, or null if there are no more entries
*/
DropBoxEntry getNextEntry(String tag, long millis);
// TODO: It may be useful to have some sort of notification mechanism
// when data is added to the dropbox, for demand-driven readers --
// for now readers need to poll the dropbox to find new data.
}