From 704199355e1c6f8b14402e01063fbae7b187b35b Mon Sep 17 00:00:00 2001 From: mar-v-in Date: Mon, 30 Mar 2015 23:55:35 +0200 Subject: [PATCH] Add some wearable APIs --- .../android/gms/common/data/DataHolder.java | 29 ++++- .../android/gms/common/data/Freezable.java | 39 +++++++ .../google/android/gms/wearable/Asset.java | 89 ++++++++++++++++ .../google/android/gms/wearable/DataItem.java | 56 ++++++++++ .../android/gms/wearable/DataItemAsset.java | 36 +++++++ .../com/google/android/gms/wearable/Node.java | 4 +- .../android/gms/wearable/PutDataRequest.java | 100 ++++++++++++++++++ .../java/org/microg/gms/common/PublicApi.java | 6 +- 8 files changed, 350 insertions(+), 9 deletions(-) create mode 100644 play-services-api/src/main/java/com/google/android/gms/common/data/Freezable.java create mode 100644 play-services-api/src/main/java/com/google/android/gms/wearable/Asset.java create mode 100644 play-services-api/src/main/java/com/google/android/gms/wearable/DataItem.java create mode 100644 play-services-api/src/main/java/com/google/android/gms/wearable/DataItemAsset.java create mode 100644 play-services-api/src/main/java/com/google/android/gms/wearable/PutDataRequest.java diff --git a/play-services-api/src/main/java/com/google/android/gms/common/data/DataHolder.java b/play-services-api/src/main/java/com/google/android/gms/common/data/DataHolder.java index 99811a18..91bfbd68 100644 --- a/play-services-api/src/main/java/com/google/android/gms/common/data/DataHolder.java +++ b/play-services-api/src/main/java/com/google/android/gms/common/data/DataHolder.java @@ -24,16 +24,23 @@ public class DataHolder extends AutoSafeParcelable { private int versionCode = 1; @SafeParceled(1) - private String[] columns; + public final String[] columns; @SafeParceled(2) - private CursorWindow[] windows; + public final CursorWindow[] windows; @SafeParceled(3) - private int statusCode; + public final int statusCode; @SafeParceled(4) - private Bundle metadata; + public final Bundle metadata; + + private DataHolder() { + columns = null; + windows = null; + statusCode = 0; + metadata = null; + } public DataHolder(String[] columns, CursorWindow[] windows, int statusCode, Bundle metadata) { this.columns = columns; @@ -48,13 +55,14 @@ public class DataHolder extends AutoSafeParcelable { protected static final int FIELD_TYPE_NULL = 0; protected static final int FIELD_TYPE_STRING = 3; + @SuppressWarnings("deprecation") @SuppressLint("NewApi") static int getCursorType(Cursor cursor, int i) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { return cursor.getType(i); } if (cursor instanceof AbstractWindowedCursor) { - CursorWindow cursorWindow = ((AbstractWindowedCursor)cursor).getWindow(); + CursorWindow cursorWindow = ((AbstractWindowedCursor) cursor).getWindow(); int pos = cursor.getPosition(); int type = -1; if (cursorWindow.isNull(pos, i)) { @@ -113,6 +121,17 @@ public class DataHolder extends AutoSafeParcelable { return dataHolder; } + + public int getCount() { + int c = 0; + if (windows != null) { + for (CursorWindow window : windows) { + c += window.getNumRows(); + } + } + return c; + } + @Override public String toString() { return "DataHolder{" + diff --git a/play-services-api/src/main/java/com/google/android/gms/common/data/Freezable.java b/play-services-api/src/main/java/com/google/android/gms/common/data/Freezable.java new file mode 100644 index 00000000..a6d69d46 --- /dev/null +++ b/play-services-api/src/main/java/com/google/android/gms/common/data/Freezable.java @@ -0,0 +1,39 @@ +/* + * Copyright 2013-2015 µg Project Team + * + * 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.google.android.gms.common.data; + +public interface Freezable { + /** + * Freeze a volatile representation into an immutable representation. Objects returned from + * this call are safe to cache. + *

+ * Note that the output of {@link #freeze} may not be identical to the parent object, but + * should be equal. + * + * @return A concrete implementation of the data object. + */ + T freeze(); + + /** + * Check to see if this object is valid for use. If the object is still volatile, this method + * will indicate whether or not the object can be safely used. + * The output of a call to {@link #freeze()} will always be valid. + * + * @return whether or not the object is valid for use. + */ + boolean isDataValid(); +} diff --git a/play-services-api/src/main/java/com/google/android/gms/wearable/Asset.java b/play-services-api/src/main/java/com/google/android/gms/wearable/Asset.java new file mode 100644 index 00000000..6967fbdd --- /dev/null +++ b/play-services-api/src/main/java/com/google/android/gms/wearable/Asset.java @@ -0,0 +1,89 @@ +/* + * Copyright 2013-2015 µg Project Team + * + * 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.google.android.gms.wearable; + +import android.net.Uri; +import android.os.ParcelFileDescriptor; + +import org.microg.gms.common.PublicApi; +import org.microg.safeparcel.AutoSafeParcelable; + +/** + * An asset is a binary blob shared between data items that is replicated across the wearable + * network on demand. + *

+ * It may represent an asset not yet added with the Android Wear network. DataItemAssets + * are representations of an asset after it has been added to the network through a + * {@link PutDataRequest}. + */ +@PublicApi +public class Asset extends AutoSafeParcelable { + + /** + * Creates an Asset using a byte array. + */ + public static Asset createFromBytes(byte[] assetData) { + return null; + } + + /** + * Creates an Asset using a file descriptor. The FD should be closed after being successfully + * sent in a putDataItem request. + */ + public static Asset createFromFd(ParcelFileDescriptor fd) { + return null; + } + + /** + * Create an Asset using an existing Asset's digest. + */ + public static Asset createFromRef(String digest) { + return null; + } + + /** + * Creates an Asset using a content URI. Google Play services must have permission to read this + * Uri. + */ + public static Asset createFromUri(Uri uri) { + return null; + } + + /** + * @return the digest associated with the asset data. A digest is a content identifier used to + * identify the asset across devices. + */ + public String getDigest() { + return null; + } + + /** + * @return the file descriptor referencing the asset. + */ + public ParcelFileDescriptor getFd() { + return null; + } + + /** + * @return the uri referencing the asset data. + */ + public Uri getUri() { + return null; + } + + public static final Creator CREATOR = new AutoCreator<>(Asset.class); +} diff --git a/play-services-api/src/main/java/com/google/android/gms/wearable/DataItem.java b/play-services-api/src/main/java/com/google/android/gms/wearable/DataItem.java new file mode 100644 index 00000000..9acb48f8 --- /dev/null +++ b/play-services-api/src/main/java/com/google/android/gms/wearable/DataItem.java @@ -0,0 +1,56 @@ +/* + * Copyright 2013-2015 µg Project Team + * + * 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.google.android.gms.wearable; + +import android.net.Uri; + +import com.google.android.gms.common.data.Freezable; + +import org.microg.gms.common.PublicApi; + +import java.util.Map; + +/** + * The base object of data stored in the Android Wear network. {@link DataItem} are replicated + * across all devices in the network. It contains a small blob of data and associated assets. + *

+ * A {@link DataItem} is identified by its Uri, which contains its creator and a path. + */ +@PublicApi +public interface DataItem extends Freezable { + /** + * A map of assets associated with this data item. + */ + Map getAssets(); + + /** + * An array of data stored at the specified {@link Uri}. + */ + byte[] getData(); + + /** + * Returns the DataItem's Uri. {@link Uri#getHost()} returns the id of the node that created it. + */ + Uri getUri(); + + /** + * Sets the data in a data item. + *

+ * The current maximum data item size limit is approximately 100k. Data items should generally be much smaller than this limit. + */ + DataItem setData(byte[] data); +} diff --git a/play-services-api/src/main/java/com/google/android/gms/wearable/DataItemAsset.java b/play-services-api/src/main/java/com/google/android/gms/wearable/DataItemAsset.java new file mode 100644 index 00000000..70057975 --- /dev/null +++ b/play-services-api/src/main/java/com/google/android/gms/wearable/DataItemAsset.java @@ -0,0 +1,36 @@ +/* + * Copyright 2013-2015 µg Project Team + * + * 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.google.android.gms.wearable; + +import com.google.android.gms.common.data.Freezable; + +/** + * A reference to an asset stored in a data item. Used to fetch file descriptors using + * DataApi#getFdForAsset(GoogleApiClient, DataItemAsset). + */ +public interface DataItemAsset extends Freezable { + /** + * @return the identifier used to address this asset in the context of an existing + * {@link DataItem}. + */ + String getDataItemKey(); + + /** + * @return the Android Wear-wide unique identifier for a particular asset. + */ + String getId(); +} diff --git a/play-services-api/src/main/java/com/google/android/gms/wearable/Node.java b/play-services-api/src/main/java/com/google/android/gms/wearable/Node.java index f308c611..f52fe4df 100644 --- a/play-services-api/src/main/java/com/google/android/gms/wearable/Node.java +++ b/play-services-api/src/main/java/com/google/android/gms/wearable/Node.java @@ -27,10 +27,10 @@ public interface Node { * Returns a human readable description of the node. Sometimes generated from the Bluetooth * device name */ - public abstract String getDisplayName(); + String getDisplayName(); /** * Returns an opaque string that represents a node in the Android Wear network. */ - public abstract String getId(); + String getId(); } diff --git a/play-services-api/src/main/java/com/google/android/gms/wearable/PutDataRequest.java b/play-services-api/src/main/java/com/google/android/gms/wearable/PutDataRequest.java new file mode 100644 index 00000000..8d049eba --- /dev/null +++ b/play-services-api/src/main/java/com/google/android/gms/wearable/PutDataRequest.java @@ -0,0 +1,100 @@ +/* + * Copyright 2013-2015 µg Project Team + * + * 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.google.android.gms.wearable; + +import android.net.Uri; + +import org.microg.gms.common.PublicApi; +import org.microg.safeparcel.AutoSafeParcelable; + +import java.util.Collections; +import java.util.Map; + +/** + * {@link PutDataRequest} is used to create new data items in the Android Wear network. + */ +@PublicApi +public class PutDataRequest extends AutoSafeParcelable { + public static final String WEAR_URI_SCHEME = "wear"; + + private DataItem dataItem; + + private PutDataRequest(DataItem dataItem) { + this.dataItem = dataItem; + } + + public static PutDataRequest create(String path) { + // TODO + return new PutDataRequest(null); + } + + public static PutDataRequest createFromDataItem(DataItem source) { + return new PutDataRequest(source); + } + + public static PutDataRequest createWithAutoAppendedId(String pathPrefix) { + return new PutDataRequest(null); + } + + public Asset getAsset(String key) { + // TODO + return null; + } + + public Map getAssets() { + // TODO + return Collections.emptyMap(); + } + + public byte[] getData() { + return dataItem.getData(); + } + + public Uri getUri() { + return dataItem.getUri(); + } + + public boolean hasAsset(String key) { + return dataItem.getAssets().containsKey(key); + } + + public PutDataRequest putAsset(String key, Asset value) { + // TODO + return this; + } + + public PutDataRequest removeAsset(String key) { + // TODO + return this; + } + + public PutDataRequest setData(byte[] data) { + // TODO + return this; + } + + @Override + public String toString() { + return toString(false); + } + + public String toString(boolean verbose) { + return "PutDataRequest{data=" + dataItem + "}"; + } + + public static final Creator CREATOR = new AutoCreator<>(PutDataRequest.class); +} diff --git a/play-services-api/src/main/java/org/microg/gms/common/PublicApi.java b/play-services-api/src/main/java/org/microg/gms/common/PublicApi.java index 45afeb22..94f84583 100644 --- a/play-services-api/src/main/java/org/microg/gms/common/PublicApi.java +++ b/play-services-api/src/main/java/org/microg/gms/common/PublicApi.java @@ -39,8 +39,10 @@ public @interface PublicApi { String until() default "latest"; /** - * @return used on a method or field to exclude it from the public api if the corresponding - * class was marked as public api + * Used on a method or field to exclude it from the public api if the corresponding class was + * marked as public api. + * + * @return true if the method or field is not part of the public api */ boolean exclude() default false; }