1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*
* Copyright (C) 2016 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.view.inputmethod;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.content.ClipDescription;
import android.content.ContentProvider;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.UserHandle;
import com.android.internal.inputmethod.IInputContentUriToken;
import java.security.InvalidParameterException;
/**
* A container object with which input methods can send content files to the target application.
*/
public final class InputContentInfo implements Parcelable {
/**
* The content URI that may or may not have a user ID embedded by
* {@link ContentProvider#maybeAddUserId(Uri, int)}. This always preserves the exact value
* specified to a constructor. In other words, if it had user ID embedded when it was passed
* to the constructor, it still has the same user ID no matter if it is valid or not.
*/
@NonNull
private final Uri mContentUri;
/**
* The user ID to which {@link #mContentUri} belongs to. If {@link #mContentUri} already
* embedded the user ID when it was specified then this fields has the same user ID. Otherwise
* the user ID is determined based on the process ID when the constructor is called.
*
* <p>CAUTION: If you received {@link InputContentInfo} from a different process, there is no
* guarantee that this value is correct and valid. Never use this for any security purpose</p>
*/
@UserIdInt
private final int mContentUriOwnerUserId;
@NonNull
private final ClipDescription mDescription;
@Nullable
private final Uri mLinkUri;
@NonNull
private IInputContentUriToken mUriToken;
/**
* Constructs {@link InputContentInfo} object only with mandatory data.
*
* @param contentUri Content URI to be exported from the input method.
* This cannot be {@code null}.
* @param description A {@link ClipDescription} object that contains the metadata of
* {@code contentUri} such as MIME type(s). This object cannot be {@code null}. Also
* {@link ClipDescription#getLabel()} should be describing the content specified by
* {@code contentUri} for accessibility reasons.
*/
public InputContentInfo(@NonNull Uri contentUri, @NonNull ClipDescription description) {
this(contentUri, description, null /* link Uri */);
}
/**
* Constructs {@link InputContentInfo} object with additional link URI.
*
* @param contentUri Content URI to be exported from the input method.
* This cannot be {@code null}.
* @param description A {@link ClipDescription} object that contains the metadata of
* {@code contentUri} such as MIME type(s). This object cannot be {@code null}. Also
* {@link ClipDescription#getLabel()} should be describing the content specified by
* {@code contentUri} for accessibility reasons.
* @param linkUri An optional {@code http} or {@code https} URI. The editor author may provide
* a way to navigate the user to the specified web page if this is not {@code null}.
* @throws InvalidParameterException if any invalid parameter is specified.
*/
public InputContentInfo(@NonNull Uri contentUri, @NonNull ClipDescription description,
@Nullable Uri linkUri) {
validateInternal(contentUri, description, linkUri, true /* throwException */);
mContentUri = contentUri;
mContentUriOwnerUserId =
ContentProvider.getUserIdFromUri(mContentUri, UserHandle.myUserId());
mDescription = description;
mLinkUri = linkUri;
}
/**
* @return {@code true} if all the fields are valid.
* @hide
*/
public boolean validate() {
return validateInternal(mContentUri, mDescription, mLinkUri, false /* throwException */);
}
/**
* Constructs {@link InputContentInfo} object with additional link URI.
*
* @param contentUri Content URI to be exported from the input method.
* This cannot be {@code null}.
* @param description A {@link ClipDescription} object that contains the metadata of
* {@code contentUri} such as MIME type(s). This object cannot be {@code null}. Also
* {@link ClipDescription#getLabel()} should be describing the content specified by
* {@code contentUri} for accessibility reasons.
* @param linkUri An optional {@code http} or {@code https} URI. The editor author may provide
* a way to navigate the user to the specified web page if this is not {@code null}.
* @param throwException {@code true} if this method should throw an
* {@link InvalidParameterException}.
* @throws InvalidParameterException if any invalid parameter is specified.
*/
private static boolean validateInternal(@NonNull Uri contentUri,
@NonNull ClipDescription description, @Nullable Uri linkUri, boolean throwException) {
if (contentUri == null) {
if (throwException) {
throw new NullPointerException("contentUri");
}
return false;
}
if (description == null) {
if (throwException) {
throw new NullPointerException("description");
}
return false;
}
final String contentUriScheme = contentUri.getScheme();
if (!"content".equals(contentUriScheme)) {
if (throwException) {
throw new InvalidParameterException("contentUri must have content scheme");
}
return false;
}
if (linkUri != null) {
final String scheme = linkUri.getScheme();
if (scheme == null ||
(!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https"))) {
if (throwException) {
throw new InvalidParameterException(
"linkUri must have either http or https scheme");
}
return false;
}
}
return true;
}
/**
* @return Content URI with which the content can be obtained.
*/
@NonNull
public Uri getContentUri() {
// Fix up the content URI when and only when the caller's user ID does not match the owner's
// user ID.
if (mContentUriOwnerUserId != UserHandle.myUserId()) {
return ContentProvider.maybeAddUserId(mContentUri, mContentUriOwnerUserId);
}
return mContentUri;
}
/**
* @return {@link ClipDescription} object that contains the metadata of {@code #getContentUri()}
* such as MIME type(s). {@link ClipDescription#getLabel()} can be used for accessibility
* purpose.
*/
@NonNull
public ClipDescription getDescription() { return mDescription; }
/**
* @return An optional {@code http} or {@code https} URI that is related to this content.
*/
@Nullable
public Uri getLinkUri() { return mLinkUri; }
/**
* Update the internal state of this object to be associated with the given token.
*
* <p>TODO(yukawa): Come up with an idea to make {@link InputContentInfo} immutable.</p>
*
* @param token special URI token obtained from the system.
* @hide
*/
public void setUriToken(IInputContentUriToken token) {
if (mUriToken != null) {
throw new IllegalStateException("URI token is already set");
}
mUriToken = token;
}
/**
* Requests a temporary read-only access permission for content URI associated with this object.
*
* <p>Does nothing if the temporary permission is already granted.</p>
*/
public void requestPermission() {
if (mUriToken == null) {
return;
}
try {
mUriToken.take();
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
/**
* Releases a temporary read-only access permission for content URI associated with this object.
*
* <p>Does nothing if the temporary permission is not granted.</p>
*/
public void releasePermission() {
if (mUriToken == null) {
return;
}
try {
mUriToken.release();
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
/**
* Used to package this object into a {@link Parcel}.
*
* @param dest The {@link Parcel} to be written.
* @param flags The flags used for parceling.
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
Uri.writeToParcel(dest, mContentUri);
dest.writeInt(mContentUriOwnerUserId);
mDescription.writeToParcel(dest, flags);
Uri.writeToParcel(dest, mLinkUri);
if (mUriToken != null) {
dest.writeInt(1);
dest.writeStrongBinder(mUriToken.asBinder());
} else {
dest.writeInt(0);
}
}
private InputContentInfo(@NonNull Parcel source) {
mContentUri = Uri.CREATOR.createFromParcel(source);
mContentUriOwnerUserId = source.readInt();
mDescription = ClipDescription.CREATOR.createFromParcel(source);
mLinkUri = Uri.CREATOR.createFromParcel(source);
if (source.readInt() == 1) {
mUriToken = IInputContentUriToken.Stub.asInterface(source.readStrongBinder());
} else {
mUriToken = null;
}
}
/**
* Used to make this class parcelable.
*/
public static final @android.annotation.NonNull Parcelable.Creator<InputContentInfo> CREATOR
= new Parcelable.Creator<InputContentInfo>() {
@Override
public InputContentInfo createFromParcel(Parcel source) {
return new InputContentInfo(source);
}
@Override
public InputContentInfo[] newArray(int size) {
return new InputContentInfo[size];
}
};
/**
* {@inheritDoc}
*/
@Override
public int describeContents() {
return 0;
}
}
|