1 /*
2  * Copyright (C) 2021 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 
17 package com.android.server.wm;
18 
19 import android.annotation.IntDef;
20 import android.annotation.Nullable;
21 import android.app.ActivityOptions;
22 import android.content.Intent;
23 import android.content.pm.ActivityInfo;
24 import android.content.pm.ResolveInfo;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 /**
30  * Callback to intercept activity starts and possibly block/redirect them.
31  */
32 public abstract class ActivityInterceptorCallback {
33     /**
34      * Intercept the launch intent based on various signals. If an interception happened, returns
35      * a new/existing non-null {@link Intent} which may redirect to another activity.
36      *
37      * @return null if no interception occurred, or a non-null intent which replaces the
38      * existing intent.
39      */
intercept(ActivityInterceptorInfo info)40     public abstract @Nullable Intent intercept(ActivityInterceptorInfo info);
41 
42     /**
43      * The unique id of each interceptor which determines the order it will execute in.
44      */
45     @IntDef(suffix = { "_ORDERED_ID" }, value = {
46             FIRST_ORDERED_ID,
47             LAST_ORDERED_ID // Update this when adding new ids
48     })
49     @Retention(RetentionPolicy.SOURCE)
50     public @interface OrderedId {}
51 
52     /**
53      * The first id, used by the framework to determine the valid range of ids.
54      */
55     static final int FIRST_ORDERED_ID = 0;
56 
57     /**
58      * The final id, used by the framework to determine the valid range of ids. Update this when
59      * adding new ids.
60      */
61     static final int LAST_ORDERED_ID = FIRST_ORDERED_ID;
62 
63     /**
64      * Data class for storing the various arguments needed for activity interception.
65      */
66     public static final class ActivityInterceptorInfo {
67         public final int realCallingUid;
68         public final int realCallingPid;
69         public final int userId;
70         public final String callingPackage;
71         public final String callingFeatureId;
72         public final Intent intent;
73         public final ResolveInfo rInfo;
74         public final ActivityInfo aInfo;
75         public final String resolvedType;
76         public final int callingPid;
77         public final int callingUid;
78         public final ActivityOptions checkedOptions;
79 
ActivityInterceptorInfo(int realCallingUid, int realCallingPid, int userId, String callingPackage, String callingFeatureId, Intent intent, ResolveInfo rInfo, ActivityInfo aInfo, String resolvedType, int callingPid, int callingUid, ActivityOptions checkedOptions)80         public ActivityInterceptorInfo(int realCallingUid, int realCallingPid, int userId,
81                 String callingPackage, String callingFeatureId, Intent intent,
82                 ResolveInfo rInfo, ActivityInfo aInfo, String resolvedType, int callingPid,
83                 int callingUid, ActivityOptions checkedOptions) {
84             this.realCallingUid = realCallingUid;
85             this.realCallingPid = realCallingPid;
86             this.userId = userId;
87             this.callingPackage = callingPackage;
88             this.callingFeatureId = callingFeatureId;
89             this.intent = intent;
90             this.rInfo = rInfo;
91             this.aInfo = aInfo;
92             this.resolvedType = resolvedType;
93             this.callingPid = callingPid;
94             this.callingUid = callingUid;
95             this.checkedOptions = checkedOptions;
96         }
97     }
98 }
99