1 /* 2 * Copyright 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.intentresolver.shared.model 17 18 import android.app.Activity 19 import android.content.Intent 20 import android.net.Uri 21 import android.os.Parcel 22 import android.os.Parcelable 23 import com.android.intentresolver.data.model.ANDROID_APP_SCHEME 24 import com.android.intentresolver.ext.readParcelable 25 import com.android.intentresolver.ext.requireParcelable 26 import java.util.Objects 27 28 /** Contains Activity-scope information about the state when started. */ 29 data class ActivityModel( 30 /** The [Intent] received by the app */ 31 val intent: Intent, 32 /** The identifier for the sending app and user */ 33 val launchedFromUid: Int, 34 /** The package of the sending app */ 35 val launchedFromPackage: String, 36 /** The referrer as supplied to the activity. */ 37 val referrer: Uri?, 38 /** True if the activity is the first activity in the task */ 39 val isTaskRoot: Boolean, 40 ) : Parcelable { 41 constructor( 42 source: Parcel 43 ) : this( 44 intent = source.requireParcelable(), 45 launchedFromUid = source.readInt(), 46 launchedFromPackage = requireNotNull(source.readString()), 47 referrer = source.readParcelable(), 48 isTaskRoot = source.readBoolean(), 49 ) 50 51 /** A package name from referrer, if it is an android-app URI */ <lambda>null52 val referrerPackage = referrer?.takeIf { it.scheme == ANDROID_APP_SCHEME }?.authority 53 describeContentsnull54 override fun describeContents() = 0 /* flags */ 55 56 override fun writeToParcel(dest: Parcel, flags: Int) { 57 dest.writeParcelable(intent, flags) 58 dest.writeInt(launchedFromUid) 59 dest.writeString(launchedFromPackage) 60 dest.writeParcelable(referrer, flags) 61 dest.writeBoolean(isTaskRoot) 62 } 63 64 companion object { 65 @JvmField 66 @Suppress("unused") 67 val CREATOR = 68 object : Parcelable.Creator<ActivityModel> { newArraynull69 override fun newArray(size: Int) = arrayOfNulls<ActivityModel>(size) 70 71 override fun createFromParcel(source: Parcel) = ActivityModel(source) 72 } 73 74 @JvmStatic 75 fun createFrom(activity: Activity): ActivityModel { 76 return ActivityModel( 77 activity.intent, 78 activity.launchedFromUid, 79 Objects.requireNonNull<String>(activity.launchedFromPackage), 80 activity.referrer, 81 activity.isTaskRoot, 82 ) 83 } 84 } 85 } 86