1 /*
2  * Copyright (C) 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.ui.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 ) : Parcelable {
39     constructor(
40         source: Parcel
41     ) : this(
42         intent = source.requireParcelable(),
43         launchedFromUid = source.readInt(),
44         launchedFromPackage = requireNotNull(source.readString()),
45         referrer = source.readParcelable()
46     )
47 
48     /** A package name from referrer, if it is an android-app URI */
<lambda>null49     val referrerPackage = referrer?.takeIf { it.scheme == ANDROID_APP_SCHEME }?.authority
50 
describeContentsnull51     override fun describeContents() = 0 /* flags */
52 
53     override fun writeToParcel(dest: Parcel, flags: Int) {
54         dest.writeParcelable(intent, flags)
55         dest.writeInt(launchedFromUid)
56         dest.writeString(launchedFromPackage)
57         dest.writeParcelable(referrer, flags)
58     }
59 
60     companion object {
61         const val ACTIVITY_MODEL_KEY = "com.android.intentresolver.ACTIVITY_MODEL"
62 
63         @JvmField
64         @Suppress("unused")
65         val CREATOR =
66             object : Parcelable.Creator<ActivityModel> {
newArraynull67                 override fun newArray(size: Int) = arrayOfNulls<ActivityModel>(size)
68                 override fun createFromParcel(source: Parcel) = ActivityModel(source)
69             }
70 
71         @JvmStatic
72         fun createFrom(activity: Activity): ActivityModel {
73             return ActivityModel(
74                 activity.intent,
75                 activity.launchedFromUid,
76                 Objects.requireNonNull<String>(activity.launchedFromPackage),
77                 activity.referrer
78             )
79         }
80     }
81 }
82